[Diao Ye learns programming] Arduino hands-on (118) --- PS2 interface module

The reference to 37 sensors and actuators has been widely circulated on the Internet. In fact, there must be more than 37 sensor modules compatible with Arduino. In view of the fact that I have accumulated some sensor and actuator modules on hand, according to the concept of practice to get true knowledge (must be done), for the purpose of learning and communication, I am going to try a series of experiments one by one, regardless of success (the program goes through) or not, They will be recorded - small progress or unsolvable problems, hoping to inspire others.

[Arduino] 168 sensor module series experiments (data code + simulation programming + graphics programming)

Title experiment 118: PS2 mouse keyboard socket interface PS2 module

insert image description here
PS/2 interface
One of the ancient interfaces, PS/2 interface used to be the most common mouse interface, originally patented by IBM, commonly known as "Xiaokou". This is a special interface for mouse and keyboard, a 6-pin circular interface. But the mouse only uses 4 pins to transmit data and power supply, and the other 2 pins are empty. The transmission rate of the PS/2 interface is slightly faster than that of the COM interface, and it is the standard interface of the ATX motherboard. It was one of the most widely used mouse interfaces at that time, but it still could not make the high-end mouse fully play its performance, and it did not support hot plugging. In the BTX motherboard specification, this is also the interface that will be eliminated soon. It should be noted that when connecting the PS/2 interface mouse, the keyboard PS/2 interface cannot be inserted by mistake (of course, the PS/2 keyboard cannot be inserted into the mouse PS/2 interface). Under normal circumstances, the motherboard that complies with the PC99 specification has a green mouse interface and a purple keyboard interface. In addition, it can also be judged from the relative position of the PS/2 interface: the keyboard interface is close to the motherboard PCB, and the mouse is above it. interface.

insert image description here
insert image description here
PS/2 communication protocol

The PS/2 mouse interface adopts a two-way synchronous serial protocol. That is, every time a pulse is sent on the clock line, a bit of data is sent on the data line. In the mutual transmission, the host has the right to control the bus, that is, it can be in any The method is to keep the clock line low, and the mouse cannot generate clock signals and send data. In the two directions of transmission, the clock signals are generated by the mouse, that is, the host does not generate communication clock signals. If To send data, the host must control the mouse to generate a clock signal. The method is as follows: the host first pulls down the clock line for at least 100μs to inhibit communication, then pulls down the data line, and finally releases the clock line. Through this sequence, the mouse is controlled to generate a clock signal. When the mouse detects At this timing state, a clock signal will be generated within 10ms. As shown in the timing segment A in Figure 3. The timing of transmitting data frames between the host and the mouse is shown in Figure 2 and Figure 3. 2.2 Data packet structure In the host program, The clock pulse of each data bit is used to trigger the interrupt, and the judgment and reception of the data bit are realized in the interrupt routine. During the experiment, through appropriate programming, the mouse data can be correctly controlled and received. But this scheme has a little deficiency, because Every CLOCK needs to generate an interrupt, frequent interrupts require a lot of host resources.

insert image description here
Pin definition of PS/2 interface

The 1st pin transmits data, the 2nd pin is idle, the 3rd pin is grounded (negative), the 4th pin is connected to +5V power supply, the 5th pin is the clock, and the 6th pin is also idle. When you disassemble the keyboard with PS/2 interface, you will find that in addition to a "circuit diagram", there is also a PCB board not much bigger than tofu. There are four solder joints marked V, G, C, and D on this circuit board—V is the Vcc power supply, which is connected to the positive pole of the power supply, and most of them are connected to the red wire; G is the ground wire, which is the negative pole of the power supply, and most of them are connected to the black wire; C is the clock, some are connected to yellow wires, some are connected to green wires; D is data, most of them are green or blue wires, and there are also yellow wires. The color is unreliable. It is recommended that you connect the negative and positive electrodes first, and then connect the remaining C and D wires to try. If it doesn’t work, just swap them.

insert image description here
insert image description here
insert image description here
insert image description here
The difference between PS/2 interface and USB interface

