大家帮我看看程序吧,,我想在TIM3的channel1中输出同频率但不同占空比的PWM..采用的策略是当TIM3发生TIM_IT_Update时,修改CCR1的值.. 但用下面的程序,程序一运行到倒数第二行TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); 就死掉了...不知道什么原因.. 如果去去掉这个语句,则正常运行,输出占空比为50%的波形,,,这个有点废话了... 俺对TIM不甚了解,大虾看看
在main.c中这样来设置TIM3和Channel1
TIM_TimeBaseStructure.TIM_Period = 9999; TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV4; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR1_Val; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(TIM3, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);
/* PWM1 Mode configuration: Channel2 */ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Disable);
/* PWM1 Mode configuration: Channel3 */ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Disable);
/* PWM1 Mode configuration: Channel4 */ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Disable);
TIM_ARRPreloadConfig(TIM3, ENABLE); TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); /* TIM3 enable counter */ TIM_Cmd(TIM3, ENABLE); 在stm3210x_it.c中,来设置中断. u16 f[]={5000,3750,2500,1250}; u8 i=1; void TIM3_IRQHandler(void) { if (TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET) { TIM_SetCompare1(TIM3,f); i++; if(i==3)i=0; TIM_ClearITPendingBit(TIM3,TIM_IT_Update); } } |