找回密码
 立即注册

QQ登录

只需一步,快速开始

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

STM32三路超声波避障程序

[复制链接]
跳转到指定楼层
楼主
ID:380211 发表于 2018-12-24 13:52 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式


单片机源程序如下:
  1. #include "main.h"
  2. #include "tim1.h"
  3. #include "tim3.h"
  4. #include "uart.h"
  5. #include "share.h"
  6. #include "delayus.h"
  7. #include "direction.h"
  8. #include "us_avoid.h"

  9. __IO uint16_t Uart_Upload_Time;//串口上传数据周期ms
  10. void SystemClock_Config(void);      

  11. int main(void)
  12. {
  13.   HAL_Init();
  14.   SystemClock_Config();
  15.         MX_TIM1_Init();//电机驱动PWM
  16.         MX_TIM3_Init();//TIM3输入捕获配置
  17.         Init_DirectionGPIO();//电机驱动芯片IO口使能
  18.         MX_USART3_Init();//串口3使能
  19.         US_GPIOInit();//超声波GPIO口配置
  20.        
  21. //        while(1)
  22. //        {
  23. //                TIM1->CCR1=L_PWM;
  24. //                TIM1->CCR2=R_PWM;
  25. //                ForWard1();
  26. //        }

  27.         while (1)
  28.   {       
  29.                 US_Obstacle_Avoid();//超声波避障
  30.                
  31.                 if(!Uart_Upload_Time)//串口数据周期发送
  32.                 {
  33.                         Uart_Upload_Time=300;//串口数据发送周期ms
  34.                         printf("L=%dcm F=%dcm R=%dcm\r\n",US1_Distance,US2_Distance,US3_Distance);//串口打印数据
  35.                 }
  36.   }
  37. }

  38. /**
  39.   * @brief System Clock Configuration
  40.   * @retval None
  41.   */
  42. void SystemClock_Config(void)
  43. {
  44.   RCC_OscInitTypeDef RCC_OscInitStruct;
  45.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  46.   RCC_PeriphCLKInitTypeDef PeriphClkInit;

  47.     /**Initializes the CPU, AHB and APB busses clocks
  48.     */
  49.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  50.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  51.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  52.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  53.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  54.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  55.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  56.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  57.   {
  58.     _Error_Handler(__FILE__, __LINE__);
  59.   }

  60.     /**Initializes the CPU, AHB and APB busses clocks
  61.     */
  62.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  63.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  64.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  65.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  66.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  67.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  68.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  69.   {
  70.     _Error_Handler(__FILE__, __LINE__);
  71.   }

  72.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  73.   PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV4;
  74.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  75.   {
  76.     _Error_Handler(__FILE__, __LINE__);
  77.   }

  78.     /**Configure the Systick interrupt time
  79.     */
  80.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  81.     /**Configure the Systick
  82.     */
  83.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  84.   /* SysTick_IRQn interrupt configuration */
  85.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  86. }



  87. /**
  88.   * @brief  This function is executed in case of error occurrence.
  89.   * @param  file: The file name as string.
  90.   * @param  line: The line in file as a number.
  91.   * @retval None
  92.   */
  93. // _Error_Handler(__FILE__, __LINE__);
  94. void _Error_Handler(char *file, int line)
  95. {
  96.   /* USER CODE BEGIN Error_Handler_Debug */
  97.   /* User can add his own implementation to report the HAL error return state */
  98.   while(1)
  99.   {
  100.   }
  101.   /* USER CODE END Error_Handler_Debug */
  102. }

  103. #ifdef  USE_FULL_ASSERT
  104. /**
  105.   * @brief  Reports the name of the source file and the source line number
  106.   *         where the assert_param error has occurred.
  107.   * @param  file: pointer to the source file name
  108.   * @param  line: assert_param error line source number
  109.   * @retval None
  110.   */
  111. void assert_failed(uint8_t* file, uint32_t line)
  112. {
  113.   /* USER CODE BEGIN 6 */
  114.   /* User can add his own implementation to report the file name and line number,
  115.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  116.   /* USER CODE END 6 */
  117. }
  118. #endif /* USE_FULL_ASSERT */


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

所有资料51hei提供下载:
Program.7z (2.9 MB, 下载次数: 43)


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

使用道具 举报

沙发
ID:397735 发表于 2019-12-7 23:03 | 只看该作者
感谢老哥
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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