一、最近在驱动步进电机需要经常用到PWM信号,刚好STM32的定时器用的比较多,下面简单介绍下STM32定时器输出PWM信号,分两种方式,
第一种是同一个定时器的四个通道输出相同频率,不同占空比的情况
第二种是同一个定时器的四个通道输出相同频率,不同占空比的情况
二、软硬件介绍
1、硬件用的是野火的STM32F103开发板,如下图
2、软件用的Keil 5,程序架构用的正点原子的
3、定时器用的TIM4的四个通道
TIM4_CH1-----------PD12
TIM4_CH2-----------PD13
TIM4_CH3-----------PD14
TIM4_CH4-----------PD15
三、实现功能
1、首先实现TIM4的四个通道输出,同样频率,不同占空比的效果。四个通道输出频率为1K,占空比为20%、30%、40%、50%。
主函数如下
#include "sys.h"
#include "delay.h"
#include "led.h"
#include "timer.h"
int main(void)
{
//TIM4_CH1 PD12
//TIM4_CH2 PD13
//TIM4_CH3 PD14
//TIM4_CH4 PD15
TIM4_PWM_Init(999,71); //频率:72000000/1000/72=1000
while(1)
{
TIM_SetCompare1(TIM4,200); //M4
TIM_SetCompare2(TIM4,300); //M3
TIM_SetCompare3(TIM4,400); //M1
TIM_SetCompare4(TIM4,500); //M2
}
}
主要需要调用的是两个库函数:
TIM4_PWM_Init(999,71);
用来实现频率的调节
TIM_SetCompare1(TIM4,200);
用来实现占空比的调节
需要用到的子函数主要是time.h
,代码如下,主要配置IO,定时器初始化等等。
#include "timer.h"
#include "led.h"
#include "usart.h"
//TIM3 PWM部分初始化
//PWM输出初始化
//arr:自动重装值
//psc:时钟预分频数
void TIM4_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //使能定时器3时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE); //使能GPIO外设和AFIO复用功能模块时钟
GPIO_PinRemapConfig(GPIO_Remap_TIM4, ENABLE); //TIM4重映射
//设置该引脚为复用输出功能,输出TIM3 CH2的PWM脉冲波形 GPIOB.5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15; //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);//初始化GPIO
//初始化TIM3
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
//初始化TIM3 Channel2 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
//CH1
TIM_OC1Init(TIM4, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM3 OC2
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable); //使能TIM3在CCR2上的预装载寄存器
//CH12
TIM_OC2Init(TIM4, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM3 OC2
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable); //使能TIM3在CCR2上的预装载寄存器
//CH13
TIM_OC3Init(TIM4, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM3 OC2
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable); //使能TIM3在CCR2上的预装载寄存器
//CH14
TIM_OC4Init(TIM4, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM3 OC2
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable); //使能TIM3在CCR2上的预装载寄存器
TIM_Cmd(TIM4, ENABLE); //使能TIM3
}
示波器抓到的图形如下:
|
|
|
|
2、然后实现TIM4的四个通道输出,不同频率,同样占空比的效果。四个通道输出占空比为50%,频率为400HZ、600HZ、800HZ、1000HZ
这个的配置基本都在子函数time.h
,具体如下:
#include "timer.h"
#include "led.h"
#include "usart.h"
//TIM3 PWM部分初始化
//PWM输出初始化
//arr:自动重装值
//psc:时钟预分频数
vu16 CCR1_Val = 30000;//M4----TIM4_CH1
vu16 CCR2_Val = 20000;//M3---TIM4_CH2
vu16 CCR3_Val = 15000;//M1---TIM4_CH3
vu16 CCR4_Val = 12000;//M2---TIM4_CH4
u16 capture = 0;
u16 capture2 = 0;
//extern vu16 CCR1_Val;
//extern vu16 CCR2_Val;
//extern vu16 CCR3_Val;
//extern vu16 CCR4_Val;
void TIM4_PWM_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //使能定时器3时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE); //使能GPIO外设和AFIO复用功能模块时钟
GPIO_PinRemapConfig(GPIO_Remap_TIM4, ENABLE); //TIM4重映射
//设置该引脚为复用输出功能,输出TIM3 CH2的PWM脉冲波形 GPIOB.5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15; //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);//初始化GPIO
//中断优先级设置
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
//初始化TIM3
TIM_TimeBaseStructure.TIM_Period = 65535; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =2; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
//初始化TIM3 Channel2 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; //PWM模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //正向通道有效
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;//反向通道无效
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //互补端的极性
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
//CH_1
TIM_OCInitStructure.TIM_Pulse = CCR1_Val; //占空时间
TIM_OC1Init(TIM4, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM3 OC2
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Disable); //使能TIM3在CCR2上的预装载寄存器
//CH_2
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OC2Init(TIM4, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM3 OC2
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Disable); //使能TIM3在CCR2上的预装载寄存器
//CH_3
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
TIM_OC3Init(TIM4, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM3 OC2
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Disable); //使能TIM3在CCR2上的预装载寄存器
//CH_4
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
TIM_OC4Init(TIM4, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM3 OC2
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Disable); //使能TIM3在CCR2上的预装载寄存器
TIM_Cmd(TIM4, ENABLE); //使能TIM3
TIM_ITConfig(TIM4, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE);
}
/
void TIM4_IRQHandler(void)
{
/* TIM4_CH1 toggling with frequency = 366.2Hz *///TIM4_CH1 PD12实测400HZ
if (TIM_GetITStatus(TIM4, TIM_IT_CC1) != RESET)
{
TIM_ClearITPendingBit(TIM4, TIM_IT_CC1 );
capture2 =TIM_GetCapture1(TIM4);//测试
TIM_SetCompare1(TIM4, capture2+CCR1_Val);//
}
/* TIM4_CH2 toggling with frequency = 732.4Hz *///TIM4_CH2 PD13实测732.5HZ
if (TIM_GetITStatus(TIM4, TIM_IT_CC2) != RESET)
{
TIM_ClearITPendingBit(TIM4, TIM_IT_CC2);
capture= TIM_GetCapture2(TIM4);
TIM_SetCompare2(TIM4, capture + CCR2_Val);
}
/* TIM4_CH3 toggling with frequency = 1464.8Hz *///TIM4_CH3 PD14实测1464HZ
if (TIM_GetITStatus(TIM4, TIM_IT_CC3) != RESET)
{
TIM_ClearITPendingBit(TIM4, TIM_IT_CC3);
capture = TIM_GetCapture3(TIM4);
TIM_SetCompare3(TIM4, capture + CCR3_Val);
}
/* TIM4_CH4 toggling with frequency = 2929.6 Hz *///TIM4_CH4 PD15实测2926HZ
if (TIM_GetITStatus(TIM4, TIM_IT_CC4) != RESET)
{
TIM_ClearITPendingBit(TIM4, TIM_IT_CC4);
capture = TIM_GetCapture4(TIM4);
TIM_SetCompare4(TIM4, capture + CCR4_Val);
}
}
示波器抓到的图形如下:
|
|
|
|
百度云链接:
不同频率,同占空比
链接:https://pan.baidu.com/s/114UfYhdBIgVrIoQZX1xPUA
提取码:ckrb
同频率,不同占空比
链接:https://pan.baidu.com/s/15LFftCSvOhFyA7TOrSD80A
提取码:hjb5