我在STM8S103K,上能够实现定时器1中的CH1通道可以实现PWM功能,把程序移植到STM8S103F中,就实现不了,但是修改STM8S103F,的通道3和4后,也能输出PWM,这是为什么,程序如下:
void
tiMER1_PWMInit(void){
TIM1_DeInit();//恢复相关寄存器至初始值
TIM1_TimeBaseInit(8, TIM1_COUNTERMODE_UP, 14355, 0);//时基初始化,预分频值1,向上计数,自动重装值49999,重复计数器值0
TIM1_OC4Init(TIM1_OCMODE_PWM1, TIM1_OUTPUTSTATE_ENABLE, 7050, TIM1_OCPOLARITY_LOW, TIM1_OCIDLESTATE_SET);
TIM1_CCxCmd(TIM1_CHANNEL_4, ENABLE); //这行区别于T2的PWM
TIM1_OC4PreloadConfig(ENABLE);//输出比较 预装载使能
TIM1_OC3Init(TIM1_OCMODE_PWM1, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE,7500, TIM1_OCPOLARITY_HIGH, TIM1_OCNPOLARITY_LOW, TIM1_OCIDLESTATE_RESET, TIM1_OCNIDLESTATE_RESET);//输出比较通道3 初始化,匹配时输出电平翻转,启用输出比较,禁
//用输出比较互补输出,脉宽5000,输出比较极性位低电平,互补输出比较极性高电平,输出比较空闲状态高电平,互补输出比较空闲状态低电平
TIM1_CCxCmd(TIM1_CHANNEL_3, ENABLE); //这行区别于T2的PWM
TIM1_OC3PreloadConfig(ENABLE);//输出比较 预装载使能
TIM1_OC2Init(TIM1_OCMODE_PWM1, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE,7500, TIM1_OCPOLARITY_HIGH, TIM1_OCNPOLARITY_LOW, TIM1_OCIDLESTATE_RESET, TIM1_OCNIDLESTATE_RESET);
TIM1_CCxCmd(TIM1_CHANNEL_2, ENABLE); //这行区别于T2的PWM
TIM1_OC2PreloadConfig(ENABLE);//输出比较 预装载使能
TIM1_OC1Init(TIM1_OCMODE_PWM1, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE,7500, TIM1_OCPOLARITY_HIGH, TIM1_OCNPOLARITY_LOW, TIM1_OCIDLESTATE_RESET, TIM1_OCNIDLESTATE_RESET);
TIM1_CCxCmd(TIM1_CHANNEL_1, ENABLE); //这行区别于T2的PWM
TIM1_OC1PreloadConfig(ENABLE);//输出比较 预装载使能
TIM1_CtrlPWMOutputs(ENABLE);//使能TIM1 主输出
TIM1_Cmd(ENABLE);//使能TIM1 计数器计数
}