stm32通用定時(shí)器的PWM輸出
配置過程:(以TIM3為例,其CH1-CH4為:PA6、PA7、PB0、PB1)
1)開啟TIM3時(shí)鐘,配置4個(gè)IO口為復(fù)用推挽輸出。
2)設(shè)置TIM3的ARR和PSC來控制PWM的周期。
3)設(shè)置TIM3的CH1-CH4的PWM模式及通道方向,使能TIM3的CH1-CH4輸出。
4)使能TIM3。
程序如下:
/*
* 函數(shù)名:TIM3_GPIO_Config
* 描述 :配置TIM3復(fù)用輸出PWM時(shí)用到的I/O
* 輸入 :無
* 輸出 :無
* 調(diào)用 :內(nèi)部調(diào)用
*/
void TIM3_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//PCLK1經(jīng)過2倍頻后作為TIM3的時(shí)鐘源等于72MHz
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //開啟TIM3時(shí)鐘
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 復(fù)用推挽輸出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
/*
* 函數(shù)名:TIM3_Mode_Config
* 描述 :配置TIM3輸出的PWM信號(hào)的模式,如周期、極性、占空比
* 輸入 :無
* 輸出 :無
* 調(diào)用 :內(nèi)部調(diào)用
*/
void TIM3_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* PWM信號(hào)電平跳變值 */
u16 CCR1_Val = 500;
u16 CCR2_Val = 375;
u16 CCR3_Val = 250;
u16 CCR4_Val = 125;
/* -----------------------------------------------------------------------
TIM3 Configuration: generate 4 PWM signals with 4 different duty cycles:
TIM3CLK = 72 MHz, Prescaler = 0x0, TIM3 counter clock = 72 MHz
TIM3 ARR Register = 999 => TIM3 Frequency = TIM3 counter clock/(ARR + 1)
TIM3 Frequency = 72 KHz.
TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR)* 100 = 50% 500/(999+1)
TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR)* 100 = 37.5%
TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR)* 100 = 25%
TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR)* 100 = 12.5%
----------------------------------------------------------------------- */ //TIM3_ARR=(TIM_Period +1)
//TIM3_CCR1=TIM_Pulse
/* Time base configuration */ //具體定時(shí)時(shí)間設(shè)置在后面
TIM_TimeBaseStructure.TIM_Period = 999; //當(dāng)定時(shí)器從0計(jì)數(shù)到999,即為1000次,為一個(gè)定時(shí)周期
TIM_TimeBaseStructure.TIM_Prescaler = 0; //設(shè)置預(yù)分頻:不預(yù)分頻,即為72MHz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ;//設(shè)置時(shí)鐘分頻系數(shù):不分頻
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上計(jì)數(shù)模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //配置為PWM模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR1_Val; //設(shè)置跳變值,當(dāng)計(jì)數(shù)器計(jì)數(shù)到這個(gè)值時(shí),電平發(fā)生跳變
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //當(dāng)定時(shí)器計(jì)數(shù)值小于CCR1_Val時(shí)為高電平
TIM_OC1Init(TIM3, &TIM_OCInitStructure); //使能通道1
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR2_Val; //設(shè)置通道2的電平跳變值,輸出另外一個(gè)占空比的PWM
TIM_OC2Init(TIM3, &TIM_OCInitStructure); //使能通道2
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR3_Val; //設(shè)置通道3的電平跳變值,輸出另外一個(gè)占空比的PWM
TIM_OC3Init(TIM3, &TIM_OCInitStructure); //使能通道3
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel4 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR4_Val; //設(shè)置通道4的電平跳變值,輸出另外一個(gè)占空比的PWM
TIM_OC4Init(TIM3, &TIM_OCInitStructure); //使能通道4
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE); // 使能TIM3重載寄存器ARR
/* TIM3 enable counter */
TIM_Cmd(TIM3, ENABLE); //使能定時(shí)器3
}
/*
* 函數(shù)名:TIM3_PWM_Init
* 描述 :TIM3 輸出PWM信號(hào)初始化,只要調(diào)用這個(gè)函數(shù)
* TIM3的四個(gè)通道就會(huì)有PWM信號(hào)輸出
* 輸入 :無
* 輸出 :無
* 調(diào)用 :外部調(diào)用
*/
void TIM3_PWM_Init(void)
{
TIM3_GPIO_Config();
TIM3_Mode_Config();
}
1)定時(shí)時(shí)間計(jì)算:(以定時(shí)1S為例)
((1+TIM_Prescaler )/72M)*(1+TIM_Period )=((1+7199)/72M)*(1+9999)=1秒 */
定時(shí)器的基本設(shè)置:
TIM_TimeBaseStructure.TIM_Prescaler = 7199;//時(shí)鐘預(yù)分頻數(shù) 例如 :時(shí)鐘頻率=72/(時(shí)鐘預(yù)分頻+1)
TIM_TimeBaseStructure.TIM_Period = 9999; // 自動(dòng)重裝載寄存器周期的值
TIM_TimeBaseStructure.TIM_CounterMode = TIM1_CounterMode_Up; //定時(shí)器模式 向上計(jì)數(shù)
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //時(shí)間分割值
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);//初始化定時(shí)器2
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //打開中斷 溢出中斷
TIM_Cmd(TIM2, ENABLE);//打開定時(shí)器
或者:
TIM_TimeBaseStructure.TIM_Prescaler = 35999;//分頻35999 72M/
(35999+1)/2=1Hz 1秒中斷溢出一次
TIM_TimeBaseStructure.TIM_Period = 2000; //計(jì)數(shù)值2000
((1+TIM_Prescaler )/72M)*(1+TIM_Period )=((1+35999)/72M)*(1+2000)=1秒 */
2)PWM CH1輸出模式(TIM_OCMode )配置:
PWM1模式是:在向上計(jì)數(shù)時(shí),當(dāng)TIMx_CNT
TIM_OCxPreloadConfig(TIM3, TIM_OCPreload_Enable);配置了各通道的比較寄存器TIM_CRR預(yù)裝載使能;使用TIM_ARRPreloadConfig()把重載寄存器TIMx_ARR使能,最后用TIM_Cmd()使能定時(shí)器TIM3,定時(shí)器外設(shè)就開始工作了。