找回密码
 立即注册

QQ登录

只需一步,快速开始

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

STM32测试MPU6050模块源码

[复制链接]
跳转到指定楼层
楼主
ID:217972 发表于 2017-7-7 19:15 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
已成功程序,望顶
所有源码资料51hei提供下载:
MPU6050 I2C读取数据 -精简版.7z (202.86 KB, 下载次数: 22)

单片机源程序如下:

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

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



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



  27. /* Private function prototypes -----------------------------------------------*/

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







  35. /* Private functions ---------------------------------------------------------*/

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

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

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

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






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

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

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


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

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


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

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

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

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

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

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

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

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

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

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

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


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


  132. }



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

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

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

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

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



  159.   
  160. }


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

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


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

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





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

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

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

  217. }


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

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



  233. /*定义 fputc 此函数为printf所用*/
  234. /*
  235. printf默认的输出设备是显示器,如果想用这个标准的输出函数向串口发送数据,
  236. 需要改写fputc这个函数。
  237. */
  238. int fputc(int ch,FILE *f)
  239. ……………………

  240. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码



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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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