最近碰到一个奇怪的问题,在STM32F103RCT6上,以下程序可以设置TIM8输出PWM,但是将相应的TIM8改成TIM1,对应TIM1的GPIO上就没有PWM输出,不知道大家有没有遇到过相同问题?
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
u32 Period ;
/*根据输出的频率计算定时的计数值,确定输出PWM的周期*/
Period = SYS_FREQ /ul_freq ;
/*电机正转,TIM1为主定时器触发TIM8从定时器*/
TIM_DeInit(TIM8);
/*定时器TIM1基本配置*/
TIM_TimeBaseStructure.TIM_Prescaler = 0; /*不预分频*/
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; /*向上计数模式*/
TIM_TimeBaseStructure.TIM_Period = Period -1 ; /*计数值*/
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; /*定义时钟分频因子,产生死去的频率为定时器时钟频率的4分频*/
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; /*累计多少次溢出中断,再产生一次溢出中断*/
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
/*定时器TIM1通道1输出两路互补PWM配置*/
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; /*PWM输出模式2*/
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; /*使能OC输出*/
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; /*使能OC互补输出*/
TIM_OCInitStructure.TIM_Pulse = Period/2 ; /*占空比*/
/*输出极性设置*/
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //正通道 极性 输出 高有效
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCPolarity_High; //反通道 极性 输出 高有效
/*刹车后的极性配置*/
TIM_OCInitStructure.TIM_OCIdleState =TIM_OCIdleState_Reset ; //正通道 失效,刹车后极性 输出 低有效
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; //反通道 失效,刹车后极性 输出 低有效
TIM_OC1Init(TIM8, &TIM_OCInitStructure);
/*TIM1输出两路互补的PWM死区配置*/
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
TIM_BDTRInitStructure.TIM_DeadTime = 6;
// TIM_BDTRInitStructure.TIM_DeadTime = 11;
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_Low ;
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
TIM_BDTRConfig(TIM8, &TIM_BDTRInitStructure);
//使能影子寄存器
TIM_ARRPreloadConfig(TIM8, ENABLE);
TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Enable);//master 定时器TIM1的比较寄存器,开影子寄存器
TIM_Cmd(TIM8, ENABLE);
/* Main Output Enable */
TIM_CtrlPWMOutputs(TIM8, ENABLE);
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
你好,我也遇到跟楼主同样的问题, 只不过我是TIM8没有输出,但是如果是手动复位后就有输出,上电复位就没有,请问你说的选项字配置具体怎么做?
一周热门 更多>