STM32 HAL库定时器输入捕获SlaveMode脉宽测量
SlaveMode模式简介
✨SlaveMode复位模式:在发生一个触发输入事件时,计数器和它的预分频器能够重新被初始化;同时,如果TIMx_CR1寄存器的URS位为低,还会产生一个更新事件UEV;然后所有的预装载寄存器(TIMx_ARR, TIMx_CCRx)都会被更新。
- 🔖当所测频率低于最小定时器捕获频率时,需要使能自动重装载和定时器溢出中断。
SlaveMode模式下当输入捕获引脚接收到脉冲的上降沿信号时,产生复位并从零开始重新计数。
- 🎋一个周期内的总计数:输入捕获上(下)升沿信号开始,到下一个上(下)升沿结束总计数个数: C N T = N ∗ ( A R R + 1 ) + C R R 1 CNT=N*(ARR+1)+CRR1 CNT=N∗(ARR+1)+CRR1
-
- N:定时器溢出次数
-
- ARR:TIMx预装载值
-
- TIMx->CRR1寄存器计数值
- 🌴定时器计数频率: f = F o s c / ( p s c + 1 ) f =Fosc/(psc+1) f=Fosc/(psc+1)
-
- Fosc:定时器的频率(主时钟频率)
-
- psc:分频系数
- 📐计一个数时间: T = 1 ÷ f T = 1 \div f T=1÷f = 1 f \frac{1}f f1
- 📏CNT总计数时间: C N T × T CNT \times T CNT×T
- 📏脉宽宽度等于上升沿开始到下一个下降沿之间的计数值: C C R 2 ∗ 1 f CCR2*\frac{1}f CCR2∗f1
📑PWM主要参数
- 🌿频率:是指1秒钟内信号从高电平到低电平再回到高电平的次数(一个周期);
- 🌿占空比:一个脉冲周期内,高电平的时间与整个周期时间的比例。
🛠输入捕获SlaveMode脉宽测量
- 🌿输入捕获到的PWM信号频率: 1 / C N T ∗ T = 1 / C N T ∗ ( 1 / f ) = f / C N T 1/CNT*T = 1/CNT*(1/f) = f/CNT 1/CNT∗T=1/CNT∗(1/f)=f/CNT
- 🌿PWM占空比: C R R 2 ÷ C N T CRR2 \div CNT CRR2÷CNT
📐最小捕获频率计算
- 🎋定时器频率: f / p s c f/psc f/psc
PSC定时器TIMx->PSC= f / ( p s c − 1 ) f/(psc-1) f/(psc−1)
- 🔧在没有开启溢出中断的情况下,最小捕获频率 = 2000000 ÷ 65535 ≈ 15.25 H z 2000 000 \div 65535 \approx 15.25Hz 2000000÷65535≈15.25Hz
- 👉🏻如果开启了溢出中断,那么捕获频率就不受限制。
- 🔧开启溢出中断的情况下,捕获频率 = 2000000 ÷ N ∗ ( A R R + 1 ) + C R R 1 2000 000 \div N*(ARR+1)+CRR1 2000000÷N∗(ARR+1)+CRR1 (其中N代表溢出次数,ARR代表装载值)
- 🔖如果被测量的频率低于1Hz,那么测量就没有多大意义了。
📝输入捕获SlaveMode脉宽测量例程
使用STM32F1利用TIM3通道1产生PWM输出信号,使用TIM2定时器作为信号输入捕获,并开启
SlaveMode
模式设置为ReSet Mode
,同时开启两路极性互补输入捕获。
- 🌿接线说明:PA6 PWM输出引脚和PA0输入捕获引脚连接到一起即可进行测量。
- 🔨TIM2输入捕获配置
- 🌿TIM3配置输出PWM占空比和频率可以行调整测试。
- 📑main程序代码
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "string.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
uint8_t USART_TX_Buff[32] = {0};
uint16_t Duty, Duty_High, ARR, PWM_f;
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM2_Init();
MX_TIM3_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1); //开启PWM输出通道:PA6
// TIM3->ARR = 1000-1;//自动重装载值
TIM3->CCR1 = 300;//捕获/比较计数值,PWM占空比5000/1000=50% f=1MHz/1000=1KHz
TIM2->PSC = 36 - 1; //预分频;f=2MHz
HAL_TIM_IC_Start(&htim2, TIM_CHANNEL_1); //开启输入捕获上升沿:PA0
HAL_TIM_IC_Start(&htim2, TIM_CHANNEL_2); //开启输入捕获下降沿:PA0
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while(1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_Delay(1000);
ARR = TIM2->CCR1 + 1; //捕获上升沿开始计数,计数值
Duty_High = TIM2->CCR2 + 1; //捕获下降沿,计数值
//f = f/psc=1000 000
//计一个数的时间T:1/f
//PWM_f = ARR/f
PWM_f = 2000/ARR;//KHz, 1/ARR*(T)= 1/ARR*(1/f)= f/ARR
Duty = Duty_High * 100 / ARR;
sprintf((char*)USART_TX_Buff, "PWM_f:%dKHz,ARR:%d,Duty_High:%d,Duty:%d%%", PWM_f, ARR, Duty_High, Duty); //打印输入捕获总计数值,高电平计数值
printf("%s \n", USART_TX_Buff);
memset((char*)USART_TX_Buff, '\0', strlen((char*)USART_TX_Buff)); //清空数组
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while(1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
- 📋串口打印
📚工程源码
链接:https://pan.baidu.com/s/1OJ6JuZt-76A-AjvvB2H3cA
提取码:p4hf