找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2052|回复: 0
收起左侧

MPU60500开发源码(周期更新欧拉角通过USB实时传输至PC)stm32源码

[复制链接]
ID:342958 发表于 2018-6-1 16:33 | 显示全部楼层 |阅读模式
a宝上花钱买的MP60500 stm32原程序,分享给大家
单片机源程序如下:
  1. /***********************************************

  2. 标题: mian.c
  3. 作者: 秋阳电子
  4. 日期: 2017/03/15
  5. 版本:v1.0
  6. MDK-ARM 版本: v4.12
  7. ST 库版本:v3.50  
  8. 功能: 周期更新欧拉角通过USB实时传输至PC
  9. 说明:
  10. *************************************************/
  11. #include "stm32f10x.h"
  12. #include "iic.h"
  13. #include "timer.h"
  14. #include "mpu6050.h"
  15. #include "hmc5883l.h"
  16. #include "ahrs.h"
  17. #include "usb_lib.h"
  18. #include "usb_desc.h"
  19. #include "usb_istr.h"
  20. #include "hw_config.h"
  21. #include "usb_pwr.h"
  22. #include "usb_pwr.h"
  23. #include <math.h>

  24. #define USB

  25. u8 flg_get_senor_data;
  26. u8 out[35]  ={0x5f, 0x60, 0};
  27. s16 gx, gy, gz, ax ,ay, az, mx, my, mz, temperature;
  28. s16 gx_offset, gy_offset, gz_offset, ax_offset, ay_offset, az_offset, mx_offset,  my_offset, mz_offset;
  29. float pitch, roll, yaw;
  30. float f_gx, f_gy, f_gz;
  31. s16 temp;
  32. /******************************************************************************/
  33. void delay(u32 count)
  34. {
  35.   for(; count != 0; count--);
  36. }
  37. /***************************************************************************/
  38. int main(void)
  39. {
  40.   GPIO_InitTypeDef GPIO_InitStructure;
  41.   NVIC_InitTypeDef NVIC_InitStructure;
  42.   u16 j, i, k;

  43.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);
  44.   
  45.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  46.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  47.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  48.    
  49.   GPIO_Init(GPIOA, &GPIO_InitStructure);
  50.   GPIO_SetBits(GPIOA,GPIO_Pin_5);  // blue led

  51. #ifdef USB
  52.   usb_system_init();
  53. #else
  54.   usart_init();
  55. #endif

  56.   delay(0x80000);
  57.                                             
  58.   iic_init();
  59.   timer_init();

  60.   mpu6050_init();
  61.   hmc5883l_init();

  62.   while (1)
  63.   {
  64.     if(flg_get_senor_data)
  65.     {
  66.       flg_get_senor_data = 0;

  67. #ifdef USB
  68.           NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
  69.           NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
  70.           NVIC_Init(&NVIC_InitStructure);
  71. #endif            
  72.          
  73.       mpu6050_get_data(&gx, &gy, &gz, &ax, &ay, &az, &temperature);
  74.       hmc5883l_get_data(&mx, &my ,&mz);

  75. #ifdef USB
  76.           NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
  77.           NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  78.           NVIC_Init(&NVIC_InitStructure);
  79. #endif
  80.      
  81.           gx -= gx_offset;
  82.           gy -= gy_offset;
  83.           gz -= gz_offset;
  84.           ax -= ax_offset;
  85.           ay -= ay_offset;
  86.           az -= az_offset;
  87.           mx -= mx_offset;
  88.           my -= my_offset;
  89.           mz -= mz_offset;
  90.            
  91.       f_gx = gx * GYRO_SCALE;
  92.           f_gy = gy * GYRO_SCALE;
  93.           f_gz = gz * GYRO_SCALE;

  94.           get_euler_angle(f_gx, f_gy, f_gz, ax, ay, az, mx, my, mz, &pitch, &roll, &yaw);

  95.           out[2] = (u8)(gx >> 8);
  96.           out[3] = (u8)(gx);
  97.           out[4] = (u8)(gy >> 8);
  98.           out[5] = (u8)(gy);
  99.           out[6] = (u8)(gz >> 8);
  100.           out[7] = (u8)(gz);
  101.           out[8] = (u8)(ax >> 8);
  102.           out[9] = (u8)(ax);
  103.           out[10] = (u8)(ay >> 8);
  104.           out[11] = (u8)(ay);
  105.           out[12] = (u8)(az >> 8);
  106.           out[13] = (u8)(az);
  107.           out[14] = (u8)(mx >> 8);
  108.           out[15] = (u8)(mx);
  109.           out[16] = (u8)(my >> 8);
  110.           out[17] = (u8)(my);
  111.           out[18] = (u8)(mz >> 8);
  112.           out[19] = (u8)(mz);

  113.           temp = (s16)(pitch * 100);
  114.           out[20] = (u8)(temp >> 8);
  115.           out[21] = (u8)(temp);

  116.           temp = (s16)(roll * 100);
  117.           out[22] = (u8)(temp >> 8);
  118.           out[23] = (u8)(temp);

  119.           temp = (s16)(yaw * 100);
  120.           out[24] = (u8)(temp >> 8);
  121.           out[25] = (u8)(temp);
  122.          
  123.           if(k++ > 5)
  124.           {
  125.             k=0;
  126. #ifdef USB
  127.         USART_To_USB_Send_Data(&out[0], 26);
  128. #else
  129.             for(j = 0; j < 26; j++)
  130.             {
  131.               USART_SendData(USART1, out[j]);
  132.           while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  133.             }
  134. #endif
  135.       }
  136.            
  137.       if(i++ >= 50)
  138.           {
  139.             i = 0;
  140.             GPIOA->ODR ^= (1 << 5);
  141.       }
  142.     }  // end if                                                           
  143.   }  // end while
  144. }
  145. /*************************************************************************************************/
  146. void USB_LP_CAN1_RX0_IRQHandler(void)
  147. {
  148.   USB_Istr();
  149. }
  150. /************************END OF FILE************************************************************/
复制代码

所有资料51hei提供下载:
9轴姿态模块源程序.rar (463.22 KB, 下载次数: 27)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表