久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3238|回復(fù): 1
打印 上一主題 下一主題
收起左側(cè)

MPU6050 I2C讀取數(shù)據(jù) 精簡版

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:224669 發(fā)表于 2017-8-4 12:59 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
I2C讀取MPU6050:
完整代碼下載:
MPU6050 I2C讀取數(shù)據(jù) -精簡版.rar (396.46 KB, 下載次數(shù): 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;//設(shè)置波特率為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);  // 若接收數(shù)據(jù)寄存器滿,則產(chǎn)生中斷,用以處理歸零操作
  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;                //無數(shù)據(jù)傳輸時,電平為低,必須和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; //速度不能太快,不然讀數(shù)出錯
  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 此函數(shù)為printf所用*/
  235. /*
  236. printf默認的輸出設(shè)備是顯示器,如果想用這個標準的輸出函數(shù)向串口發(fā)送數(shù)據(jù),
  237. 需要改寫fputc這個函數(shù)。
  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****/
復(fù)制代碼


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

沙發(fā)
ID:228360 發(fā)表于 2019-10-16 14:17 | 只看該作者
謝謝分享。
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 久草精品视频 | 日韩一区中文字幕 | 中文字幕在线一区 | 婷婷丁香在线视频 | 欧美视频免费在线观看 | 中文字幕在线视频免费观看 | 久久精品中文字幕 | 亚洲精品一区二区 | 视频一区二区中文字幕 | 欧美成人a∨高清免费观看 色999日韩 | 99久久国产免费 | 日韩精品一二三 | 91精品国产色综合久久 | 人人做人人澡人人爽欧美 | 狠狠爱一区二区三区 | 一级做受毛片免费大片 | 日韩精品三区 | www成人免费视频 | 久久久精品国产 | 四虎影视一区二区 | 国产欧美日韩在线播放 | 日本视频在线播放 | 中文字幕二区 | 久久久久国产精品午夜一区 | 女生羞羞网站 | 精品久久久久一区 | 欧美日韩国产高清视频 | 久草久草久草 | 天堂男人av | 在线免费黄色小视频 | 色吧综合网 | 黄色一级大片在线免费看产 | 四虎影院欧美 | 亚洲一区二区电影在线观看 | 欧美女优在线观看 | 欧美性a视频 | 国产免费一区二区三区免费视频 | 亚洲手机视频在线 | 久久久久久女 | 欧美一级一区 | 欧美一级在线视频 |