测量PWM频率和占空比的时序图与步骤
时序图
测量占空比:PWM输入模式
内部原理图
两个ICx信号被映射至同一个Tlx输入。 这2个ICx信号为边沿有效,但是极性相反。一个上升沿,一个下降沿
PWM输入模式下测量PWM频率和占空比的步骤: 1.PWM信号由TI1进入,配置T11FP1为触发信号,上升沿捕获。 2当上升沿的时候Ic1和ic2同时捕获,计数器CNT清零。 3.到了下降沿的时候,IC2捕获,此时计数器CNT的值被锁存到捕获寄存器CCR2中。 4.到了下一个上升沿的时候,lC1捕获,计数器CNT的值被锁存到捕获寄存器CCR1中。 5.其中CCR2测量的是脉宽,CCR1测量的是周期。 占空比=脉宽/周期
CubeMX的基础配置
在测量PWM输出频率CubeMX配置的基础之上对测量占空比中所需要的设置进行配置 参考上一篇文章蓝桥杯STM32G431——测量输入捕获PWM频率
相较于频率的测量,实际上只增加了一个通道二并且配置为非直接输入捕获模式和 下降沿触发 对定时器3的配置如图所示和定时器2相同完成配置即可
测量PWM频率和占空比的编程
pwm_tim.c文件
对测量PWM初始化函数的定时器2和定时器3进行了修改
#include "pwm_tim.h"
TIM_HandleTypeDef htim3;
TIM_HandleTypeDef htim2;
void PWM_TIM2_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_SlaveConfigTypeDef sSlaveConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_IC_InitTypeDef sConfigIC = {0};
htim2.Instance = TIM2;
htim2.Init.Prescaler = 79;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 65535;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_IC_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sSlaveConfig.SlaveMode = TIM_SLAVEMODE_RESET;
sSlaveConfig.InputTrigger = TIM_TS_TI1FP1;
sSlaveConfig.TriggerPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
sSlaveConfig.TriggerFilter = 0;
if (HAL_TIM_SlaveConfigSynchro(&htim2, &sSlaveConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
sConfigIC.ICFilter = 0;
if (HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_FALLING;
sConfigIC.ICSelection = TIM_ICSELECTION_INDIRECTTI;
if (HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_2) != HAL_OK)
{
Error_Handler();
}
}
void PWM_TIM3_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_SlaveConfigTypeDef sSlaveConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_IC_InitTypeDef sConfigIC = {0};
htim3.Instance = TIM3;
htim3.Init.Prescaler = 79;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 65535;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_IC_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
sSlaveConfig.SlaveMode = TIM_SLAVEMODE_RESET;
sSlaveConfig.InputTrigger = TIM_TS_TI1FP1;
sSlaveConfig.TriggerPolarity = TIM_INPUTCHANNELPOLARITY_FALLING;
sSlaveConfig.TriggerFilter = 0;
if (HAL_TIM_SlaveConfigSynchro(&htim3, &sSlaveConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_FALLING;
sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
sConfigIC.ICFilter = 0;
if (HAL_TIM_IC_ConfigChannel(&htim3, &sConfigIC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
sConfigIC.ICSelection = TIM_ICSELECTION_INDIRECTTI;
if (HAL_TIM_IC_ConfigChannel(&htim3, &sConfigIC, TIM_CHANNEL_2) != HAL_OK)
{
Error_Handler();
}
}
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(tim_baseHandle->Instance==TIM2)
{
__HAL_RCC_TIM2_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
HAL_NVIC_SetPriority(TIM2_IRQn, 3, 0);
HAL_NVIC_EnableIRQ(TIM2_IRQn);
}
else if(tim_baseHandle->Instance==TIM3)
{
__HAL_RCC_TIM3_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_4;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
HAL_NVIC_SetPriority(TIM3_IRQn, 3, 0);
HAL_NVIC_EnableIRQ(TIM3_IRQn);
}
else if(tim_baseHandle->Instance==TIM6)
{
__HAL_RCC_TIM6_CLK_ENABLE();
HAL_NVIC_SetPriority(TIM6_DAC_IRQn, 2, 0);
HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn);
}
}
main.c主函数的编写
#include "main.h"
#include "stdio.h"
#include "string.h"
#include "lcd.h"
#include "basic_tim6.h"
#include "pwm_tim.h"
变量
__IO uint32_t uwTick_LCD_State_Pointer;
unsigned char Lcd_Disp_String[21];
uint8_t i;
uint16_t pwm1_count_rise;
uint16_t pwm1_count_down;
uint16_t pwm2_count_rise;
uint16_t pwm2_count_down;
float duty1;
float duty2;
void SystemClock_Config(void);
void LCD_Proc(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
LCD_Init();
LCD_Clear(Magenta);
LCD_SetBackColor(Magenta);
LCD_SetTextColor(Blue);
BASIC_TIM6_Init();
HAL_TIM_Base_Start_IT(&htim6);
PWM_TIM2_Init();
HAL_TIM_Base_Start_IT(&htim2);
HAL_TIM_IC_Start_IT(&htim2 , TIM_CHANNEL_1);
HAL_TIM_IC_Start_IT(&htim2 , TIM_CHANNEL_2);
PWM_TIM3_Init();
HAL_TIM_Base_Start_IT(&htim3);
HAL_TIM_IC_Start_IT(&htim3 , TIM_CHANNEL_1);
HAL_TIM_IC_Start_IT(&htim3 , TIM_CHANNEL_2);
while (1)
{
LCD_Proc();
}
}
void LCD_Proc(void)
{
if(uwTick-uwTick_LCD_State_Pointer<300) return;
uwTick_LCD_State_Pointer=uwTick;
memset(Lcd_Disp_String,0,sizeof(Lcd_Disp_String));
sprintf((char*)Lcd_Disp_String, " Timer6_Num : %03d" ,i);
LCD_DisplayStringLine(Line4, Lcd_Disp_String);
memset(Lcd_Disp_String,0,sizeof(Lcd_Disp_String));
sprintf((char*)Lcd_Disp_String, "pwm1:%4dHz,%5.2f%% ",(unsigned int)1000000/pwm1_count_rise,duty1*100);
LCD_DisplayStringLine(Line5, Lcd_Disp_String);
memset(Lcd_Disp_String,0,sizeof(Lcd_Disp_String));
sprintf((char*)Lcd_Disp_String, "pwm2:%4dHz,%5.2f%% ",(unsigned int)1000000/pwm2_count_rise,duty2*100);
LCD_DisplayStringLine(Line6, Lcd_Disp_String);
}
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM2)
{
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
{
pwm2_count_rise = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1)+1;
duty2=(float)pwm2_count_down/pwm2_count_rise;
}
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
{
pwm2_count_down = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2)+1;
}
}
if(htim->Instance == TIM3)
{
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
{
pwm1_count_rise = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1)+1;
duty1=(float)pwm1_count_down/pwm1_count_rise;
}
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
{
pwm1_count_down = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2)+1;
}
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM6)
{
i++;
HAL_TIM_Base_Start_IT(&htim6);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV3;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
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_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
void Error_Handler(void)
{
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
}
#endif
效果图展示为
|