赞
踩
以TIM14为例,查看IO口资源分布图
标红TIM14_CH1为TIM14的通道1。由于TIM14只有一个通道,所有只有CH1。例如TIM2就拥有4个通道(CH1,CH2,CH3,CH4)
其他寄存器查看STM32F4xx中文参考手册
- typedef struct
- {
- uint16_t TIM_OCMode; //输出模式
-
- uint16_t TIM_OutputState; //输出状态
-
- uint16_t TIM_OutputNState; //互补通道的输出状态
-
- uint16_t TIM_Pulse; //占空比
-
- uint16_t TIM_OCPolarity; //输出极性
-
- uint16_t TIM_OCNPolarity; //互补通道的输出极性
-
- uint16_t TIM_OCIdleState; //空闲状态
-
- uint16_t TIM_OCNIdleState; //互补通道的空闲状态
- } TIM_OCInitTypeDef;
- typedef struct
- {
-
- uint16_t TIM_OSSRState; /*指定在运行模式下使用的“关闭状态”选项。*/
-
-
- uint16_t TIM_OSSIState; /*指定在空闲状态中使用的关闭状态*/
-
- uint16_t TIM_LOCKLevel; /*指定锁级别参数.*/
-
-
- uint16_t TIM_DeadTime; /*指定输出关断和接通之间的延迟时间*/
-
- uint16_t TIM_Break; /*指定是否启用 TIM 中断输入 */
-
- uint16_t TIM_BreakPolarity; /*指定 TIM 断开输入引脚极性*/
-
-
- uint16_t TIM_AutomaticOutput; /*指定是否启用 TIM 自动输出功能 */
-
- } TIM_BDTRInitTypeDef;
TIM_OSSRState:
TIM_OSSRState_Enable | 使能TIM OSSR状态 |
TIM_OSSRState_Disable | 失能TIM OSSR状态 |
TIM_OSSIState
TIM_OSSIState_Enable | 使能TIM OSSI状态 |
TIM_OSSIState_Disable | 失能TIM OSSI状态 |
TIM_LOCKLevel
TIM_LOCKLevel_OFF | 不锁任何位 |
TIM_LOCKLevel_1 | 使用锁电平1 |
TIM_LOCKLevel_2 | 使用锁电平2 |
TIM_LOCKLevel_3 | 使用锁电平3 |
TIM_DeadTime
计算参考这篇文章https://blog.csdn.net/hmc_123/article/details/109549573
TIM_Break
TIM_Break_Enable | 使能TIM刹车输入 |
TIM_Break_Diable | 失能TIM刹车输入 |
TIM_BreakPolarity
TIM_BreakPolarity_Low | TIM刹车输入管脚极性低 |
TIM_BreakPolarity_High | TIM刹车输入管脚极性高 |
TIM_AutomaticOutput
TIM_AutomaticOutput_Enable | 自动输出功能使能 |
TIM_AutomaticOutput_Diable | 自动输出功能失能 |
调用对应RCC时钟函数
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14,ENABLE);
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);
将IO配置为复用模式及初始化
- GPIO_InitTypeDef GPIO_PWMTYPE;
-
- GPIO_PinAFConfig(GPIOF,GPIO_PinSource9,GPIO_AF_TIM14);
-
- GPIO_PWMTYPE.GPIO_Pin = GPIO_Pin_9;
-
- GPIO_PWMTYPE.GPIO_OType = GPIO_OType_PP;
-
- GPIO_PWMTYPE.GPIO_Mode = GPIO_Mode_AF;
-
- GPIO_PWMTYPE.GPIO_Speed = GPIO_Speed_100MHz;
-
- GPIO_PWMTYPE.GPIO_PuPd = GPIO_PuPd_UP;
-
- GPIO_Init(GPIOF,&GPIO_PWMTYPE);
将定时器进行初始化
- TIM_TimeBaseInitTypeDef TIM_PWMTYPE;
-
- TIM_PWMTYPE.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
-
- TIM_PWMTYPE.TIM_Period = arr; //自动重装载值
-
- TIM_PWMTYPE.TIM_Prescaler = psc; //预分频
-
- TIM_PWMTYPE.TIM_ClockDivision = TIM_CKD_DIV1;
-
- TIM_TimeBaseInit(TIM14,&TIM_PWMTYPE);
配置OC初始化外设函数
- TIM_OCInitTypeDef TIM_OCInitType;
-
- TIM_OCInitType.TIM_OCMode = TIM_OCMode_PWM2; //模式2
-
- TIM_OCInitType.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
-
- TIM_OCInitType.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性:TIM输出比较极性低
-
- TIM_OC1Init(TIM14,&TIM_OCInitType); //初始化配置TIM14在CCR1的各个参数
使能通道1预装载寄存器
TIM_OC1PreloadConfig(TIM14,TIM_OCPreload_Enable); //使能TIM14在CCR1上的预装载寄存器
ARPE进行使能
TIM_ARRPreloadConfig(TIM14,ENABLE); //ARPE使能
使能定时器
TIM_Cmd(TIM14,ENABLE); //使能定时器
主函数对中断进行分组
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
对频率进行计算
- TIM14的频率为84Mhz,我们将频率取为2k
- 公式为 84000000/2000=42000;及ARR为42000,psc为0;
- 也可以使用Tout=((arr+1)*(psc+1))/Ft us.
- Tout为时间 单位:us
- Ft=定时器工作频率,单位:Mhz
- 将频率转换为时钟T=1/F:T=1/2000=500 us
- psc我们取值为0;计算arr
- 500=(arr+1*0+1)/84 = 500*84 =42,000;
使用TIM_SetCompare1();函数进行赋值
TIM_SetCompare1(TIM14,PWM_DATA);
- void Tim14_PWM_Init(u16 arr,u16 psc)
- {
-
- GPIO_InitTypeDef GPIO_PWMTYPE;
-
- TIM_TimeBaseInitTypeDef TIM_PWMTYPE;
-
- TIM_OCInitTypeDef TIM_OCInitType;
-
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14,ENABLE);
-
- GPIO_PinAFConfig(GPIOF,GPIO_PinSource9,GPIO_AF_TIM14);
-
- GPIO_PWMTYPE.GPIO_Pin = GPIO_Pin_9;
-
- GPIO_PWMTYPE.GPIO_OType = GPIO_OType_PP;
-
- GPIO_PWMTYPE.GPIO_Mode = GPIO_Mode_AF;
-
- GPIO_PWMTYPE.GPIO_Speed = GPIO_Speed_100MHz;
-
- GPIO_PWMTYPE.GPIO_PuPd = GPIO_PuPd_UP;
-
- GPIO_Init(GPIOF,&GPIO_PWMTYPE);
-
-
- TIM_PWMTYPE.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
-
- TIM_PWMTYPE.TIM_Period = arr; //自动重装载值
-
- TIM_PWMTYPE.TIM_Prescaler = psc; //预分频
-
- TIM_PWMTYPE.TIM_ClockDivision = TIM_CKD_DIV1;
-
- TIM_TimeBaseInit(TIM14,&TIM_PWMTYPE);
-
-
- TIM_OCInitType.TIM_OCMode = TIM_OCMode_PWM2; //模式2
-
- TIM_OCInitType.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
-
- TIM_OCInitType.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性:TIM输出比较极性低
-
- TIM_OC1Init(TIM14,&TIM_OCInitType); //初始化配置TIM14在CCR1的各个参数
-
-
-
- TIM_OC1PreloadConfig(TIM14,TIM_OCPreload_Enable); //使能TIM14在CCR1上的预装载寄存器
-
- TIM_ARRPreloadConfig(TIM14,ENABLE); //ARPE使能
-
- TIM_Cmd(TIM14,ENABLE); //使能定时器
- }
-
- int main(void)
- {
-
- u16 PWM_DATA=0;
- // u8 t=1;
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- uart_init(115200);
- delay_init(168);
- Tim14_PWM_Init(2100,0);
- while(1)
- {
-
- delay_ms(5);
- // if(t){PWM_DATA++;}
- // else {PWM_DATA--;}
- // if(PWM_DATA>2797) t=0;
- // if(PWM_DATA==5) t=1;
- PWM_DATA=1600;
- TIM_SetCompare1(TIM14,PWM_DATA);
-
- }
- }
- void TIMER1_Init_PWM(void)
- {
-
- GPIO_InitTypeDef GPIO_PWMInit;
-
- TIM_TimeBaseInitTypeDef TIM1_TIMERType;
-
- TIM_OCInitTypeDef TIM1_PWMOC; //选择第一通道
-
- TIM_BDTRInitTypeDef TIM1_BDTRType;
-
- //启动对应RCC时钟
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
-
- /
-
- //复用引脚///
- GPIO_PinAFConfig(GPIOA,GPIO_PinSource8,GPIO_AF_TIM1);
-
- GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_TIM1);
-
-
- //初始化定义
- GPIO_PWMInit.GPIO_Mode = GPIO_Mode_AF;
-
- GPIO_PWMInit.GPIO_OType = GPIO_OType_PP;
-
- GPIO_PWMInit.GPIO_Pin = GPIO_Pin_7|GPIO_Pin_8;
-
- GPIO_PWMInit.GPIO_Speed = GPIO_Speed_100MHz;
-
- GPIO_PWMInit.GPIO_PuPd = GPIO_PuPd_UP;
-
- GPIO_Init(GPIOA,&GPIO_PWMInit);
-
-
-
- 定时器初始化定义/
- TIM1_TIMERType.TIM_ClockDivision = TIM_CKD_DIV1;
-
- TIM1_TIMERType.TIM_Period = 2787; //自动重装载值
-
- TIM1_TIMERType.TIM_Prescaler = 0; //分频系数
-
- TIM1_TIMERType.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
-
- TIM_TimeBaseInit(TIM1,&TIM1_TIMERType);
-
-
-
- //PWM初始化配置/
- TIM1_PWMOC.TIM_OCMode = TIM_OCMode_PWM1; //使用脉冲调制宽度模式1
-
- TIM1_PWMOC.TIM_Pulse = 0; //占空比 取值必须在0x0000到0xFFFF
-
- TIM1_PWMOC.TIM_OCIdleState = TIM_OCIdleState_Reset; //输出空闲时为低电平
-
- TIM1_PWMOC.TIM_OutputState = TIM_OutputState_Enable; //输出使能
-
- TIM1_PWMOC.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性高
-
- TIM1_PWMOC.TIM_OutputNState = TIM_OutputNState_Enable; //互补输出打开
-
- TIM1_PWMOC.TIM_OCNIdleState = TIM_OCNIdleState_Reset; //互补输出空闲时为低电平
-
- TIM1_PWMOC.TIM_OCNPolarity = TIM_OCNPolarity_High; //互补输出极性为高
-
- TIM_OC1Init(TIM1,&TIM1_PWMOC);
-
- TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable);
- ///
-
-
- 刹车死区配置///
-
- TIM1_BDTRType.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable; //自动输出功能使能
-
- TIM1_BDTRType.TIM_Break = TIM_Break_Disable; //失能刹车输入
-
- TIM1_BDTRType.TIM_BreakPolarity = TIM_BreakPolarity_High; //刹车输入管脚极性高
-
- TIM1_BDTRType.TIM_DeadTime = 0x14; //死区时间配置 参考CSDN计算方法,这里是3us
-
- TIM1_BDTRType.TIM_LOCKLevel = TIM_LOCKLevel_OFF; //锁电平参数:不锁任何位
-
- TIM1_BDTRType.TIM_OSSIState = TIM_OSSIState_Disable; //设置在运行模式下非工作状态
-
- TIM1_BDTRType.TIM_OSSRState = TIM_OSSRState_Disable; //设置在运行模式下非工作状态
-
- TIM_BDTRConfig(TIM1,&TIM1_BDTRType);
- ///
-
- TIM_ARRPreloadConfig(TIM1,ENABLE); //ARPE使能
-
- TIM_Cmd(TIM1,ENABLE); //使能TIM1
-
- TIM_CtrlPWMOutputs(TIM1,ENABLE); //开启OC和OCN输出
-
- }
-
- int main(void)
- {
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
-
- delay_init(168);
-
- TIMER1_Init_PWM();
- LED_init();
-
- while(1){
-
- TIM_SetCompare1(TIM1,1394);
-
- LED0 = !LED0;
-
- delay_ms(500);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。