初级会员
- 积分
- 168
- 金钱
- 168
- 注册时间
- 2020-9-2
- 在线时间
- 41 小时
|
我用ucos串口接收数组,它能够接收到数组,但是接收的数据前两个字节是错误的,我用的是正点原子的例程:
接收中断函数
void USART1_IRQHandler(void)
{
OS_ERR err;
OSIntEnter();
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断(接收到的数据必须是0x0d 0x0a结尾)
{
//USART_ClearITPendingBit(USART1,USART_IT_RXNE);
Rx_buff[Rx_Cnt]=USART_ReceiveData(USART1);//(USART1->DR); //读取接收到的数据;
Rx_Cnt++;
if(Rx_Cnt>(Rx_len-1))
{
OSTaskSemPost((OS_TCB *)&AnalyDataTaskTCB, //目标任务
(OS_OPT )OS_OPT_POST_NONE, //没选项要求
(OS_ERR *)&err);
Rx_Cnt=0;
}
}
//USART_ClearITPendingBit(USART1,USART_IT_RXNE);
OSIntExit();
}
接收到的数据并发送信号量到指定任务
void AnalyData_task(void *p_arg)
{
unsigned char checkSum;
OS_ERR err;
CPU_TS ts;
p_arg = p_arg;
while(1)
{
OSTaskSemPend ((OS_TICK )0, //无期限等待
(OS_OPT )OS_OPT_PEND_BLOCKING, //如果信号量不可用就等待
(CPU_TS *)&ts, //获取信号量被发布的时间戳
(OS_ERR *)&err); //返回错误类型
//printf("USART1 wait success!\r\n");
printf("Rx_buff[0]:0x%02hhx\r\n",Rx_buff[0]);
printf("Rx_buff[1]:0x%02hhx\r\n",Rx_buff[1]);
printf("Rx_buff[2]:0x%02hhx\r\n",Rx_buff[2]);
printf("Rx_buff[3]:0x%02hhx\r\n",Rx_buff[3]);
printf("Rx_buff[4]:0x%02hhx\r\n",Rx_buff[4]);
printf("Rx_buff[5]:0x%02hhx\r\n",Rx_buff[5]);
printf("Rx_buff[6]:0x%02hhx\r\n",Rx_buff[6]);
printf("Rx_buff[7]:0x%02hhx\r\n",Rx_buff[7]);
printf("Rx_buff[8]:0x%02hhx\r\n",Rx_buff[8]);
printf("Rx_buff[9]:0x%02hhx\r\n",Rx_buff[9]);
if(1)
{
checkSum=0xff+0xff+Rx_buff[2]+Rx_buff[3]+Rx_buff[4]+Rx_buff[5]+Rx_buff[6]+Rx_buff[7]+Rx_buff[8];
checkSum=~checkSum;
checkSum=checkSum&0xff;
printf("checkSum:0x%02x\r\n",checkSum);
if(Rx_buff[9]==checkSum)
{
printf("Rx_buff[9]:0x%02x\r\n",Rx_buff[9]);
if(Rx_buff[2]==0x02)
{
printf("hight speed\r\n");
}
if(Rx_buff[2]==0x01)
{
printf("low speed\r\n");
}
//发送消息
OSTaskQPost((OS_TCB* )&BLDCTaskTCB, //向bldc任务发送消息
(void* )&Rx_buff[3],
(OS_MSG_SIZE)1,
(OS_OPT )OS_OPT_POST_FIFO,
(OS_ERR* )&err);
OSTaskQPost((OS_TCB* )&SERVO1TaskTCB, //向上下舵机任务发送消息
(void* )&Rx_buff[4],
(OS_MSG_SIZE)1,
(OS_OPT )OS_OPT_POST_FIFO,
(OS_ERR* )&err);
OSTaskQPost((OS_TCB* )&SERVO2TaskTCB, //向左右舵机任务发送消息
(void* )&Rx_buff[5],
(OS_MSG_SIZE)1,
(OS_OPT )OS_OPT_POST_FIFO,
(OS_ERR* )&err);
OSTaskQPost((OS_TCB* )&MOTOTaskTCB, //向抬杆任务发送消息
(void* )&Rx_buff[6],
(OS_MSG_SIZE)1,
(OS_OPT )OS_OPT_POST_FIFO,
(OS_ERR* )&err);
OSTaskQPost((OS_TCB* )&DJTaskTCB, //向DJ任务发送消息
(void* )&Rx_buff[8],
(OS_MSG_SIZE)1,
(OS_OPT )OS_OPT_POST_FIFO,
(OS_ERR* )&err);
}
else
{
clearBuff();
}
}
else
{
clearBuff();
}
OSTimeDlyHMSM(0,0,0,0,OS_OPT_TIME_HMSM_STRICT,&err); //延时200ms
}
}
|
-
|