源文件 timer.c
#include <timer.h>
#include <delay.h>
/*--------GPIO--------*/
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE); //ENABLE TIM1
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO,ENABLE); //ENABLE GPIOA
/*--------TIM1-CH1 PA8 --------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //TIM1-CH1对应GPIO为PA8
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //输出最大速度为50MHz
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化上述配置
/*--------TIM1-CHN1 -> PB13 TIM1-BK1N -> PB12 --------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_12; //TIM1-CH1N /TIM1-BK1N
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_InitStructure.GPIO_Pin);
}
/*-------- 配置高级定时器TIM1的工作模式--------*/
void TIM1_Config(u32 Freq,u8 PulseNum, u8 duty_cycle)
{
u16 ARR; //自动重装值
u16 PSC = 1; //时钟预分频系数
u16 CCR; //待装入捕获比较寄存器的脉冲值
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; //时基结构体
TIM_OCInitTypeDef TIM_OCInitStructure; //输出比较结构体
TIM_BDTRInitTypeDef TIM_BDTRInitStructure; //刹车和死区时间结构体
/*根据PWM脉冲频率与寄存器上限关系确定时钟预分频系数PSC以及自动重装载值*/
while(SystemCoreClock/PSC/Freq>65535)
{
PSC++;
}
ARR = SystemCoreClock/PSC/Freq;
/*时基初始化,配置TIM1的时钟输出频率等相关参数*/
TIM_TimeBaseInitStructure.TIM_Period = (ARR-1); //自动重装载寄存器的值,累计TIM_Period+1个频率后产生一个更新或中断
TIM_TimeBaseInitStructure.TIM_Prescaler = (PSC-1); //驱动计数器CNT的时钟,它的时钟等于FCK_INT/(PSC)
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分频系数。0:不分频;TIM_CKD_DIV1:设置时钟分频系数,1分频
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //计数器计数模式:向上计数模式
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = (PulseNum-1); //指定PWM脉冲个数
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStructure); //初始化定时器
TIM_SelectOnePulseMode(TIM1,TIM_OPMode_Single); //单脉冲模式,用于输出特定个数的PWM脉冲
/*设置PWM的输出方式,PWM模式配置,通道1*/
CCR = ARR*(100-duty_cycle)/100;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //互补输出使能
TIM_OCInitStructure.TIM_Pulse = CCR; //设置占空比
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出通道电平极性配置:把PWM模式的有效电平设置为高电平
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //互补输出通道电平极性配置:把PWM模式的有效电平设置为高电平
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; //输出通道空闲电平极性配置,配置刹车功能用到
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset; //互补输出通道空闲电平极性配置,配置刹车功能用到
TIM_OC1Init(TIM1,&TIM_OCInitStructure); //根据TIM_OCInitStructure中指定的参数初始化外设TIM1
TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable); //开启TIM1_CCR1寄存器的预装载功能
/*--------------------刹车和死区结构体初始化--------------------*/
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Disable;
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Disable;
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable;
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High; //当BKIN引脚检测到高电平的时候,输出比较信号被禁止,就好像是刹车一样。意思就是没有PWM输出了;
TIM_BDTRInitStructure.TIM_DeadTime = 1;
TIM_BDTRConfig(TIM1,&TIM_BDTRInitStructure);
TIM_ARRPreloadConfig(TIM1, ENABLE); //使能TIM1在ARR上的预装载寄存器
TIM_Cmd(TIM1, ENABLE); //使能TIM1
TIM_CtrlPWMOutputs(TIM1,ENABLE); //主输出使能
}
头文件:
timer.h
#ifndef __TIMER_H
#define __TIMER_H
#include "sys.h"
#include "led.h"
void GPIO_Config(void);
void TIM1_Config(u32 Freq,u8 PulseNum, u8 duty_cycle);
#endif
主函数:
#include "delay.h"
#include "sys.h"
#include "timer.h"
int main(void)
{
delay_init(); //延时函数初始化
GPIO_Config();
while(1)
{
TIM1_Config(100000,4, 50); //频率100kHZ 脉冲数量4 占空比50%
delay_ms(200);
}
}
运行结果:
如图 黄色通道为PB13互补输出,紫色通道为PA8输出,目前互补输出异常