找回密码
 立即注册

QQ登录

只需一步,快速开始

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

STM32F103C8T6平衡小车(HAL库版)源程序

[复制链接]
跳转到指定楼层
楼主
ID:466381 发表于 2021-3-9 01:17 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
最近在弄平衡小车,然后移植了一下平衡小车的代码到cubemx上,现在发上来供大家学习。

  1. /* USER CODE END Header */
  2. /* Includes ------------------------------------------------------------------*/
  3. #include "main.h"
  4. #include "adc.h"
  5. #include "tim.h"
  6. #include "usart.h"
  7. #include "gpio.h"

  8. /* Private includes ----------------------------------------------------------*/
  9. /* USER CODE BEGIN Includes */
  10. float Voltage;                                                                                                          //电池电压采样相关的变量
  11. float pitch,roll,yaw;                                                                                 //欧拉角
  12. short aacx,aacy,aacz;                                                                                //加速度传感器原始数据
  13. short gyrox,gyroy,gyroz;                                                                        //陀螺仪原始数据
  14. float UltrasonicWave_Distance;                              //超声波测距
  15. int   Encoder_Left,Encoder_Right;        //左右编码器的脉冲计数
  16. int          Moto1,Moto2;                                                                                    //计算出来的最终赋给电机的PWM
  17. int          Flag_Qian,Flag_Hou,Flag_Left,Flag_Right; //蓝牙遥控相关的变量
  18. unsigned char flag_UltrasonicWave;
  19. uint8_t RECEIVE_SJJ;
  20. /* USER CODE END Includes */

  21. /* Private typedef -----------------------------------------------------------*/
  22. /* USER CODE BEGIN PTD */

  23. /* USER CODE END PTD */

  24. /* Private define ------------------------------------------------------------*/
  25. /* USER CODE BEGIN PD */
  26. /* USER CODE END PD */

  27. /* Private macro -------------------------------------------------------------*/
  28. /* USER CODE BEGIN PM */

  29. /* USER CODE END PM */

  30. /* Private variables ---------------------------------------------------------*/

  31. /* USER CODE BEGIN PV */

  32. /* USER CODE END PV */

  33. /* Private function prototypes -----------------------------------------------*/
  34. void SystemClock_Config(void);
  35. /* USER CODE BEGIN PFP */

  36. /* USER CODE END PFP */

  37. /* Private user code ---------------------------------------------------------*/
  38. /* USER CODE BEGIN 0 */

  39. /* USER CODE END 0 */

  40. /**
  41.   * @brief  The application entry point.
  42.   * @retval int
  43.   */
  44. int main(void)
  45. {
  46.   /* USER CODE BEGIN 1 */

  47.   /* USER CODE END 1 */

  48.   /* MCU Configuration--------------------------------------------------------*/

  49.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  50.   HAL_Init();

  51.   /* USER CODE BEGIN Init */

  52.   /* USER CODE END Init */

  53.   /* Configure the system clock */
  54.   SystemClock_Config();

  55.   /* USER CODE BEGIN SysInit */

  56.   /* USER CODE END SysInit */

  57.   /* Initialize all configured peripherals */
  58.   MX_GPIO_Init();
  59.   MX_TIM2_Init();
  60.   MX_TIM4_Init();
  61.   MX_TIM1_Init();
  62.   MX_USART1_UART_Init();
  63.   MX_ADC1_Init();
  64.   MX_TIM3_Init();
  65.   /* USER CODE BEGIN 2 */
  66.         HAL_UART_Receive_IT(&huart1,&RECEIVE_SJJ,1);
  67.   /* USER CODE END 2 */

  68.   /* Infinite loop */
  69.   /* USER CODE BEGIN WHILE */
  70.                
  71. /*****************************************************************************/               
  72.         /*uart3_init(115200);            //=====初始化串口3
  73.         NVIC_Configuration();                                         //=====中断优先级分组,其中包含了所有的中断优先级的配置,方便管理和一次性修改。
  74.         Adc_Init();                    //=====初始化ADC
  75.         Encoder_Init_TIM2();           //=====初始化编码器2
  76.         Encoder_Init_TIM4();           //=====初始化编码器4
  77.         Timer3_Init(5000,7199);                     //=====超声波定时器初始化*/
  78.         OLED_Init();                   //=====OLED初始化
  79.         OLED_Clear();                                                                         //=====OLED清屏
  80.         MPU_Init();                                                                     //=====初始化MPU6050
  81.         mpu_dmp_init();                                                                 //=====初始化MPU6050的DMP模式                                         
  82.         //UltrasonicWave_Configuration();//=====初始化超声波的硬件IO口
  83.         //TIM1_PWM_Init(7199,0);                            //=====初始化PWM 10KHZ,用于驱动电机。
  84.         delay_ms(1000);                                                                 //=====延时1s 解决小车上电轮子乱转的问题
  85.         delay_ms(1000);                                                                 //=====延时1s 解决小车上电轮子乱转的问题
  86.         Motor_Init();                                                                         //=====初始化与电机连接的硬件IO接口
  87.         //MPU6050_EXTI_Init();                                         //=====MPU6050 5ms定时中断初始化
  88.         oled_first_show();                                           //只需要显示一次的字符,在此刷新一次即可。        
  89.   while (1)
  90.   {
  91.     /* USER CODE END WHILE */

  92.     /* USER CODE BEGIN 3 */
  93.                 oled_show();
  94.                 //Set_Pwm(7000,7000);
  95.                 delay_ms(50); //20HZ的显示频率,屏幕无需时刻刷新。
  96.   }
  97.   /* USER CODE END 3 */
  98. }

  99. /**
  100.   * @brief System Clock Configuration
  101.   * @retval None
  102.   */
  103. void SystemClock_Config(void)
  104. {
  105.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  106.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  107.   RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  108.   /** Initializes the RCC Oscillators according to the specified parameters
  109.   * in the RCC_OscInitTypeDef structure.
  110.   */
  111.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  112.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  113.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  114.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  115.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  116.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  117.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  118.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  119.   {
  120.     Error_Handler();
  121.   }
  122.   /** Initializes the CPU, AHB and APB buses clocks
  123.   */
  124.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  125.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  126.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  127.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  128.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  129.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  130.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  131.   {
  132.     Error_Handler();
  133.   }
  134.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  135.   PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
  136.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  137.   {
  138.     Error_Handler();
  139.   }
  140.   /** Enables the Clock Security System
  141.   */
  142.   HAL_RCC_EnableCSS();
  143. }

  144. /* USER CODE BEGIN 4 */

  145. /* USER CODE END 4 */

  146. /**
  147.   * @brief  This function is executed in case of error occurrence.
  148.   * @retval None
  149.   */
  150. void Error_Handler(void)
  151. {
  152.   /* USER CODE BEGIN Error_Handler_Debug */
  153.   /* User can add his own implementation to report the HAL error return state */
  154.   __disable_irq();
  155.   while (1)
  156.   {
  157.   }
  158.   /* USER CODE END Error_Handler_Debug */
  159. }

  160. #ifdef  USE_FULL_ASSERT
  161. /**
  162.   * @brief  Reports the name of the source file and the source line number
  163.   *         where the assert_param error has occurred.
  164.   * @param  file: pointer to the source file name
  165.   * @param  line: assert_param error line source number
  166.   * @retval None
  167.   */
  168. void assert_failed(uint8_t *file, uint32_t line)
  169. {
  170.   /* USER CODE BEGIN 6 */
  171.   /* User can add his own implementation to report the file name and line number,
  172.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  173.   /* USER CODE END 6 */
  174. }
  175. #endif /* USE_FULL_ASSERT */

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


CHAOJIPINGHENGCHE.7z

5.2 MB, 下载次数: 89, 下载积分: 黑币 -5

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

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

使用道具 举报

沙发
ID:332674 发表于 2021-3-21 14:22 | 只看该作者
大佬可以加个好友交流下吗?我也是用hal库写,为什么您的6050程序我移植过去用不了呢?求助
回复

使用道具 举报

板凳
ID:332674 发表于 2021-3-21 14:23 | 只看该作者
大佬能加个好友交流下吗?我也是用hal库在写最近,但是为什么移植你的6050代码过去用不了呢?求助
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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