原理:定时器1为主模式,定时器8为从模式,TIM1的定时器使能操作作为触发输出[TRGO]触发TIM8并使能TIM8的计数器,同时输出两路频率、占空比以及脉冲数量(小于256个,高级定时器重复计数功能为8位)可调PWM波形。
关键代码:
定时器1(TIM1)设为主模式:
TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable);
TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_Enable);
定时器2(TIM8)设为从模式
TIM_SelectSlaveMode(TIM8, TIM_SlaveMode_Trigger);
TIM_SelectInputTrigger(TIM8, TIM_TS_ITR0);
STM32F103系列主从定时器Trigger设置详见STM32F103系列定时器通道对应IO汇总
定时器1开启计数功能、定时器2计数功能无需设定,即既不使能也不禁用
TIM_Cmd(TIM1, ENABLE); //启动定时器TIM1的计数功能
//TIM_Cmd(TIM8, ENABLE); // 无需启动定时器TIM8的计数功能,该功能由trgo信号使能
测试结果:
① 橘色:定时器TIM1:频率8MHz;脉冲个数8个;占空比0.5;
② 蓝色:定时器TIM8:频率2MHz;脉冲个数2个;占空比0.5;
橘色和蓝色同步输出,误差仅4~5ns可忽略
主函数:文章来源:https://www.toymoban.com/news/detail-535456.html
#include "sys.h"
#include "delay.h"
#include "pwm.h"
int main(void)
{
delay_init();
GPIO_Configuration();
while(1)
{
PWM_TIM1_TIM8(8000E3,50,8, 2000E3,50,2); // Freq1, duty_cycle1,PulseNum1,Freq2, duty_cycle2,PulseNum2
delay_ms(20);
}
}
定时器配置 pwm.c:文章来源地址https://www.toymoban.com/news/detail-535456.html
#include "pwm.h"
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8 | RCC_APB2Periph_TIM1, ENABLE); //使能TIM1 TIM8
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE); //使能GPIOC,使能复用
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_FullRemap_TIM1,ENABLE); //串口重映射 PA11-->PE14
/*-------- 主定时器 TIM1-CH4 -> PA11 (PE14) --------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
/*--------从定时器 TIM8-CH2 -> PC7 --------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_SetBits(GPIOC,GPIO_Pin_7);
// GPIO_SetBits(GPIOE,GPIO_Pin_14);
}
// Freq : 脉冲频率
// PulseNum : 脉冲数量
// duty_cycle : 占空比
void PWM_TIM1_TIM8(u32 Freq0, u8 duty_cycle0, u8 PulseNum0, u32 Freq1, u8 duty_cycle1, u8 PulseNum)
{
u16 ARR0;
u16 CCR0;
u16 PSC0 = 1;
u16 ARR1;
u16 CCR1;
u16 PSC1 = 1;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
while(SystemCoreClock/PSC0/Freq0>65535)
{
PSC0++;
}
ARR0 = SystemCoreClock/PSC0/Freq0;
TIM_TimeBaseStructure.TIM_Period = (ARR0-1); // 设置重装载数据
TIM_TimeBaseStructure.TIM_Prescaler = (PSC0-1); // 定时器时钟分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0; // 计数器时钟分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // 向上计数模式
TIM_TimeBaseStructure.TIM_RepetitionCounter = (PulseNum0 -1); // 脉冲数量
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_SelectOnePulseMode(TIM1, TIM_OPMode_Single);
TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable);
TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_Enable);
CCR0 = ARR0*(100-duty_cycle0) /100;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;;
TIM_OCInitStructure.TIM_Pulse = CCR0; // 比较值,当为PWM2模式且输出极性为High,表示低电平持续时间
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OC4Init(TIM1, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
while(SystemCoreClock/PSC1/Freq1>65535)
{
PSC1++;
}
ARR1 = SystemCoreClock/PSC1/Freq1;
TIM_TimeBaseStructure.TIM_Period = (ARR1-1); // 设置重装载数据
TIM_TimeBaseStructure.TIM_Prescaler = (PSC1-1); // 定时器时钟分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0; // 计数器时钟分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // 向上计数模式
TIM_TimeBaseStructure.TIM_RepetitionCounter = (PulseNum -1); // 脉冲数量
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
TIM_SelectOnePulseMode(TIM8, TIM_OPMode_Single);
TIM_SelectSlaveMode(TIM8, TIM_SlaveMode_Trigger);
TIM_SelectInputTrigger(TIM8, TIM_TS_ITR0);
CCR1 = ARR1*(100-duty_cycle1) /100;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;;
TIM_OCInitStructure.TIM_Pulse = CCR1; // 比较值,当为PWM2模式且输出极性为High,表示低电平持续时间
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OC2Init(TIM8, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM8, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM8, ENABLE);
TIM_CtrlPWMOutputs(TIM8, ENABLE);
TIM_Cmd(TIM1, ENABLE); //启动定时器TIM1的计数功能
//TIM_Cmd(TIM8, ENABLE); // 无需启动定时器TIM8的计数功能,该功能由trgo信号使能
}
到了这里,关于STM32主从模式实现两路同步PWM脉冲输出,频率、占空比可调的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!