PS/2 Keyboard to LCD

General discussion on mikroC PRO for PIC32.
Author
Message
chris11jed
Posts: 156
Joined: 15 Jun 2011 06:37

PS/2 Keyboard to LCD

#1 Post by chris11jed » 11 Nov 2022 23:22

Hi all,

I can't get anything to display on a 2x16 LCD when pressing keys on a PS/2 keyboard. And would like more experienced eyes to take a look over my code, to see if there's something I am missing please?

MCU:
  • PIC32MX695F512L
Timing:
  • (Primary) 25MHz resonator
  • (Secondary) 32.768kHz resonator (not used in this code)
  • (Sys-Clk Speed) 100MHz
Software:
  • mikroC PRO for PIC32 v.4.0.0
I have verified my keyboard and circuitry works on another PCB, with another MCU.
The MCU is a PIC18F45K22, running at 16MHz
The key presses are displayed upon a GLCD.

Each code for each MCU programs fine.

The code for the PCB with a PIC18F45K22 and GLCD, which works fine, is as follows:

Code: Select all

'=============================================================================='
program PIC18F45K22_POTs_and_Keyboard_V03
'==============================================================================' Include Section
'==============================================================================' Symbol Section
'==============================================================================' Constant Section
'==============================================================================' Structure Section
'==============================================================================' Variables Section
'------------------------------------------------------------------------------' Port Pins
'--------------------------------------------' Port A
'
'--------------------------------------------' Port B
Dim GLCD_DataPort as byte at PortB
'
'--------------------------------------------' Port C
Dim PS2_Data as sbit at RC0_bit
Dim PS2_Data_Direction as sbit at TRISC0_bit
'
Dim PS2_Clock as sbit at RC1_bit
Dim PS2_Clock_Direction as sbit at TRISC1_bit
'
Dim GLCD_BKLT as sbit at LATC2_bit
Dim GLCD_BKLT_Dir as sbit at TRISC2_bit
'
'--------------------------------------------' Port D
Dim GLCD_CS1 as sbit at RD2_bit
Dim GLCD_CS1_Direction as sbit at TRISD2_bit
'
Dim GLCD_CS2 as sbit at RD3_bit
Dim GLCD_CS2_Direction as sbit at TRISD3_bit
'
Dim GLCD_RS as sbit at RD4_bit
Dim GLCD_RS_Direction as sbit at TRISD4_bit
'
Dim GLCD_RW as sbit at RD5_bit
Dim GLCD_RW_Direction as sbit at TRISD5_bit
'
Dim GLCD_EN as sbit at RD6_bit
Dim GLCD_EN_Direction as sbit at TRISD6_bit
'
Dim GLCD_RST as sbit at RD7_bit
Dim GLCD_RST_Direction as sbit at TRISD7_bit
'
'--------------------------------------------' Port E
'
'------------------------------------------------------------------------------' General variables
Dim keydata,
    special,
    pressed as byte                                                            ' Keyboard library
'
Dim Presses_Hold as byte                                                       ' Keyboard results holding file
'
Dim Keyboard_Press_Char as byte                                                ' Keyboard final data holder, I2C data organisation
'
Dim GLCD_BKLT_PWM,
    OS_GLCD_BKLT_PWM as byte                                                   ' GLCD Backlight pwm
'
Dim Keyboard_Character as char[2]                                              ' GLCD display data
'
Dim SomeText as char[32]                                                       ' GLCD display data
'
Dim Txt as string[4]                                                           ' GLCD display data
'
Dim Address_Str as string[2]                                                   ' GLCD display data
'
Dim GLCD_Page as byte                                                          ' GLCD organisation file
'
Dim Keyboard_FLG as byte                                                       ' Keyboard Flag
'
'==============================================================================' Functions Section
'==============================================================================' Procedure Section
'==============================================================================' Interrupt Section
Sub procedure Interrupt()                                                      ' ISR
'------------------------------------------------------------------------------'
End sub
'==============================================================================' PIC Set-Up Section
Sub procedure Init_PIC()                                                       ' Configure Ports and pins, and PIC registers
'------------------------------------------------------------------------------'
    ANSELA = 0                                                                 ' Make all digital for now
    ANSELB = 0
    ANSELC = 0
    ANSELD = 0
    ANSELE = 0

    ADCON0 = 0x00                                                              ' Clear A/D Control register for now
    ADCON1 = 0x00
    ADCON2 = 0x00

    CM1CON0 = 0x00                                                             ' Clear Comparitors
    CM2CON0 = 0x00
    CM2CON1 = 0x00
