<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > STM32學(xué)習(xí)筆記6(TIM模塊定時(shí)器)

          STM32學(xué)習(xí)筆記6(TIM模塊定時(shí)器)

          作者: 時(shí)間:2016-11-28 來(lái)源:網(wǎng)絡(luò) 收藏
          TIM模塊定時(shí)器向上溢出 & 輸出比較

          首先我們必須肯定ST公司的實(shí)力,也承認(rèn)STM32的確是一款非常不錯(cuò)的Cortex-M3核單片機(jī),但是,他的手冊(cè)實(shí)在是讓人覺(jué)得無(wú)法理解,尤其是其中的TIM模塊,沒(méi)有條理可言,看了兩天幾乎還是不知所云,讓人很是郁悶。同時(shí)配套的固件庫(kù)的說(shuō)明也很難和手冊(cè)上的寄存器對(duì)應(yīng)起來(lái),研究起來(lái)非常費(fèi)勁!功能強(qiáng)大倒是真的,但至少也應(yīng)該配套一個(gè)讓人看的明白的說(shuō)明吧~~兩天時(shí)間研究了STM32定時(shí)器的最最基礎(chǔ)的部分,把定時(shí)器最基礎(chǔ)的兩個(gè)功能實(shí)現(xiàn)了,余下的功能有待繼續(xù)學(xué)習(xí)。首先有一點(diǎn)需要注意:FWLib固件庫(kù)目前的最新版應(yīng)該是V2.0.x,V1.0.x版本固件庫(kù)中,TIM1模塊被獨(dú)立出來(lái),調(diào)用的函數(shù)與其他定時(shí)器不同;在V2.0系列版本中,取消了TIM1.h,所有的TIM模塊統(tǒng)一調(diào)用TIM.h即可。網(wǎng)絡(luò)上流傳的各種代碼有許多是基于v1版本的固件庫(kù),在移植到v2版本固件庫(kù)時(shí),需要做些修改。本文的所有程序都是基于V2.0固件庫(kù)。

          本文引用地址:http://www.ex-cimer.com/article/201611/322603.htm

          以下是定時(shí)器向上溢出示例代碼:

          C語(yǔ)言: TIM1模塊產(chǎn)生向上溢出事件//Step1.時(shí)鐘設(shè)置:?jiǎn)?dòng)TIM1RCC_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í)0NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;//響應(yīng)優(yōu)先級(jí)1NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//允許中斷NVIC_Init(&NVIC_InitStructure);//寫(xiě)入設(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總開(kāi)關(guān):開(kāi)啟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語(yǔ)言: 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è)置為T(mén)IM1的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;//這里必須是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個(gè)脈沖TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//使能TIM1_CH1通道TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//輸出為正邏輯TIM_OC1Init(TIM1, &TIM_OCInitStructure);//寫(xiě)入配置

          //清中斷TIM_ClearFlag(TIM1, TIM_FLAG_CC1);

          //TIM1中斷源設(shè)置,開(kāi)啟相應(yīng)通道的捕捉比較中斷TIM_ITConfig(TIM1, TIM_IT_CC1, ENABLE);

          //TIM1開(kāi)啟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)榈凸牡男枰髂K需要分別獨(dú)立開(kāi)啟時(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ū),可以用來(lái)驅(qū)動(dòng)H橋。下面的代碼,是利用TIM1模塊的1、2通道產(chǎn)生一共4路PWM的代碼例子,類(lèi)似代碼也可以參考ST的固件庫(kù)中相應(yīng)exampleC語(yǔ)言: TIM1模塊產(chǎn)生PWM,帶死區(qū)//Step1.開(kāi)啟TIM和相應(yīng)端口時(shí)鐘//啟動(dòng)GPIORCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD,ENABLE);//啟動(dòng)AFIORCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);//啟動(dòng)TIM1RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

          //Step2. GPIO做相應(yīng)設(shè)置,為AF輸出//PA.8/9口設(shè)置為T(mén)IM1的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è)置為T(mén)IM1_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_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開(kāi)啟TIM_Cmd(TIM1, ENABLE);//TIM1_OC通道輸出PWM(一定要加)TIM_CtrlPWMOutputs(TIM1, ENABLE);

          }其實(shí),PWM模塊還可以有很多花樣可以玩,比方在異常時(shí)(如CPU時(shí)鐘有問(wèn)題),可以緊急關(guān)閉輸出,以免發(fā)生電路燒毀等嚴(yán)重事故

          《九九的STM32筆記》整理(2)

          這是一個(gè)綜合的例子,演示了ADC模塊、DMA模塊和USART模塊的基本使用。我們?cè)谶@里設(shè)置ADC為連續(xù)轉(zhuǎn)換模式,常規(guī)轉(zhuǎn)換序列中有兩路轉(zhuǎn)換通道,分別是ADC_CH10(PC0)和ADC_CH16(片內(nèi)溫度傳感器)。因?yàn)槭褂昧俗詣?dòng)多通道轉(zhuǎn)換,數(shù)據(jù)的取出工作最適合使用DMA方式取出,so,我們?cè)趦?nèi)存里開(kāi)辟了一個(gè)u16 AD_Value[2]數(shù)組,并設(shè)置了相應(yīng)的DMA模塊,使ADC在每個(gè)通道轉(zhuǎn)換結(jié)束后啟動(dòng)DMA傳輸,其緩沖區(qū)數(shù)據(jù)量為2個(gè)HalfWord,使兩路通道的轉(zhuǎn)換結(jié)果自動(dòng)的分別落到 AD_Value[0]和AD_Value[1]中。然后,在主函數(shù)里,就無(wú)需手動(dòng)啟動(dòng)AD轉(zhuǎn)換,等待轉(zhuǎn)換結(jié)束,再取結(jié)果了。我們可以在主函數(shù)里隨時(shí)取AD_Value中的數(shù)值,那里永遠(yuǎn)都是最新的AD轉(zhuǎn)換結(jié)果。如果我們定義一個(gè)更大的AD_Value數(shù)組,并調(diào)整DMA的傳輸數(shù)據(jù)量(BufferSize)可以實(shí)現(xiàn)AD結(jié)果的循環(huán)隊(duì)列存儲(chǔ),從而可以進(jìn)行各種數(shù)字濾波算法。接著,取到轉(zhuǎn)換結(jié)果后,根據(jù)V=(AD_Value/4096)*Vref+的公式可以算出相應(yīng)通道的電壓值,也可以根據(jù) T(℃) = (1.43 - Vad)/34*10^(-6) + 25的算法,得到片內(nèi)溫度傳感器的測(cè)量溫度值了。通過(guò)重新定義putchar函數(shù),及包含"stdio.h"頭文件,我們可以方便的使用標(biāo)準(zhǔn)C的庫(kù)函數(shù)printf(),實(shí)現(xiàn)串口通信。相關(guān)的官方例程,可以參考FWLib V2.0的ADCADC1_DMA和USARTprintf兩個(gè)目錄下的代碼。

          本代碼例子是基于萬(wàn)利199的開(kāi)發(fā)板EK-STM32F實(shí)現(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();//啟動(dòng)第一次AD轉(zhuǎn)換ADC_SoftwareStartConvCmd(ADC1, ENABLE);//因?yàn)橐呀?jīng)配置好了DMA,接下來(lái)AD自動(dòng)連續(xù)轉(zhuǎn)換,結(jié)果自動(dòng)保存在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)換溫度值計(jì)算公式如下://V25 - VSENSE// T(℃) = ------------ + 25//Avg_Slope//V25: 溫度傳感器在25℃時(shí) 的輸出電壓,典型值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();//如果外部晶振啟動(dòng)成功,則進(jìn)行下一步操作if(HSEStartUpStatus==SUCCESS){//設(shè)置HCLK(AHB時(shí)鐘)=SYSCLKRCC_HCLKConfig(RCC_SYSCLK_Div1);

          //PCLK1(APB1) = HCLK/2RCC_PCLK1Config(RCC_HCLK_Div2);

          //PCLK2(APB2) = HCLKRCC_PCLK2Config(RCC_HCLK_Div1);//設(shè)置ADC時(shí)鐘頻率RCC_ADCCLKConfig(RCC_PCLK2_Div2);

          //FLASH時(shí)序控制//推薦值:SYSCLK = 0~24MHzLatency=0//SYSCLK = 24~48MHz Latency=1//SYSCLK = 48~72MHz Latency=2FLASH_SetLatency(FLASH_Latency_2);//開(kāi)啟FLASH預(yù)取指功能FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

          //PLL設(shè)置 SYSCLK/1 * 9 = 8*1*9 = 72MHzRCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);//啟動(dòng)PLLRCC_PLLCmd(ENABLE);//等待PLL穩(wěn)定while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);//系統(tǒng)時(shí)鐘SYSCLK來(lái)自PLL輸出RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//切換時(shí)鐘后等待系統(tǒng)時(shí)鐘穩(wěn)定while(RCC_GetSYSCLKSource()!=0x08);

          }

          //下面是給各模塊開(kāi)啟時(shí)鐘//啟動(dòng)GPIORCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD,ENABLE);//啟動(dòng)AFIORCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);//啟動(dòng)USART1RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//啟動(dòng)DMA時(shí)鐘RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);//啟動(dòng)ADC1時(shí)鐘RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

          }


          上一頁(yè) 1 2 下一頁(yè)

          評(píng)論


          技術(shù)專(zhuān)區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();