极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 28067|回复: 3

关于PS2鼠标连接Arduino不需要上拉电阻的理解

[复制链接]
发表于 2012-1-31 10:35:34 | 显示全部楼层 |阅读模式
本帖最后由 幻生幻灭 于 2012-2-1 08:09 编辑

{:soso__16672557930365918974_4:} [程序链接]

在PS/2连接器上有四个管脚:电源地、+5V、数据和时钟。主机提供+5V,并且键盘/鼠标的地线连接到主机的电源地上。数据和时钟都是集电极开路(OC)的,因此任何你连接到PS/2鼠标、键盘或主机的设备在时钟和数据线上都要有一个大的上拉电阻(一般取10KΩ)。置“0”就把线拉低,置“1”就让线上浮成高电平。参考右图中数据和时钟线的一般接口。(注意:如果你打算使用象51这样的微控制器,由于它们的I/O管脚是双向的,你可以跳过晶体管和缓冲门,并且通用同一个管脚进行输入和输出。)



如图,PS2鼠标连接单片机照理说应该接上拉电阻的,但为啥米没有呢?{:soso__7191104941957964981_1:}
抱着好奇的态度我打开了库文件arduino-0022\libraries\ps2\ps2.cpp
发现以下代码,很奇怪为什么Input也可以Write?
  1. /*
  2. * according to some code I saw, these functions will
  3. * correctly set the clock and data pins for
  4. * various conditions.  It's done this way so you don't need
  5. * pullup resistors.
  6. */
  7. void
  8. PS2::gohi(int pin)
  9. {
  10.         pinMode(pin, INPUT);
  11.         digitalWrite(pin, HIGH);
  12. }

  13. void
  14. PS2::golo(int pin)
  15. {
  16.         pinMode(pin, OUTPUT);
  17.         digitalWrite(pin, LOW);
  18. }
复制代码
在群里询问后,得到了以下解释,感谢ArdyPro
If the pin is configured as an INPUT, writing a HIGH value with digitalWrite() will enable an internal 20K pullup resistor (see the tutorial on digital pins).
[原文连接]

Pullup Resistors
Often it is useful to steer an input pin to a known state if no input is present. This can be done by adding a pullup resistor (to +5V), or a pulldown resistor (resistor to ground) on the input, with 10K being a common value.

There are also convenient 20K pullup resistors built into the Atmega chip that can be accessed from software. These built-in pullup resistors are accessed in the following manner.

pinMode(pin, INPUT);           // set pin to input
digitalWrite(pin, HIGH);       // turn on pullup resistors

结论:当Pin用于输入端时,Atmega chip 内置的20K上拉电阻可以通过以上代码用软件形式激活!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2013-1-11 13:17:17 | 显示全部楼层
pinMode(pin, INPUT_PULLUP) 等效
回复 支持 反对

使用道具 举报

发表于 2013-1-11 13:41:49 | 显示全部楼层
其实就是启动了内置上拉了
回复 支持 反对

使用道具 举报

发表于 2014-1-2 18:21:01 | 显示全部楼层
不加上了拉电阻?? 哥哥啊,电阻下面是个三极管啊,如果D脚输出的是高电平,三极管一导通,就相当于5V直接短路了, 还搞毛啊
回复 支持 反对

使用道具 举报

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

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

Archiver|联系我们|极客工坊

GMT+8, 2024-4-27 08:41 , Processed in 0.042133 second(s), 22 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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