'------------------------------------------------------------------------------'
    TRISA = 0
    LATA = 0
    
    TRISB = 0
    LATB = 0
    
    TRISC = 0
    LATC = 0
    
    TRISD = 0
    LATD = 0
    
    TRISE = 0
    LATE = 0
'------------------------------------------------------------------------------'
    GLCD_BKLT_Dir = 0                                                          ' CCP1
    GLCD_BKLT = 0
'------------------------------------------------------------------------------'

'------------------------------------------------------------------------------'
End sub
'==============================================================================' Variables Set-Up Section
Sub procedure Init_Vari()                                                      ' Clear or set variables
'------------------------------------------------------------------------------'
    '--------------------------------------------------------------------------' Keyboard
    keydata = 0
    special = 0
    pressed = 0
    Presses_Hold = 0
    Keyboard_Press_Char = 0
    Keyboard_FLG = 0
    
    '--------------------------------------------------------------------------' GLCD
    GLCD_BKLT_PWM = 128
    OS_GLCD_BKLT_PWM = 128
                         '12
    Keyboard_Character[0] = " "
    Keyboard_Character[1] = 0

               '12345678901234567890123456789012
    SomeText = "12345678901234567890123456789012"
          '1234
    Txt = "1234"
                  '12
    Address_Str = "12"
    GLCD_Page = 0
'------------------------------------------------------------------------------'
End sub
'==============================================================================' Main Program Section
main:
 '-----------------------------------------------------------------------------'
 Init_PIC()                                                                    ' Configure PIC
 Init_Vari()                                                                   ' Ready variables
 '-----------------------------------------------------------------------------'
 Ps2_Config()                                                                  ' Initiate PS/2 Keyboard
 Delay_ms(100)                                                                 ' Wait
 '-----------------------------------------------------------------------------'
 PWM1_Init(5000)                                                               ' Initiate the PWM
 PWM1_Start()                                                                  ' GLCD Backlight PWM
 PWM1_Set_Duty(OS_GLCD_BKLT_PWM)                                               ' Initiate backlight
 '-----------------------------------------------------------------------------'
 GLCD_Init()                                                                   ' Initiate GLCD
 GLCD_Fill(0x00)                                                               ' Clear GLCD
 GLCD_Set_Font(@Font_Glcd_5x7, 5, 7, 32)                                       ' Load font **** 21.5 characters per page ***
 '-----------------------------------------------------------------------------'
