STM32 TIM8初始化的配置與通用定時器的配置不同,我最開始時按照通用定時器配置,無論是配置一個通道,還是配置所有通道,死活不出來pwm。接著查資料說要添加這一句代碼:TIM_CtrlPWMOutputs(TIM8,ENABLE),添加后只有通道4有輸出,其它三路沒有輸出,但是還是有點小激動啊。后來看到一個網友說要添加下面幾行代碼:TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset ;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
添加后果然四路PWM均正常輸出。我測試后其實只要添加:TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset ;就可正常輸出pwm,感謝萬能度娘。
我還發(fā)現(xiàn)輸出的pwm波形與TIM_OCIdleState設置的狀態(tài)成和TIM_OCPolarity設置的極性相關,例如:TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset ;TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low ; 時波形的占空比若為25%;那么:TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset ;TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High ; 時波形的占空比為75%;
TIM8還有很多沒有搞明白的地方,需要繼續(xù)學習哥只是個傳說。最后附上TIM8的初始化代碼(注釋忽略):
void TIM8_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);//使能定時器3時鐘
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //使能GPIO外設和AFIO復用功能模塊時鐘
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9; //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復用推挽輸出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);//初始化GPIO
//初始化TIM8
TIM_TimeBaseStructure.TIM_Period = arr; //設置在下一個更新事件裝入活動的自動重裝載寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //設置用來作為TIMx時鐘頻率除數(shù)的預分頻值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //設置時鐘分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上計數(shù)模式
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); //根據(jù)TIM_TimeBaseInitStruct中指定的參數(shù)初始化TIMx的時間基數(shù)單位
//初始化TIM8 Channel2 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //選擇定時器模式:TIM脈沖寬度調制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比較輸出使能
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset ;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low ; //輸出極性:TIM輸出比較極性高
TIM_OC1Init(TIM8, &TIM_OCInitStructure); //根據(jù)T指定的參數(shù)初始化外設TIM8 OC2
TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Enable); //使能TIM8在CCR2上的預裝載寄存器
TIM_SetCompare1(TIM8,474);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //選擇定時器模式:TIM脈沖寬度調制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比較輸出使能
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset ;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low ; //輸出極性:TIM輸出比較極性高
TIM_OC2Init(TIM8, &TIM_OCInitStructure); //根據(jù)T指定的參數(shù)初始化外設TIM8 OC2
TIM_OC2PreloadConfig(TIM8, TIM_OCPreload_Enable); //使能TIM8在CCR2上的預裝載寄存器
TIM_SetCompare2(TIM8,700);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //選擇定時器模式:TIM脈沖寬度調制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比較輸出使能
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset ;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low ; //輸出極性:TIM輸出比較極性高
TIM_OC3Init(TIM8, &TIM_OCInitStructure); //根據(jù)T指定的參數(shù)初始化外設TIM8 OC2
TIM_OC3PreloadConfig(TIM8, TIM_OCPreload_Enable); //使能TIM8在CCR2上的預裝載寄存器
TIM_SetCompare3(TIM8,900);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //選擇定時器模式:TIM脈沖寬度調制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比較輸出使能
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset ;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low ; //輸出極性:TIM輸出比較極性高
TIM_OC4Init(TIM8, &TIM_OCInitStructure); //根據(jù)T指定的參數(shù)初始化外設TIM8 OC2
TIM_OC4PreloadConfig(TIM8, TIM_OCPreload_Enable); //使能TIM8在CCR2上的預裝載寄存器
TIM_SetCompare4(TIM8,1200);
TIM_Cmd(TIM8, ENABLE); //不使能TIM8
TIM_CtrlPWMOutputs(TIM8,ENABLE);
}