STM32通用定時(shí)器TIM25基本用法
按以下步驟編程:
1.系統(tǒng)初始化,主要初始化時(shí)鐘等。
2.GPIO初始化,用于LED,有了燈就便于觀察了。
3.TIM3的配置。
4.NVIC的配置。
5.編寫中斷服務(wù)函數(shù)。
void GPIO_PA_Init(){//PA8管腳配置GPIO_InitTypeDef GPIO_InitStructure;GPIO_DeInit(GPIOA);GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//推挽輸出RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);//使能端口時(shí)鐘AGPIO_Init(GPIOA,&GPIO_InitStructure);}void TIMER3_Init(){TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;TIM_DeInit(TIM3);RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);TIM_TimeBaseStructure.TIM_Period=10000;//ARR的值TIM_TimeBaseStructure.TIM_Prescaler=7200-1;TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//向上計(jì)數(shù)模式TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);TIM_Cmd(TIM3, ENABLE); //開啟時(shí)鐘}void NVIC_Configuration(){ NVIC_InitTypeDef NVIC_InitStructure;NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); // 搶占式優(yōu)先級(jí)別 NVIC_InitStructure.NVIC_IRQChannel =TIM3_IRQChannel;//指定中斷源NVIC_InitStructure.NVIC_IRQChannelSubPriority =0;// 指定響應(yīng)優(yōu)先級(jí)別1NVIC_InitStructure.NVIC_IRQChannelCmd =ENABLE;NVIC_Init(&NVIC_InitStructure);}int main(void){ Stm32_Clock_Init(9); //系統(tǒng)時(shí)鐘設(shè)置delay_init(72); //延時(shí)初始化GPIO_PA_Init();TIMER3_Init();NVIC_Configuration();while(1); }void TIM3_IRQHandler(){if(TIM_GetITStatus(TIM3 , TIM_IT_Update) ==SET){TIM_ClearITPendingBit(TIM3 ,TIM_FLAG_Update);if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_8))GPIO_ResetBits(GPIOA, GPIO_Pin_8);else GPIO_SetBits(GPIOA,GPIO_Pin_8);}}
評(píng)論