PS/2 Keyboard Issue at Startup...

I am using the PS2Keyboard class and find that I get junk from the keyboard ALWAYS after power-up, but if I reset the Arduino it reads the keyboard correctly until a power cycle. Does anyone know why this happens?

What does reset do to the data pins? Is it just clearing the global buffer used by the PS2Keyboard class?

Thanks.

When the keyboard gets power, it starts its POST (Power On Self Test) routine. This test is what causes the NumLock, CapsLock and ScrollLock leds to flash, by the way. When the keyboard finishes the test, it sends 0xAA to the host (your Arduino in this case) to indicate a successful power-on test, or an error code to indicate a problem. This POST takes a short time to run, but it does take time, and it does result in a transmission from the keyboard to the host.

When you reset the Arduino, the keyboard does not get power applied as it does when you power cycle.

Try to put a delay for one or two seconds in setup() to see if that helps.

If it doesn't, can you tell us if the "junk" is always the same, or does it vary? Use Serial.print(..., HEX) to see what hex code the keyboard is sending.

Lots of people use that library successfully, we can surely find out what's going wrong for you.

The value I see is 0x8a. I haven't looked to see what the scan code is before it is converted to 8a.

I have added delays and they don't help. I also have a fairly slow 16x2 LCD (connected via a 74HC164 shift register) that initializes and has several delays as it initializes.

I print out the values both to Serial and to the LCD. I have code for handling the PS/2 BAT (0xAA), and if PS2Keyboard.begin() is called as the first thing in Setup I usually see the LCD print PS/2 BAT, and everything works fine, but there is still a race condition. For whatever reason, sometimes I don't see the BAT, and I suspect its because the ISR has already missed a few bits of it. From there on out everything is messed up.

I added a method in the PS2Keyboard class called reset(), which does the following:

void PS2Keyboard::reset()
{
ps2Keyboard_CurrentBuffer = 0;
ps2Keyboard_CharBuffer = 0;
ps2Keyboard_BufferPos = 0;
ps2Keyboard_BreakActive = false;
}

When I get a bad character, if I call reset, I seem to lose the current input, but then everything else that is read is fine.

I am thinking that I may need to connect the PS/2 VCC to something (transistor maybe?) that holds it down until I send a signal from a digital pin to turn it on. This way the ISR can be ready before the device can send anything. (Before you call pinMode and digitalWrite on a pin, what value does it hold?)

I am also thinking about modifying the library to check the parity bit and the start and stop bits, and reject input until they are correct. Has anyone tried either of these?

Thanks.