关键配置
- 对应GPIO
- NVIC中断管理
- 定时器初始化
- 定时器输出模式
TIM_OC
初始化(模式为TIM_OCMode_Toggle
)
频率与占空比的计算
- 计数频率的计算
若使用比较输出,则ARR表示计数的上限,基本无用。计数频率由TIM_Prescaler
成员变量配置。若配置TIM_Prescaler=71
,输入时钟为 72 M H z 72MHz 72MHz,则计数频率为:
f = 72 M 72 + 1 = 1 M f=\frac{72M}{72+1}=1M f=72+172M=1M - 计数规则
当计数器数到CCR
所储存的值时,输出电平就会翻转。 - 频率和占空比的配置
由计数规律我们可以通过修改CCR
值的方法来得到我们想要的频率和占空比。
比如:计数频率为 1 M 1M 1M,想要得到 10 K 10K 10K,占空比为 80 % 80\% 80%的PWM波。
令空闲输出电平为0,关闭影子寄存器以便立即赋值。
当程序开始,立即进入中断,为CCR
赋值为20,此时电平为低电平;
当计数器数过20,翻转电平,变为高电平,再次进入中断,为CCR
赋值为20+80;
当计数器再数80,翻转电平,变为低电平,再次进入中断,为CCR
赋值为20+80+20;
…
于是可见,数100为一个周期,其中20为低电平,80为高电平。输出PWM波频率 1 M 1000 = 10 K H z \frac{1M}{1000}=10KHz 10001M=10KHz,占空比 80 % 80\% 80%.
示例代码
从PA6和PA7输出两路PWM;PA6频率为10KHz,占空比为80% ;PA7频率为1KHz,占空比为20%
PWM驱动配置:
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);
// PA1 PA2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the TIM3 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void PWM_Config()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_Configuration();
NVIC_Configuration();
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 65535; // 在TIM_OCMode_Toggle模式下ARR几乎无用
TIM_TimeBaseStructure.TIM_Prescaler = 71; // 72分频,计数频率为72M/72=1M
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//计数方式为向上计数
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* Output Compare Toggle Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; // 空闲状态为低电平
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable); // 关闭影子寄存器,当给CCR赋值时,即刻赋值
/* Output Compare Toggle Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Disable);
TIM_Cmd(TIM3, ENABLE);
TIM_ITConfig(TIM3, TIM_IT_CC1 | TIM_IT_CC2, ENABLE);
}
中断代码如下:
extern __IO uint16_t CCR1_Val;
extern __IO uint16_t CCR2_Val;
uint16_t capture = 0;
u8 pa6_state=0,pa7_state=0;
float zhankong1=0.2;
float zhankong2=0.2;
void TIM3_IRQHandler(void)
{
/* TIM3_CH1 toggling with frequency = 10K Hz */
if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC1 );
capture = TIM_GetCapture1(TIM3);
if(pa6_state ==0)
{
TIM_SetCompare1(TIM3, capture + CCR1_Val*zhankong1 );
pa6_state = 1;
}
else
{
TIM_SetCompare1(TIM3, capture + CCR1_Val*(1-zhankong1) );
pa6_state = 0;
}
}
/* TIM3_CH2 toggling with frequency = 1k Hz */
if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
capture = TIM_GetCapture2(TIM3);
if(pa7_state ==0)
{
TIM_SetCompare2(TIM3, capture + CCR2_Val*zhankong2 );
pa6_state = 1;
}
else
{
TIM_SetCompare2(TIM3, capture + CCR2_Val*(1-zhankong2) );
pa6_state = 0;
}
}
}