STM32-13-中断01和9-5点亮4个灯

lemon Lv5

1.代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "stm32f10x.h"
#include "stm32f10x_it.h"
#include "sys.h"
#include "Delay.h"
#include "led.h"
#include "key.h"
#include "exit.h"

#define LED1 PBout(0)
#define LED2 PBout(1)
#define LED3 PBout(9)
#define LED4 PBout(14)

#define KEY1 PCin(0)
#define KEY2 PCin(1)
#define KEY3 PCin(10)
#define KEY4 PCin(11)

void KEY_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}

void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_9 | GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_9 | GPIO_Pin_14);
}

void EXTI0_IRQHandler(void)
{
static u8 k1 = 0;
if(EXTI_GetITStatus(EXTI_Line0) != RESET)
{
Delay(20);
if(KEY1 == 0)
{
if(k1 == 0)
LED1 = 1;
else
LED1 = 0;
k1 = !k1;
}
EXTI_ClearITPendingBit(EXTI_Line0);
}
}

void EXTI1_IRQHandler(void)
{
static u8 k2 = 0;
if(EXTI_GetITStatus(EXTI_Line1) != RESET)
{
Delay(20);
if(KEY2 == 0)
{
if(k2 == 0)
LED2 = 1;
else
LED2 = 0;
k2 = !k2;
}
EXTI_ClearITPendingBit(EXTI_Line1);
}
}

void EXTI15_10_IRQHandler(void)
{
static u8 k3 = 0;
static u8 k4 = 0;

if(EXTI_GetITStatus(EXTI_Line10) != RESET)
{
Delay(20);
if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_10) == 0)
{
if(k3 == 0)
LED3 = 1;
else
LED3 = 0;
k3 = !k3;
}
EXTI_ClearITPendingBit(EXTI_Line10);
}

if(EXTI_GetITStatus(EXTI_Line11) != RESET)
{
Delay(20);
if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_11) == 0)
{
if(k4 == 0)
LED4 = 1;
else
LED4 = 0;
k4 = !k4;
}
EXTI_ClearITPendingBit(EXTI_Line11);
}
}

void EXTI_Config(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);

GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource0);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource1);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource10);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource11);

EXTI_InitStructure.EXTI_Line = EXTI_Line0 | EXTI_Line1 | EXTI_Line10 | EXTI_Line11;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
NVIC_Init(&NVIC_InitStructure);

NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
NVIC_Init(&NVIC_InitStructure);
}

int main(void)
{
LED_Init();
KEY_Init();
EXTI_Config();
while(1);
}

2.模拟图

a


代码下载链接(123云盘):https://www.123684.com/s/wFuijv-i8Zgh

  • 标题: STM32-13-中断01和9-5点亮4个灯
  • 作者: lemon
  • 创建于 : 2025-07-22 23:10:25
  • 更新于 : 2025-07-23 00:14:00
  • 链接: https://lemon2003.github.io/post/20250722231025.html
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论
目录
STM32-13-中断01和9-5点亮4个灯