摩尔芯闻 > 行业新闻 > 半导体 > stm32学习笔记---计数器定时中断(1s)

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

eefocus ·2019-01-02 22:11·电子工程世界
阅读:1651

#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');

}

}

 

 
分享到:
微信 新浪微博 QQ空间 LinkedIn

上一篇:充电桩是未来十多年的新蓝海?

下一篇:关于stm32定时器定时周期计算

打开摩尔直播,更多新闻内容
半导体大咖直播分享高清观看
立即下载