我用定时器1实现4路不同频率的PWM,现在可以实现比较功能并按要求触发了中断,可以在中断程序里对其它端口实现符合要求的PWM信号,但无法在外设对应的端口输出PWM信号,我现在调试的是通道1,但PB13无信号输出,程序如下,请大家帮忙分析下问题到底出在哪里,谢谢。
void TIM_Configuration(void)
{
#define PrescalerValue 7199 //
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //PB13配置成复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;//
TIM_OCInitTypeDef TIM_OCInitStructure; //
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_PrescalerConfig(TIM1, PrescalerValue, TIM_PSCReloadMode_Immediate); //预分频值即时装入
//CC1ÅäÖÃÊä³ö±È½Ïģʽ
TIM_OCInitStructure.TIM_OCMode =TIM_OCMode_Toggle; //输出比较翻转模式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 10000;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Disable);//禁止CCR1寄存器预装
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM1, TIM_IT_CC1, ENABLE);//开启中断
TIM_Cmd(TIM1, ENABLE);
}
void TIM1_CC_IRQHandler(void)
{
if (TIM_GetITStatus(TIM1, TIM_IT_CC1) != RESET)
{
Pause_LED=!Pause_LED; //利用其它端口输出脉冲,测试用
if(Pause_LED)TIM1->CCR1+=50000; //5秒输出高电平
else TIM1->CCR1+=10000; // 1秒输出低电平
TIM_ClearITPendingBit(TIM1, TIM_IT_CC1);
}
} |