定时中断判断IO检测波形转成串口数据
void sumulation_Uart_Send_Data(void)
{
uint8_t i = 0,j;
uint8_t temp = 0;
OUTPUT_LOW_LEVEL();
delay_us(1500);
for(i=0;i<4;i++) //四个字节
{
temp = myusart.rcbuf[i];
for (j=0;j<8;j++)
{
PA12_OUT ^= 1; //异或就是取反;
if (temp&0x80) //先发送第七位
{
delay_us(750);
}else
{
delay_us(250);
}
temp<<=1;
}
//simulation_Uart_Send_Byte(temp);
}
OUTPUT_HIGH_LEVEL(); //空闲信号
}
#include "timer.h" #include "led.h" #include "usart.h" #include "com.h" void TIM3_Int_Init(u16 arr,u16 psc) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟使能 TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 计数到5000为500ms TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值 10Khz的计数频率 TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位 TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //使能指定的TIM3中断,允许更新中断 NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能 NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器 TIM_Cmd(TIM3, ENABLE); //使能TIMx外设 } //定时器2中断服务程序 u8 time_i=0; void TIM3_IRQHandler(void) //TIM3中断 50us { if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源 { time_i++; if(time_i>=20) //1ms { time_i=0; if(myusart.timout>0) { if(--myusart.timout==0) { myusart.reflag=1; //收到一帧数据 } } } TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //清除TIMx的中断待处理位:TIM 中断源 } } void TIM2_Int_Init(u16 arr,u16 psc) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //时钟使能 TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 计数到5000为500ms TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值 10Khz的计数频率 TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式 TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位 TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE ); //使能指定的TIM2中断,允许更新中断 NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //TIM2中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能 NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器 TIM_Cmd(TIM2, ENABLE); //使能TIMx外设 } //定时器2中断服务程序 #define PA0 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0) u8 TIM2_i=0; u32 TIM2_a,TIM2_b; u8 TIM2_c[10]; u8 TIM2_flag=0; u8 TIM2_dat; void TIM2_sendback(void) { unsigned char t; for(t=0;t<4;t++) { USART_SendData(USART1, TIM2_c[t]);//向串口1发送数据 while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET); } } #define R250 7 #define L250 3 #define R750 20 #define L750 10 #define R1500 35 #define L1500 25 #define H2500 50 void checkIO(u32 x) { if(x>0) { if(TIM2_flag&1) { TIM2_c[TIM2_i]<<=1; if ((x>L250)&&(x<R250) ) TIM2_c[TIM2_i]&=~0x01; else if ((x>L750)&&(x<R750) ) TIM2_c[TIM2_i]|=0x01; else TIM2_flag = 0; if(++TIM2_dat>=8) { TIM2_dat = 0; TIM2_i++; } } } } void TIM2_IRQHandler(void) //TIM2中断 60us { if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源 { if(PA0==0) { checkIO(TIM2_b); TIM2_a++; TIM2_b = 0; }else { checkIO(TIM2_a); if ((TIM2_a>L1500)&&(TIM2_a<R1500) ) { TIM2_flag |= 1; TIM2_i = 0; TIM2_dat = 0; } if ((TIM2_b>H2500)&&(TIM2_flag&1)) //OK { //串口发生数据 TIM2_flag = 0; TIM2_sendback(); } TIM2_a = 0; TIM2_b++; } TIM_ClearITPendingBit(TIM2, TIM_IT_Update ); //清除TIMx的中断待处理位:TIM 中断源 } }