Features of the PS2 interface:

1. Physical connection: Generally, a keyboard with a five-pin connector is called an AT keyboard, while a keyboard with a six-pin mini-DIN connector is called a PS/2 keyboard. In fact, only four pins of these two connectors make sense. They are Clock, Data, +5V and Ground respectively. In the physical connection between the PS/2 keyboard and the PC, it is enough to ensure that the four lines correspond one by one. The PS/2 keyboard is supplied with +5V power by the PS/2 port of the PC, and the other two pins Clock and Data are both open collectors, so a pull-up resistor with a large resistance must be connected. They usually maintain a high level, and are pulled to a low level when there is an output, and then automatically rise to a high level.

2. Electrical characteristics: PS/2 communication protocol is a two-way synchronous serial communication protocol.

The two ends of the communication are synchronized through Clock and exchange data through Data. If any party wants to suppress the communication of the other party, it only needs to pull the Clock to a low level. If it is the communication between PC and PS/2 keyboard, the PC must be the host, that is to say, the PC can inhibit the PS/2 keyboard from sending data, but the PS/2 keyboard will not inhibit the PC from sending data. Generally, the maximum clock frequency for data transmission between two devices is 33kHz, and most PS/2 devices work at 10-20kHz. The recommended value is around 15kHz, that is to say, the duration of Clock high and low levels is 40μs.

Advantages of the USB interface:

1. Can be hot swapped. That is, when users use external devices, they do not need to shut down and restart, but directly plug in the USB to use when the computer is working.

2. Easy to carry. Most USB devices are known for being "small, light, and thin". For users, it is very convenient to carry a large amount of data with them. Of course, the USB hard drive is the first choice.

3. Unified standards. The common ones are hard disk with IDE interface, mouse and keyboard with serial port, printer scanner with parallel port, but with USB, all these application peripherals can be connected to personal computer with the same standard. Mouse, USB printer, etc.

4. Multiple devices can be connected. USB often has multiple interfaces on personal computers, and several devices can be connected at the same time. If a USB HUB with four ports is connected, it can be connected again; four USB devices, and so on, can be connected as much as possible. , connect all your home devices to one PC at the same time without any problems.

USB supports hot plugging, but PS2 interface does not, so in terms of convenience, USB is better than PS2. Secondly, the transfer rate of mouse USB interface is higher than that of PS2, which is mainly reflected in the mouse movement in games, but generally speaking, it does not feel good. Come out, there is no difference as to whether the keyboard interface is USB or PS2, but the most important thing is that USB supports hot plugging, which is very important, and the USB interface is also a mainstream standard, and PS2 has completely lost its meaning. Including the price, USB is not much more expensive than PS2, so the USB interface wins.

But in terms of key combinations, PS/2 can achieve no conflicts, and only a maximum of 6 keys can be pressed at the same time under USB. A data packet of a USB keyboard is only 8 bytes (why there are only 8 bytes, this is because the agreement stipulates, why the agreement is so stipulated, because the speed of USB was slow in the early days, and the maximum number of packets transmitted by using interrupts is 8 bytes), Among them, 2 bytes mark the status, and the remaining 6 bytes are used to record the scan code of the currently pressed or popped-up button. Therefore, the USB keyboard can describe the status of 6 buttons at most at the same time.

In terms of stability, PS/2 is still better than USB. Of course, although USB is not as stable as PS/2, it can have good expansion, such as adding a USB HUB to a USB keyboard.

insert image description here
About the conversion between USB interface and PS/2 interface There are
four USB interfaces in the computer, the two on both sides are power lines, and the two in the middle are data lines. The 4 wires of the usb interface are generally distributed as follows. It should be noted that the positive and negative poles must not be reversed, otherwise the south bridge chip of the usb device or computer will be burned: black wire: gnd power supply negative red wire
:
vcc Positive pole of power supply
Green wire: data+ signal positive pole
White wire: data- signal negative pole

insert image description here
This module is converted from PS2 interface to common 2.54MM interface, which is convenient for connecting computer keyboard or mouse with arduino.

