Skip to content

Commit 2cd1987

Browse files
committed
add: 添加GPIO的使用示例
1 parent 716d4a4 commit 2cd1987

1 file changed

Lines changed: 69 additions & 0 deletions

File tree

docs/library/gpio.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,72 @@ AirMCU 上的 GPIO 外设支持中断。
8989
```cpp
9090
void attachInterrupt(uint32_t pin, callback_function_t callback, uint32_t mode)
9191
```
92+
93+
- `pin`:要配置的引脚号。
94+
- `callback`:中断回调函数。
95+
- `mode`:中断触发模式。可以是以下值之一:
96+
97+
- `CHANGE`:引脚状态发生变化时触发中断。
98+
- `RISING`:引脚状态从低电平变为高电平时触发中断。
99+
- `FALLING`:引脚状态从高电平变为低电平时触发中断。
100+
- `LOW`:引脚状态为低电平时触发中断。
101+
- `HIGH`:引脚状态为高电平时触发中断。
102+
103+
### detachInterrupt
104+
105+
要从特定引脚分离中断,请使用 `detachInterrupt` 函数来分离 GPIO。
106+
107+
```cpp
108+
void detachInterrupt(uint32_t channel)
109+
```
110+
111+
- `channel`:要分离的引脚号。
112+
113+
## 示例代码
114+
115+
### GPIO 输入和输出模式
116+
117+
```cpp
118+
const auto LED = PB0;
119+
const auto BUTTON = PF4;
120+
121+
uint8_t stateLED = 0;
122+
123+
void setup() {
124+
pinMode(LED, OUTPUT);
125+
pinMode(BUTTON,INPUT_PULLUP);
126+
}
127+
128+
void loop() {
129+
if(!digitalRead(BUTTON)){
130+
stateLED = stateLED^1;
131+
digitalWrite(LED,stateLED);
132+
}
133+
}
134+
```
135+
136+
### GPIO 中断
137+
138+
```cpp
139+
const auto ledPin = PB0;
140+
141+
const auto interruptPin = PF4;
142+
143+
volatile byte state = LOW;
144+
145+
void setup() {
146+
pinMode(ledPin, OUTPUT);
147+
148+
pinMode(interruptPin, INPUT_PULLDOWN);
149+
150+
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
151+
}
152+
153+
void loop() {
154+
digitalWrite(ledPin, state);
155+
}
156+
157+
void blink() {
158+
state = !state;
159+
}
160+
```

0 commit comments

Comments
 (0)