参考文章
STM32之定时器中断控制LED闪烁
https://blog.csdn.net/qq_36554582/article/details/81235078
led.h
#ifndef __LED_H
#define __LED_H
#include "sys.h"
#define LEDPORT GPIOB //定义IO接口
#define LED1 GPIO_Pin_0 //定义IO接口
#define LED2 GPIO_Pin_1 //定义IO接口
void LED_Init(void);//初始化
#endif
led.c
#include "led.h"
void LED_Init(void){ //LED灯的接口初始化
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Pin = LED1 | LED2; //选择端口号(0~15或all)
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //选择IO接口工作方式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //设置IO接口速度(2/10/50MHz)
GPIO_Init(LEDPORT, &GPIO_InitStructure);
}
time3.h
#ifndef __TIME3_H
#define __TIME3_H
#include "sys.h"
void time3_init(u16 per, u16 pre); //定时器3初始化
void NVIC_INIT(void); //嵌套向量控制器初始化
#endif
time3.c
#include "time3.h"
#include "led.h"
void time3_init(u16 per, u16 pre)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//使能TIM3的时钟
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;//分频因子0
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//计数方式为向上计数
TIM_TimeBaseStructure.TIM_Period=per;//计数器计数值
TIM_TimeBaseStructure.TIM_Prescaler=pre;//分频系数
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);//使能TIM3的更新中断
TIM_Cmd(TIM3,ENABLE);//使能TIM3
}
void NVIC_INIT(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);//设定中断优先级分组0
NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn;//设定定时器3中断向量
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;//使能响应中断向量的中断响应
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;//配置中断向量的抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority=3;//配置中断向量的响应优先级
NVIC_Init(&NVIC_InitStructure);
}
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET)//判断是否发生了更新(溢出)中断
{
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);//清除溢出中断标志位
}
if(GPIO_ReadOutputDataBit(LEDPORT, LED1))
GPIO_WriteBit(LEDPORT,LED1,(BitAction)(0));
else
GPIO_WriteBit(LEDPORT,LED1,(BitAction)(1));
}
main.c
#include "stm32f10x.h"
#include "sys.h"
#include "delay.h"
#include "led.h"
#include "time3.h"
int main (void) {
RCC_Configuration();
LED_Init();
NVIC_INIT();
time3_init(7199,9999);
while(1){
}
}
参数7199和9999的含义
这里7199代表定时器3的计数器从0计数到7199,共计7200个数
这里9999代表定时器计数的频率为72MHz / (9999+1)=7200Hz,即周期为1/7200s
那么共计时7200×(1/7200s) = 1s
除了使用中断方式,还可以使用查询方式实现led闪烁
time3.h省略NVIC_INIT
time3.c为
#include "time.h"
#include "led.h"
void time3_init(u16 per,u16 pre)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//使能TIM3的时钟
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;//TIM_CKD_DIV1是.h文件中已经定义好的,TIM_CKD_DIV1=0,也就是时钟分频因子为0
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//计数方式为向上计数
TIM_TimeBaseStructure.TIM_Period=per;//周期
TIM_TimeBaseStructure.TIM_Prescaler=pre;//分频系数
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);//使能TIM3的更新中断
TIM_Cmd(TIM3,ENABLE);//使能TIM3
}
main.c为
#include "stm32f10x.h"
#include "sys.h"
#include "delay.h"
#include "led.h"
#include "time3.h"
int main (void) {
RCC_Configuration();
LED_Init();
time3_init(7199,9999);
while(1){
if(TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET)//判断是否发生了更新(溢出)中断
{
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);//清除溢出中断标志位
if(GPIO_ReadOutputDataBit(LEDPORT, LED1))
GPIO_WriteBit(LEDPORT,LED1,(BitAction)(0));
else
GPIO_WriteBit(LEDPORT,LED1,(BitAction)(1));
}
}
}
网友评论