请选择 进入手机版 | 继续访问电脑版

你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【我的STM32U5 项目秀】+04-MPU6050在STM32U5上的移植

[复制链接]
shenxiaolin_mai 发布时间:2023-2-6 23:36

今天晚上花了一晚上的时间做了简单地移植,目前算是成功了,但是代码以及上位机的展示还是比较卡顿,不知道为什么,可能还是跟STM32U575板载的串口有关系,经常链接不上,上电之后需要复位一次,才可正常运行,还有就是MPU6050的采样频率不宜过快,否则会导致MPU6050的fifo溢出,从而导致后面无法正常读取数据,造成假死的现象,

在测试过程中,我使用的PC8、PC9两个引脚作为通信口,模拟IIC总线,完成6050的数据读取,再通过STM32U575自带的串口将数据上送出来,按照匿名四轴飞行器的格式将数据上送,并且使用匿名的上位机软件做显示,首先上一个图,和大家分享一下:1.png

测试的时候有卡顿,源码将通过附件的形式分享给大家,欢迎大家一起测试,一起来玩。整体的代码比较简单,涉及到的计算也是原子那边写好的,稍微做改动就可以用了。具体的代码结构如下图

2.png

3.png

这里就代码就只贴主函数的代码了,有兴趣的可以直接下载附件

``

include "stm32u5xx_ll_icache.h"

include "stm32u5xx_ll_pwr.h"

include "stm32u5xx_ll_crs.h"

include "stm32u5xx_ll_rcc.h"

include "stm32u5xx_ll_bus.h"

include "stm32u5xx_ll_system.h"

include "stm32u5xx_ll_exti.h"

include "stm32u5xx_ll_cortex.h"

include "stm32u5xx_ll_utils.h"

include "stm32u5xx_ll_dma.h"

include "stm32u5xx_ll_gpio.h"

include "stm32u5xx_ll_lpgpio.h"

include "Tim.h"

//

include "mpu6050.h"

include "inv_mpu.h"

include "inv_mpu_dmp_motion_driver.h"

/! \brief main function \param[in] none \param[out] none \retval none / float pitch,roll,yaw; //欧拉角 short aacx,aacy,aacz; //加速度传感器原始数据 short gyrox,gyroy,gyroz; //陀螺仪原始数据 short temp; //温度

//发送加速度传感器数据和陀螺仪数据 //aacx,aacy,aacz:x,y,z三个方向上面的加速度值 //gyrox,gyroy,gyroz:x,y,z三个方向上面的陀螺仪值

void usart1_send_char(unsigned char c) { LL_USART_TransmitData8(USART1, (unsigned char)c); while(RESET == LL_USART_IsActiveFlag_TC(USART1));

} //传送数据给匿名四轴上位机软件(V2.6版本) //fun:功能字. 0XA0~0XAF //data:数据缓存区,最多28字节!! //len:data区有效数据个数 void usart1_niming_report(unsigned char fun,unsigned char*data,unsigned char len) { unsigned char send_buf[32]; unsigned char i; if(len>28) return; //最多28字节数据 send_buf[len+3]=0; //校验数置零 send_buf[0]=0X88; //帧头 send_buf[1]=fun; //功能字 send_buf[2]=len; //数据长度 for(i=0;i<len;i++)send_buf[3+i]=data[i]; //复制数据 for(i=0;i<len+3;i++) send_buf[len+3]+=send_buf[i]; //计算校验和 for(i=0;i<len+4;i++) usart1_send_char(send_buf[i]); //发送数据到串口1 }

