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

【经验分享】STM32开源代码——MPU6050程序

[复制链接]
STMCU小助手 发布时间:2022-2-5 23:00
  1. #include "led.h"# d' {" u3 c0 i$ d
  2. #include "delay.h"9 i! u  ~  I! g0 o  C3 b* Q# Q; x
  3. #include "key.h"
    $ V! D. P1 ~) l) x& a' h  n4 Z
  4. #include "sys.h", m, n' |) F) _3 C: S
  5. #include "usart.h"9 M/ V! p3 a' G' S
  6. #include "mpu6050.h"3 a+ U: y, n- H& n4 g
  7. #include "inv_mpu.h"! h0 z  s" h) [9 v
  8. #include "inv_mpu_dmp_motion_driver.h"7 F* |3 D0 k1 G; R" u* J% i7 I
  9. /************************************************
    5 Q4 |; _1 q$ _" I/ d# L
  10. ALIENTEK精英STM32开发板7 t6 ]9 l0 s' E
  11. 作者:唯恋殊雨
    ! I/ ~6 W2 I, `4 r8 Z6 A
  12. CSDN博客:https://blog.csdn.net/tichimi3375& T& T& I8 ]& {) S# l
  13. SCL-PB6( M* Q6 E7 l/ w' P
  14. SDA-PB72 N6 C( e3 o, F, V% w, i1 L$ C1 s
  15. ************************************************/
    - ]& {1 D9 b6 E8 G  h
  16. int main(void)
    8 s8 a) {3 ]9 p  S3 W& i. u9 }
  17. {
    2 i. ?" B: i& Q
  18.     u8 t=0,report=1;            //默认开启上报7 f6 T$ ?/ b$ q* v! h) f; ^
  19.     u8 key;% ?% E# A8 z8 S; h. z
  20.     float pitch,roll,yaw;         //欧拉角: W. T& V, @, ^: U% g) \- z1 y% q* j
  21.     short aacx,aacy,aacz;        //加速度传感器原始数据) Y) w' _; `5 F; n5 A+ g
  22.     short gyrox,gyroy,gyroz;    //陀螺仪原始数据
    5 A; }7 f/ t7 K5 L
  23.     short temp;                    //温度
    6 ^9 N/ H8 c; K' u3 `1 s. n  {
  24.     SystemInit();
    6 B( u9 @7 e; Z# v+ m
  25.     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);     //设置NVIC中断分组2:2位抢占优先级,2位响应优先级, W  q/ ]% [) `. _% @8 t2 b
  26.     uart_init(115200);         //串口初始化为500000% u/ ~! _7 a+ T7 @1 X* @
  27.     delay_init();    //延时初始化5 y7 M$ q/ d. B3 V  g
  28.     LED_Init();                      //初始化与LED连接的硬件接口. q/ c  `7 a0 Z* Q* Z
  29.     KEY_Init();                    //初始化按键9 D: W; u- z! G% E/ e# W( W( _  b1 U
  30.     MPU_Init();                    //初始化MPU6050
    ) `" V8 C1 y, h  N6 g0 C
  31.     while(mpu_dmp_init())& Y. t9 Q0 z6 c8 y
  32.      {& b1 W7 j' T% F& _, y# Q) {* N7 }8 ]
  33.         printf("\n\rMPU6050 Error\n\r");; g1 k  `) c0 j, C! @5 Q. W  n
  34.         delay_ms(200);, t3 z; ~: z  o0 m
  35.     }
    , w5 _3 {& D( g8 A! o- w. y
  36.      while(1)& c+ I% J5 W/ @/ p  ?( H
  37.     {
    , k* K. S$ d* M! R/ Z
  38.         key=KEY_Scan(0);
    0 o6 ?/ b1 U2 H8 L
  39.         if(key==KEY0_PRES)
    - ?( ~6 }% J* |9 a# ~# g9 t5 P
  40.         {
    # C# h/ H. E  c- K
  41.             report=!report;
    & p* ]# E- n/ F, p
  42.             if(report)printf("\n\rUPLOAD ON \n\r");
    ) K: }% [- |$ O/ n- V6 K7 e
  43.             else printf("\n\rUPLOAD OFF\n\r");9 ~2 j1 s3 e9 D0 U7 m2 Z0 f
  44.         }9 j4 h) V6 x* b7 [/ `
  45.         if(mpu_dmp_get_data(&pitch,&roll,&yaw)==0)& J# b" P+ O7 v6 H3 l
  46.         {8 m0 x4 W* l, L7 b+ B; e# ]
  47.             temp=MPU_Get_Temperature();    //得到温度值7 o3 c2 E+ S; D- |& @# I
  48.             MPU_Get_Accelerometer(&aacx,&aacy,&aacz);    //得到加速度传感器数据
    # M% l9 y0 I9 y) e% M
  49.             MPU_Get_Gyroscope(&gyrox,&gyroy,&gyroz);    //得到陀螺仪数据
    ! j0 `. `, J* C$ _& D, r
  50.             if(report)mpu6050_send_data(aacx,aacy,aacz,gyrox,gyroy,gyroz);//用自定义帧发送加速度和陀螺仪原始数据
    ( s4 `. i% J& g' ~* r9 q
  51.             if(report)usart1_report_imu(aacx,aacy,aacz,gyrox,gyroy,gyroz,(int)(roll*100),(int)(pitch*100),(int)(yaw*10));
    8 Z' q8 i  D/ M" k" I
  52.             if((t%10)==0)
    + z4 ], Q& I$ N: K. {
  53.             {
    8 A5 _, R- {6 B" w
  54.                 printf("\n\rtemp:%f\n\r",temp/100.0);8 J3 |7 t- C5 A1 n0 n" V) x
  55.                 printf("\n\rpitch:%f\n\r",pitch*10);
    ' a  ~( l( I: @5 B/ y% L! c; e# L
  56.                 printf("\n\rroll:%f\n\r",roll*10);: T+ E7 z$ @( B7 H9 r3 J
  57.                 printf("\n\ryaw:%f\n\r",yaw*10);
    1 H) A! }3 ?% R  _; d1 v* R
  58.                 t=0;
    0 _6 C% q* b. E. V% w- [1 j1 e' ?
  59.             }
    # e/ Z! g, q1 h2 T" ]- B
  60.         }
    . u; G: Z8 E# e# H  x0 _
  61.         t++;& e- F# l6 @# ~# R! e
  62.     }" p5 r; E! v9 ~& B5 a" Q! ?
  63. }
复制代码
. U' g: q& ?. y

& q% `7 a, i2 D) ]9 J8 o9 i
赞 收藏 评论0 发布时间:2022-2-5 23:00

举报

0个回答

所属标签

相似技术帖

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版