陀螺仪JY62底层代码

麦克斯同学RARJY62底层代码.rar  2.8KB

资源介绍:

JY62底层驱动程序,内容简洁,亲测能用。代码是基于STM32F103标准库的,可移植性强,直接把我的文件添加到你的工程中就能用。可以移植到其他型号的单片机中使用,例如:TI的TM4C123,只是底层函数不一样,操作方法是一样的,移植起来很简单。

#include "mt_jy62.h" #include <string> #include <stdio> #include "hal_usart.h" #include "mt_pid.h" static int32_t n = 0 ; int32_t state = 0; static uint8_t buffer[11]; imu_data_t acceleration, gyroscope, angle; int32_t k = 0; char analysis_flag = 0; void imu_analysis(void) { static float last_angle_z = 0, angle_z = 0; switch (buffer[1]) { case 0x51: acceleration.x = (float )((int16_t)(buffer[2]|(buffer[3]&lt;<8 xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed> 90) { // k = -1; k--; } if (last_angle_z > 90 && angle_z &lt; -90) { // k = 1; k++; } last_angle_z = angle_z; angle.z = angle_z + k*360; } static uint8_t get_verify_code() { uint32_t sum = 0; for (uint8_t i = 0; i &lt; 10; ++i) { sum += buffer[i]; } return sum&0xff; } void USART1_IRQHandler(void) //串口3中断服务程序 { uint32_t data; if(USART_GetITStatus(USART1,USART_IT_RXNE) != RESET) { data = USART_ReceiveData(USART1)&0xff; if (state == ERROR) { n = 0; } buffer[n] = (uint8_t)data; if (n == 0) { if (buffer[0] == 0x55) { state = DOING; } else { state = ERROR; } } else if (n == 10) { if (buffer[10] == get_verify_code()) { state = SUCCESS; } else { state = ERROR; } } // else // { // state = ERROR; // } n += 1; if (state == SUCCESS) { imu_analysis(); //analysis_flag = 1; state = ERROR; } else { analysis_flag = 0; } USART_ClearITPendingBit(USART1,USART_IT_RXNE); } }
100+评论
captcha