极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 13315|回复: 1

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

[复制链接]
发表于 2018-6-2 22:42:47 | 显示全部楼层 |阅读模式
本帖最后由 Stormer 于 2018-6-3 01:05 编辑



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

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


回复

使用道具 举报

发表于 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 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-19 19:38 , Processed in 0.043062 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表