STM32串口超時(shí)判斷方式接收未知長(zhǎng)度數(shù)據(jù)
void USART1_IRQHandler(void){ u8 res; if(USART1->SR&(1<<5))//接收到數(shù)據(jù){ res=USART1->DR; if(USART1_Recv_LenCNT=0; //計(jì)數(shù)器清空if(Rec_Over_Flag==0)TIM3_Set(1); //使能定時(shí)器4的中斷 USART1_RX_BUF[USART1_Recv_Len++]=res; //記錄接收到的值 }else {Rec_Over_Flag=1; //強(qiáng)制標(biāo)記接收完成} } } timer.c定時(shí)器中斷函數(shù)//定時(shí)器3中斷服務(wù)程序 void TIM3_IRQHandler(void){ if(TIM3->SR&0X0001)//溢出中斷{ Rec_Over_Flag=1;TIM3->SR&=~(1<<0); //清除中斷標(biāo)志位TIM3_Set(0);LED1=!LED1; //RxTimeout等于0,變換LED1表明該次發(fā)送結(jié)束 } }//設(shè)置TIM4的開(kāi)關(guān)//sta:0,關(guān)閉;1,開(kāi)啟;void TIM3_Set(u8 sta){if(sta){TIM3->CNT=0; //計(jì)數(shù)器清空TIM3->CR1|=1<<0; //使能定時(shí)器3}else TIM3->CR1&=~(1<<0);//關(guān)閉定時(shí)器3 }
main.c主函數(shù)
int main(void){ u8 t;u8 Password[7]="123456";u8 Tishi1[]="操作碼長(zhǎng)度不對(duì)";u8 Tishi2[]="操作碼不正確";u8 Tishi3[]="指令成功執(zhí)行!";u16 times=0; Stm32_Clock_Init(9); //系統(tǒng)時(shí)鐘設(shè)置delay_init(72); //延時(shí)初始化 uart_init(72,57600); //串口初始化為57600LED_Init(); //初始化與LED連接的硬件接口 Timerx_Init(99,7199); //10kHz的技術(shù)頻率,計(jì)數(shù)到100為10mswhile(1){ if(Rec_Over_Flag==1){ if(USART1_Recv_Len!=0x06) //USART1_Recv_Len接收數(shù)據(jù)的長(zhǎng)度不等于6{ Putstrings(Tishi1);}else // USART1_Recv_Len接收數(shù)據(jù)的長(zhǎng)度等于6{for(t=0;t