insert image description here
insert image description here
PS2 Interface Module Parameters

Working voltage: 5V (DC)

Interface: 4PIN interface 5V, GND, DAT, CLK

Output signal: digital signal

Size: 21 17 17mm

Weight: 5.5g

insert image description here
insert image description here
insert image description here
Connect the PS/2 interface with the Arduino according to the pins in the figure, and the corresponding relationship is shown in the table below.
5V : - Arduino 5V out
Ground : - Arduino GND
Clock : - Arduino Pin 3
Data : - Arduino Pin 8

insert image description here
Arduino experiment open source code

/*

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

实验一百一十八:PS2鼠标 键盘插座接口 PS2模块

1、安装< PS2Keyboard.h>库

(https://www.pjrc.com/teensy/arduino_libraries/PS2Keyboard.zip)

2、项目:在 Arduino 上使用 PS/2 键盘进行输入测试

3、PS/2 接口与 Arduino 接脚

5V :    - Arduino 5V out

Ground : - Arduino GND

Clock :  - Arduino Pin 3

Data :    - Arduino Pin 8

*/

  

#include <PS2Keyboard.h>

const int DataPin = 8;

const int IRQpin =  3;

PS2Keyboard keyboard;



void setup() {
    
    

  //keyboard.begin(DataPin, IRQpin, PS2Keymap_US);

  keyboard.begin(DataPin, IRQpin, PS2Keymap_German);

  //keyboard.begin(DataPin, IRQpin, PS2Keymap_French);

  Serial.begin(9600);

  Serial.println("International Keyboard Test:");

}



void loop() {
    
    

  if (keyboard.available()) {
    
    

  char c = keyboard.read();

  Serial.println(c);

  }

}

Experimental serial port return

insert image description here
Arduino experiment open source code 2

/*

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

实验一百一十八:PS2鼠标 键盘插座接口 PS2模块

1、安装< PS2Keyboard.h>库

(https://www.pjrc.com/teensy/arduino_libraries/PS2Keyboard.zip)

2、项目:在 Arduino 上使用 PS/2 键盘进行输入测试(识别一些特殊按键)

3、PS/2 接口与 Arduino 接脚

5V :    - Arduino 5V out

Ground : - Arduino GND

Clock :  - Arduino Pin 3

Data :    - Arduino Pin 8

*/

  

#include <PS2Keyboard.h>

const int DataPin = 8;

const int IRQpin =  3;

PS2Keyboard keyboard;



void setup() {
    
    

  delay(1000);

  keyboard.begin(DataPin, IRQpin);

  Serial.begin(9600);

  Serial.println("Keyboard Test:");

}



void loop() {
    
    

  if (keyboard.available()) {
    
    

  // read the next key

  char c = keyboard.read();

  // check for some of the special keys

  if (c == PS2_ENTER) {
    
    

    Serial.println();

  } else if (c == PS2_TAB) {
    
    

    Serial.print("[Tab]");

  } else if (c == PS2_ESC) {
    
    

    Serial.print("[ESC]");

  } else if (c == PS2_PAGEDOWN) {
    
    

    Serial.print("[PgDn]");

  } else if (c == PS2_PAGEUP) {
    
    

    Serial.print("[PgUp]");

  } else if (c == PS2_LEFTARROW) {
    
    

    Serial.print("[Left]");

  } else if (c == PS2_RIGHTARROW) {
    
    

    Serial.print("[Right]");

  } else if (c == PS2_UPARROW) {
    
    

    Serial.print("[Up]");

  } else if (c == PS2_DOWNARROW) {
    
    

    Serial.print("[Down]");

  } else if (c == PS2_DELETE) {
    
    

    Serial.print("[Del]");

  } else {
    
     

    // otherwise, just print all normal characters

    Serial.print(c);

  }

  }

}

Experimental serial port return

insert image description here
Arduino experiment scene diagram

insert image description here

Guess you like

Origin blog.csdn.net/weixin_41659040/article/details/131342224
Recommended