一、最近在驱动步进电机需要经常用到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_PWM_Init(999,71);
while(1)
{
TIM_SetCompare1(TIM4,200);
TIM_SetCompare2(TIM4,300);
TIM_SetCompare3(TIM4,400);
TIM_SetCompare4(TIM4,500);
}
}
主要需要调用的是两个库函数: TIM4_PWM_Init(999,71); 用来实现频率的调节 TIM_SetCompare1(TIM4,200); 用来实现占空比的调节
需要用到的子函数主要是time.h ,代码如下,主要配置IO,定时器初始化等等。
#include "timer.h"
#include "led.h"
#include "usart.h"
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);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_TIM4, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler =psc;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC4Init(TIM4, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_Cmd(TIM4, ENABLE);
}
示波器抓到的图形如下:
2、然后实现TIM4的四个通道输出,不同频率,同样占空比的效果。四个通道输出占空比为50%,频率为400HZ、600HZ、800HZ、1000HZ 这个的配置基本都在子函数time.h ,具体如下:
#include "timer.h"
#include "led.h"
#include "usart.h"
vu16 CCR1_Val = 30000;
vu16 CCR2_Val = 20000;
vu16 CCR3_Val = 15000;
vu16 CCR4_Val = 12000;
u16 capture = 0;
u16 capture2 = 0;
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);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_TIM4, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler =2;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
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;
TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Disable);
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Disable);
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Disable);
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
TIM_OC4Init(TIM4, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Disable);
TIM_Cmd(TIM4, ENABLE);
TIM_ITConfig(TIM4, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE);
}
void TIM4_IRQHandler(void)
{
//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 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 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);
}
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
|