STM32學(xué)習(xí)筆記6(TIM模塊定時器)
首先我們必須肯定ST公司的實力,也承認STM32的確是一款非常不錯的Cortex-M3核單片機,但是,他的手冊實在是讓人覺得無法理解,尤其是其中的TIM模塊,沒有條理可言,看了兩天幾乎還是不知所云,讓人很是郁悶。同時配套的固件庫的說明也很難和手冊上的寄存器對應(yīng)起來,研究起來非常費勁!功能強大倒是真的,但至少也應(yīng)該配套一個讓人看的明白的說明吧~~兩天時間研究了STM32定時器的最最基礎(chǔ)的部分,把定時器最基礎(chǔ)的兩個功能實現(xiàn)了,余下的功能有待繼續(xù)學(xué)習(xí)。首先有一點需要注意:FWLib固件庫目前的最新版應(yīng)該是V2.0.x,V1.0.x版本固件庫中,TIM1模塊被獨立出來,調(diào)用的函數(shù)與其他定時器不同;在V2.0系列版本中,取消了TIM1.h,所有的TIM模塊統(tǒng)一調(diào)用TIM.h即可。網(wǎng)絡(luò)上流傳的各種代碼有許多是基于v1版本的固件庫,在移植到v2版本固件庫時,需要做些修改。本文的所有程序都是基于V2.0固件庫。
本文引用地址:http://m.butianyuan.cn/article/201611/322603.htm以下是定時器向上溢出示例代碼:
C語言: TIM1模塊產(chǎn)生向上溢出事件//Step1.時鐘設(shè)置:啟動TIM1RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
//Step2.中斷NVIC設(shè)置:允許中斷,設(shè)置優(yōu)先級NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQChannel;//更新事件NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//搶占優(yōu)先級0NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;//響應(yīng)優(yōu)先級1NVIC_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)部時鐘//TIM_InternalClockConfig(TIM1);
//TIM1基本設(shè)置//設(shè)置預(yù)分頻器分頻系數(shù)71,即APB2=72M, TIM1_CLK=72/72=1MHz//TIM_Period(TIM1_ARR)=1000,計數(shù)器向上計數(shù)到1000后產(chǎn)生更新事件,計數(shù)值歸零//向上計數(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); //清中斷}
下面是輸出比較功能實現(xiàn)TIM1_CH1管腳輸出指定頻率的脈沖:
C語言: TIM1模塊實現(xiàn)輸出比較,自動翻轉(zhuǎn)并觸發(fā)中斷//Step1.啟動TIM1,同時還要注意給相應(yīng)功能管腳啟動時鐘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基本計數(shù)器設(shè)置TIM_BaseInitStructure.TIM_Period = 0xffff;//這里必須是65535TIM_BaseInitStructure.TIM_Prescaler = 71;//預(yù)分頻71,即72分頻,得1MTIM_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個脈沖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,使得下一個TIM事件也需要2000個脈沖,//另一種方式是清零脈沖計數(shù)器//TIM_SetCounter(TIM2,0x0000);}}
關(guān)于TIM的操作,要注意的是STM32處理器因為低功耗的需要,各模塊需要分別獨立開啟時鐘,所以,一定不要忘記給用到的模塊和管腳使能時鐘,因為這個原因,浪費了我好多時間阿~~!
九九的STM32筆記(二)TIM模塊產(chǎn)生PWM
這個是STM32的PWM輸出模式,STM32的TIM1模塊是增強型的定時器模塊,天生就是為電機控制而生,可以產(chǎn)生3組6路PWM,同時每組2路PWM為互補,并可以帶有死區(qū),可以用來驅(qū)動H橋。下面的代碼,是利用TIM1模塊的1、2通道產(chǎn)生一共4路PWM的代碼例子,類似代碼也可以參考ST的固件庫中相應(yīng)exampleC語言: TIM1模塊產(chǎn)生PWM,帶死區(qū)//Step1.開啟TIM和相應(yīng)端口時鐘//啟動GPIORCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD,ENABLE);//啟動AFIORCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);//啟動TIM1RCC_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基本計數(shù)器設(shè)置(設(shè)置PWM頻率)//頻率=TIM1_CLK/(ARR+1)TIM_BaseInitStructure.TIM_Period = 1000-1;TIM_BaseInitStructure.TIM_Prescaler = 72-1;TIM_BaseInitStructure.TIM_ClockDivision = 0;TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;TIM_BaseInitStructure.TIM_RepetitionCounter = 0;TIM_TimeBaseInit(TIM1, &TIM_BaseInitStructure);//啟用ARR的影子寄存器(直到產(chǎn)生更新事件才更改設(shè)置)TIM_ARRPreloadConfig(TIM1, ENABLE);
//TIM1_OC1模塊設(shè)置(設(shè)置1通道占空比)TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;TIM_OCInitStructure.TIM_Pulse = 120;TIM_OC1Init(TIM1, &TIM_OCInitStructure);//啟用CCR1寄存器的影子寄存器(直到產(chǎn)生更新事件才更改設(shè)置)TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
//TIM2_OC2模塊設(shè)置(設(shè)置2通道占空比)TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;TIM_OCInitStructure.TIM_Pulse = 680;TIM_OC2Init(TIM1, &TIM_OCInitStructure);//啟用CCR2寄存器的影子寄存器(直到產(chǎn)生更新事件才更改設(shè)置)TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);//死區(qū)設(shè)置TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;TIM_BDTRInitStructure.TIM_DeadTime = 0x90;//這里調(diào)整死區(qū)大小0-0xffTIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);//TIM1開啟TIM_Cmd(TIM1, ENABLE);//TIM1_OC通道輸出PWM(一定要加)TIM_CtrlPWMOutputs(TIM1, ENABLE);
}其實,PWM模塊還可以有很多花樣可以玩,比方在異常時(如CPU時鐘有問題),可以緊急關(guān)閉輸出,以免發(fā)生電路燒毀等嚴重事故
《九九的STM32筆記》整理(2)
這是一個綜合的例子,演示了ADC模塊、DMA模塊和USART模塊的基本使用。我們在這里設(shè)置ADC為連續(xù)轉(zhuǎn)換模式,常規(guī)轉(zhuǎn)換序列中有兩路轉(zhuǎn)換通道,分別是ADC_CH10(PC0)和ADC_CH16(片內(nèi)溫度傳感器)。因為使用了自動多通道轉(zhuǎn)換,數(shù)據(jù)的取出工作最適合使用DMA方式取出,so,我們在內(nèi)存里開辟了一個u16 AD_Value[2]數(shù)組,并設(shè)置了相應(yīng)的DMA模塊,使ADC在每個通道轉(zhuǎn)換結(jié)束后啟動DMA傳輸,其緩沖區(qū)數(shù)據(jù)量為2個HalfWord,使兩路通道的轉(zhuǎn)換結(jié)果自動的分別落到 AD_Value[0]和AD_Value[1]中。然后,在主函數(shù)里,就無需手動啟動AD轉(zhuǎn)換,等待轉(zhuǎn)換結(jié)束,再取結(jié)果了。我們可以在主函數(shù)里隨時取AD_Value中的數(shù)值,那里永遠都是最新的AD轉(zhuǎn)換結(jié)果。如果我們定義一個更大的AD_Value數(shù)組,并調(diào)整DMA的傳輸數(shù)據(jù)量(BufferSize)可以實現(xiàn)AD結(jié)果的循環(huán)隊列存儲,從而可以進行各種數(shù)字濾波算法。接著,取到轉(zhuǎn)換結(jié)果后,根據(jù)V=(AD_Value/4096)*Vref+的公式可以算出相應(yīng)通道的電壓值,也可以根據(jù) T(℃) = (1.43 - Vad)/34*10^(-6) + 25的算法,得到片內(nèi)溫度傳感器的測量溫度值了。通過重新定義putchar函數(shù),及包含"stdio.h"頭文件,我們可以方便的使用標準C的庫函數(shù)printf(),實現(xiàn)串口通信。相關(guān)的官方例程,可以參考FWLib V2.0的ADCADC1_DMA和USARTprintf兩個目錄下的代碼。
本代碼例子是基于萬利199的開發(fā)板EK-STM32F實現(xiàn),CPU=STM32F103VBT6
#i nclude "stm32f10x_lib.h"#i nclude "stdio.h"
#define ADC1_DR_Address((u32)0x4001244C)vu16 AD_Value[2];vu16 i=0;s16 Temp;u16 Volt;
void RCC_Configuration(void);void GPIO_Configuration(void);void NVIC_Configuration(void);void USART1_Configuration(void);void ADC1_Configuration(void);void DMA_Configuration(void);
int fputc(int ch, FILE *f);void Delay(void);u16 GetTemp(u16 advalue);u16 GetVolt(u16 advalue);int main(void){RCC_Configuration();GPIO_Configuration();NVIC_Configuration();USART1_Configuration();DMA_Configuration();ADC1_Configuration();//啟動第一次AD轉(zhuǎn)換ADC_SoftwareStartConvCmd(ADC1, ENABLE);//因為已經(jīng)配置好了DMA,接下來AD自動連續(xù)轉(zhuǎn)換,結(jié)果自動保存在AD_Value處while (1){Delay();Temp = GetTemp(AD_Value[1]);Volt = GetVolt(AD_Value[0]);USART_SendData(USART1, 0x0c);//清屏//注意,USART_SendData函數(shù)不檢查是否發(fā)送完成//等待發(fā)送完成while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
printf("電壓:%d.%d 溫度:%d.%d℃", Volt/100, Volt0, Temp/100, Temp0);}}
int fputc(int ch, FILE *f){//USART_SendData(USART1, (u8) ch);USART1->DR = (u8) ch;while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET){}
return ch;}
void Delay(void){u32 i;for(i=0;i<0x4f0000;i++);return;}
u16 GetTemp(u16 advalue){u32 Vtemp_sensor;s32 Current_Temp;//ADC轉(zhuǎn)換結(jié)束以后,讀取ADC_DR寄存器中的結(jié)果,轉(zhuǎn)換溫度值計算公式如下://V25 - VSENSE// T(℃) = ------------ + 25//Avg_Slope//V25: 溫度傳感器在25℃時 的輸出電壓,典型值1.43 V。// VSENSE:溫度傳感器的當(dāng)前輸出電壓,與ADC_DR 寄存器中的結(jié)果ADC_ConvertedValue之間的轉(zhuǎn)換關(guān)系為://ADC_ConvertedValue * Vdd// VSENSE = --------------------------//Vdd_convert_value(0xFFF)// Avg_Slope:溫度傳感器輸出電壓和溫度的關(guān)聯(lián)參數(shù),典型值4.3 mV/℃。
Vtemp_sensor = advalue * 330 / 4096;Current_Temp = (s32)(143 - Vtemp_sensor)*10000/43 + 2500;return (s16)Current_Temp;}
u16 GetVolt(u16 advalue){return (u16)(advalue * 330 / 4096);}
void RCC_Configuration(void){ErrorStatus HSEStartUpStatus;
//使能外部晶振RCC_HSEConfig(RCC_HSE_ON);//等待外部晶振穩(wěn)定HSEStartUpStatus = RCC_WaitForHSEStartUp();//如果外部晶振啟動成功,則進行下一步操作if(HSEStartUpStatus==SUCCESS){//設(shè)置HCLK(AHB時鐘)=SYSCLKRCC_HCLKConfig(RCC_SYSCLK_Div1);
//PCLK1(APB1) = HCLK/2RCC_PCLK1Config(RCC_HCLK_Div2);
//PCLK2(APB2) = HCLKRCC_PCLK2Config(RCC_HCLK_Div1);//設(shè)置ADC時鐘頻率RCC_ADCCLKConfig(RCC_PCLK2_Div2);
//FLASH時序控制//推薦值:SYSCLK = 0~24MHzLatency=0//SYSCLK = 24~48MHz Latency=1//SYSCLK = 48~72MHz Latency=2FLASH_SetLatency(FLASH_Latency_2);//開啟FLASH預(yù)取指功能FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
//PLL設(shè)置 SYSCLK/1 * 9 = 8*1*9 = 72MHzRCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);//啟動PLLRCC_PLLCmd(ENABLE);//等待PLL穩(wěn)定while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);//系統(tǒng)時鐘SYSCLK來自PLL輸出RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//切換時鐘后等待系統(tǒng)時鐘穩(wěn)定while(RCC_GetSYSCLKSource()!=0x08);
}
//下面是給各模塊開啟時鐘//啟動GPIORCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD,ENABLE);//啟動AFIORCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);//啟動USART1RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//啟動DMA時鐘RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);//啟動ADC1時鐘RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
}
評論