新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > STM32單片機學習(5) 定時器中斷實驗

STM32單片機學習(5) 定時器中斷實驗

作者: 時間:2016-11-19 來源:網(wǎng)絡 收藏
/*
*	* 軟件功能:	 定時器中斷實驗* */#include "stm32f10x.h"#include "delay.h"void RCC_Configuration(void);void GPIO_Configuration(void);void NVIC_Configuration(void);void TIM3_Configuration(u16 arr,u16 psc);/*函數(shù): int main(void)功能: main主函數(shù)參數(shù): 無返回: 無/int main(void){RCC_Configuration();GPIO_Configuration();NVIC_Configuration();TIM3_Configuration(4999,7199);//10Khz的計數(shù)頻率,計數(shù)到5000為500ms  //delay_init(72);GPIO_ResetBits(GPIOB,GPIO_Pin_0); while(1);   }/*函數(shù): void RCC_Configuration(void)功能: 復位和時鐘控制 配置參數(shù): 無返回: 無/void RCC_Configuration(void){ErrorStatus HSEStartUpStatus;                    //定義外部高速晶體啟動狀態(tài)枚舉變量RCC_DeInit();                                    //復位RCC外部設備寄存器到默認值RCC_HSEConfig(RCC_HSE_ON);                       //打開外部高速晶振HSEStartUpStatus = RCC_WaitForHSEStartUp();      //等待外部高速時鐘準備好if(HSEStartUpStatus == SUCCESS)                  //外部高速時鐘已經(jīng)準別好{FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //開啟FLASH預讀緩沖功能,加速FLASH的讀取。所有程序中必須的用法.位置:RCC初始化子函數(shù)里面,時鐘起振之后FLASH_SetLatency(FLASH_Latency_2);                    //flash操作的延時RCC_HCLKConfig(RCC_SYSCLK_Div1);               //配置AHB(HCLK)時鐘等于==SYSCLKRCC_PCLK2Config(RCC_HCLK_Div1);                //配置APB2(PCLK2)鐘==AHB時鐘RCC_PCLK1Config(RCC_HCLK_Div2);                //配置APB1(PCLK1)鐘==AHB1/2時鐘RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);  //配置PLL時鐘 == 外部高速晶體時鐘 * 9 = 72MHzRCC_PLLCmd(ENABLE);                                   //使能PLL時鐘while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)    //等待PLL時鐘就緒{}RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);            //配置系統(tǒng)時鐘 = PLL時鐘while(RCC_GetSYSCLKSource() != 0x08)                  //檢查PLL時鐘是否作為系統(tǒng)時鐘{}}RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);  //允許 GPIOB、AFIO時鐘RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //時鐘使能}/*函數(shù): void GPIO_Configuration(void)功能: GPIO配置參數(shù): 無返回: 無/void GPIO_Configuration(void){GPIO_InitTypeDef GPIO_InitStructure;        //定義GPIO初始化結(jié)構(gòu)體GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); 	   //PB用于輸出控制LED燈}void NVIC_Configuration(void)	 //中斷分組和優(yōu)先級配置	 詳見《STM32的函數(shù)說明(中文).pdf》P165{NVIC_InitTypeDef NVIC_InitStructure;NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;  //TIM3中斷NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  //先占優(yōu)先級0級NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;  //從優(yōu)先級3級NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能NVIC_Init(&NVIC_InitStructure);  //初始化NVIC寄存器}void TIM3_Configuration(u16 arr,u16 psc)	  //TIM3定時器配置   {TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;TIM_TimeBaseStructure.TIM_Period = arr; //設置在下一個更新事件裝入活動的自動重裝載寄存器周期的值	TIM_TimeBaseStructure.TIM_Prescaler =psc; //設置用來作為TIMx時鐘頻率除數(shù)的預分頻值TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //設置時鐘分割:TDTS = Tck_timTIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上計數(shù)模式TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根據(jù)指定的參數(shù)初始化TIMx的時間基數(shù)單位/*((1+7199[TIM_Prescaler] )/72M)*(1+4999[TIM_Period] )=500,000us=500ms */ TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //使能指定的TIM3中斷,允許更新中斷TIM_Cmd(TIM3, ENABLE);  //使能TIMx}//定時器3中斷服務程序void TIM3_IRQHandler(void)   //TIM3中斷{if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)  //檢查TIM3更新中斷發(fā)生與否{TIM_ClearITPendingBit(TIM3, TIM_IT_Update  );  //清除TIMx更新中斷標志 if(!GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0)) 	GPIO_SetBits(GPIOB,GPIO_Pin_0); else   GPIO_ResetBits(GPIOB,GPIO_Pin_0); }}


評論


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

關(guān)閉