#include "stm32f4xx.h"
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "timer.h"
int main(void){
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init(168);
LED_Init();
TIM3_Init(5000-1,8400-1);//定时器时钟84M,分频系数8400,所以84M/8400=10Khz的计数频率,计数5000次为500ms
while(1){
LED0=!LED0;
delay_ms(200);
};
}
#include "timer.h"
#include "led.h"
//通用定时器3中断初始化
//arr:自动重装值。
//psc:时钟预分频数
//定时器溢出时间计算方法:Tout=((arr+1)*(psc+1))/Ft us.
//Ft=定时器工作频率,单位:Mhz
//这里使用的是定时器3!
void TIM3_Init(u16 arr,u16 psc){
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStrcter;
NVIC_InitTypeDef NVIC_InitStrcter;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
TIM_TimeBaseInitStrcter.TIM_ClockDivision=TIM_CKD_DIV1;//时钟分频因子 (Clock division)
TIM_TimeBaseInitStrcter.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStrcter.TIM_Period=arr;
TIM_TimeBaseInitStrcter.TIM_Prescaler=psc;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStrcter);
//允许定时器3更新中断
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM3,ENABLE);//使能定时器
NVIC_InitStrcter.NVIC_IRQChannel=TIM3_IRQn;
NVIC_InitStrcter.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStrcter.NVIC_IRQChannelPreemptionPriority=0x01;
NVIC_InitStrcter.NVIC_IRQChannelSubPriority=0x02;
NVIC_Init(&NVIC_InitStrcter);
}
void TIM3_IRQHandler(void ){
if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET){
LED1=!LED1;
}
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
}