stm32f407外部中断控制led灯使用模块化设计
时间: 2023-10-22 21:07:48 浏览: 60
好的,针对您的问题,我可以给您提供以下模块化设计的示例代码:
1. 首先,我们需要将 LED 相关的代码封装到一个独立的模块中,例如 led.c 和 led.h。led.c 文件中可以定义一些控制 LED 灯的函数,例如 LED_Init() 和 LED_Toggle() 等。led.h 文件中则需要声明这些函数的原型。
2. 然后,我们需要将外部中断相关的代码封装到一个独立的模块中,例如 exti.c 和 exti.h。exti.c 文件中可以定义一些配置外部中断的函数,例如 EXTI_Init() 和 EXTI_IRQHandler() 等。exti.h 文件中则需要声明这些函数的原型。
3. 最后,在主函数中调用这些模块的函数即可。
下面是一份示例代码,仅供参考:
led.c 文件:
```
#include "led.h"
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// 使能 GPIOD 时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
// 配置 LED 引脚为推挽输出模式
GPIO_InitStructure.GPIO_Pin = LED_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LED_PORT, &GPIO_InitStructure);
// 初始状态为关闭
GPIO_SetBits(LED_PORT, LED_PIN);
}
void LED_Toggle(void)
{
GPIO_ToggleBits(LED_PORT, LED_PIN);
}
```
led.h 文件:
```
#ifndef __LED_H__
#define __LED_H__
#include "stm32f4xx.h"
#define LED_PIN GPIO_Pin_12
#define LED_PORT GPIOD
void LED_Init(void);
void LED_Toggle(void);
#endif
```
exti.c 文件:
```
#include "exti.h"
#include "led.h"
void EXTI_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
// 使能 GPIOA 和 SYSCFG 时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
// 配置 PA0 引脚为输入模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 配置 EXTI0 中断线路
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
// 配置 EXTI0 中断优先级
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void EXTI_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line0) != RESET)
{
// 等待一段时间,以消除抖动
for(int i = 0; i < 100000; i++);
// 切换 LED 灯状态
LED_Toggle();
EXTI_ClearITPendingBit(EXTI_Line0);
}
}
```
exti.h 文件:
```
#ifndef __EXTI_H__
#define __EXTI_H__
#include "stm32f4xx.h"
void EXTI_Init(void);
void EXTI_IRQHandler(void);
#endif
```
main.c 文件:
```
#include "stm32f4xx.h"
#include "led.h"
#include "exti.h"
int main(void)
{
LED_Init();
EXTI_Init();
while(1);
return 0;
}
```
这份代码中,我们将控制 LED 灯的代码封装到了 led.c 和 led.h 中,将配置外部中断的代码封装到了 exti.c 和 exti.h 中。在主函数中,我们只需要调用 LED_Init() 和 EXTI_Init() 即可完成硬件的初始化。当外部中断触发时,中断回调函数 EXTI_IRQHandler() 会被调用,然后在其中调用 LED_Toggle() 函数切换 LED 灯的状态。
相关推荐
![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)