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

查看: 4155|回复: 6

【HAL库每天一例】第085例:MPU6050陀螺仪(LCD液晶显示&匿名...

[复制链接]

122

主题

129

回帖

0

蝴蝶豆

论坛元老

最后登录
2019-5-28
发表于 2016-8-6 09:18:13 | 显示全部楼层 |阅读模式
本帖最后由 haohao663 于 2016-8-6 09:22 编辑

【HAL库每天一例】系列例程从今天开始持续更新。。。。。
我们将坚持每天至少发布一个基于YS-F1Pro开发板的HAL库例程,
该系列例程将带领大家从零开始使用HAL库,后面会持续添加模块应用例程。
同样的,我们还程序发布基于HAL库的指导文档和视频教程,欢迎持续关注,并提出改进意见。

例程下载:
资料包括程序、相关说明资料以及软件使用截图

百度云盘:https://pan.baidu.com/s/1slN8rIt 密码:u6m1
360云盘:http://yunpan.cn/OcPiRp3wEcA92u密码 cfb6
(硬石YS-F1Pro开发板HAL库例程持续更新\2. 软件设计之高级裸机例程(HAL库版本)\YSF1_HAL-118. MPU6050陀螺仪
/**
  ******************************************************************************
  *                           硬石YS-F1Pro开发板例程功能说明
  *
  *  例程名称: 3. MPU6050陀螺仪(LCD液晶显示&匿名上位机)
  *   
  ******************************************************************************
  * 说明:
  * 本例程配套硬石stm32开发板YS-F1Pro使用。
  *
  * 淘宝:
  * 论坛:硬石电子社区
  * 版权归硬石嵌入式开发团队所有,请勿商用。
  ******************************************************************************
  */

【1】例程简介
  I2C总线是飞利浦公司开发的两线式串行总线。用于连接微控制器和外围设备。它是同步通信的一
种特殊形式,具有接口线少,控制方式简单,器件封装形式小,通信速率较高等优点。通过串行数据
(SDA)线和串行时钟 (SCL)线在连接到总线的器件间传递信息。每个器件都有一个唯一的地址识
别,而且都可以作为一个发送器或接收器
  
【2】跳线帽情况
******* 为保证例程正常运行,必须插入以下跳线帽 **********
丝印编号     IO端口      目标功能引脚        出厂默认设置
  JP1        PA10        TXD(CH340G)          已接入
  JP2        PA9         RXD(CH340G)          已接入
  
【3】操作及现象
使用开发板配套的MINI USB线连接到开发板标示“调试串口”字样的MIMI USB接口(需要安装驱动),
在电脑端打开串口调试助手工具,设置参数为115200 8-N-1。下载完程序之后,将MPU6050接入I2C的
接口,在开发板的LCD显示屏即可看到相应的数值。
/******************* (C) COPYRIGHT 2015-2020 硬石嵌入式开发团队 *****END OF FILE****/
CubeMX_1.jpg
CubeMX_2.jpg
CubeMX_3.jpg
CubeMX_4.jpg
CubeMX_5.jpg
CubeMX_6.jpg
CubeMX_7.jpg
CubeMX_8.jpg
CubeMX_9.jpg
CubeMX_10.jpg
CubeMX_12.jpg



main函数内容

  1. int main(void)
  2. {  
  3.   uint32_t lcdid;
  4.   inv_error_t result;
  5.     unsigned char accel_fsr,  new_temp = 0;
  6.     unsigned short gyro_rate, gyro_fsr;
  7.     unsigned long timestamp;
  8.     struct int_param_s int_param;
  9.   
  10.   HAL_Init();

  11.   SystemClock_Config();
  12.   
  13.   lcdid=BSP_LCD_Init();

  14.   MX_DEBUG_USART_Init();
  15.   
  16.   MX_SPIFlash_Init();
  17.   
  18.     MPU_INT_GPIO_Init();
  19.   
  20.     I2C_Bus_Init();

  21.   printf("LCD ID=0x%08X\n",lcdid);
  22.   printf("mpu 6050 test start");
  23.   
  24.   LCD_Clear(0,0,LCD_DEFAULT_WIDTH,LCD_DEFAULT_HEIGTH,WHITE);
  25.   
  26.   LCD_BK_ON();
  27.    
  28.   LCD_DispString_EN_CH ( 20, 20, (const uint8_t *)"This is a MPU6050 demo", BACKGROUND, BLACK,USB_FONT_24);

  29.     result = mpu_init(&int_param);
  30.   if (result) {
  31.     MPL_LOGE("Could not initialize gyro.result =  %d\n",result);
  32.     LCD_DispString_EN_CH(0,40,(const uint8_t *)"No MPU6050 detceted!Please check the hardware connection.", BACKGROUND,RED,USB_FONT_24);

  33.   }
  34.   else
  35.   {
  36.     LCD_DispString_EN_CH(30,40,(const uint8_t *)"MPU6050 decteted!", BACKGROUND,BLACK,USB_FONT_24);
  37.   }

  38.       result = inv_init_mpl();
  39.       if (result) {
  40.           MPL_LOGE("Could not initialize MPL.\n");
  41.       }

  42.     inv_enable_quaternion();
  43.     inv_enable_9x_sensor_fusion();

  44.     inv_enable_fast_nomot();

  45.     inv_enable_gyro_tc();

  46. #ifdef COMPASS_ENABLED

  47.     inv_enable_vector_compass_cal();
  48.     inv_enable_magnetic_disturbance();
  49. #endif
  50.     inv_enable_eMPL_outputs();

  51.   result = inv_start_mpl();
  52.   if (result == INV_ERROR_NOT_AUTHORIZED) {
  53.       while (1) {
  54.           MPL_LOGE("Not authorized.\n");
  55.       }
  56.   }
  57.   if (result) {
  58.       MPL_LOGE("Could not start the MPL.\n");
  59.   }

  60. #ifdef COMPASS_ENABLED
  61.     mpu_set_sensors(INV_XYZ_GYRO | INV_XYZ_ACCEL | INV_XYZ_COMPASS);
  62. #else
  63.     mpu_set_sensors(INV_XYZ_GYRO | INV_XYZ_ACCEL);
  64. #endif

  65.     mpu_configure_fifo(INV_XYZ_GYRO | INV_XYZ_ACCEL);
  66.     mpu_set_sample_rate(DEFAULT_MPU_HZ);
  67. #ifdef COMPASS_ENABLED

  68.     mpu_set_compass_sample_rate(1000 / COMPASS_READ_MS);
  69. #endif
  70.     mpu_get_sample_rate(&gyro_rate);
  71.     mpu_get_gyro_fsr(&gyro_fsr);
  72.     mpu_get_accel_fsr(&accel_fsr);
  73. #ifdef COMPASS_ENABLED
  74.     mpu_get_compass_fsr(&compass_fsr);
  75. #endif

  76.     inv_set_gyro_sample_rate(1000000L / gyro_rate);
  77.     inv_set_accel_sample_rate(1000000L / gyro_rate);
  78. #ifdef COMPASS_ENABLED

  79.     inv_set_compass_sample_rate(COMPASS_READ_MS * 1000L);
  80. #endif

  81.     inv_set_gyro_orientation_and_scale(
  82.             inv_orientation_matrix_to_scalar(gyro_pdata.orientation),
  83.             (long)gyro_fsr<<15);
  84.     inv_set_accel_orientation_and_scale(
  85.             inv_orientation_matrix_to_scalar(gyro_pdata.orientation),
  86.             (long)accel_fsr<<15);
  87. #ifdef COMPASS_ENABLED
  88.     inv_set_compass_orientation_and_scale(
  89.             inv_orientation_matrix_to_scalar(compass_pdata.orientation),
  90.             (long)compass_fsr<<15);
  91. #endif

  92. #ifdef COMPASS_ENABLED
  93.     hal.sensors = ACCEL_ON | GYRO_ON | COMPASS_ON;
  94. #else
  95.     hal.sensors = ACCEL_ON | GYRO_ON;
  96. #endif
  97.     hal.dmp_on = 0;
  98.     hal.report = 0;
  99.     hal.rx.cmd = 0;
  100.     hal.next_pedo_ms = 0;
  101.     hal.next_compass_ms = 0;
  102.     hal.next_temp_ms = 0;

  103.   get_tick_count(×tamp);

  104.     dmp_load_motion_driver_firmware();
  105.     dmp_set_orientation(
  106.         inv_orientation_matrix_to_scalar(gyro_pdata.orientation));
  107.     dmp_register_tap_cb(tap_cb);

  108.     dmp_register_android_orient_cb(android_orient_cb);

  109.     hal.dmp_features = DMP_FEATURE_6X_LP_QUAT | DMP_FEATURE_TAP |
  110.         DMP_FEATURE_ANDROID_ORIENT | DMP_FEATURE_SEND_RAW_ACCEL | DMP_FEATURE_SEND_CAL_GYRO |
  111.         DMP_FEATURE_GYRO_CAL;
  112.     dmp_enable_feature(hal.dmp_features);
  113.     dmp_set_fifo_rate(DEFAULT_MPU_HZ);
  114.     mpu_set_dmp_state(1);
  115.     hal.dmp_on = 1;


  116.   while(1){
  117.       
  118.     unsigned long sensor_timestamp;
  119.     int new_data = 0;
  120.     if (__HAL_USART_GET_FLAG(&husart_debug, USART_FLAG_RXNE)) {

  121.         __HAL_USART_CLEAR_FLAG(&husart_debug, USART_FLAG_RXNE);
  122.         handle_input();            
  123.     }
  124.     get_tick_count(×tamp);
  125. #ifdef COMPASS_ENABLED

  126.         if ((timestamp > hal.next_compass_ms) && !hal.lp_accel_mode &&
  127.             hal.new_gyro && (hal.sensors & COMPASS_ON)) {
  128.             hal.next_compass_ms = timestamp + COMPASS_READ_MS;
  129.             new_compass = 1;
  130.         }
  131. #endif

  132.         if (timestamp > hal.next_temp_ms) {
  133.             hal.next_temp_ms = timestamp + TEMP_READ_MS;
  134.             new_temp = 1;
  135.         }

  136.     if (hal.motion_int_mode) {

  137.         mpu_lp_motion_interrupt(500, 1, 5);

  138.         inv_accel_was_turned_off();
  139.         inv_gyro_was_turned_off();
  140.         inv_compass_was_turned_off();
  141.         inv_quaternion_sensor_was_turned_off();

  142.         while (!hal.new_gyro) {}

  143.         mpu_lp_motion_interrupt(0, 0, 0);
  144.         hal.motion_int_mode = 0;
  145.     }

  146.     if (!hal.sensors || !hal.new_gyro) {
  147.         continue;
  148.     }   
  149.         if (hal.new_gyro && hal.lp_accel_mode) {
  150.             short accel_short[3];
  151.             long accel[3];
  152.             mpu_get_accel_reg(accel_short, &sensor_timestamp);
  153.             accel[0] = (long)accel_short[0];
  154.             accel[1] = (long)accel_short[1];
  155.             accel[2] = (long)accel_short[2];
  156.             inv_build_accel(accel, 0, sensor_timestamp);
  157.             new_data = 1;
  158.             hal.new_gyro = 0;
  159.         } else if (hal.new_gyro && hal.dmp_on) {
  160.             short gyro[3], accel_short[3], sensors;
  161.             unsigned char more;
  162.             long accel[3], quat[4], temperature;

  163.             dmp_read_fifo(gyro, accel_short, quat, &sensor_timestamp, &sensors, &more);
  164.             if (!more)
  165.                 hal.new_gyro = 0;
  166.             if (sensors & INV_XYZ_GYRO) {

  167.                 inv_build_gyro(gyro, sensor_timestamp);
  168.                 new_data = 1;
  169.                 if (new_temp) {
  170.                     new_temp = 0;

  171.                     mpu_get_temperature(&temperature, &sensor_timestamp);
  172.                     inv_build_temp(temperature, sensor_timestamp);
  173.                 }
  174.             }
  175.             if (sensors & INV_XYZ_ACCEL) {
  176.                 accel[0] = (long)accel_short[0];
  177.                 accel[1] = (long)accel_short[1];
  178.                 accel[2] = (long)accel_short[2];
  179.                 inv_build_accel(accel, 0, sensor_timestamp);
  180.                 new_data = 1;
  181.             }
  182.             if (sensors & INV_WXYZ_QUAT) {
  183.                 inv_build_quat(quat, 0, sensor_timestamp);
  184.                 new_data = 1;
  185.             }
  186.         } else if (hal.new_gyro) {
  187.             short gyro[3], accel_short[3];
  188.             unsigned char sensors, more;
  189.             long accel[3], temperature;

  190.             hal.new_gyro = 0;
  191.             mpu_read_fifo(gyro, accel_short, &sensor_timestamp,
  192.                 &sensors, &more);
  193.             if (more)
  194.                 hal.new_gyro = 1;
  195.             if (sensors & INV_XYZ_GYRO) {

  196.                 inv_build_gyro(gyro, sensor_timestamp);
  197.                 new_data = 1;
  198.                 if (new_temp) {
  199.                     new_temp = 0;

  200.                     mpu_get_temperature(&temperature, &sensor_timestamp);
  201.                     inv_build_temp(temperature, sensor_timestamp);
  202.                 }
  203.             }
  204.             if (sensors & INV_XYZ_ACCEL) {
  205.                 accel[0] = (long)accel_short[0];
  206.                 accel[1] = (long)accel_short[1];
  207.                 accel[2] = (long)accel_short[2];
  208.                 inv_build_accel(accel, 0, sensor_timestamp);
  209.                 new_data = 1;
  210.             }
  211.         }
  212. #ifdef COMPASS_ENABLED
  213.         if (new_compass) {
  214.             short compass_short[3];
  215.             long compass[3];
  216.             new_compass = 0;

  217.             if (!mpu_get_compass_reg(compass_short, &sensor_timestamp)) {
  218.                 compass[0] = (long)compass_short[0];
  219.                 compass[1] = (long)compass_short[1];
  220.                 compass[2] = (long)compass_short[2];

  221.                 inv_build_compass(compass, 0, sensor_timestamp);
  222.             }
  223.             new_data = 1;
  224.         }
  225. #endif

  226.         if (new_data) {
  227.             inv_execute_on_data();

  228.             read_from_mpl();
  229.         }
  230.     }
  231. }
复制代码
匿名上位机截图.JPG


CubeMX_11.jpg
<
回复

使用道具 举报

1

主题

629

回帖

0

蝴蝶豆

金牌会员

最后登录
2020-3-7
发表于 2016-8-6 09:29:58 | 显示全部楼层
谢谢分享
回复 支持 反对

使用道具 举报

100

主题

3617

回帖

1

蝴蝶豆

论坛元老

最后登录
2020-12-8
发表于 2016-8-6 14:17:27 | 显示全部楼层
回复 支持 反对

使用道具 举报

0

主题

7

回帖

0

蝴蝶豆

新手上路

最后登录
2017-2-27
发表于 2016-8-11 15:56:10 | 显示全部楼层
参考下,谢谢分享
回复 支持 反对

使用道具 举报

11

主题

58

回帖

0

蝴蝶豆

中级会员

最后登录
2020-10-16
发表于 2018-9-18 10:38:53 | 显示全部楼层
mark
回复 支持 反对

使用道具 举报

0

主题

6

回帖

0

蝴蝶豆

金牌会员

最后登录
2020-12-8
发表于 2018-11-4 13:58:14 | 显示全部楼层
谢谢分享
回复 支持 反对

使用道具 举报

1

主题

77

回帖

0

蝴蝶豆

中级会员

最后登录
2019-4-2
发表于 2018-11-12 11:25:52 | 显示全部楼层
这个地面站的软件哪里下载的呢
回复 支持 反对

使用道具 举报

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版