初级会员
- 积分
- 85
- 金钱
- 85
- 注册时间
- 2022-3-28
- 在线时间
- 24 小时
|
楼主 |
发表于 2023-2-15 15:31:57
|
显示全部楼层
刚才参照状态机试了试,好像还是哪块条件不对,大佬帮忙看看代码,结果如图,想接受固定数目(比如10个),但是发送了10多个,也能显示。@LcwSwust @huanan @a5820736 @原子哥
#define HEAD1 0x01
#define HEAD2 0x02
#define HEAD3 0x04
#define HLEN 0x08
#define TYPE 0x10
#define DATA 0x20
#define CHECK 0x40
#if EN_USART1_RX
u8 USART_RX_BUF[USART_REC_LEN];
u8 data_type = 0;
void USART1_IRQHandler(void)
{
u8 Res;
static unsigned char RecState = HEAD1;
static unsigned char len = 0;
static unsigned char CheckSum = 0;
static unsigned char Len_total = 0;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
Res = USART_ReceiveData(USART1);
switch(RecState)
{
case HEAD1: if(Res == 0xAA)
RecState = HEAD2;
else RecState = HEAD1;
break;
case HEAD2:
if(Res == 0x44)
RecState = HEAD3;
else if (Res == 0xAA)
RecState = HEAD2;
else RecState =HEAD1;
break;
case HEAD3:
if(Res == 0x12)
RecState = HLEN;
else
RecState = HEAD1;
break;
case HLEN:
RecState = TYPE;
Len_total = Res;
break;
case TYPE:
RecState = DATA;
data_type = Res; //指令类型的定义
break;
case DATA:
if(len >= 10)
{
RecState = CHECK
}
USART_RX_BUF[len++] = Res; //数据的获取
break;
case CHECK: //校验为真,返回TRUE,主程序判断TRUE来执行程序
RecState = HEAD1;
break;
default: RecState = HEAD1;
break;
}
}
main函数
while(1)
{
for(t=0;t<103;t++)
{
USART_SendData(USART1, USART_RX_BUF[t]);//向串口1发送数据
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束
}
times++;
if(times%30==0)LED0=!LED0;//闪烁LED,提示系统正在运行.
delay_ms(100);
}
|
|