连接实现
数据帧分析
结算后的数据帧格式
数据接收的程序编写
特别提示 为了使你的系统软件更快地响应数据字节的接收,建议使用中断方式接收数据。 而不是使用查询模式接收字节。中断程序越精简越好,下面给出一个数据帧接收的范例程序。基于 单片机。移植需要开启串口中断,并在相应的中断程序中编写以下代码STM32F
数据接收与程序编写
数据的校验
C语言实现
/*
编写者:lisn3188
网址:www.chiplab7.com
作者E-mail:lisn3188@163.com
编译环境:MDK-Lite Version: 4.23
初版时间: 2012-05-25
测试: 本程序已在第七实验室的mini IMU上完成测试
功能:
使用串行中断程序接收mini IMU 的数据。
移植者需在自己编写对应的串口初始化程序,并开启接收中断。
在中断子程序中 需要做相应的改动
为了使用这个程序,你需要:
1.初始化你的串口,将波特率与模块的波特率设置成一致的
2.开启串口的接收中断,并参考以下程序编写中断子程序
参考 void USART2_IRQHandler(void)
3.在主程序的循环体中,定时调用子程序:
void UART2_CommandRoute(void)
以检查是否收到新的数据帧
所有的姿态数据和ADC值都会在 UART2_CommandRoute
中进行更新。
4.使用 以下值会自动更新:
float yaw, //偏航角
pitch,//俯仰
roll, //滚转
alt, //高度
tempr,//温度
press;//气压
int16_t ax, ay, az;//加速度计
int16_t gx, gy, gz;//陀螺仪
int16_t hx, hy, hz;//磁力计
------------------------------------
*/
//uart reicer flag
#define b_uart_head 0x80 //收到A5 头 标志位
#define b_rx_over 0x40 //收到完整的帧标志
// USART Receiver buffer
#define RX_BUFFER_SIZE 100 //接收缓冲区字节数
volatile unsigned char rx_buffer[RX_BUFFER_SIZE]; //接收数据缓冲区
volatile unsigned char rx_wr_index; //缓冲写指针
volatile unsigned char RC_Flag; //接收状态标志字节
//解算后的角度值
float yaw, //偏航角
pitch,//俯仰
roll, //滚转
alt, //高度
tempr,//温度
press;//气压
//ADC值
int16_t ax, ay, az;//加速度计
int16_t gx, gy, gz;//陀螺仪
int16_t hx, hy, hz;//磁力计
//GPS位置信息
float GPS_Altitude , //天线离海平面的高度,-9999.9到9999.9米
Latitude_GPS , //纬度 单位为度
Longitude_GPS , //经度 单位为度
Speed_GPS , //地面速率 单位 米每秒
Course_GPS ; //地面航向(000.0~359.9度,以真北为参考基准)
unsigned char GPS_STA_Num ;
//在接收完一帧IMU姿态报告后,调用这个子程序来取出姿态数据
void UART2_Get_IMU(void)
{
int16_t temp;
temp = 0;
temp = rx_buffer[2];
temp <<= 8;
temp |= rx_buffer[3];
if(temp&0x8000){
temp = 0-(temp&0x7fff);
}else temp = (temp&0x7fff);
yaw=(float)temp / 10.0f; //偏航角
temp = 0;
temp = rx_buffer[4];
temp <<= 8;
temp |= rx_buffer[5];
if(temp&0x8000){
temp = 0-(temp&0x7fff);
}else temp = (temp&0x7fff);
pitch=(float)temp / 10.0f;//俯仰
temp = 0;
temp = rx_buffer[6];
temp <<= 8;
temp |= rx_buffer[7];
if(temp&0x8000){
temp = 0-(temp&0x7fff);
}else temp = (temp&0x7fff);
roll=(float)temp / 10.0f;//滚转
temp = 0;
temp