'==============================================================================' Main Program Loop Section
 While (TRUE)
  '----------------------------------------------------------------------------'
  If Ps2_Key_Read(keydata, special, pressed) then                              ' Looks for Keyboard key press
     '-------------------------------------------------------------------------'
     If (pressed <> 0) and (special = 0) and (keydata <> 0) then               ' Common key
        ' Common key pressed
        '----------------------------------------------------------------------'
        Keyboard_Character[0] = chr(keydata)
        Keyboard_FLG = 0xF0
        '----------------------------------------------------------------------'
       Else
        If (pressed <> 0) and (special = 1) and (keydata <> 0) then            ' Special Key
           ' Special Key pressed
           '-------------------------------------------------------------------'
           Select Case Keydata                                                 ' Printable words for special key press
             '-----------------------------------------------------------------'
             Case 1
                              '12345678901234567890123456789012
                   Sometext = "F1"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 2
                              '12345678901234567890123456789012
                   Sometext = "F2"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 3
                              '12345678901234567890123456789012
                   Sometext = "F3"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 4
                              '12345678901234567890123456789012
                   Sometext = "F4"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 5
                              '12345678901234567890123456789012
                   Sometext = "F5"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 6
                              '12345678901234567890123456789012
                   Sometext = "F6"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 7
                              '12345678901234567890123456789012
                   Sometext = "F7"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 8
                              '12345678901234567890123456789012
                   Sometext = "F8"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 9
                              '12345678901234567890123456789012
                   Sometext = "F9"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 10
                              '12345678901234567890123456789012
                   Sometext = "F10"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 11
                              '12345678901234567890123456789012
                   Sometext = "F11"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 12
                              '12345678901234567890123456789012
                   Sometext = "F12"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 13
                              '12345678901234567890123456789012
                   Sometext = "Enter"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 14
                              '12345678901234567890123456789012
                   Sometext = "Page Up"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 15
                              '12345678901234567890123456789012
                   Sometext = "Page Down"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 16
                              '12345678901234567890123456789012
                   Sometext = "Backspace"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 17
                              '12345678901234567890123456789012
                   Sometext = "Insert"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 18
                              '12345678901234567890123456789012
                   Sometext = "Delete"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 19
                              '12345678901234567890123456789012
                   Sometext = "Windows"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 20
                              '12345678901234567890123456789012
                   Sometext = "Ctrl"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 21
                              '12345678901234567890123456789012
                   Sometext = "Shift"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 22
                              '12345678901234567890123456789012
                   Sometext = "Alt"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 23
                              '12345678901234567890123456789012
                   Sometext = "Print Screen"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 24
                              '12345678901234567890123456789012
                   Sometext = "Pause"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 25
                              '12345678901234567890123456789012
                   Sometext = "Caps Lock"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 26
                              '12345678901234567890123456789012
                   Sometext = "End"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 27
                              '12345678901234567890123456789012
                   Sometext = "Home"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 28
                              '12345678901234567890123456789012
                   Sometext = "Scroll Lock"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 29
                              '12345678901234567890123456789012
                   Sometext = "Num Lock"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 30
                              '12345678901234567890123456789012
                   Sometext = "Left Arrow"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 31
                              '12345678901234567890123456789012
                   Sometext = "Right Arrow"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 32
                              '12345678901234567890123456789012
                   Sometext = "Up Arrow"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 33
                              '12345678901234567890123456789012
                   Sometext = "Down Arrow"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 34
                              '12345678901234567890123456789012
                   Sometext = "Escape"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
             Case 35
                              '12345678901234567890123456789012
                   Sometext = "Tab"
                   goto Leave_Keydata_Select
             '-----------------------------------------------------------------'
           End select
           Leave_Keydata_Select:
           Keyboard_FLG = 0x0F
           '-------------------------------------------------------------------'
        End if
     End if
     '-------------------------------------------------------------------------'
  End if
  '----------------------------------------------------------------------------'
  If (Keyboard_FLG <> 0) then
     GLCD_Fill(0x00)
     If (Keyboard_FLG = 0xF0) then                                             ' Common Key Press
        Keyboard_FLG = 0
        GLCD_Write_Char(Keyboard_Character[0], 10, 3, 2)
     End if
     If (Keyboard_FLG = 0x0F) then                                             ' Special Key Press
        Keyboard_FLG = 0
        GLCD_Write_Text(sometext, 5, 3, 2)
     End if
     Leave_Keybaord_FLG:
  End if
  '----------------------------------------------------------------------------'
 Wend
end.
The code for the PCB with a PIC32MX695F512L and LCD, which isn't working right, is as follows:

Code: Select all

// ----------------------------------------------------------------------------- LCD Connections
sbit LCD_RS   at LATA0_bit;
sbit LCD_EN   at LATA1_bit;
sbit LCD_D4   at LATA4_bit;
sbit LCD_D5   at LATA5_bit;
sbit LCD_D6   at LATA6_bit;
sbit LCD_D7   at LATA7_bit;
sbit LCD_BKLT at LATD0_bit;

sbit LCD_RS_Direction   at TRISA0_bit;
sbit LCD_EN_Direction   at TRISA1_bit;
sbit LCD_D4_Direction   at TRISA4_bit;
sbit LCD_D5_Direction   at TRISA5_bit;
sbit LCD_D6_Direction   at TRISA6_bit;
sbit LCD_D7_Direction   at TRISA7_bit;
sbit LCD_BKLT_Direction at TRISD0_bit;

// ----------------------------------------------------------------------------- Keyboard Connections
sbit PS2_Data  at RF12_bit;
sbit PS2_Clock at RF13_bit;

sbit PS2_Data_Direction  at TRISF12_bit;
sbit PS2_Clock_Direction at TRISF13_bit;

unsigned short Keydata, Special, Down;
unsigned short Common_Character;
char Txt[16];
int i = 0;
int KB_FLG = 0;

