STM32串口超時判斷方式接收未知長度數(shù)據(jù)
void USART1_IRQHandler(void){ u8 res; if(USART1->SR&(1<<5))//接收到數(shù)據(jù){ res=USART1->DR; if(USART1_Recv_LenCNT=0; //計數(shù)器清空if(Rec_Over_Flag==0)TIM3_Set(1); //使能定時器4的中斷 USART1_RX_BUF[USART1_Recv_Len++]=res; //記錄接收到的值 }else {Rec_Over_Flag=1; //強制標(biāo)記接收完成} } } timer.c定時器中斷函數(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的開關(guān)//sta:0,關(guān)閉;1,開啟;void TIM3_Set(u8 sta){if(sta){TIM3->CNT=0; //計數(shù)器清空TIM3->CR1|=1<<0; //使能定時器3}else TIM3->CR1&=~(1<<0);//關(guān)閉定時器3 }
main.c主函數(shù)
int main(void){ u8 t;u8 Password[7]="123456";u8 Tishi1[]="操作碼長度不對";u8 Tishi2[]="操作碼不正確";u8 Tishi3[]="指令成功執(zhí)行!";u16 times=0; Stm32_Clock_Init(9); //系統(tǒng)時鐘設(shè)置delay_init(72); //延時初始化 uart_init(72,57600); //串口初始化為57600LED_Init(); //初始化與LED連接的硬件接口 Timerx_Init(99,7199); //10kHz的技術(shù)頻率,計數(shù)到100為10mswhile(1){ if(Rec_Over_Flag==1){ if(USART1_Recv_Len!=0x06) //USART1_Recv_Len接收數(shù)據(jù)的長度不等于6{ Putstrings(Tishi1);}else // USART1_Recv_Len接收數(shù)據(jù)的長度等于6{for(t=0;t看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();