找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2795|回复: 1
打印 上一主题 下一主题
收起左侧

MPU6050 I2C读取数据 精简版

[复制链接]
跳转到指定楼层
楼主
ID:224669 发表于 2017-8-4 12:59 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
I2C读取MPU6050:
完整代码下载:
MPU6050 I2C读取数据 -精简版.rar (396.46 KB, 下载次数: 28)


  1. /******************** (C) COPYRIGHT 2007 STMicroelectronics ********************
  2. * File Name          : main.c
  3. * Author             : MCD Application Team
  4. * Version            : V1.0
  5. * Date               : 10/08/2007
  6. * Description        : Main program body
  7. ********************************************************************************
  8. * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  9. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
  10. * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
  11. * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
  12. * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
  13. * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  14. *******************************************************************************/

  15. /* Includes ------------------------------------------------------------------*/
  16. #include "stm32f10x.h"
  17. #include "stdio.h"   //添加文件,为printf 所用
  18. #include "delay.h"
  19. #include "sys.h"
  20. #include "math.h"
  21. #include "MPU6050.h"



  22. USART_InitTypeDef USART_InitStructure;
  23. TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  24. USART_ClockInitTypeDef  USART_ClockInitStructure;
  25. SPI_InitTypeDef  SPI_InitStructure;
  26. EXTI_InitTypeDef EXTI_InitStructure;
  27. ErrorStatus HSEStartUpStatus;



  28. /* Private function prototypes -----------------------------------------------*/

  29. void USART_Configuration(void);
  30. void RCC_Configuration(void);
  31. void GPIO_Configuration(void);
  32. void NVIC_Configuration(void);
  33. void TIM_Configuration(void);
  34. void EXTI_Configuration(void);
  35. void I2C_Configuration(void);







  36. /* Private functions ---------------------------------------------------------*/

  37. /*******************************************************************************
  38. * Function Name  : main
  39. * Description    : Main program
  40. * Input          : None
  41. * Output         : None
  42. * Return         : None
  43. *******************************************************************************/
  44. int main(void)
  45. {
  46.   u8 i=0;
  47.   s16 accgyo[7]={0};
  48. //  u8 u[2]={0};

  49.   delay_init(72);  
  50.   NVIC_Configuration();
  51.   RCC_Configuration();   
  52.   GPIO_Configuration();          
  53.   USART_Configuration();
  54.   I2C_Configuration();

  55.   MPU6050_Initialize();                  //寄存器初始化
  56.    
  57. //  MPU6050_I2C_BufferRead(0xd0, u, MPU6050_RA_WHO_AM_I, 1);
  58. //  printf("$$$   %d",u[0]);

  59.    
  60.   while(1)
  61.   {          
  62.              MPU6050_GetRawAccelGyro(accgyo);
  63.            printf("%10d%10d%10d%10d%10d%10d\r\n",accgyo[0],accgyo[1],accgyo[2],accgyo[3],accgyo[4],accgyo[5]);
  64.            delay_ms(50);
  65.    }
  66. }






  67. /**********   USART   ***********/
  68. void USART_Configuration(void)
  69. {
  70.   
  71.   USART_InitStructure.USART_BaudRate = 38400;//设置波特率为38400
  72.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;// 8位传输
  73.   USART_InitStructure.USART_StopBits = USART_StopBits_1; //1个停止位
  74.   USART_InitStructure.USART_Parity = USART_Parity_No;    //无校验位
  75.   USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
  76.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  77.   USART_Init(USART1, &USART_InitStructure);

  78. //  USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
  79. //  USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
  80. //  USART_ClockInitStructure.USART_CPHA = USART_CPHA_1Edge;
  81. //  USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
  82. //  USART_ClockInit(USART1 , &USART_ClockInitStructure);

  83.   /* Enable USART1  使能串口1*/
  84.   USART_Cmd(USART1, ENABLE);
  85.   USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);  // 若接收数据寄存器满,则产生中断,用以处理归零操作
  86. }


  87. /*********   RCC   *********/
  88. void RCC_Configuration(void)
  89. {                                                                                                                               
  90.   /* RCC system reset(for debug purpose) */
  91.   RCC_DeInit();

  92.   /* Enable HSE */
  93.   RCC_HSEConfig(RCC_HSE_ON);


  94.   while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);
  95.   /* Wait till HSE is ready */
  96.   HSEStartUpStatus = RCC_WaitForHSEStartUp();

  97.   if(HSEStartUpStatus == SUCCESS)
  98.   {
  99.     /* Enable Prefetch Buffer */
  100.     FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

  101.     /* Flash 2 wait state */
  102.     FLASH_SetLatency(FLASH_Latency_2);

  103.     /* HCLK = SYSCLK */
  104.     RCC_HCLKConfig(RCC_SYSCLK_Div1);

  105.     /* PCLK2 = HCLK */
  106.     RCC_PCLK2Config(RCC_HCLK_Div1);

  107.     /* PCLK1 = HCLK/2 */
  108.     RCC_PCLK1Config(RCC_HCLK_Div2);

  109.     /* PLLCLK = 8MHz * 9 = 72 MHz */
  110.     RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

  111.     /* Enable PLL */
  112.     RCC_PLLCmd(ENABLE);

  113.     /* Wait till PLL is ready */
  114.     while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  115.     {
  116.     }

  117.     /* Select PLL as system clock source */
  118.     RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

  119.     /* Wait till PLL is used as system clock source */
  120.     while(RCC_GetSYSCLKSource() != 0x08)
  121.     {
  122.     }
  123. }


  124.   
  125.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  126.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  127.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  128.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE);
  129.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1 , ENABLE);  
  130.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  131.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
  132.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 ,ENABLE);


  133. }



  134. /************   GPIO   **************/
  135. void GPIO_Configuration(void)
  136. {
  137.   GPIO_InitTypeDef GPIO_InitStructure;

  138.   /* Configure USART1 Tx (PA.09) as alternate function push-pull */
  139.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  140.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  141.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  142.   GPIO_Init(GPIOA, &GPIO_InitStructure);

  143.   /* Configure USART1 Rx (PA.10) as input floating */
  144.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  145.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  146.   GPIO_Init(GPIOA, &GPIO_InitStructure);

  147.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  148.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  149.   GPIO_Init(GPIOA, &GPIO_InitStructure);
  150.   
  151.   /* GPIOB Configuration: Pin10 in Output   LED1*/
  152.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  153.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  154.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  
  155.   GPIO_Init(GPIOB, &GPIO_InitStructure);

  156.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  157.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  158.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;  
  159.   GPIO_Init(GPIOB, &GPIO_InitStructure);



  160.   
  161. }


  162. /**********   SPI  **********/
  163. void SPI1_Init(void)
  164. {

  165. SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  166. SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  167. SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  168. SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;                //无数据传输时,电平为低,必须和CS5532时序一致
  169. SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
  170. SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  171. SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128; //速度不能太快,不然读数出错
  172. SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  173. SPI_InitStructure.SPI_CRCPolynomial = 7;
  174. SPI_Init(SPI1, &SPI_InitStructure);
  175. //使能SPI1
  176. SPI_Cmd(SPI1, ENABLE);  
  177. }


  178. void EXTI_Configuration(void)
  179. {
  180.   GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1);

  181.   /* Configure EXTI Line11 to generate an interrupt on falling edge */  
  182.   EXTI_InitStructure.EXTI_Line = EXTI_Line1;
  183.   EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  184.   EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  185.   EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  186.   EXTI_Init(&EXTI_InitStructure);
  187. }





  188. /*******************************************************************************
  189. * Function Name  : NVIC_Configuration
  190. * Description    : Configures the nested vectored interrupt controller.
  191. * Input          : None
  192. * Output         : None
  193. * Return         : None
  194. *******************************************************************************/
  195. void NVIC_Configuration(void)
  196. {
  197.           NVIC_InitTypeDef NVIC_InitStructure;
  198. #ifdef  VECT_TAB_RAM
  199.   /* Set the Vector Table base location at 0x20000000 */
  200.   NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
  201. #else  /* VECT_TAB_FLASH  */
  202.   /* Set the Vector Table base location at 0x08000000 */
  203.   NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
  204. #endif

  205. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  206.   
  207.   /* Enable the EXTI15_10 Interrupt */
  208.   NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
  209.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  210.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  211.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  212.   NVIC_Init(&NVIC_InitStructure);

  213.     NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  214.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  215.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  216.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  217.   NVIC_Init(&NVIC_InitStructure);

  218. }


  219. void I2C_Configuration(void)
  220. {
  221.         I2C_InitTypeDef  I2C_InitStructure;

  222.         I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
  223.         I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
  224.         I2C_InitStructure.I2C_OwnAddress1 =0xc0; // MPU6050 7-bit adress = 0x68, 8-bit adress = 0xD0;
  225.         I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
  226.         I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  227.         I2C_InitStructure.I2C_ClockSpeed = MPU6050_I2C_Speed;
  228.   
  229.   /* Apply I2C configuration after enabling it */
  230.   I2C_Init(MPU6050_I2C, &I2C_InitStructure);
  231.   /* I2C Peripheral Enable */  
  232.   I2C_Cmd(MPU6050_I2C, ENABLE);
  233. }



  234. /*定义 fputc 此函数为printf所用*/
  235. /*
  236. printf默认的输出设备是显示器,如果想用这个标准的输出函数向串口发送数据,
  237. 需要改写fputc这个函数。
  238. */
  239. int fputc(int ch,FILE *f)
  240. {
  241.     /*Place your implementation of fputc here,
  242.       e.g. write a character to the usart*/
  243.      USART_SendData(USART1, (u8) ch);  
  244.     /* Loop until the end of transmission */
  245.     while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
  246.     return ch;
  247. }





  248. #ifdef  DEBUG
  249. /*******************************************************************************
  250. * Function Name  : assert_failed
  251. * Description    : Reports the name of the source file and the source line number
  252. *                  where the assert_param error has occurred.
  253. * Input          : - file: pointer to the source file name
  254. *                  - line: assert_param error line source number
  255. * Output         : None
  256. * Return         : None
  257. *******************************************************************************/
  258. void assert_failed(u8* file, u32 line)
  259. {
  260.   /* User can add his own implementation to report the file name and line number,
  261.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  262.   /* Infinite loop */
  263.   while (1)
  264.   {
  265.   }
  266. }
  267. #endif

  268. /******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/
复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:228360 发表于 2019-10-16 14:17 | 只看该作者
谢谢分享。
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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