void main() {
  CHECON = 0x32;                                                                // Cache Control Register (Copied from LCD Library Help files)
  AD1PCFG = 0xFFFF;                                                             // Configure AN pins as digital I/O
  JTAGEN_bit = 0;                                                               // Disable JTAG
  TROEN_bit = 0;                                                                // Disable the Trace Port
  TDOEN_bit = 0;                                                                // 2-wire JTAG protocol does not use TDO
  PMAEN = 0;                                                                    // This and PMCON 'ON' bit must be cleared so as to make LATE usable.
  PMCON = 0;                                                                    // Disable Parallel Port Contol
  
  TRISA = 0;
  TRISB = 0;
  TRISC = 0;
  TRISD = 0;
  TRISE = 0;
  TRISF = 0;
  TRISG = 0;
  
  LATA = 0;
  LATB = 0;
  LATC = 0;
  LATD = 0;
  LATE = 0;
  LATF = 0;
  LATG = 0;
  
  Lcd_Init();
  Lcd_Cmd(_LCD_CLEAR);
  Lcd_Cmd(_LCD_CURSOR_OFF);
  LCD_BKLT_Direction = 0;
  LCD_BKLT = 1;                                                                 // LCD Backlight 'ON'
  
  Ps2_Config();
  Delay_ms(100);

             //1234567890123456
  strcpy(Txt, "                ");                                              // LCD 16 characters; Clear text
  Common_Character = 0;
  KB_FLG = 0;
               //1234567890123456
  Lcd_Out(2, 1, "--Ready To Go!--");
  
  while(1) {
    if (Ps2_Key_Read(&Keydata, &Special, &Down)) {                              // PS/2
      if (Down && !Special && Keydata) {                                        // Common Key Press
         //strcpy(Txt, "                ");                                       // Clear text
         //strcpy(Txt, Keydata);                                                  // Option to try
         Common_Character = Keydata;                                            // Option to try
      }
      else if (Down && Special && Keydata) {                                    // Special Key Press
        strcpy(Txt, "                ");                                        // Clear text
        switch (Keydata) {                                                      // Find key press and alter text to display
          case 1:
                 strcpy(Txt, "F1");
                 break;
          case 2:
                 strcpy(Txt, "F2");
                 break;
          case 3:
                 strcpy(Txt, "F3");
                 break;
          case 4:
                 strcpy(Txt, "F4");
                 break;
          case 5:
                 strcpy(Txt, "F5");
                 break;
          case 6:
                 strcpy(Txt, "F6");
                 break;
          case 7:
                 strcpy(Txt, "F7");
                 break;
          case 8:
                 strcpy(Txt, "F8");
                 break;
          case 9:
                 strcpy(Txt, "F9");
                 break;
          case 10:
                 strcpy(Txt, "F10");
                 break;
          case 11:
                 strcpy(Txt, "F11");
                 break;
          case 12:
                 strcpy(Txt, "F12");
                 break;
          case 13:
                 strcpy(Txt, "Enter");
                 break;
          case 14:
                 strcpy(Txt, "Page Up");
                 break;
          case 15:
                 strcpy(Txt, "Page Down");
                 break;
          case 16:
                 strcpy(Txt, "Backspace");
                 break;
          case 17:
                 strcpy(Txt, "Insert");
                 break;
          case 18:
                 strcpy(Txt, "Delete");
                 break;
          case 19:
                 strcpy(Txt, "Windows");
                 break;
          case 20:
                 strcpy(Txt, "Ctrl");
                 break;
          case 21:
                 strcpy(Txt, "Shift");
                 break;
          case 22:
                 strcpy(Txt, "Alt");
                 break;
          case 23:
                 strcpy(Txt, "Print Screen");
                 break;
          case 24:
                 strcpy(Txt, "Pause");
                 break;
          case 25:
                 strcpy(Txt, "Caps Lock");
                 break;
          case 26:
                 strcpy(Txt, "End");
                 break;
          case 27:
                 strcpy(Txt, "Home");
                 break;
          case 28:
                 strcpy(Txt, "Scroll Lock");
                 break;
          case 29:
                 strcpy(Txt, "Num Lock");
                 break;
          case 30:
                 strcpy(Txt, "Left Arrow");
                 break;
          case 31:
                 strcpy(Txt, "Right Arrow");
                 break;
          case 32:
                 strcpy(Txt, "Up Arrow");
                 break;
          case 33:
                 strcpy(Txt, "Down Arrow");
                 break;
          case 34:
                 strcpy(Txt, "Escape");
                 break;
          case 35:
                 strcpy(Txt, "Tab");
                 break;
        }
        KB_FLG = 2;
      }
    }
    Delay_ms(1);
    
    if (KB_FLG == 1) {                                                          // Display Common Key Press
      KB_FLG = 0;
      Lcd_Cmd(_LCD_CLEAR);
      //Lcd_Chr(1, 1, Txt);                                                       // Option to try: Result; Didn't work
      Lcd_Chr(1, 1, Common_Character);                                          // Option to try: Result; Didn't work
    }
    else if (KB_FLG == 2) {                                                     // Display Special Key Press Text
      KB_FLG = 0;
      Lcd_Cmd(_LCD_CLEAR);
      Lcd_Out(1, 1, Txt);                                                       // Option to try: Result; Didn't work
    }
  }
}
There's nothing that displays on the LCD. And at the very least, I would have expected to see the text for the special keys to appear.
I'm also a little unsure about how to approach the 'common key press to LCD character' part of the code.
As in the BASIC code, I can use chr(x) code; "Keyboard_Character[0] = chr(keydata)".
Side question would then be, is there something similar for C? Or is just entering the unsigned short value of 'keydata' into "Lcd_Chr(1, 1, Common_Character);" okay?

