高级定时器TIM1输出PWM波
高级定时器(TIM1和TIM8)相较于通用定时器,它的函数配置更多,一个都不能少,否则输出不了PWM波,所以在此写下通用定时器和高级定时器PWM波配置的区别,以记录问题。
通用定时器TIM3通道1配置
void TIM3_CH1_PWM_Init(u16 per,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* 开启时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
/*配置GPIO的模式和IO口 */
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
//复用推挽输出
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3,ENABLE);
//改变指定管脚的映射
TIM_TimeBaseInitStructure.TIM_Period=per;
//自动装载值
TIM_TimeBaseInitStructure.TIM_Prescaler=psc;
//分频系数
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
//设置向上计数模式
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OC1Init(TIM3,&TIM_OCInitStructure);
//输出比较通道1初始化
TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);
//使能TIMx在 CCR1 上的预装载寄存器
TIM_ARRPreloadConfig(TIM3,ENABLE);
//使能预装载寄存器
TIM_CtrlPWMOutputs(TIM3,ENABLE);
TIM_Cmd(TIM3,ENABLE);
//使能定时器
}
【高级定时器TIM1输出PWM波】高级定时器TIM1通道1的配置
void TIM1_CH1_PWM_Init(u16 per,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;
TIM_OCInitTypeDefTIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = per;
TIM_TimeBaseStructure.TIM_Prescaler =psc;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
//此处结构体必须全部配置,否则输出不了PWM波!!!!
TIM_OCInitStructure.TIM_Pulse = 0;
//待装入捕获比较寄存器的脉冲值
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
//TIM1脉冲宽度调制模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
//使能输出比较状态
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
//使能输出比较N状态
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
//TIM1 输出比较极性高
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
//TIM1 输出比较N极性高
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
//当 MOE=0 重置 TIM1 输出比较空闲状态
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
//当 MOE=0 重置 TIM1 输出比较 N 空闲状态
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
//输出比较通道1初始化
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
//使能TIMx在 CCR1 上的预装
TIM_ARRPreloadConfig(TIM1, ENABLE);
//使能预装载寄存器
TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_Cmd(TIM1, ENABLE);
//使能定时器
}
推荐阅读
- 唐嫣可真会穿,西装搭牛仔裤都能穿出高级感,一双大长腿太抢镜
- 鹿鸣高级营养老师徐老师分享应该注意的6种食物
- Java基础-高级特性-枚举实现状态机
- HTTP高级(Cookie,Session|HTTP高级(Cookie,Session ,LocalStorage )
- 芯灵思SinlinxA33开发板Linux内核定时器编程
- 程序员|【高级Java架构师系统学习】毕业一年萌新的Java大厂面经,最新整理
- Kotlin泛型的高级特性(六)
- Swift高级应用|Swift高级应用 -01
- 你也可以拍出高级感的照片
- 前端|web前端dya07--ES6高级语法的转化&render&vue与webpack&export