找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1585|回复: 1
收起左侧

基于STM32F103C8T6使用CUBEMX编写超声波测距源程序

[复制链接]
ID:909009 发表于 2021-7-23 10:31 | 显示全部楼层 |阅读模式
给需要学习CUBEMX编写超声波测距

单片机源程序如下:

  1. /* Includes ------------------------------------------------------------------*/
  2. #include "main.h"
  3. #include "tim.h"
  4. #include "usart.h"
  5. #include "gpio.h"

  6. /* Private includes ----------------------------------------------------------*/
  7. /* USER CODE BEGIN Includes */
  8. #include "stdio.h"
  9. #define TRIG_ON HAL_GPIO_WritePin(TRIG_GPIO_Port, TRIG_Pin, GPIO_PIN_SET);
  10. #define TRIG_OFF HAL_GPIO_WritePin(TRIG_GPIO_Port, TRIG_Pin, GPIO_PIN_RESET);
  11. /* USER CODE END Includes */

  12. /* Private typedef -----------------------------------------------------------*/
  13. /* USER CODE BEGIN PTD */
  14. float S1,S2,distance;
  15. /* USER CODE END PTD */

  16. /* Private define ------------------------------------------------------------*/
  17. /* USER CODE BEGIN PD */
  18. /* USER CODE END PD */

  19. /* Private macro -------------------------------------------------------------*/
  20. /* USER CODE BEGIN PM */

  21. /* USER CODE END PM */

  22. /* Private variables ---------------------------------------------------------*/

  23. /* USER CODE BEGIN PV */

  24. /* USER CODE END PV */

  25. /* Private function prototypes -----------------------------------------------*/
  26. void SystemClock_Config(void);
  27. /* USER CODE BEGIN PFP */

  28. /* USER CODE END PFP */

  29. /* Private user code ---------------------------------------------------------*/
  30. /* USER CODE BEGIN 0 */

  31. /* USER CODE END 0 */

  32. /**
  33.   * @brief  The application entry point.
  34.   * @retval int
  35.   */
  36. int main(void)
  37. {
  38.   /* USER CODE BEGIN 1 */

  39.   /* USER CODE END 1 */

  40.   /* MCU Configuration--------------------------------------------------------*/

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

  43.   /* USER CODE BEGIN Init */

  44.   /* USER CODE END Init */

  45.   /* Configure the system clock */
  46.   SystemClock_Config();

  47.   /* USER CODE BEGIN SysInit */

  48.   /* USER CODE END SysInit */

  49.   /* Initialize all configured peripherals */
  50.   MX_GPIO_Init();
  51.   MX_TIM2_Init();
  52.   MX_USART1_UART_Init();
  53.   /* USER CODE BEGIN 2 */
  54.   HAL_TIM_Base_Start_IT(&htim2);
  55.   /* USER CODE END 2 */

  56.   /* Infinite loop */  
  57.   /* USER CODE BEGIN WHILE */
  58.   while (1)
  59.   {   
  60.                 htim2.Instance ->CNT =0;
  61.                 TRIG_OFF ;
  62.                 TRIG_ON ;
  63.                 HAL_Delay(1);
  64.                 TRIG_OFF ;
  65.                 while(HAL_GPIO_ReadPin(ECHO_GPIO_Port,ECHO_Pin)==0);
  66.                 S1= htim2.Instance ->CNT ;
  67.                 while(HAL_GPIO_ReadPin(ECHO_GPIO_Port,ECHO_Pin)==1) ;
  68.                 S2= htim2.Instance ->CNT ;
  69.                 distance=(S2-S1)*0.034/2;
  70.                 printf("Distance is : %0.1fmm\r\n",distance);
  71.                 HAL_Delay(500);
  72.     /* USER CODE END WHILE */
  73.    
  74.     /* USER CODE BEGIN 3 */
  75.   }
  76.   /* USER CODE END 3 */
  77. }

  78. /**
  79.   * @brief System Clock Configuration
  80.   * @retval None
  81.   */
  82. void SystemClock_Config(void)
  83. {
  84.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  85.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  86.   /** Initializes the RCC Oscillators according to the specified parameters
  87.   * in the RCC_OscInitTypeDef structure.
  88.   */
  89.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  90.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  91.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  92.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  93.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
  94.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  95.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  96.   {
  97.     Error_Handler();
  98.   }
  99.   /** Initializes the CPU, AHB and APB buses clocks
  100.   */
  101.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  102.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  103.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  104.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  105.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  106.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  107.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  108.   {
  109.     Error_Handler();
  110.   }
  111. }

  112. /* USER CODE BEGIN 4 */

  113. /* USER CODE END 4 */

  114. /**
  115.   * @brief  This function is executed in case of error occurrence.
  116.   * @retval None
  117.   */
  118. void Error_Handler(void)
  119. {
  120.   /* USER CODE BEGIN Error_Handler_Debug */
  121.   /* User can add his own implementation to report the HAL error return state */
  122.   __disable_irq();
  123.   while (1)
  124.   {
  125.   }
  126.   /* USER CODE END Error_Handler_Debug */
  127. }

  128. #ifdef  USE_FULL_ASSERT
  129. /**
  130.   * @brief  Reports the name of the source file and the source line number
  131.   *         where the assert_param error has occurred.
  132.   * @param  file: pointer to the source file name
  133.   * @param  line: assert_param error line source number
  134.   * @retval None
  135.   */
  136. void assert_failed(uint8_t *file, uint32_t line)
  137. {
  138.   /* USER CODE BEGIN 6 */
  139.   /* User can add his own implementation to report the file name and line number,
  140.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  141.   /* USER CODE END 6 */
  142. }
  143. #endif /* USE_FULL_ASSERT */

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

Keil代码下载:
CHB5.7z (260.09 KB, 下载次数: 38)

评分

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

查看全部评分

回复

使用道具 举报

ID:786547 发表于 2022-4-10 20:30 来自手机 | 显示全部楼层
谁能根据作者的程序画一个仿真图吗?
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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