Stormer 发表于 2018-6-2 22:42:47

为何引脚不设置pinMode,一样可以输出?

本帖最后由 Stormer 于 2018-6-3 01:05 编辑



比如说,在setup()里仅仅写一句 digitalWrite(2, HIGH);2号引脚依然可以输出2.8V的电压。

是不是说,如果不写 pinMode(2, OUTPUT);就没有给2号引脚加上拉电阻?


shouzama 发表于 2018-6-3 01:51:37

我記得 ATMEGA328P 的 I/O 在未指定 PINMODE 時,
內定是 INPUT 作用,要用來 OUTPUT 就必須以 PINMODE
指定為 OUTPUT

這裏有一段說明(截自 ATMEGA328P DATASHEET)

If PORTxn is written to '1' when the pin is configured as an input pin, the pull-up resistor is activated. To
switch the pull-up resistor off, PORTxn has to be written to '0' or the pin has to be configured as an output
pin. The port pins are tri-stated when reset condition becomes active, even if no clocks are running.

If PORTxn is written to '1' when the pin is configured as an output pin, the port pin is driven high. If
PORTxn is written logic zero when the pin is configured as an output pin, the port pin is driven low.

大致上也說明了你遇到的狀況,當未指定 PINMODE (即內定
為 INPUT)時,若對這個 PORT 進行設定為 1 的動作時,
PULL HIGH 機制會作動,也因此你可以量到 PORT 有高電位,
但那並不是用來驅動外部的 DRIVE HIGH 準位,用途差很多

ARDUINO 有專用指令 pinMode( 2, INPUT_PULLUP );
來達到 INPUT PULL HIGH 的作用,並且不會造成 I/O 的混淆,
我個人習慣用這種方式來指定輸入埠 PULL HIGH
页: [1]
查看完整版本: 为何引脚不设置pinMode,一样可以输出?