新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 轉(zhuǎn)兩個(gè)stm32定時(shí)器的使用代碼

轉(zhuǎn)兩個(gè)stm32定時(shí)器的使用代碼

作者: 時(shí)間:2016-11-30 來源:網(wǎng)絡(luò) 收藏
具體沒有測(cè)試,先收藏,以后去嘗試一下

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = Pluse;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Set;

本文引用地址:http://m.butianyuan.cn/article/201611/324048.htm

TIM_OC1Init(BLDC_TIMER_NUM, &TIM_OCInitStructure);

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
Pluse = MotorA.TimerPeriod - Pluse;
TIM_OCInitStructure.TIM_Pulse = Pluse;
TIM_OC2Init(BLDC_TIMER_NUM, &TIM_OCInitStructure);

#define Fsys 72000000ul // system freq 72MHz
#define Fpwm 20000 // PWM freq 20K

void ConfigTimer(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM4,ENABLE);
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB,ENABLE);

TIM_DeInit(TIM4);
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1;
TIM_TimeBaseStructure.TIM_Period = (Fsys/2) / Fpwm;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputState_Disable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_Pulse = (((Fsys/2) / Fpwm) * 20) / 100;
TIM_OC3Init(TIM4,&TIM_OCInitStructure);

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_Pulse = (((Fsys/2) / Fpwm) * (100-20)) / 100;
TIM_OC4Init(TIM4,&TIM_OCInitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);

TIM_CtrlPWMOutputs(TIM4, ENABLE);
TIM_Cmd(TIM4,ENABLE);
}

基本設(shè)置如下:
1)配置定時(shí)器的計(jì)數(shù)器為中間對(duì)齊計(jì)數(shù),即先向上計(jì)數(shù)再向下計(jì)數(shù)。
2)在該定時(shí)器上選擇2個(gè)通道,并分別配置為輸出比較模式,并配置在比較成功時(shí)翻轉(zhuǎn)對(duì)應(yīng)的引腳輸出。
3)配置自動(dòng)重裝載寄存器TIMx_ARR為要求輸出頻率的一半。
4)假定CC1為第一個(gè)輸出信號(hào)的通道,再假定第一個(gè)信號(hào)的正脈沖寬度對(duì)應(yīng)為W1,則配置TIMx_CCR1為TIMx_ARR-W1/2。
5) 同4),假定CC2為第二個(gè)輸出信號(hào)的通道,正脈沖寬度對(duì)應(yīng)為W2,配置TIMx_CCR2為W2/2。

----------------------------------------------
下面以一個(gè)例子說明:

假設(shè)要求輸出的信號(hào)頻率為10kHz,占空比為1:3。
再假設(shè)定時(shí)器的輸入時(shí)鐘為72MHz。

輸出信號(hào)的頻率10kHz,換算為計(jì)數(shù)器的數(shù)值為7200。
按照上述3),設(shè)置TIMx_ARR=3600

輸出信號(hào)1的高電平時(shí)間W1,換算為計(jì)數(shù)器的數(shù)值為W1=7200/4=1800
按照上述4),設(shè)置TIMx_CC1=3600 - W1/2=2700

輸出信號(hào)2的高電平時(shí)間W2,換算為計(jì)數(shù)器的數(shù)值為W2=7200/4=1800
按照上述5),設(shè)置TIMx_CC2=2/2=450

參照下圖,圖中紅線表示計(jì)數(shù)器的數(shù)值變化:
①當(dāng)計(jì)數(shù)器的數(shù)值從0向上計(jì)數(shù),達(dá)到TIMx_CC1時(shí),CC1匹配成功,CC1的輸出電平翻轉(zhuǎn);
②計(jì)數(shù)器繼續(xù)向上計(jì)數(shù),達(dá)到TIMx_ARR時(shí)開始調(diào)頭向下計(jì)數(shù);當(dāng)計(jì)數(shù)器的數(shù)值下降到TIMx_CC1時(shí),CC1再次匹配成功,CC1的輸出電平再次翻轉(zhuǎn);
③計(jì)數(shù)器繼續(xù)向下計(jì)數(shù),達(dá)到到TIMx_CC2時(shí),CC2匹配成功,CC2的輸出電平翻轉(zhuǎn);
④計(jì)數(shù)器繼續(xù)向下計(jì)數(shù),減到0時(shí)開始調(diào)頭向上計(jì)數(shù);當(dāng)計(jì)數(shù)器的數(shù)值上升到TIMx_CC2時(shí),CC2再次匹配成功,CC2的輸出電平再次翻轉(zhuǎn);

如此循環(huán),得到連續(xù)的相位互為180度的兩路輸出波形。

注意:上述描述是一個(gè)原理性的說明,但能夠輸出要求的波形并且占空比可調(diào),實(shí)際編程計(jì)算中需要可能需要對(duì)某些數(shù)值加1或者減1,以達(dá)到準(zhǔn)確地輸出。



關(guān)鍵詞: stm32定時(shí)器使用代

評(píng)論


技術(shù)專區(qū)

關(guān)閉