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
- (Primary) 25MHz resonator
- (Secondary) 32.768kHz resonator (not used in this code)
- (Sys-Clk Speed) 100MHz
- mikroC PRO for PIC32 v.4.0.0
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.
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
}
}
}
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.