我在定時(shí)器3的例程上面改,改了很久都沒改好,百度看別人說定時(shí)器1和定時(shí)器8需要額外的指令用于PWM輸出,今天下午硬件的人來要東西了,來不及研究了,于是網(wǎng)上找了個(gè)定時(shí)器8的4個(gè)通道全部可以輸出的例程記錄一下,已經(jīng)經(jīng)過本人試驗(yàn)是OK的。
1.調(diào)用:
TIM8_PWM_Init(1439 ,0); //不分頻。PWM頻率=72000000/1440=50Khz
2.函數(shù)源碼:
//TIM8 PWM部分初始化 //PWM輸出初始化 //arr:自動(dòng)重裝值 //psc:時(shí)鐘預(yù)分頻數(shù) 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); //使能定時(shí)器3時(shí)鐘 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //使能GPIO外設(shè)和AFIO復(fù)用功能模塊時(shí)鐘 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; //復(fù)用推挽輸出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure);//初始化GPIO //初始化TIM8 TIM_TimeBaseStructure.TIM_Period = arr; //設(shè)置在下一個(gè)更新事件裝入活動(dòng)的自動(dòng)重裝載寄存器周期的值 TIM_TimeBaseStructure.TIM_Prescaler =psc; //設(shè)置用來作為TIMx時(shí)鐘頻率除數(shù)的預(yù)分頻值 TIM_TimeBaseStructure.TIM_ClockDivision = 0; //設(shè)置時(shí)鐘分割:TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上計(jì)數(shù)模式 TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); //根據(jù)TIM_TimeBaseInitStruct中指定的參數(shù)初始化TIMx的時(shí)間基數(shù)單位 //初始化TIM8 Channel2 PWM模式 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //選擇定時(shí)器模式:TIM脈沖寬度調(diào)制模式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ù)初始化外設(shè)TIM8 OC2 TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Enable); //使能TIM8在CCR2上的預(yù)裝載寄存器 TIM_SetCompare1(TIM8,474); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //選擇定時(shí)器模式:TIM脈沖寬度調(diào)制模式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ù)初始化外設(shè)TIM8 OC2 TIM_OC2PreloadConfig(TIM8, TIM_OCPreload_Enable); //使能TIM8在CCR2上的預(yù)裝載寄存器 TIM_SetCompare2(TIM8,700); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //選擇定時(shí)器模式:TIM脈沖寬度調(diào)制模式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ù)初始化外設(shè)TIM8 OC2 TIM_OC3PreloadConfig(TIM8, TIM_OCPreload_Enable); //使能TIM8在CCR2上的預(yù)裝載寄存器 TIM_SetCompare3(TIM8,900); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //選擇定時(shí)器模式:TIM脈沖寬度調(diào)制模式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ù)初始化外設(shè)TIM8 OC2 TIM_OC4PreloadConfig(TIM8, TIM_OCPreload_Enable); //使能TIM8在CCR2上的預(yù)裝載寄存器 TIM_SetCompare4(TIM8,1200); TIM_Cmd(TIM8, ENABLE); //不使能TIM8 TIM_CtrlPWMOutputs(TIM8,ENABLE); }審核編輯:湯梓紅
-
PWM
+關(guān)注
關(guān)注
114文章
5153瀏覽量
213454 -
STM32
+關(guān)注
關(guān)注
2266文章
10876瀏覽量
354928 -
定時(shí)器
+關(guān)注
關(guān)注
23文章
3241瀏覽量
114516 -
函數(shù)
+關(guān)注
關(guān)注
3文章
4308瀏覽量
62445
原文標(biāo)題:stm32的定時(shí)器8輸出PWM
文章出處:【微信號:mcu168,微信公眾號:硬件攻城獅】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
評論