This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

PS2 keyboard reader

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; }

Parents
  • I know it could be too much but I would like an help for the second part.
    This code sending the the commands 0xED and 0x07 to the keyboard to switch on the LEDs sometimes switches on only two LEDs but I expected to switch on the all three LEDs. Could you please tell me where could be the problem ?

    void delay()
            {
             unsigned char a;
             for (a = 0; a < 200; a++);
            }
    // Send a command to a PS2 keyboard
    void sendCmd(unsigned char com)
                            {
                            bit parity = 1;
                            unsigned int n;
                            DATA = 1;
                            CLOCK = 1;
                            delay();
                            CLOCK = 0;
                            delay();
                            DATA = 0;
    
                            CLOCK = 1;       // Partenza dati
    
                            while(CLOCK);    // Aspetto la prima discesa
                            while(!CLOCK);   // Alla prossima discesa la tastiera si aspetta D0
    
                            for (n = 0; n < 8; n++)
                                    {
                                    DATA = com & 0x01;
                                    parity = parity ^ (com & 0x01);
                                    com = com >> 1;
                                    while(CLOCK);
                                    while(!CLOCK);
                                    }
                            DATA = parity;
                            while(CLOCK);                   // Aspetto la prima discesa
                            while(!CLOCK)
                            DATA = 0;                       // Stop bit
                            while(CLOCK);                   // Aspetto la prima discesa
                            while(!CLOCK);
                            DATA = 1;
                            CLOCK = 1;
                            }
    

Reply
  • I know it could be too much but I would like an help for the second part.
    This code sending the the commands 0xED and 0x07 to the keyboard to switch on the LEDs sometimes switches on only two LEDs but I expected to switch on the all three LEDs. Could you please tell me where could be the problem ?

    void delay()
            {
             unsigned char a;
             for (a = 0; a < 200; a++);
            }
    // Send a command to a PS2 keyboard
    void sendCmd(unsigned char com)
                            {
                            bit parity = 1;
                            unsigned int n;
                            DATA = 1;
                            CLOCK = 1;
                            delay();
                            CLOCK = 0;
                            delay();
                            DATA = 0;
    
                            CLOCK = 1;       // Partenza dati
    
                            while(CLOCK);    // Aspetto la prima discesa
                            while(!CLOCK);   // Alla prossima discesa la tastiera si aspetta D0
    
                            for (n = 0; n < 8; n++)
                                    {
                                    DATA = com & 0x01;
                                    parity = parity ^ (com & 0x01);
                                    com = com >> 1;
                                    while(CLOCK);
                                    while(!CLOCK);
                                    }
                            DATA = parity;
                            while(CLOCK);                   // Aspetto la prima discesa
                            while(!CLOCK)
                            DATA = 0;                       // Stop bit
                            while(CLOCK);                   // Aspetto la prima discesa
                            while(!CLOCK);
                            DATA = 1;
                            CLOCK = 1;
                            }
    

Children