3. 定時器控制 LED 燈閃爍(定時器 1 )
阿新 • • 發佈:2019-02-18
#include <TIM1.H> void TIM1_Init(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; //使能定時器1時鐘 RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);//定時器1在橋2上 TIM_TimeBaseStructure.TIM_Period = 10000-1;//ARR TIM_TimeBaseStructure.TIM_Prescaler = 7200;//PSC TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM1, & TIM_TimeBaseStructure); //使能定時器1中斷 TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE); //配置定時器1中斷優先順序 NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;//重新整理中斷 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); //使能定時器1 TIM_Cmd(TIM1,ENABLE); }
#ifndef _TIM1_H_
#define _TIM1_H_
#include <sys.h>
void TIM1_Init(void);
#endif
主函式
//程式目的:定時器1控制led閃爍 #include <sys.h> #include <led.h> #include <tim1.h> int main() { NVIC_Configuration(); Led_Init(); TIM1_Init(); while(1) { } } void TIM1_UP_IRQHandler(void) { TIM_ClearFlag(TIM1,TIM_FLAG_Update); LED=~LED; }