你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32G431RBTx 基本模块 TIM IC捕获

[复制链接]
STMCU小助手 发布时间:2023-3-1 21:07
IC
1.原理图以及配置元素

1ec98dedb3fc4da284aa9fe4ede4daa7.png

由图可知PA15为输入捕获接口与一个XL555信号发生器连在了一起。


2.CubeMx的配置步骤

f65e13b419f440f8961473f62a0da716.png

83b70ddd96984131a69cb4a22d6b3216.png

47509aa3cbf24f0e86f0c6eca3da3703.png


3.生成工程
点击GENERATE CODE生成代码后点击Open Project即可。

a195445de7b5488ca841e1a6a743b1d0.png

4.测试代码
interrupt.h
  1. #ifndef __INTERRUPT_H__
  2. #define __INTERRUPT_H__

  3. #include "main.h"

  4. void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim);

  5. #endif

复制代码

interrupt.c
  1. #include "interrupt.h"

  2. unsigned int Period = 0;
  3. unsigned int HighTime = 0;

  4. void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
  5. {
  6.         if(htim->Instance == TIM2)
  7.         {
  8.                 if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
  9.                 {
  10.                         Period = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1); //读取直接通道
  11.                         HighTime = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2); //读取间接通道
  12.                         __HAL_TIM_SetCounter(htim, 0);
  13.                         HAL_TIM_IC_Start_IT(htim, TIM_CHANNEL_1); //开启中断以便于下次采值
  14.                         HAL_TIM_IC_Start_IT(htim, TIM_CHANNEL_2);
  15.                 }
  16.         }
  17. }

复制代码

main.c
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>© Copyright (c) 2023 STMicroelectronics.
  10.   * All rights reserved.</center></h2>
  11.   *
  12.   * This software component is licensed by ST under BSD 3-Clause license,
  13.   * the "License"; You may not use this file except in compliance with the
  14.   * License. You may obtain a copy of the License at:
  15.   *                        opensource.org/licenses/BSD-3-Clause
  16.   *
  17.   ******************************************************************************
  18.   */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. #include "tim.h"
  23. #include "gpio.h"

  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26. #include "lcd.h"
  27. #include "stdio.h"
  28. /* USER CODE END Includes */

  29. /* Private typedef -----------------------------------------------------------*/
  30. /* USER CODE BEGIN PTD */

  31. /* USER CODE END PTD */

  32. /* Private define ------------------------------------------------------------*/
  33. /* USER CODE BEGIN PD */
  34. /* USER CODE END PD */

  35. /* Private macro -------------------------------------------------------------*/
  36. /* USER CODE BEGIN PM */

  37. /* USER CODE END PM */

  38. /* Private variables ---------------------------------------------------------*/

  39. /* USER CODE BEGIN PV */
  40. extern unsigned int Period;
  41. extern unsigned int HighTime;
  42. char text[30];
  43. /* USER CODE END PV */

  44. /* Private function prototypes -----------------------------------------------*/
  45. void SystemClock_Config(void);
  46. /* USER CODE BEGIN PFP */

  47. /* USER CODE END PFP */

  48. /* Private user code ---------------------------------------------------------*/
  49. /* USER CODE BEGIN 0 */

  50. /* USER CODE END 0 */

  51. /**
  52.   * @brief  The application entry point.
  53.   * @retval int
  54.   */
  55. int main(void)
  56. {
  57.   /* USER CODE BEGIN 1 */

  58.   /* USER CODE END 1 */

  59.   /* MCU Configuration--------------------------------------------------------*/

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

  62.   /* USER CODE BEGIN Init */

  63.   /* USER CODE END Init */

  64.   /* Configure the system clock */
  65.   SystemClock_Config();

  66.   /* USER CODE BEGIN SysInit */

  67.   /* USER CODE END SysInit */

  68.   /* Initialize all configured peripherals */
  69.   MX_GPIO_Init();
  70.   MX_TIM2_Init();
  71.   /* USER CODE BEGIN 2 */
  72.         HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_1);
  73.         HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_2);
  74.         LCD_Init();
  75.         LCD_Clear(Black);
  76.         LCD_SetBackColor(Black);
  77.         LCD_SetTextColor(White);
  78.   /* USER CODE END 2 */

  79.   /* Infinite loop */
  80.   /* USER CODE BEGIN WHILE */
  81.   while (1)
  82.   {
  83.     /* USER CODE END WHILE */

  84.     /* USER CODE BEGIN 3 */
  85.                 sprintf(text, "frq:%.2f", 1000000.0 / Period); //分频成1MHz所以这里1000000.0作为被除数
  86.                 LCD_DisplayStringLine(Line0,  text);
  87.                 sprintf(text, "Duty:%.2f", HighTime / (float)Period * 100); //占空比为高电平时间 / 周期 * 100%
  88.                 LCD_DisplayStringLine(Line1,  text);
  89.   }
  90.   /* USER CODE END 3 */
  91. }

  92. /**
  93.   * @brief System Clock Configuration
  94.   * @retval None
  95.   */
  96. void SystemClock_Config(void)
  97. {
  98.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  99.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  100.   /** Configure the main internal regulator output voltage
  101.   */
  102.   HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
  103.   /** Initializes the RCC Oscillators according to the specified parameters
  104.   * in the RCC_OscInitTypeDef structure.
  105.   */
  106.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  107.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  108.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  109.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  110.   RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV3;
  111.   RCC_OscInitStruct.PLL.PLLN = 20;
  112.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  113.   RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  114.   RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  115.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  116.   {
  117.     Error_Handler();
  118.   }
  119.   /** Initializes the CPU, AHB and APB buses clocks
  120.   */
  121.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  122.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  123.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  124.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  125.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  126.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  127.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  128.   {
  129.     Error_Handler();
  130.   }
  131. }

  132. /* USER CODE BEGIN 4 */

  133. /* USER CODE END 4 */

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

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

  164. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

复制代码

5.演示效果


64a2c93e89354389a19e280244fc7820.gif

————————————————
版权声明:火花页.


收藏 评论0 发布时间:2023-3-1 21:07

举报

0个回答

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版