STM32实战3.定时器控制LED灯闪烁(定时器1)

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



int main(void)
{
	NVIC_Configuration();
	LED_Init();
	delay_init();
	TIM1_Init();
	
	while(1)
	{
		
		
		
	}
	
	
	
	
	
}


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


	
	
}

#include "TIM1.h"



void TIM1_Init(void)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	//开启TIM1的使能定时器
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
	

	
	TIM_TimeBaseStructure.TIM_Period = (10000 - 1);//ARR
	TIM_TimeBaseStructure.TIM_Prescaler = (7200 - 1);//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);
	
	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"


void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);//初始化时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
STM32实战3.定时器控制LED灯闪烁(定时器1)_第1张图片

你可能感兴趣的:(STM32实战3.定时器控制LED灯闪烁(定时器1))