Anybody can find the problem in this chunk of code ? When I hit 'A' I get 1C as code but when I release I get a 70 instead of an F0. If you can of course. It seams that the first bit is never read. Thank you.
unsigned char getKey() { unsigned char n,dato; n = 0; // Inizializza il contatore dato = 0; // Inizializza il dato while(CLOCK); // Se il clock rimane a 1 aspetta while(!CLOCK); // La prossima discesa e' quella con il primo bit for (n = 0; n < 7; n++) { while(CLOCK); // Aspetta il bit n-esimo if (DATA) dato = dato | 0x80; // Bitwise OR dato = dato >> 1; // Shift a destra while(!CLOCK); // Aspetta la risalita del clock } while(CLOCK); // Aspetta il parity bit parity = DATA; while(!CLOCK); while(CLOCK); // Aspetta lo stop bit while(!CLOCK);
return dato; }
Is your clock phase correct? I don't know the outside of your hardware but doesn't the keyboard read the data on rising edge of the clock signal?
You seem to set the data when the clock goes high, unless you are inverting the signal on the outside.
Here is a link about the protocol: www.computer-engineering.org/.../