stm32实现SBUS传输
1. 基本配置
串口波特率:100k,9位数据位,偶校验,2位停止位,利用dma空闲中断进行接收数据和处理数据


接收函数
int8_t BSP_UART_IDLE_PollData(UART_HandleTypeDef *huart, struct RF207S_SBUS_DataStructure *rf207_sbus_data)
{int8_t nret = 0;if (Uart_Receive_Data.rxok == 0) //没有接收完就退出{nret = -1;}else{if (Uart_Receive_Data.rxlen < 25) //帧不完整{nret = -2;}else if (Uart_Receive_Data.rxbuf[0] != 0x0f || Uart_Receive_Data.rxbuf[24] != 0x00) //帧错误{nret = -3;}if (nret == 0){//遥控器的取值范围:-100 ---->100 返回值的范围0----100// Min:354 Max:1696 ----->1342 chx-353/1342. == y*100rf207_sbus_data->ch1 = (((int16_t) Uart_Receive_Data.rxbuf[2] << 8) + ((int16_t) Uart_Receive_Data.rxbuf[1])) & 0x07ff;rf207_sbus_data->ch1 = (rf207_sbus_data->ch1-353)*100/1342.;rf207_sbus_data->ch2 = (((int16_t) Uart_Receive_Data.rxbuf[3] << 5) + ((int16_t) Uart_Receive_Data.rxbuf[2] >> 3))& 0x07ff;rf207_sbus_data->ch2 = (rf207_sbus_data->ch2-353)*100/1342.;rf207_sbus_data->ch3 = (((int16_t) Uart_Receive_Data.rxbuf[5] << 10) + ((int16_t) Uart_Receive_Data.rxbuf[4] << 2) + ((int16_t) Uart_Receive_Data.rxbuf[3] >> 6)) & 0x07ff;rf207_sbus_data->ch3 = (rf207_sbus_data->ch3-353)*100/1342.;rf207_sbus_data->ch4 = (((int16_t) Uart_Receive_Data.rxbuf[6] << 7) + ((int16_t) Uart_Receive_Data.rxbuf[5] >> 1))& 0x07ff;rf207_sbus_data->ch4 = (rf207_sbus_data->ch4-353)*100/1342.;rf207_sbus_data->ch5 =(((int16_t)Uart_Receive_Data.rxbuf[9]<<9) + ((int16_t)Uart_Receive_Data.rxbuf[8]<<1) + ((int16_t)buf[7]>>7)) & 0x07ff;rf207_sbus_data->ch5 = (rf207_sbus_data->ch5-353)*100/1342.;rf207_sbus_data->ch6 =(((int16_t)Uart_Receive_Data.rxbuf[10]<<6) + ((int16_t)Uart_Receive_Data.rxbuf[9]>>2)) & 0x07ff;rf207_sbus_data->ch6 = (rf207_sbus_data->ch6-353)*100/1342.;rf207_sbus_data->ch7 =(((int16_t)Uart_Receive_Data.rxbuf[11]<<3) + ((int16_t)Uart_Receive_Data.rxbuf[10]>>5)) & 0x07ff;rf207_sbus_data->ch7 = (rf207_sbus_data->ch7-353)*100/1342.;rf207_sbus_data->ch8 =(((int16_t)Uart_Receive_Data.rxbuf[13]<<8) + (int16_t)Uart_Receive_Data.rxbuf[12]) & 0x07ff;rf207_sbus_data->ch8 = (rf207_sbus_data->ch8-353)*100/1342.;rf207_sbus_data->ch9 =(((int16_t)Uart_Receive_Data.rxbuf[14]<<5) + ((int16_t)Uart_Receive_Data.rxbuf[13]>>3)) & 0x07ff;rf207_sbus_data->ch9 = (rf207_sbus_data->ch9-353)*100/1342.;rf207_sbus_data->ch10 =(((int16_t)Uart_Receive_Data.rxbuf[16]<<10) + ((int16_t)Uart_Receive_Data.rxbuf[15]<<2) + ((int16_t)Uart_Receive_Data.rxbuf[14]>>6)) & 0x07ff;rf207_sbus_data->ch10 = (rf207_sbus_data->ch10-353)*100/1342.;}Uart_Receive_Data.rxok = 0;__HAL_UART_ENABLE_IT(huart, UART_IT_IDLE); //使能串口空闲中断memset(Uart_Receive_Data.rxbuf, 0, UART_RECEIVE_IDLE_RX_MAXLEN);HAL_UART_Receive_DMA(huart, Uart_Receive_Data.rxbuf, UART_RECEIVE_IDLE_RX_MAXLEN); //开启串口DMA接收}return nret;
}
空闲中断函数(将函数放在默认的空闲中断之前)记得在it中断文件中包含该函数的头文件
void BSP_UART_IDLE_CallBack(UART_HandleTypeDef *huart)
{if ((__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) != RESET)){HAL_UART_DMAStop(huart);__HAL_UART_CLEAR_IDLEFLAG(huart);Uart_Receive_Data.rxlen = UART_RECEIVE_IDLE_RX_MAXLEN - __HAL_DMA_GET_COUNTER(huart->hdmarx);Uart_Receive_Data.rxok = 1;}
}

避坑指南:
用串口调试sbus:
串口用的是小端模式,是低位先发,刚好sbus一个通道的值是16位,所以在串口里显示的应该是两个16进制的数,后面的16进制数是高位,
建议直接用调试直接看内存数据
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
