STM32 TIM1 輸出脈沖范例
TIM模塊定時(shí)器向上溢出 & 輸出比較
首先我們必須肯定ST公司的實(shí)力,也承認(rèn)STM32的確是一款非常不錯(cuò)的Cortex-M3核單片機(jī),但是,他的手冊(cè)實(shí)在是讓人覺得無法理解,尤其是其中的TIM模塊,沒有條理可言,看了兩天幾乎還是不知所云,讓人很是郁悶。同時(shí)配套的固件庫的說明也很難和手冊(cè)上的寄存器對(duì)應(yīng)起來,研究起來非常費(fèi)勁!功能強(qiáng)大倒是真的,但至少也應(yīng)該配套一個(gè)讓人看的明白的說明吧~~
兩天時(shí)間研究了STM32定時(shí)器的最最基礎(chǔ)的部分,把定時(shí)器最基礎(chǔ)的兩個(gè)功能實(shí)現(xiàn)了,余下的功能有待繼續(xù)學(xué)習(xí)。
首先有一點(diǎn)需要注意:FWLib固件庫目前的最新版應(yīng)該是V2.0.x,V1.0.x版本固件庫中,TIM1模塊被獨(dú)立出來,調(diào)用的函數(shù)與其他定時(shí)器不同;在V2.0系列版本中,取消了TIM1.h,所有的TIM模塊統(tǒng)一調(diào)用TIM.h即可。網(wǎng)絡(luò)上流傳的各種代碼有許多是基于v1版本的固件庫,在移植到v2版本固件庫時(shí),需要做些修改。本文的所有程序都是基于V2.0固件庫。
以下是定時(shí)器向上溢出示例代碼:
C語言: TIM1模塊產(chǎn)生向上溢出事件
//Step1.時(shí)鐘設(shè)置:啟動(dòng)TIM1
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
//Step2.中斷NVIC設(shè)置:允許中斷,設(shè)置優(yōu)先級(jí)
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQChannel; //更新事件
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //搶占優(yōu)先級(jí)0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //響應(yīng)優(yōu)先級(jí)1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //允許中斷
NVIC_Init(&NVIC_InitStructure); //寫入設(shè)置
//Step3.TIM1模塊設(shè)置
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
//TIM1 使用內(nèi)部時(shí)鐘
//TIM_InternalClockConfig(TIM1);
//TIM1基本設(shè)置
//設(shè)置預(yù)分頻器分頻系數(shù)71,即APB2=72M, TIM1_CLK=72/72=1MHz
//TIM_Period(TIM1_ARR)=1000,計(jì)數(shù)器向上計(jì)數(shù)到1000后產(chǎn)生更新事件,計(jì)數(shù)值歸零
//向上計(jì)數(shù)模式
//TIM_RepetitionCounter(TIM1_RCR)=0,每次向上溢出都產(chǎn)生更新事件
TIM_BaseInitStructure.TIM_Period = 1000;
TIM_BaseInitStructure.TIM_Prescaler = 71;
TIM_BaseInitStructure.TIM_ClockDivision = 0;
TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_BaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_BaseInitStructure);
//清中斷,以免一啟用中斷后立即產(chǎn)生中斷
TIM_ClearFlag(TIM1, TIM_FLAG_Update);
//使能TIM1中斷源
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
//TIM1總開關(guān):開啟
TIM_Cmd(TIM1, ENABLE);
}
//Step4.中斷服務(wù)子程序:
void TIM1_UP_IRQHandler(void)
{
GPIOC->ODR ^= (1<<4); //閃燈
TIM_ClearITPendingBit(TIM1, TIM_FLAG_Update); //清中斷
}
下面是輸出比較功能實(shí)現(xiàn)TIM1_CH1管腳輸出指定頻率的脈沖:
C語言: TIM1模塊實(shí)現(xiàn)輸出比較,自動(dòng)翻轉(zhuǎn)并觸發(fā)中斷
//Step1.啟動(dòng)TIM1,同時(shí)還要注意給相應(yīng)功能管腳啟動(dòng)時(shí)鐘
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
//Step2. PA.8口設(shè)置為TIM1的OC1輸出口
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//Step3.使能TIM1的輸出比較匹配中斷
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//Step4. TIM模塊設(shè)置
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
//TIM1基本計(jì)數(shù)器設(shè)置
TIM_BaseInitStructure.TIM_Period = 0xffff; //這里必須是65535
TIM_BaseInitStructure.TIM_Prescaler = 71; //預(yù)分頻71,即72分頻,得1M
TIM_BaseInitStructure.TIM_ClockDivision = 0;
TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_BaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_BaseInitStructure);
//TIM1_OC1模塊設(shè)置
TIM_OCStructInit(& TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; //管腳輸出模式:翻轉(zhuǎn)
TIM_OCInitStructure.TIM_Pulse = 2000; //翻轉(zhuǎn)周期:2000個(gè)脈沖
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //使能TIM1_CH1通道
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //輸出為正邏輯
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //寫入配置
//清中斷
TIM_ClearFlag(TIM1, TIM_FLAG_CC1);
//TIM1中斷源設(shè)置,開啟相應(yīng)通道的捕捉比較中斷
TIM_ITConfig(TIM1, TIM_IT_CC1, ENABLE);
//TIM1開啟
TIM_Cmd(TIM1, ENABLE);
//通道輸出使能
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
Step5.中斷服務(wù)子程序
void TIM1_CC_IRQHandler(void)
{
u16 capture;
if(TIM_GetITStatus(TIM1, TIM_IT_CC1) == SET)
{
TIM_ClearITPendingBit(TIM1, TIM_IT_CC1 );
capture = TIM_GetCapture1(TIM1);
TIM_SetCompare1(TIM1, capture + 2000);
//這里解釋下:
//將TIM1_CCR1的值增加2000,使得下一個(gè)TIM事件也需要2000個(gè)脈沖,
//另一種方式是清零脈沖計(jì)數(shù)器
//TIM_SetCounter(TIM2,0x0000);
}
}
關(guān)于TIM的操作,要注意的是STM32處理器因?yàn)榈凸牡男枰?,各模塊需要分別獨(dú)立開啟時(shí)鐘,所以,一定不要忘記給用到的模塊和管腳使能時(shí)鐘,因?yàn)檫@個(gè)原因,浪費(fèi)了我好多時(shí)間阿~~!
九九的STM32筆記(二)TIM模塊產(chǎn)生PWM
這個(gè)是STM32的PWM輸出模式,STM32的TIM1模塊是增強(qiáng)型的定時(shí)器模塊,天生就是為電機(jī)控制而生,可以產(chǎn)生3組6路PWM,同時(shí)每組2路PWM為互補(bǔ),并可以帶有死區(qū),可以用來驅(qū)動(dòng)H橋。
下面的代碼,是利用TIM1模塊的1、2通道產(chǎn)生一共4路PWM的代碼例子,類似代碼也可以參考ST的固件庫中相應(yīng)example
C語言: TIM1模塊產(chǎn)生PWM,帶死區(qū)
//Step1.開啟TIM和相應(yīng)端口時(shí)鐘
//啟動(dòng)GPIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD,
ENABLE);
//啟動(dòng)AFIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
//啟動(dòng)TIM1
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
//Step2. GPIO做相應(yīng)設(shè)置,為AF輸出
//PA.8/9口設(shè)置為TIM1的OC1輸出口
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(GPIOA, &GPIO_InitStructure);
//PB.13/14口設(shè)置為TIM1_CH1N和TIM1_CH2N輸出口
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//Step3. TIM模塊初始化
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
//TIM1基本計(jì)數(shù)器設(shè)置(設(shè)置PWM頻率)
//頻率=TIM1_CLK/(ARR+1)
TIM_BaseInitStructure.TIM_Period = 1000-1;
TIM_BaseInitStructure.TIM_Prescal