本文主要是介绍【stm-3】定时器定时中断定时器外部时钟,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.定时器定时中断
(1)main.c
#include "Device/Include/stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
#include "timer.h"
uint16_t Num;
int main(void)
{OLED_Init();Timer_Init();OLED_ShowString(1,1,"Num:");//OLED_Clear();while(1){OLED_ShowNum(1,5,Num,5);OLED_ShowNum(2,5,TIM_GetCounter(TIM2),5);}
}
(2)timer.c
#include "Device/Include/stm32f10x.h" // Device header
extern uint16_t Num;
void Timer_Init(void)
{RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);TIM_InternalClockConfig(TIM2);TIM_TimeBaseInitTypeDef TIM_TimeBaseInitstructure;TIM_TimeBaseInitstructure.TIM_ClockDivision=TIM_CKD_DIV1;TIM_TimeBaseInitstructure.TIM_CounterMode=TIM_CounterMode_Up;TIM_TimeBaseInitstructure.TIM_Period=10000-1;//定时器周期,实际就是ARR的值。计10000个数TIM_TimeBaseInitstructure.TIM_Prescaler=14400-1;//7200-1;//PSC.72MHZ下7200分频TIM_TimeBaseInitstructure.TIM_RepetitionCounter=0;//重复计数器,高级控制寄存器专用寄存器位,这里不用设置TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitstructure);//初始化定时器TIM_ClearFlag(TIM2,TIM_FLAG_Update);//手动清除标志位TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);//开启计数器中断NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);NVIC_InitTypeDef NVIC_InitTypeDefstructure;NVIC_InitTypeDefstructure.NVIC_IRQChannel=TIM2_IRQn;//设置中断通道NVIC_InitTypeDefstructure.NVIC_IRQChannelCmd=ENABLE;NVIC_InitTypeDefstructure.NVIC_IRQChannelPreemptionPriority=2;NVIC_InitTypeDefstructure.NVIC_IRQChannelSubPriority=1;NVIC_Init(&NVIC_InitTypeDefstructure);TIM_Cmd(TIM2,ENABLE);//计数器使能
}
void TIM2_IRQHandler(void)
{if(TIM_GetITStatus(TIM2,TIM_IT_Update)==SET)//检查中断标志位{Num++;TIM_ClearITPendingBit(TIM2,TIM_IT_Update);//清除中断标志位}
}
(3)timer.h
#ifndef __TIMER_H
#define __TIMER_Hvoid Timer_Init(void);#endif
2.定时器外部时钟
(1)timer.c
#include "Device/Include/stm32f10x.h" // Device header
extern uint16_t Num;
void Timer_Init(void)
{RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);GPIO_InitTypeDef GPIO_Initstructure;GPIO_Initstructure.GPIO_Mode=GPIO_Mode_IPU;GPIO_Initstructure.GPIO_Pin=GPIO_Pin_0;GPIO_Initstructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOA,&GPIO_Initstructure);TIM_ETRClockMode2Config(TIM2,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_NonInverted,0x0F);//外部时钟->外部时钟模式2->不分频->高电平或者上升沿有效->TIM_TimeBaseInitTypeDef TIM_TimeBaseInitstructure;TIM_TimeBaseInitstructure.TIM_ClockDivision=TIM_CKD_DIV1;TIM_TimeBaseInitstructure.TIM_CounterMode=TIM_CounterMode_Up;//定时器计数方式TIM_TimeBaseInitstructure.TIM_Period=10-1;//定时器周期,实际就是ARR的值。计9个数TIM_TimeBaseInitstructure.TIM_Prescaler=2-1;//每遮挡一次,CNT值+1,加到9,上面num+1。TIM_TimeBaseInitstructure.TIM_RepetitionCounter=0;//重复计数器,高级控制寄存器专用寄存器位,这里不用设置TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitstructure);//初始化定时器TIM_ClearFlag(TIM2,TIM_FLAG_Update);//手动清除标志位TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);//开启计数器中断NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);NVIC_InitTypeDef NVIC_InitTypeDefstructure;NVIC_InitTypeDefstructure.NVIC_IRQChannel=TIM2_IRQn;//设置中断通道NVIC_InitTypeDefstructure.NVIC_IRQChannelCmd=ENABLE;NVIC_InitTypeDefstructure.NVIC_IRQChannelPreemptionPriority=2;NVIC_InitTypeDefstructure.NVIC_IRQChannelSubPriority=1;NVIC_Init(&NVIC_InitTypeDefstructure);TIM_Cmd(TIM2,ENABLE);//计数器使能
}
void TIM2_IRQHandler(void)
{if(TIM_GetITStatus(TIM2,TIM_IT_Update)==SET)//检查中断标志位{Num++;TIM_ClearITPendingBit(TIM2,TIM_IT_Update);//清除中断标志位}
}
这篇关于【stm-3】定时器定时中断定时器外部时钟的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!