void mpu6050_send_data(short aacx,short aacy,short aacz,short gyrox,short gyroy,short gyroz) { unsigned char tbuf[12]; tbuf[0]=(aacx>>8)&0XFF; tbuf[1]=aacx&0XFF; tbuf[2]=(aacy>>8)&0XFF; tbuf[3]=aacy&0XFF; tbuf[4]=(aacz>>8)&0XFF; tbuf[5]=aacz&0XFF; tbuf[6]=(gyrox>>8)&0XFF; tbuf[7]=gyrox&0XFF; tbuf[8]=(gyroy>>8)&0XFF; tbuf[9]=gyroy&0XFF; tbuf[10]=(gyroz>>8)&0XFF; tbuf[11]=gyroz&0XFF; usart1_niming_report(0XA1,tbuf,12);//自定义帧,0XA1 }

//通过串口1上报结算后的姿态数据给电脑 //aacx,aacy,aacz:x,y,z三个方向上面的加速度值 //gyrox,gyroy,gyroz:x,y,z三个方向上面的陀螺仪值 //roll:横滚角.单位0.01度。 -18000 -> 18000 对应 -180.00 -> 180.00度 //pitch:俯仰角.单位 0.01度。-9000 - 9000 对应 -90.00 -> 90.00 度 //yaw:航向角.单位为0.1度 0 -> 3600 对应 0 -> 360.0度 void usart1_report_imu(short aacx,short aacy,short aacz,short gyrox,short gyroy,short gyroz,short roll,short pitch,short yaw) { unsigned char tbuf[28]; unsigned char i; for(i=0;i<28;i++)tbuf[i]=0;//清0 tbuf[0]=(aacx>>8)&0XFF; tbuf[1]=aacx&0XFF; tbuf[2]=(aacy>>8)&0XFF; tbuf[3]=aacy&0XFF; tbuf[4]=(aacz>>8)&0XFF; tbuf[5]=aacz&0XFF; tbuf[6]=(gyrox>>8)&0XFF; tbuf[7]=gyrox&0XFF; tbuf[8]=(gyroy>>8)&0XFF; tbuf[9]=gyroy&0XFF; tbuf[10]=(gyroz>>8)&0XFF; tbuf[11]=gyroz&0XFF; tbuf[18]=(roll>>8)&0XFF; tbuf[19]=roll&0XFF; tbuf[20]=(pitch>>8)&0XFF; tbuf[21]=pitch&0XFF; tbuf[22]=(yaw>>8)&0XFF; tbuf[23]=yaw&0XFF; usart1_niming_report(0XAF,tbuf,28);//飞控显示帧,0XAF }

int main(void) { LL_AHB3_GRP1_EnableClock(LL_AHB3_GRP1_PERIPH_PWR); SystemClock_Config(); SystemPower_Config(); MX_GPIO_Init(); MX_TIM1_Init(); MX_USART1_UART_Init(); MX_ICACHE_Init(); printf("STM32U575 MPU6050 Test Demo!\r\n"); //---- MPU_Init(); //初始化MPU6050 mpu_dmp_init(); while(1) { if(mpu_dmp_get_data(&pitch,&roll,&yaw)==0) { MPU_Get_Accelerometer(&aacx,&aacy,&aacz); //得到加速度传感器数据 MPU_Get_Gyroscope(&gyrox,&gyroy,&gyroz); //得到陀螺仪数据 mpu6050_send_data(aacx,aacy,aacz,gyrox,gyroy,gyroz);//用自定义帧发送加速度和陀螺仪原始数据 usart1_report_imu(aacx,aacy,aacz,gyrox,gyroy,gyroz,(int)(roll100),(int)(pitch100),(int)(yaw*10)); } //
// MPU_IIC_SDA_H; // MPU_IIC_SCL_H; // LL_GPIO_ResetOutputPin(LED1_GPIO_Port, LED1_Pin); // delay_1ms(200); // MPU_IIC_SDA_L; // MPU_IIC_SCL_L; // LL_GPIO_SetOutputPin(LED1_GPIO_Port,LED1_Pin); delay_1ms(20);

} / USER CODE END 3 / }

ifdef USE_FULL_ASSERT

/**

  • @brief Reports the name of the source file and the source line number
  • where the assert_param error has occurred.
    • @param file: pointer to the source file name
    • @param line: assert_param error line source number
    • @retval None / void assert_failed(uint8_t file, uint32_t line) { / USER CODE BEGIN 6 / / User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d", file, line) /

/ Infinite loop / while (1) { } / USER CODE END 6 / }

endif / USE_FULL_ASSERT /

这里将就吧代码发送上来,欢迎大家一起来玩:

代码:Demo-MPU6050-STM32U575.rar

收藏 评论0 发布时间:2023-2-6 23:36

举报

0个回答
关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新和工艺
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版