doc/docs/arduino内置函数.md
2025-05-27 20:16:07 +08:00

70 lines
1.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## 引脚操作
* **pinMode(pin, mode)**
设置引脚的工作模式。
* `pin`:引脚编号
* `mode`:可以是
* `INPUT`:输入模式
* `OUTPUT`:输出模式
* `INPUT_PULLUP`:带内部上拉电阻的输入模式
示例:
```cpp
pinMode(13, OUTPUT);
pinMode(2, INPUT);
pinMode(3, INPUT_PULLUP);
```
* **digitalWrite(pin, value)**
控制数字引脚输出高电平或低电平。
* `value`:可以是 `HIGH` 或 `LOW`
示例:
```cpp
digitalWrite(13, HIGH); // 输出高电平
digitalWrite(13, LOW); // 输出低电平
```
* `value`:可以是 `1` 或 `0`
示例:
```cpp
digitalWrite(13, 1); // 输出高电平
digitalWrite(13, 0); // 输出低电平
```
* **digitalRead(pin)**
读取数字引脚的电平状态。返回值是 `HIGH` 或 `LOW`。
示例:
```cpp
int state = digitalRead(2);
if (state == HIGH) {
// 引脚为高电平
}
```
* **analogRead(pin)**
读取模拟引脚的电压值,返回值范围为 `0 ~ 1023`。
* 通常用于 A0\~A5
示例:
```cpp
int value = analogRead(A0);
```
* **analogWrite(pin, value)**
使用 PWM 输出模拟信号。
* `value` 范围为 `0 ~ 255`,对应输出占空比
* 仅适用于支持 PWM 的引脚(如 3、5、6、9、10、11
示例:
```cpp
analogWrite(9, 128); // 50% 占空比
```