用外部中断控制四个按键控制12个跑马灯,基于STM32F103R6
时间: 2024-06-04 08:12:25 浏览: 178
本文将介绍如何使用STM32F103R6微控制器和外部中断控制四个按键来控制12个跑马灯的方法。
硬件设计
1. 硬件连接
将STM32F103R6微控制器的引脚连接到12个LED灯和4个按键。可以使用以下引脚连接:
- PA0~PA3:用于连接4个按键
- PB0~PB3、PB4~PB7、PC13~PC15:用于连接12个LED灯
2. 外部中断
在STM32F103R6微控制器中,有16个外部中断线。在本例中,我们将使用PA0~PA3引脚作为外部中断输入。每个引脚都可以配置为下降沿触发或上升沿触发。在本例中,我们将使用下降沿触发。
软件设计
1. 系统时钟和中断初始化
首先,我们需要初始化系统时钟和中断。在本例中,我们将使用PLL时钟,并初始化4个外部中断线。
2. GPIO初始化
接下来,我们需要初始化GPIO引脚。在本例中,我们将使用PB0~PB3、PB4~PB7和PC13~PC15作为输出引脚,用于连接12个LED灯。我们还需要初始化PA0~PA3引脚作为输入引脚,用于连接4个按键。
3. 中断处理函数
当按下任何一个按键时,将触发一个外部中断,然后跑马灯将改变方向或速度。我们需要编写一个中断处理函数来处理外部中断。
4. 主函数
在主函数中,我们将无限循环,等待外部中断的触发。当接收到一个外部中断时,我们将调用中断处理函数来改变跑马灯的方向或速度。
完整代码
以下是基于STM32F103R6的控制12个跑马灯的完整代码:
相关问题
用外部中断实现四个按键控制12个跑马灯,基于STM32F103R6
以下是基于STM32F103R6的代码实现:
首先需要初始化GPIO口和外部中断,如下所示:
```c
// 初始化GPIO口和外部中断
void GPIO_EXTI_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
// 使能GPIOA和GPIOB时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
// 配置PA0、PA1、PB0和PB1为上拉输入模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// 配置PA0、PA1、PB0和PB1对应的外部中断线路
EXTI_InitStructure.EXTI_Line = EXTI_Line0 | EXTI_Line1 | EXTI_Line2 | EXTI_Line3;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
// 配置外部中断优先级
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);
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn;
NVIC_Init(&NVIC_InitStructure);
}
```
然后定义一个全局变量用于记录当前按下的按键编号:
```c
volatile uint8_t key_num = 0;
```
在每个外部中断的中断服务函数中,将对应的按键编号存储到全局变量中:
```c
void EXTI0_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line0) != RESET)
{
key_num = 1;
EXTI_ClearITPendingBit(EXTI_Line0);
}
}
void EXTI1_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line1) != RESET)
{
key_num = 2;
EXTI_ClearITPendingBit(EXTI_Line1);
}
}
void EXTI2_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line2) != RESET)
{
key_num = 3;
EXTI_ClearITPendingBit(EXTI_Line2);
}
}
void EXTI3_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line3) != RESET)
{
key_num = 4;
EXTI_ClearITPendingBit(EXTI_Line3);
}
}
```
然后在主函数中实现跑马灯控制逻辑,根据按键编号控制对应的LED灯亮灭:
```c
int main(void)
{
GPIO_EXTI_Init();
// 初始化PB12~PB15为输出模式
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
while (1)
{
// 根据按键编号控制LED灯亮灭
switch (key_num)
{
case 1:
GPIO_SetBits(GPIOB, GPIO_Pin_12);
GPIO_ResetBits(GPIOB, GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
break;
case 2:
GPIO_SetBits(GPIOB, GPIO_Pin_13);
GPIO_ResetBits(GPIOB, GPIO_Pin_12 | GPIO_Pin_14 | GPIO_Pin_15);
break;
case 3:
GPIO_SetBits(GPIOB, GPIO_Pin_14);
GPIO_ResetBits(GPIOB, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_15);
break;
case 4:
GPIO_SetBits(GPIOB, GPIO_Pin_15);
GPIO_ResetBits(GPIOB, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14);
break;
default:
GPIO_ResetBits(GPIOB, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
break;
}
key_num = 0;
delay_ms(50);
}
}
```
其中,`delay_ms`函数是一个简单的延时函数,可以使用CubeMX自动生成的代码,也可以自己编写。
用外部中断四个按键分别控制四组跑马灯,基于STM32F103R6
本文将介绍如何使用STM32F103R6外部中断控制四组跑马灯。
步骤:
1. 硬件连接:
将四个按键分别连接到STM32F103R6的四个外部中断引脚,同时将四组跑马灯连接到GPIO口。
2. 代码实现:
首先需要初始化外部中断,设置中断优先级和触发方式,具体代码如下:
```c
void EXTI_Config(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable GPIOA clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
/* Configure PA0, PA1, PA2 and PA3 as input with pull-up */
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Connect EXTI0-3 Line to PA0-3 pin */
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource2);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource3);
/* Configure EXTI0-3 */
EXTI_InitStructure.EXTI_Line = EXTI_Line0 | EXTI_Line1 | EXTI_Line2 | EXTI_Line3;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/* Enable and set EXTI0-3 Interrupt to the highest priority */
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);
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x03;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
```
然后需要编写中断处理函数,在中断函数中实现跑马灯的控制,具体代码如下:
```c
void EXTI0_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line0) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line0);
// Control the first group of running lights
// ...
}
}
void EXTI1_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line1) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line1);
// Control the second group of running lights
// ...
}
}
void EXTI2_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line2) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line2);
// Control the third group of running lights
// ...
}
}
void EXTI3_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line3) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line3);
// Control the fourth group of running lights
// ...
}
}
```
以上代码中的四个中断处理函数分别对应四个外部中断,每个函数中都需要根据按键的触发来控制对应的跑马灯。
3. 跑马灯控制:
在中断处理函数中,需要编写代码来控制跑马灯的运行,以下是一个简单的跑马灯控制代码:
```c
void run_light(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
GPIO_SetBits(GPIOx, GPIO_Pin);
delay_ms(100);
GPIO_ResetBits(GPIOx, GPIO_Pin);
}
```
该函数可以让一个LED灯亮100ms后熄灭。
完整代码如下:
```c
#include "stm32f10x.h"
void EXTI_Config(void);
void run_light(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
int main(void)
{
EXTI_Config();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
while (1)
{
}
}
void EXTI_Config(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable GPIOA clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
/* Configure PA0, PA1, PA2 and PA3 as input with pull-up */
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Connect EXTI0-3 Line to PA0-3 pin */
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource2);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource3);
/* Configure EXTI0-3 */
EXTI_InitStructure.EXTI_Line = EXTI_Line0 | EXTI_Line1 | EXTI_Line2 | EXTI_Line3;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/* Enable and set EXTI0-3 Interrupt to the highest priority */
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);
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x03;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void run_light(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
GPIO_SetBits(GPIOx, GPIO_Pin);
delay_ms(100);
GPIO_ResetBits(GPIOx, GPIO_Pin);
}
void EXTI0_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line0) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line0);
// Control the first group of running lights
run_light(GPIOB, GPIO_Pin_0);
run_light(GPIOB, GPIO_Pin_1);
run_light(GPIOB, GPIO_Pin_2);
run_light(GPIOB, GPIO_Pin_3);
}
}
void EXTI1_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line1) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line1);
// Control the second group of running lights
run_light(GPIOB, GPIO_Pin_0);
run_light(GPIOB, GPIO_Pin_1);
}
}
void EXTI2_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line2) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line2);
// Control the third group of running lights
run_light(GPIOB, GPIO_Pin_2);
run_light(GPIOB, GPIO_Pin_3);
}
}
void EXTI3_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line3) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line3);
// Control the fourth group of running lights
run_light(GPIOB, GPIO_Pin_0);
run_light(GPIOB, GPIO_Pin_3);
}
}
```
阅读全文