SBUS协议
遥控器通信部分我用的SBUS协议,简单介绍编程需要用到的参数:
1.波特率=100K,数据位=8bit,停止位=2bit(编程的时候可以选择用9位数据+1位停止位),偶校验。
2.协议字长25个字节,格式为[startbyte] [data1] [data2] … [data22] [flags] [endbyte],其中startbyte=0xf0,endbyte=0x00
3.编码,25个字节,有用的只有22个。需要把22个字节变成16个数据
通道1使用data1中的8位,而data2
使用3位,通道2使用后5位来自data2的数据和来自data3的6位等
ch1的11位=data2的低3位+data1的8位;
例如:data1=00110110,data2=11001111;
这时ch1=111 00110110=1846;通道1 的值就为1846;
ch2=data3的低6位+data2的高5位;
ch3=data5的低1位+data4的8位+data3的高2位;
ch4=4 7;
ch5=7 4;
ch6=2 8 1;
ch7=5 6;
ch8=8 3;
ch9=6 5;
ch10=1 8 2;
ch11=4 7;
ch12=7 4;
ch13=2 8 1;
ch14=5 6;
ch15=8 3;
ch16=6 5;
这里引用某位博主整理的方法
原文链接:https://blog.csdn.net/peach_orange/article/details/52958385
实现步骤
请一定要严格按照步骤进行!!!
我就是漏了第一步后面调了好几个小时!
步骤一
硬件取反,因为sbus认为高电平是0,低电平是1,。建议硬件取反,软件麻烦不说可能还会造成后续的一些问题。这步不做后面全错。我用的如下电路:
初始化串口1
void uart_init(u32 bound){
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //使能USART1,GPIOA时钟
//USART1_TX GPIOA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.9
//USART1_RX GPIOA.10初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//串口波特率
USART_InitStructure.USART_WordLength = USART_WordLength_9b;//字长为9位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStructure.USART_Parity = USART_Parity_Even;//无奇偶校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
USART_Init(USART1, &USART_InitStructure); //初始化串口1
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启串口接受中断
USART_Cmd(USART1, ENABLE); //使能串口1
}
步骤三
在中断函数中设置接受协议
void USART1_IRQHandler(void) //串口1中断服务程序
{
u8 Res,i;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
Res =USART_ReceiveData(USART1); //读取接收到的数据
USART_RX_BUF[USART_RX_STA] = Res;
if (USART1_RX_STA == 0 && USART1_RX_BUF[USART1_RX_STA] != 0x0F) USART_RX_STA=0;; //帧头不对,丢掉
else
{
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//接收数据错误,重新开始接收
if (USART1_RX_BUF[0] == 0x0F && USART1_RX_BUF[24] == 0x00 && USART1_RX_STA == 25) //接受完一帧数据
{
Sbus_Data_Count(USART_RX_BUF,data);
for (i = 0; i<25; i++) //清空缓存区
USART1_RX_BUF[i] = 0;
USART1_RX_STA = 0;
}
}
}
}