大家好,紧急求助,我想利用STM1和STM3两个定时器的PWM输出,产生下图所示的一个波形
想法是,让两个定时器产生同样的两个PWM输出,但是两个错时输出,一开一关,程序是这样的:
void Init_TIM1(void)
{
TIM1_DeInit();
TIM1_TimeBaseInit(0,TIM1_COUNTERMODE_UP,TIM1ARRValue,0);// 10KHz 12MHz
TIM1_OC2Init(TIM1_OCMODE_PWM1, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_DISABLE, TIM1CompareValue, TIM1_OCPOLARITY_HIGH, TIM1_OCNPOLARITY_LOW, TIM1_OCIDLESTATE_SET, TIM1_OCNIDLESTATE_RESET);
TIM1_OC2PreloadConfig(ENABLE);
TIM1_ARRPreloadConfig(ENABLE);
TIM1_ITConfig(TIM1_IT_UPDATE, ENABLE);
TIM1_Cmd(ENABLE);
TIM1_CtrlPWMOutputs(ENABLE);
}
void Init_TIM3(void)
{
TIM3_DeInit();
TIM3_TimeBaseInit(TIM3_PRESCALER_1,TIM1ARRValue);// 10KHz 20MHz
TIM3_OC1Init(TIM3_OCMODE_PWM1, TIM3_OUTPUTSTATE_ENABLE, TIM3CompareValue, TIM3_OCPOLARITY_HIGH);
TIM3_OC1PreloadConfig(ENABLE);
TIM3_ARRPreloadConfig(ENABLE);
TIM3_ITConfig(TIM3_IT_UPDATE, ENABLE);
TIM3_Cmd(DISABLE);
}
@far @interrupt void TIM1_UPD_OVF_BRK_IRQHandler(void)
{
TIM1_Cmd(DISABLE);
TIM3_SetCompare1(TIM3CompareValue);
TIM3_Cmd(ENABLE);
TIM1_ClearFlag(TIM1_FLAG_UPDATE);
}
@far @interrupt void TIM3_UPD_OVF_BRK_IRQHandler(void)
{
TIM1_SetCompare2(TIM1CompareValue);
TIM1_Cmd(ENABLE);
TIM3_Cmd(DISABLE);
TIM3_ClearFlag(TIM3_FLAG_UPDATE);
}
可是用示波器看最终出来的波形,两个输出的波形一模一样,请大侠指点一下问题出在哪里?或者指点下怎样能得到这种波形,不胜感激,谢谢!!!
此帖出自
小平头技术问答
你这个很简单,没必要用两个定时器,如果用tim1的话你看看互补功能,一个通道加互补通道就能实现.如果tim3用两个通道轮换输出就行了.
一周热门 更多>