I have checked that pins 40 (RF12) and 39 (RF13) aren't using any peripheral; RF12 (SS4/U5RX/U2CTS - SPI-4 and UART-5 and UART-2), RF13 (SCK4/U5TX/U2RTS - SPI-4 and UART-2 and UART-5).
Both UART and SPI registers 'ON' bits are '0' by default.

I have also checked that the PCB tracks and circuitry are the same as the PCB tracks and circuitry for the PIC18F45K22 as well. All checks out.

So, am a bit stuck.
Please help :)

Thank you for your time.

Edit:
After posting this, I tried out the exact circuit found within the help files.
Where PS2_Data is on RD0, and PS2_Clock is on RD1
I could not get the key presses to display on the LCD with this set up either.

AntiMember
Posts: 135
Joined: 02 Jan 2020 19:00

Re: PS/2 Keyboard to LCD

#2 Post by AntiMember » 12 Nov 2022 08:32

Init_PIC(); // Configure PIC ???
Init_Vari(); // Configure variables ???

chris11jed
Posts: 156
Joined: 15 Jun 2011 06:37

Re: PS/2 Keyboard to LCD

#3 Post by chris11jed » 12 Nov 2022 09:41

Init_PIC() and Init_Vari() are procedures in the code for the PIC18F45K22, using mikroBASIC.

Init_PIC() sets up registers, ports and pins. Where Init_vari() sets up variables. They are called prior to the main loop.

Both these are not used in the code for PIC32MX695F512L, using mikroC.

AntiMember
Posts: 135
Joined: 02 Jan 2020 19:00

Re: PS/2 Keyboard to LCD

#4 Post by AntiMember » 12 Nov 2022 10:26

And if you check:

Code: Select all

// ----------------------------------------------------------------------------- LCD Connections
sbit LCD_RS   at LATA0_bit;
sbit LCD_EN   at LATA1_bit;
sbit LCD_D4   at LATA4_bit;
sbit LCD_D5   at LATA5_bit;
sbit LCD_D6   at LATA6_bit;
sbit LCD_D7   at LATA7_bit;
sbit LCD_BKLT at LATD0_bit;

sbit LCD_RS_Direction   at TRISA0_bit;
sbit LCD_EN_Direction   at TRISA1_bit;
sbit LCD_D4_Direction   at TRISA4_bit;
sbit LCD_D5_Direction   at TRISA5_bit;
sbit LCD_D6_Direction   at TRISA6_bit;
sbit LCD_D7_Direction   at TRISA7_bit;
sbit LCD_BKLT_Direction at TRISD0_bit;

// ----------------------------------------------------------------------------- Keyboard Connections
sbit PS2_Data  at RF12_bit;
sbit PS2_Clock at RF13_bit;

sbit PS2_Data_Direction  at TRISF12_bit;
sbit PS2_Clock_Direction at TRISF13_bit;

unsigned short Keydata, Special, Down;
unsigned short Common_Character;
char Txt[16];
int i = 0;
int KB_FLG = 0;

