stm32学习笔记---计数器定时中断(1s)

stm32学习笔记---计数器定时中断(1s)_第1张图片

 stm32学习笔记---计数器定时中断(1s)_第2张图片


#include "tim_driver.h"


//tim2³õʼ»¯:1ÃëÖÓ¸üÐÂÊý¾Ý
void tim2_init(void)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	
	//ʹÄÜʱÖÓ
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
	
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStruct.TIM_CounterMode   = TIM_CounterMode_Up;
	TIM_TimeBaseInitStruct.TIM_Period        = 9999;   
	TIM_TimeBaseInitStruct.TIM_Prescaler     = 7199;
	TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct);
	
	//ʹÄÜUPDATAµÄÖжÏÅäÖÃλ
	TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
	
	//¶¨Ê±Æ÷ʹÄÜ
	TIM_Cmd(TIM2,ENABLE);
}

//tim2Öжϳõʼ»¯
void tim2_NVIC_init(void)
{
	NVIC_InitTypeDef NVIC_InitStruct;
	
	NVIC_InitStruct.NVIC_IRQChannel      = TIM2_IRQn;  
	NVIC_InitStruct.NVIC_IRQChannelCmd   = ENABLE;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority  = 0;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority   = 3;

	NVIC_Init(&NVIC_InitStruct);
}
void TIM2_IRQHandler(void)
{

	if(TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)

		TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
		uart1_putc('A');
	}
}

 

 

 

你可能感兴趣的:(嵌入式)