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

幻生幻灭 于 2012-2-1 08:09 编辑



程序链接


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


mouse.jpg





如图,PS2鼠标连接单片机照理说应该接上拉电阻的,但为啥米没有呢?

抱着好奇的态度我打开了库文件arduino-0022\libraries\ps2\ps2.cpp

发现以下代码,很奇怪为什么Input也可以Write?/*
* according to some code I saw, these functions will
* correctly set the clock and data pins for
* various conditions.It's done this way so you don't need
* pullup resistors.
*/
void
PS2::gohi(int pin)
{
pinMode(pin, INPUT);
digitalWrite(pin, HIGH);
}

void
PS2::golo(int pin)
{
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
}复制代码在群里询问后,得到了以下解释,感谢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上拉电阻可以通过以上代码用软件形式激活!
via - 极客工坊

标签: Arduino教程