void main() {
  CHECON = 0x32;                                                                // Cache Control Register (Copied from LCD Library Help files)
  AD1PCFG = 0xFFFF;                                                             // Configure AN pins as digital I/O
  JTAGEN_bit = 0;                                                               // Disable JTAG
  TROEN_bit = 0;                                                                // Disable the Trace Port
  TDOEN_bit = 0;                                                                // 2-wire JTAG protocol does not use TDO
  PMAEN = 0;                                                                    // This and PMCON 'ON' bit must be cleared so as to make LATE usable.
  PMCON = 0;                                                                    // Disable Parallel Port Contol
  
  TRISA = 0;
  TRISB = 0;
  TRISC = 0;
  TRISD = 0;
  TRISE = 0;
  TRISF = 0;
  TRISG = 0;
  
  LATA = 0;
  LATB = 0;
  LATC = 0;
  LATD = 0;
  LATE = 0;
  LATF = 0;
  LATG = 0;
 
  Ps2_Config();
  Delay_ms(100); 
  
  Lcd_Init();
  Delay_ms(100); 
  Lcd_Cmd(_LCD_CLEAR);
  Lcd_Cmd(_LCD_CURSOR_OFF);
  LCD_BKLT_Direction = 0;
  LCD_BKLT = 1;                                                   // LCD Backlight 'ON'
  
  Lcd_Out(1, 1, "LCD Initialised");                       //  Check init LCD
  Delay_ms(2000);  
                                                                  
  
             //1234567890123456
  strcpy(Txt, "                ");                                              // LCD 16 characters; Clear text
  Common_Character = 0;
  KB_FLG = 0;
               //1234567890123456
  Lcd_Out(2, 1, "--Ready To Go!--");
  
  while(1) {
    if (Ps2_Key_Read(&Keydata, &Special, &Down)) {                              // PS/2
      if (Down && !Special && Keydata) {                                        // Common Key Press
         //strcpy(Txt, "                ");                                       // Clear text
         //strcpy(Txt, Keydata);                                                  // Option to try
         Common_Character = Keydata;                                            // Option to try
      }
      else if (Down && Special && Keydata) {                                    // Special Key Press
        strcpy(Txt, "                ");                                        // Clear text
        switch (Keydata) {                                                      // Find key press and alter text to display
          case 1:
                 strcpy(Txt, "F1");
                 break;
          case 2:
                 strcpy(Txt, "F2");
                 break;
          case 3:
                 strcpy(Txt, "F3");
                 break;
          case 4:
                 strcpy(Txt, "F4");
                 break;
          case 5:
                 strcpy(Txt, "F5");
                 break;
          case 6:
                 strcpy(Txt, "F6");
                 break;
          case 7:
                 strcpy(Txt, "F7");
                 break;
          case 8:
                 strcpy(Txt, "F8");
                 break;
          case 9:
                 strcpy(Txt, "F9");
                 break;
          case 10:
                 strcpy(Txt, "F10");
                 break;
          case 11:
                 strcpy(Txt, "F11");
                 break;
          case 12:
                 strcpy(Txt, "F12");
                 break;
          case 13:
                 strcpy(Txt, "Enter");
                 break;
          case 14:
                 strcpy(Txt, "Page Up");
                 break;
          case 15:
                 strcpy(Txt, "Page Down");
                 break;
          case 16:
                 strcpy(Txt, "Backspace");
                 break;
          case 17:
                 strcpy(Txt, "Insert");
                 break;
          case 18:
                 strcpy(Txt, "Delete");
                 break;
          case 19:
                 strcpy(Txt, "Windows");
                 break;
          case 20:
                 strcpy(Txt, "Ctrl");
                 break;
          case 21:
                 strcpy(Txt, "Shift");
                 break;
          case 22:
                 strcpy(Txt, "Alt");
                 break;
          case 23:
                 strcpy(Txt, "Print Screen");
                 break;
          case 24:
                 strcpy(Txt, "Pause");
                 break;
          case 25:
                 strcpy(Txt, "Caps Lock");
                 break;
          case 26:
                 strcpy(Txt, "End");
                 break;
          case 27:
                 strcpy(Txt, "Home");
                 break;
          case 28:
                 strcpy(Txt, "Scroll Lock");
                 break;
          case 29:
                 strcpy(Txt, "Num Lock");
                 break;
          case 30:
                 strcpy(Txt, "Left Arrow");
                 break;
          case 31:
                 strcpy(Txt, "Right Arrow");
                 break;
          case 32:
                 strcpy(Txt, "Up Arrow");
                 break;
          case 33:
                 strcpy(Txt, "Down Arrow");
                 break;
          case 34:
                 strcpy(Txt, "Escape");
                 break;
          case 35:
                 strcpy(Txt, "Tab");
                 break;
        }
        KB_FLG = 2;
      }
    }
    Delay_ms(1);
    
    if (KB_FLG == 1) {                                                          // Display Common Key Press
      KB_FLG = 0;
      Lcd_Cmd(_LCD_CLEAR);
      //Lcd_Chr(1, 1, Txt);                                                       // Option to try: Result; Didn't work
      Lcd_Chr(1, 1, Common_Character);                                          // Option to try: Result; Didn't work
    }
    else if (KB_FLG == 2) {                                                     // Display Special Key Press Text
      KB_FLG = 0;
      Lcd_Cmd(_LCD_CLEAR);
      Lcd_Out(1, 1, Txt);                                                       // Option to try: Result; Didn't work
    }
  }
}
If "LCD Initialised" is not displayed on LСD, try temporarily excluding Ps2_Config().

