jy-61传感器STM32F1版本,供参考使用
单片机源程序如下:
- #include "led.h"
- #include "delay.h"
- #include "key.h"
- #include "sys.h"
- #include "usart.h"
- #include "oled.h"
- #include "string.h"
- extern unsigned char Re_buf[11],temp_buf[11],counter;
- extern unsigned char sign,t,he;
- float a[3],w[3],angle[3],T;
- extern unsigned char Temp[11];
- char dis_main[20];
- int main(void)
- {
- u16 t;
- u16 len;
- u16 times=0;
- delay_init(); //延时函数初始化
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
- uart_init(115200); //串口初始化为115200
- LED_Init(); //LED端口初始化
- KEY_Init(); //初始化与按键连接的硬件接口
- OLED_Init();
- while(1)
- {
-
- if(sign)
- {
- memcpy(Temp,Re_buf,11);
- sign=0;
- if(Re_buf[0]==0x55) //检查帧头
- {
- switch(Re_buf[1])
- {
- case 0x51: //标识这个包是加速度包
- a[0] = ((short)(Temp[3]<<8 | Temp[2]))/32768.0*16; //X轴加速度
- a[1] = ((short)(Temp[5]<<8 | Temp[4]))/32768.0*16; //Y轴加速度
- a[2] = ((short)(Temp[7]<<8 | Temp[6]))/32768.0*16; //Z轴加速度
- T = ((short)(Temp[9]<<8 | Temp[8]))/340.0+36.25; //温度
- break;
- case 0x52: //标识这个包是角速度包
- w[0] = ((short)(Temp[3]<<8| Temp[2]))/32768.0*2000; //X轴角速度
- w[1] = ((short)(Temp[5]<<8| Temp[4]))/32768.0*2000; //Y轴角速度
- w[2] = ((short)(Temp[7]<<8| Temp[6]))/32768.0*2000; //Z轴角速度
- T = ((short)(Temp[9]<<8| Temp[8]))/340.0+36.25; //温度
- break;
- case 0x53: //标识这个包是角度包
- angle[0] = ((short)(Temp[3]<<8| Temp[2]))/32768.0*180; //X轴滚转角(x 轴)
- angle[1] = ((short)(Temp[5]<<8| Temp[4]))/32768.0*180; //Y轴俯仰角(y 轴)
- angle[2] = ((short)(Temp[7]<<8| Temp[6]))/32768.0*180; //Z轴偏航角(z 轴)
- T = ((short)(Temp[9]<<8| Temp[8]))/340.0+36.25; //温度
- //printf("X轴角度:%.2f Y轴角度:%.2f Z轴角度:%.2f\r\n",angle[0],angle[1],angle[2]);
- break;
- default: break;
- }
- }
-
- }
- // sprintf(dis_main," ");
- // OLED_ShowString(0,0,(u8*)dis_main);
- // sprintf(dis_main," ");
- // OLED_ShowString(0,2,(u8*)dis_main);
-
- //显示角度
- sprintf(dis_main,"X=%d Y=%d ",(int )(angle[0]),(int)(angle[1]));
- OLED_ShowString(0,0,(u8*)dis_main);
- sprintf(dis_main,"Z=%d T=%d ",(int )(angle[2]),(int)(T));
- OLED_ShowString(0,2,(u8*)dis_main);
-
-
-
-
-
-
-
- }
- }
复制代码
所有资料51hei提供下载:
JY61测试代码.7z
(194.9 KB, 下载次数: 30)
|