1. 程式人生 > >定時器控制LED燈閃爍(定時器1)

定時器控制LED燈閃爍(定時器1)

TIM.c檔案

#include "TIM1.h"






void TIM1_Init(void)
{
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
 NVIC_InitTypeDef NVIC_InitStructure;
 //使能定時器1時鐘
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);  


TIM_TimeBaseStructure.TIM_Period = (10000-1);//ARR
TIM_TimeBaseStructure.TIM_Prescaler = (7200-1);
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);
    //配置定時器中斷優先等級
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);

}

主函式

#include "sys.h"
#include "led.h"
#include "TIM1.h"
#include "delay.h"


int main()
{   
NVIC_Configuration();
Led_Init();
TIM1_Init();
delay_init();
while(1)
{
 delay_ms(100);
}
}


void  TIM1_UP_IRQHandler(void)
{
 TIM_ClearFlag(TIM1,TIM_FLAG_Update);
    LED=~LED;

}