chris11jed
Posts: 156
Joined: 15 Jun 2011 06:37

Re: PS/2 Keyboard to LCD

#5 Post by chris11jed » 12 Nov 2022 10:42

I have tried the addition to the code you suggested, and the LCD displays the new text, "LCD Initialised" on the first row.

But the Keyboard presses still don't show up.

hexreader
Posts: 1784
Joined: 27 Jun 2010 12:07
Location: England

Re: PS/2 Keyboard to LCD

#6 Post by hexreader » 12 Nov 2022 11:13

Is your keyboard 3.3 Volt compatible?

I own four PS2 keyboards. Only one works at 3.3 Volts: the others need 5 Volts

EDIT:
I see from the following post that you are supplying 5 Volts to keyboard - so there should be no issue.

PIC32 pins RF12, RF13, are 5V compatible, so no problem there either.
Last edited by hexreader on 12 Nov 2022 12:05, edited 2 times in total.
Start every day with a smile...... (get it over with) :)

chris11jed
Posts: 156
Joined: 15 Jun 2011 06:37

Re: PS/2 Keyboard to LCD

#7 Post by chris11jed » 12 Nov 2022 11:33

It's an old Microsoft keyboard, and says on the back 5 volts.

Besides, the PCB routes 5V to the Vcc pin. So using 3V3 would need 'surgery' on traces.

I used a multimeter on the signal side of the pull-up resistors (1K resistors, just like the library help page shows), with these results;
  • Data signal = 4.5V
  • Clock signal = 300mV
I was googling PS2 keyboard communication protocol for clues. On this page PS/2 Mouse/Keyboard Protocol when you scroll down a little way, under the bold-text heading "Communication: General Description". It states that;
  • Data = high, Clock = low: Communication Inhibited.
And I think, for some reason, this is the cause of my issue. Or, it might be?

I re-checked the traces from the MCU pins, to the female USB connection header. No solder bridges, the PCB manufacturer obviously tested it fine, and there's no points where traces, vias, or anything else come close together.

So, I'm wondering if there's something in the PS2 Library for PIC32 that's at play here? Not saying it is. But just canvasing ideas. Especially if my code, which pretty-much mimics the PS2 Library example, is fine.

Edit:
I have tested the Data and Clock lines with my crappy little oscilloscope.
  • The Clock line is low at just above 2 volts, but pulses high (4.5 volts) about every 9ms. The Data line is constantly low at 2 volts.
  • When I press the MCLR button, the Clock line stays high (4.5V) for 200ms. Goes low (~2V) for just over 50ms. Then stays high for 100ms, before the 'pulsing high' every 9ms or so. All the while, the Data line is low. Except for one high pulse when the Clock line begins its second high time (100ms).


So, I am not sure what is going on. But it doesn't seem like this is the correct behaviour.

AntiMember
Posts: 135
Joined: 02 Jan 2020 19:00

Re: PS/2 Keyboard to LCD

#8 Post by AntiMember » 12 Nov 2022 12:54

chris11jed wrote:
12 Nov 2022 11:33
It's an old Microsoft keyboard, and says on the back 5 volts.
...........
I re-checked the traces from the MCU pins, to the female USB connection header. No solder bridges, the PCB manufacturer obviously tested it fine, and there's no points where traces, vias, or anything else come close together.
So, I'm wondering if there's something in the PS2 Library for PIC32 that's at play here? Not saying it is. But just canvasing ideas. Especially if my code, which pretty-much mimics the PS2 Library example, is fine.
...........
I never understood, PS2 or USB ?..
Check the keyboard on the computer, if possible.
When power is ON, all LEDs on a good keyboard blink once.
With an incorrect connection the datа and clock will not work.
USB and PS2 are not compatible!

chris11jed
Posts: 156
Joined: 15 Jun 2011 06:37

Re: PS/2 Keyboard to LCD

#9 Post by chris11jed » 12 Nov 2022 13:30

Keyboard works on the computer. It has a usb male connector.

When I use it on the PIC18F45K22 with GLCD, using the PS2 Library, written in mikroBASIC, it works a treat.

