使用一个定时器两通道输出两路频率不等方波
输出比较模式
输出比较模式的各种输出模式
1:(冻结模式)冻结模式。定时器作为普通定时器使用,不使用输出比较功能。
2/3:(比较输出模式1)匹配时输出有效/无效电平模式。如递增计数器,比较寄存器内部数值提前设定好,当计数器相等或者大于比较值时,匹配,产生/不产生有效信号输出。当计数器值小于比较值时,不产生/产生有效信号输出。
4:(比较输出模式2)电平翻转模式。当匹配时,引脚状态翻转。步进电机控制常用的模式。
5/6:(强制输出模式)强置为无效/有效电平模式。不管比较寄存器和计数器数值,强制设置比较寄存器的输出。7/8: ( PWM模式)PWM1和PWM2模式。
推荐一篇大佬的文章用于比较PWM模式和输出比较模式输出方波的区别STM32输出比较模式和PWM模式 比较
输出比较模式下: CCR(捕获/比较寄存器)==CNT(计数值)时,翻转输出电平。
PWM模式下:CNT<CCR时输出一种电平,CNT > CCR时输出相反的电平。
总结如下:
PWM模式:ARR(自动重装载寄存器)设置频率,CCR设置占空比,频率和占空比可以任意设置,起始相位不能设置
输出比较模式:ARR设置频率,CCR设置相位,频率和起始相位可以任意设置,占空比不能设置。输出频率为理论计算值一半。
本文使用输出比较模式电平翻转模式实现两路频率不等方波的输出
一个定时器两通道输出两路频率不等方波配置过程
定时器通道选择
选择定时器4的通道1和通道2分别对应PA11和PA12的引脚复用功能
定时器初始化CubeMX的主要配置过程
1、选择相应的引脚PA11和PA12
2、选择定时器的模式为输出比较模式
3、配置定时器初始化
4、打开中断
5、配置中断服务优先级
配置pwm.c文件(包含定时器2、3、4、6的初始化)
本文章主要是学习使用定时器4产生两路不同频率的方波
#include "pwm.h"
TIM_HandleTypeDef htim2; //句柄结构体
TIM_HandleTypeDef htim3;
TIM_HandleTypeDef htim4;
void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle) //用于定时器4的引脚初始化函数
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(timHandle->Instance==TIM4)
{
/* USER CODE BEGIN TIM4_MspPostInit 0 */
/* USER CODE END TIM4_MspPostInit 0 */
__HAL_RCC_GPIOA_CLK_ENABLE();
/**TIM4 GPIO Configuration
PA11 ------> TIM4_CH1
PA12 ------> TIM4_CH2
*/
GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF10_TIM4;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USER CODE BEGIN TIM4_MspPostInit 1 */
/* USER CODE END TIM4_MspPostInit 1 */
}
}
void Squ_Ootput_TIM4_Init(void) //定时器4方波输出函数初始化
{
/* USER CODE BEGIN TIM4_Init 0 */
/* USER CODE END TIM4_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
/* USER CODE BEGIN TIM4_Init 1 */
/* USER CODE END TIM4_Init 1 */
htim4.Instance = TIM4;
htim4.Init.Prescaler = 79;
htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
htim4.Init.Period = 65535;
htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
if (HAL_TIM_Base_Init(&htim4) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_OC_Init(&htim4) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_TOGGLE;
sConfigOC.Pulse = 100;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_OC_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_OC_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM4_Init 2 */
/* USER CODE END TIM4_Init 2 */
HAL_TIM_MspPostInit(&htim4);
}
void PWM_Input_TIM2_Init(void) //定时器2 PWM输入初始化函数
{
/* 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_Input_TIM3_Init(void) //定时器3 PWM输入初始化函数
{
/* 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)
{
/* USER CODE BEGIN TIM2_MspInit 0 */
/* USER CODE END TIM2_MspInit 0 */
/* 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);
/* USER CODE BEGIN TIM2_MspInit 1 */
/* USER CODE END TIM2_MspInit 1 */
}
else if(tim_baseHandle->Instance==TIM3)
{
/* USER CODE BEGIN TIM3_MspInit 0 */
/* USER CODE END TIM3_MspInit 0 */
/* 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);
/* USER CODE BEGIN TIM3_MspInit 1 */
/* USER CODE END TIM3_MspInit 1 */
}
else if(tim_baseHandle->Instance==TIM4)
{
/* USER CODE BEGIN TIM4_MspInit 0 */
/* USER CODE END TIM4_MspInit 0 */
/* TIM4 clock enable */
__HAL_RCC_TIM4_CLK_ENABLE();
/* TIM4 interrupt Init */
HAL_NVIC_SetPriority(TIM4_IRQn, 3, 0);
HAL_NVIC_EnableIRQ(TIM4_IRQn);
/* USER CODE BEGIN TIM4_MspInit 1 */
/* USER CODE END TIM4_MspInit 1 */
}
else if(tim_baseHandle->Instance==TIM6)
{
/* USER CODE BEGIN TIM6_MspInit 0 */
/* USER CODE END TIM6_MspInit 0 */
/* 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);
/* USER CODE BEGIN TIM6_MspInit 1 */
/* USER CODE END TIM6_MspInit 1 */
}
}
配置pwm.h文件
#ifndef __PWM_H
#define __PWM_H
#include "main.h"
extern TIM_HandleTypeDef htim2;
extern TIM_HandleTypeDef htim3;
extern TIM_HandleTypeDef htim4;
void PWM_Input_TIM2_Init(void);
void PWM_Input_TIM3_Init(void);
void Squ_Ootput_TIM4_Init(void);
#endif
配置main.c文件
#include "main.h"
#include "stdio.h"
#include "string.h"
#include "basic_tim6.h"
#include "pwm.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_Input_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_Input_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输入捕获模式并开启中断
//方波输出初始化
Squ_Ootput_TIM4_Init();
HAL_TIM_OC_Start_IT(&htim4 , TIM_CHANNEL_1); //启动定时器4开启通道1输出比较模式并开启中断
HAL_TIM_OC_Start_IT(&htim4 , TIM_CHANNEL_2); //启动定时器4开启通道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) //输入捕获PWM输入中断回调函数
{
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)
{
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_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim) //输出比较输出方波中断回调函数
{
if(htim->Instance == TIM4)
{
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1) //定时器4通道一对应PA11
{
__HAL_TIM_SetCompare(htim,TIM_CHANNEL_1,(__HAL_TIM_GetCounter(htim)+100)); //输出频率为5kHz的方波
}
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2) //定时器4通道一对应PA12
{
__HAL_TIM_SetCompare(htim,TIM_CHANNEL_2,(__HAL_TIM_GetCounter(htim)+500)); //输出频率为1kHz的方波
}
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM6)
{
i++;
HAL_TIM_Base_Start_IT(&htim6);
}
}
注:
代码全部编写完成后若没有示波器测量PA11和PA12输出的方波频率可以使用杜邦线将PA11和PA12,将连接555芯片的跳线帽取下,与捕获输入的引脚PB4和PA15相连。测量是否为设置的频率在LCD上显示。验证代码是否有误。