找回密码
 注册

QQ登录

只需一步,快速开始

搜索

STM32测试MPU6050模块串口输出的程序

[复制链接]
coolice 发表于 2021-12-15 16:20:01 | 显示全部楼层 |阅读模式
注意事项:
(1)主程序编译会出现缺乏"stm32f10x_lib.h"文件
解决办法:在附件中主程序目录添加附件库"STM32F10x",在target opition中的C/C++选项卡中的include path路径中添加"STM32F10x"路径。
(2)MPU模块必须与STM32主控板共地,也就是说用板载电源供电最好。(我原先用的单独供电模块,结果串口输出一直为零)
主程序和库下载:
STM32-串口输出MPU-6050.zip (1.86 MB, 售价: 3 E币)
STM32F10x.zip (140.98 KB)

部分程序代码:完整程序请下载相关附件
  1. ********************************************************************************
  2. ** 函数名称 : USART1_Configuration(void)
  3. ** 函数功能 : 串口1初始化
  4. ** 输    入        : 无
  5. ** 输    出        : 无
  6. ** 返    回        : 无
  7. ********************************************************************************
  8. */
  9. void USART1_Configuration(void)
  10. {

  11. USART_InitTypeDef USART_InitStructure;
  12. USART_ClockInitTypeDef  USART_ClockInitStructure;

  13. RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 |RCC_APB2Periph_USART1, ENABLE  );

  14. USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;                        // 时钟低电平活动
  15. USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;                                // 时钟低电平
  16. USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;                                // 时钟第二个边沿进行数据捕获
  17. USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;                // 最后一位数据的时钟脉冲不从SCLK输出
  18. /* Configure the USART1 synchronous paramters */
  19. USART_ClockInit(USART1, &USART_ClockInitStructure);                                        // 时钟参数初始化设置
  20.                                                                                                                                          
  21. USART_InitStructure.USART_BaudRate = 115200;                                                  // 波特率为:115200
  22. USART_InitStructure.USART_WordLength = USART_WordLength_8b;                          // 8位数据
  23. USART_InitStructure.USART_StopBits = USART_StopBits_1;                                  // 在帧结尾传输1个停止位
  24. USART_InitStructure.USART_Parity = USART_Parity_No ;                                  // 奇偶失能
  25. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;        // 硬件流控制失能

  26. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;                  // 发送使能+接收使能
  27. /* Configure USART1 basic and asynchronous paramters */
  28. USART_Init(USART1, &USART_InitStructure);
  29.    
  30.   /* Enable USART1 */
  31. USART_ClearFlag(USART1, USART_IT_RXNE);                         //清中断,以免一启用中断后立即产生中断
  32. USART_ITConfig(USART1,USART_IT_RXNE, ENABLE);                //使能USART1中断源
  33. USART_Cmd(USART1, ENABLE);                                                        //USART1总开关:开启
  34. }


  35. /*
  36. ********************************************************************************
  37. ** 函数名称 : NVIC_Configuration(void)
  38. ** 函数功能 : 中断初始化
  39. ** 输    入        : 无
  40. ** 输    出        : 无
  41. ** 返    回        : 无
  42. ********************************************************************************
  43. */
  44. void NVIC_Configuration(void)
  45. {
  46.   NVIC_InitTypeDef NVIC_InitStructure;  
  47.   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

  48.   NVIC_InitStructure.NVIC_IRQChannel = WWDG_IRQChannel;
  49.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  50.   NVIC_Init(&NVIC_InitStructure);

  51. }

  52. /*
  53. ********************************************************************************
  54. ** 函数名称 : WWDG_Configuration(void)
  55. ** 函数功能 : 看门狗初始化
  56. ** 输    入        : 无
  57. ** 输    出        : 无
  58. ** 返    回        : 无
  59. ********************************************************************************
  60. */
  61. void WWDG_Configuration(void)
  62. {
  63.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, ENABLE);        
  64.   WWDG_SetPrescaler(WWDG_Prescaler_8);                      //  WWDG clock counter = (PCLK1/4096)/8 = 244 Hz (~4 ms)  
  65.   WWDG_SetWindowValue(0x41);                                 // Set Window value to 0x41
  66.   WWDG_Enable(0x50);                       // Enable WWDG and set counter value to 0x7F, WWDG timeout = ~4 ms * 64 = 262 ms
  67.   WWDG_ClearFlag();                               // Clear EWI flag
  68.   WWDG_EnableIT();                               // Enable EW interrupt
  69. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|小黑屋|ELEOK |网站地图

GMT+8, 2024-4-23 18:27

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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