When I use it on the PIC32MX695F512L with LCD, using the PS2 Library, written in mikroC, it doesn't work. And gets the results I spoke of in my last reply under the 'Edit' section.

Reading some things online, keyboards that have usb connections do use the PS2 protocol. And the PS2 Library is geared towards keyboards. Which is why I always used it for PIC18's. Maybe PIC32's are a little more 'picky' about it? Not sure.

I hesitate to use the USB Library, because one, the VBUS, RG2 and RG3 pins are already taken on the PCB. And two, I haven't got a development board for these chips, as mikro sold out. So there's no way of testing that Library. A third one is, I have no idea how to go about writing code for a keyboard using the USB Library at the moment. Again, the PS2 Library works... for PIC18's ...so I thought it should for PIC32 as well. As I mentioned above, the info I'm seeing from a few 'PS2 keyboard protocol' pages suggest a keyboard with a usb male connector, will use PS2 protocol.

AntiMember
Posts: 135
Joined: 02 Jan 2020 19:00

Re: PS/2 Keyboard to LCD

#10 Post by AntiMember » 12 Nov 2022 15:01

Clearly, a universal keyboard.
Then check according to: https://pinoutguide.com/InputCables/usb ... nout.shtml
PS2_Data at RF12_bit = 2 = Data- USB
PS2_Clock at RF13_bit = 3 = Data+ USB
Also try swapping inits...
Ps2_Config();
Delay_ms(100);

Lcd_Init();
Delay_ms(100);

Did the authors of the library themselves at PIC32 check it?.. :)

chris11jed
Posts: 156
Joined: 15 Jun 2011 06:37

Re: PS/2 Keyboard to LCD

#11 Post by chris11jed » 12 Nov 2022 22:56

Checked the pins as per the link. All correctly assigned.

Swapped the inits as well. Still not working.

AntiMember
Posts: 135
Joined: 02 Jan 2020 19:00

Re: PS/2 Keyboard to LCD

#12 Post by AntiMember » 13 Nov 2022 08:42

You didn't set a flag = 1.

Code: Select all

      if (Down && !Special && Keydata) {                                        // Common Key Press
         KB_FLG = 1;                                                                                                              //<<<<<<<<<!!!
         Common_Character = Keydata;                                            // Option to try
      }

chris11jed
Posts: 156
Joined: 15 Jun 2011 06:37

Re: PS/2 Keyboard to LCD

#13 Post by chris11jed » 13 Nov 2022 09:16

:lol: Yeah I noticed that when I woke up and saw your earlier reply.

Fixed it up as you've suggested. Still nothing...

Then I thought I was parsing the keydata wrong, for some reason, and that's why nothing is displayed upon the Lcd.

So I just commented out all parsing code in the PS2 section except for the flag, and then commented out all the code that prints the parsed data to the Lcd in the section where a set flag is too.

And just printed to the lcd "Common Character" in the section that deals with that flag, and printed to the lcd "Special Character" in the section that deals with that flag.

Still nothing. :roll:

Then I de-soldered the 1K pull-up resistors on the data and clock lines. Soldered in new ones. Tried it all again... and nothing. :?

At this point, I just think there's something wrong with the clock line being low. It shouldn't be low while the data line is high. That's telling the host or the device communication is halted. Why this is happening, I am not sure.

By the way... I really do appreciate your ideas, your looking into all areas, your help. Thank you! :D

I think the code as it stands, with the flag set in both Common-Key and Special-Key sections of the PS2 code, would normally work. Unless you see something else. I think there's some issue elsewhere. But I am just not sure where?

AntiMember
Posts: 135
Joined: 02 Jan 2020 19:00

Re: PS/2 Keyboard to LCD

#14 Post by AntiMember » 13 Nov 2022 09:55

Well, instead of the current program loop, try writing a level switch on F12, F13 with a delay of 500ms.
View levels with a multimeter or LEDs.

chris11jed
Posts: 156
Joined: 15 Jun 2011 06:37

Re: PS/2 Keyboard to LCD

#15 Post by chris11jed » 13 Nov 2022 11:11

Did this;

First test: Two LEDs. Anodes connected together, connected to usb female Vcc. One cathode to RF12, the other cathode to RF13. LED's light bright when both port pins are LOW. LED's low illumination (not completely off) when both port pins are HIGH.

Second test: Two LEDs. Cathodes connected together, connected to usb female GND. One anode to R12, the other anode to RF13. LED's light bright when both port pins are HIGH. LED's are completely off when port pins are LOW.

Post Reply

Return to “mikroC PRO for PIC32 General”