找回密码
 立即注册

QQ登录

只需一步,快速开始

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

STM32F103驱动超声波程序(HAL库版)TM1640显示

[复制链接]
跳转到指定楼层
楼主
又是很无聊,弄了个超声波出来,主要运用定时器TIM2_CH1捕获,TM1640显示

超声波:TRIG:PA1   ECHO:PA0
TM1640:SCLK:PA11  SDIO:PA12
单位:厘米

制作出来的实物图如下:


单片机源程序如下:

  1. /* Includes ------------------------------------------------------------------*/
  2. #include "main.h"
  3. #include "tim.h"
  4. #include "gpio.h"
  5. #include "tm1640.h"
  6. /* Private includes ----------------------------------------------------------*/
  7. /* USER CODE BEGIN Includes */

  8. /* USER CODE END Includes */

  9. /* Private typedef -----------------------------------------------------------*/
  10. /* USER CODE BEGIN PTD */

  11. /* USER CODE END PTD */

  12. /* Private define ------------------------------------------------------------*/
  13. /* USER CODE BEGIN PD */
  14. /* USER CODE END PD */

  15. /* Private macro -------------------------------------------------------------*/
  16. /* USER CODE BEGIN PM */

  17. /* USER CODE END PM */

  18. /* Private variables ---------------------------------------------------------*/

  19. /* USER CODE BEGIN PV */

  20. /* USER CODE END PV */

  21. /* Private function prototypes -----------------------------------------------*/
  22. void SystemClock_Config(void);
  23. /* USER CODE BEGIN PFP */

  24. /* USER CODE END PFP */

  25. /* Private user code ---------------------------------------------------------*/
  26. /* USER CODE BEGIN 0 */
  27. uint32_t capture_Buf[3] = {0};   //存放计数值
  28. uint8_t capture_Cnt = 0;    //状态标志位
  29. uint32_t high_time;   //高电平时间
  30. /* USER CODE END 0 */

  31. /**
  32.   * @brief  The application entry point.
  33.   * @retval int
  34.   */
  35. int main(void)
  36. {
  37.   /* USER CODE BEGIN 1 */
  38.         uint8_t i;
  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.   /* USER CODE BEGIN 2 */
  53.         TM1640_Init();
  54.   /* USER CODE END 2 */

  55.   /* Infinite loop */
  56.   /* USER CODE BEGIN WHILE */
  57.   while (1)
  58.   {
  59.     /* USER CODE END WHILE */
  60. switch (capture_Cnt){
  61.                 case 0:
  62.                 capture_Cnt++;
  63.                 i=200;
  64.                 HAL_GPIO_WritePin(TRIG_GPIO_Port,TRIG_Pin,GPIO_PIN_SET);
  65.                 while(i--);
  66.                 HAL_GPIO_WritePin(TRIG_GPIO_Port,TRIG_Pin,GPIO_PIN_RESET);
  67.                 __HAL_TIM_SET_CAPTUREPOLARITY(&htim2, TIM_CHANNEL_1, TIM_INPUTCHANNELPOLARITY_RISING);
  68.                 HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_1);        //启动输入捕获       或者: __HAL_TIM_ENABLE(&htim5);

  69.                 break;
  70.                 case 3:
  71.                 high_time = capture_Buf[1]- capture_Buf[0];    //高电平时间
  72.                 //HAL_UART_Transmit(&huart1, (uint8_t *)high_time, 1, 0xffff);   //发送高电平时间
  73.                 high_time*=17;
  74.                 TM1640_display(0,high_time%1000000/100000);
  75.                 TM1640_display(1,high_time%100000/10000);
  76.                 TM1640_display(2,high_time%10000/1000+10);
  77.                 TM1640_display(3,high_time%1000/100);
  78.                 TM1640_display(4,high_time%100/10);
  79.                 TM1640_display(5,high_time%10);
  80.                 HAL_Delay(400);   //延时1S
  81.                 capture_Cnt = 0;  //清空标志位
  82.                 break;

  83.     /* USER CODE BEGIN 3 */
  84.   }
  85.   /* USER CODE END 3 */
  86. }
  87. }
  88. /**
  89.   * @brief System Clock Configuration
  90.   * @retval None
  91.   */
  92. void SystemClock_Config(void)
  93. {
  94.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  95.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  96.   /** Initializes the RCC Oscillators according to the specified parameters
  97.   * in the RCC_OscInitTypeDef structure.
  98.   */
  99.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  100.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  101.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  102.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  103.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  104.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  105.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  106.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  107.   {
  108.     Error_Handler();
  109.   }
  110.   /** Initializes the CPU, AHB and APB buses clocks
  111.   */
  112.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  113.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  114.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  115.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  116.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  117.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  118.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  119.   {
  120.     Error_Handler();
  121.   }
  122.   /** Enables the Clock Security System
  123.   */
  124.   HAL_RCC_EnableCSS();
  125. }

  126. /* USER CODE BEGIN 4 */

  127. /* USER CODE END 4 */

  128. /**
  129.   * @brief  This function is executed in case of error occurrence.
  130.   * @retval None
  131.   */
  132. void Error_Handler(void)
  133. {
  134.   /* USER CODE BEGIN Error_Handler_Debug */
  135.   /* User can add his own implementation to report the HAL error return state */
  136.   __disable_irq();
  137.   while (1)
  138.   {
  139.   }
  140.   /* USER CODE END Error_Handler_Debug */
  141. }

  142. #ifdef  USE_FULL_ASSERT
  143. /**
  144.   * @brief  Reports the name of the source file and the source line number
  145.   *         where the assert_param error has occurred.
  146.   * @param  file: pointer to the source file name
  147.   * @param  line: assert_param error line source number
  148.   * @retval None
  149.   */
  150. void assert_failed(uint8_t *file, uint32_t line)
  151. {
  152.   /* USER CODE BEGIN 6 */
  153.   /* User can add his own implementation to report the file name and line number,
  154.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  155.   /* USER CODE END 6 */
  156. }
  157. #endif /* USE_FULL_ASSERT */

  158. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  159. void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
  160. {
  161.        
  162.         if(TIM2 == htim->Instance)
  163.         {
  164.                                 switch(capture_Cnt){
  165.                                 case 1:
  166.                                 capture_Buf[0] = HAL_TIM_ReadCapturedValue(&htim2,TIM_CHANNEL_1);//获取当前的捕获值.
  167.                                 __HAL_TIM_SET_CAPTUREPOLARITY(&htim2,TIM_CHANNEL_1,TIM_ICPOLARITY_FALLING);  //设置为下降沿捕获
  168.                                 capture_Cnt++;
  169.                                 break;
  170.                                 case 2:
  171.                                 capture_Buf[1] = HAL_TIM_ReadCapturedValue(&htim2,TIM_CHANNEL_1);//获取当前的捕获值.
  172.                                 HAL_TIM_IC_Stop_IT(&htim2,TIM_CHANNEL_1); //停止捕获   或者: __HAL_TIM_DISABLE(&htim5);
  173.                                 capture_Cnt++;   
  174.                 }
  175.        
  176.         }
  177.        
  178. }
复制代码

程序51hei提供下载:
buhuo_chaoshengbo.7z (5.14 MB, 下载次数: 22)

链接:https://pan.baidu.com/s/1oibDniL-afTCHEvdAmVmFA
提取码:ly45


评分

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

查看全部评分

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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