NVIC中断应用:精准延时的LED闪烁实验相关代码
时间: 2023-07-09 11:21:02 浏览: 81
好的,下面为您提供相关代码:
```c
#include "stm32f10x.h"
void Delay_ms(uint16_t nms);
void LED_Init(void);
void TIM2_Init(void);
void NVIC_Config(void);
int main(void)
{
LED_Init();
TIM2_Init();
NVIC_Config();
while (1)
{
// 主程序中不需要做任何操作,LED的闪烁由中断服务程序控制
}
}
void Delay_ms(uint16_t nms)
{
uint16_t i;
while (nms--)
{
for (i = 0; i < 12000; i++);
}
}
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void TIM2_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 1000 - 1; // 定时器自动重载值
TIM_TimeBaseStructure.TIM_Prescaler = 7200 - 1; // 定时器预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_Cmd(TIM2, ENABLE);
}
void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
}
void TIM2_IRQHandler(void)
{
static uint8_t LED_Status = 0;
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
if (LED_Status == 0)
{
GPIO_SetBits(GPIOB, GPIO_Pin_12);
LED_Status = 1;
}
else
{
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
LED_Status = 0;
}
}
}
```
在这段代码中,我们首先定义了`Delay_ms()`函数,用于实现毫秒级的延时,然后定义了`LED_Init()`函数和`TIM2_Init()`函数,分别用于初始化LED和定时器。在`NVIC_Config()`函数中,我们配置了NVIC中断控制器,使定时器溢出时触发中断。最后,在`TIM2_IRQHandler()`函数中,我们实现了LED的闪烁功能。
在主程序中,我们只需要初始化LED和定时器,然后让程序进入死循环即可,LED的闪烁由中断服务程序控制。
相关推荐
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)