其他分享
首页 > 其他分享> > 蓝桥杯STM32G431——测量两路PWM和占空比

蓝桥杯STM32G431——测量两路PWM和占空比

作者:互联网

测量两路PWM和占空比

测量PWM频率和占空比的时序图与步骤

时序图

在这里插入图片描述

测量PWM频率和占空比的步骤:

1.PWM信号由TI1进入,配置T11FP1为触发信号,上升沿捕获
2当上升沿的时候Ic1和ic2同时捕获,计数器CNT清零
3.到了下降沿的时候,IC2捕获,此时计数器CNT的值被锁存到捕获寄存器CCR2中。
4.到了下一个上升沿的时候,lC1捕获,计数器CNT的值被锁存到捕获寄存器CCR1中。
5.其中CCR2测量的是脉宽,CCR1测量的是周期。
占空比=脉宽/周期

PWM输入模式内部原理图

在这里插入图片描述
在这里插入图片描述
两个ICx信号被映射至同一个Tlx输入。
这2个ICx信号为边沿有效,但是极性相反。一个上升沿,一个下降沿

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)
{

/* USER CODE BEGIN TIM2_Init 0 */

  /* USER CODE END TIM2_Init 0 */

  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_SlaveConfigTypeDef sSlaveConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};
  TIM_IC_InitTypeDef sConfigIC = {0};

  /* USER CODE BEGIN TIM2_Init 1 */

  /* USER CODE END TIM2_Init 1 */
  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();
  }
  /* USER CODE BEGIN TIM2_Init 2 */

  /* USER CODE END TIM2_Init 2 */

}



/* TIM3 init function */
void PWM_TIM3_Init(void)
{

   /* USER CODE BEGIN TIM3_Init 0 */

  /* USER CODE END TIM3_Init 0 */

  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_SlaveConfigTypeDef sSlaveConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};
  TIM_IC_InitTypeDef sConfigIC = {0};

  /* USER CODE BEGIN TIM3_Init 1 */

  /* USER CODE END TIM3_Init 1 */
  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();
  }
  /* USER CODE BEGIN TIM3_Init 2 */

  /* USER CODE END TIM3_Init 2 */


}

void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
{

	GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(tim_baseHandle->Instance==TIM2)
  {
    /* TIM2 clock enable */
    __HAL_RCC_TIM2_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**TIM2 GPIO Configuration
    PA15     ------> TIM2_CH1
    */
    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);

    /* TIM2 interrupt Init */
    HAL_NVIC_SetPriority(TIM2_IRQn, 3, 0);
    HAL_NVIC_EnableIRQ(TIM2_IRQn);
  }
  else if(tim_baseHandle->Instance==TIM3)
  {
    /* TIM3 clock enable */
    __HAL_RCC_TIM3_CLK_ENABLE();

    __HAL_RCC_GPIOB_CLK_ENABLE();
    /**TIM3 GPIO Configuration
    PB4     ------> TIM3_CH1
    */
    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);

    /* TIM3 interrupt Init */
    HAL_NVIC_SetPriority(TIM3_IRQn, 3, 0);
    HAL_NVIC_EnableIRQ(TIM3_IRQn);
  }
  else if(tim_baseHandle->Instance==TIM6)
  {
    /* TIM6 clock enable */
    __HAL_RCC_TIM6_CLK_ENABLE();

    /* TIM6 interrupt Init */
    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;	//LCD减速中使用的
unsigned char Lcd_Disp_String[21];

uint8_t i;
//用于pwm回调函数中的变量
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);

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{

  HAL_Init();
  /* Configure the system clock */
  SystemClock_Config();
	//**LCD初始化
	LCD_Init();
	LCD_Clear(Magenta);
	LCD_SetBackColor(Magenta);
	LCD_SetTextColor(Blue);
	
	//基础定时器6的初始化
	BASIC_TIM6_Init();
	HAL_TIM_Base_Start_IT(&htim6);

	//PWM初始化
	PWM_TIM2_Init(); 	//定时器2初始化
	HAL_TIM_Base_Start_IT(&htim2);	//启动定时器2
	HAL_TIM_IC_Start_IT(&htim2 , TIM_CHANNEL_1);	//启动定时器2开启通道1输入捕获并开启中断
	HAL_TIM_IC_Start_IT(&htim2 , TIM_CHANNEL_2);	//启动定时器2开启通道2输入捕获并开启中断
	
	PWM_TIM3_Init();	//定时器3初始化
	HAL_TIM_Base_Start_IT(&htim3);	//启动定时器3
	HAL_TIM_IC_Start_IT(&htim3 , TIM_CHANNEL_1);	//启动定时器3开启通道1输入捕获并开启中断
	HAL_TIM_IC_Start_IT(&htim3 , TIM_CHANNEL_2);	//启动定时器3开启通道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); //频率值为1M/pwm_count 1M=1000000
	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)	//加判断语句用于判别是time2还是time3
  {
		if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)	//通道1上升沿有效
		{
			pwm2_count_rise = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1)+1;
			duty2=(float)pwm2_count_down/pwm2_count_rise;	//在第二次上升沿触发时算出占空比为下降沿触发时的count/上升沿触发时的count
		}
		if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)	//通道2下降沿有效
		{
			pwm2_count_down = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2)+1;
		}
	}
	
	if(htim->Instance == TIM3)	//对pwm1的处理同上相同
  {
		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);
	}
}

/**
  * @brief System Clock Configuration
`  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
  /** 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.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();
  }
  /** 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_DIV1;
  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 */

  /* 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 */

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

效果图展示为
在这里插入图片描述

标签:TIM3,HAL,STM32G431,蓝桥,TIM,Init,GPIO,占空比,RCC
来源: https://blog.csdn.net/qq_52542756/article/details/122755347