本帖最后由 dcp 于 2011-10-13 03:38 编辑
芯片是STM32F103RCT6
IAR6.21
2.01库
tim1 tim4 开PWM和溢出中断,tim1 tim4溢出中断正常工作,PWM波正常,但tim4对应的PWM波不能关闭
TIM_CtrlPWMOutputs(TIM1, ENABLE); 打开PWM
TIM_CtrlPWMOutputs(TIM1, DISABLE); 可正常关闭PWM
TIM_CtrlPWMOutputs(TIM4, ENABLE); 打开PWM
TIM_CtrlPWMOutputs(TIM4, DISABLE); 不能正常关闭PWM,这句有没有都有一样有PWM波形
请教是哪里设置出错?
void tim1_PWM_init(u16 PWM_frequency)// PWM+定时中断
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Timer1 Channel 2, PA8 */
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);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_DeInit(TIM1);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//TIM_TimeBaseStructure.TIM_Period = 7; ////1us*(499+1)=500us
//TIM_TimeBaseStructure.TIM_Prescaler = 71; //(71+1)/72000000=1us
TIM_TimeBaseStructure.TIM_Period = (72000 / PWM_frequency - 1); //
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
//-------
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
//TIM_OCInitStructure.TIM_Pulse = CCR1_Val;//占空比
TIM_OCInitStructure.TIM_Pulse = (72000 / PWM_frequency - 1) / 2; //
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
// TIM_OC1Init(TIM1,&TIM_OCInitStructure);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
//========
NVIC_InitTypeDef NVIC_InitStructure;
//Enable the TIM1 gloabal Interrupt TIM1_UP_IRQChannel
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQChannel;//TIM1_UP_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ClearFlag(TIM1, TIM_FLAG_Update);
TIM_ARRPreloadConfig(TIM1, DISABLE);
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM1, ENABLE);
}
void tim4_PWM_init(u16 PWM_frequency)// PWM+定时中断
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //M2CY
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
TIM_DeInit(TIM4);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//TIM_TimeBaseStructure.TIM_Period = 7; ////1us*(499+1)=500us
//TIM_TimeBaseStructure.TIM_Prescaler = 71; //(71+1)/72000000=1us
TIM_TimeBaseStructure.TIM_Period = (72000 / PWM_frequency - 1); //
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
//-------
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
//TIM_OCInitStructure.TIM_Pulse = CCR1_Val;//占空比
TIM_OCInitStructure.TIM_Pulse = (72000 / PWM_frequency - 1) / 2; //
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
//TIM_OC3Init(TIM4,&TIM_OCInitStructure);
TIM_CtrlPWMOutputs(TIM4, ENABLE);
//========
NVIC_InitTypeDef NVIC_InitStructure;
//Enable the TIM1 gloabal Interrupt TIM1_UP_IRQChannel
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 5;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 5;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ClearFlag(TIM4, TIM_FLAG_Update);
TIM_ARRPreloadConfig(TIM4, DISABLE);
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM4, ENABLE);
}
void TIM1_UP_IRQHandler(void)
{
if(TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
LED9B();
}
}
void TIM4_IRQHandler(void)
{
if(TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
LED8B();
}
} |