02 – How to connect a LCD to the bottom half of a Port?

In the previous example the top 4 I/O pins of port B that is B.4, B.5, B.6 and B.7 were used as data lines to the LCD display. To be more flexible one sometimes needs to use the bottom 4 I/O pins of a port. In this example answers the question: How to connect a LCD to the bottom half of a Port. The bottom 4 I/O pins of port C that is C.0, C.1, C.2 and C3 is used as the data lines to the LCD. By implementing this on the PICAXE-18M2 the I2C scl pin is freed to be used in a real-time clock application.  


 

The Circuit Diagram is shown above with the audio programming socket and the two programming resistors of 22k and 10k and a LCD connected to port C.

As in the previous example the basic code to use the LCD and send text to it is given below. To understand the LCD sub procedures  initiation (init), character (wrchr) and write instruction (wrins) the manufacturers LCD data sheet needs to be consulted. It is not necessary to understand the sub procedures to use them and the code example below demonstrates the use of these sub procedures. 


 

‘##########################################################
‘# This LCD code uses the Upper part of Port B for the LCD parallel mode and       #
‘# it illustates the LCD example in manual 3                                                                   #
‘##########################################################

EEPROM 0,(“Technology Tutor”)       ‘ store the text in the EEPROM memory

gosub init                           ‘ initialise LCD

main:

let b1 = 1                            ‘ set b1 to ‘clear display’ instruction
gosub wrins                       ‘ send instruction to LCD

let b1 = 12                         ‘ set b1 to ‘hide cursor’ instruction
gosub wrins                       ’ send instruction to LCD

For b0 = 0 to 15              ‘ For…next loop – Read Technology Tutor text into LCD

read b0, b1                ‘ read letter from EEPROM into variable b1
gosub wrchr              ‘ send character to LCD

Next b0

pause 400                       ‘ pause for 0.4s
goto main                        ‘ loop
end

init:
‘########################################################
‘# Initiation subprocedure for the LCD                                                                                      #
‘# Use Port C For LCD                                                                                                                        #
‘# C.7 – 4 (RS)                                                                                                                                         #
‘# C.6 – 6 (E)                                                                                                                                            #
‘# C.3 – 14 (DB7)                                                                                                                                   #
‘# C.2 – 13 (DB6)                                                                                                                                    #
‘# C.1 – 12 (DB5)                                                                                                                                    #
‘# C.0 – 11 (DB4)                                                                                                                                    #
‘########################################################

let pinsC = 0                         ‘ Clear all output lines
let b3 = 0                              ‘ Reset variable b3
let dirsC = %11001111       ‘ Set pins 0-3,6,7 as output lines
pause 200                            ‘ Wait 200 ms for LCD to reset.
let pinsC = %00000011     ‘ Set to 8-bit operation
pulsout C.6,1                        ‘ Send data by pulsing ‘enable’
pause 10                               ‘ Wait 10 ms
pulsout C.6,1                        ‘ Send data again
pulsout C.6,1                        ‘ Send data again
let pinsC = %00000010    ‘ Set to 4-bit operation.
pulsout C.6,1                        ‘ Send data
pulsout C.6,1                        ‘ Send data again
let pinsC = %00001000    ’ Set to two line operation
pulsout C.6,1                        ‘ Send data
let b1 = 14                            ‘ Screen on, cursor on instruction
gosub wrins                          ‘ Write instruction to LCD
return

wrchr:
‘#########################################################
‘# Write Character to LCD subprocedure, sends one character to the LCD             #
‘#########################################################

let b2 = b1 / 16                   ‘ Put high nibble of b1 into b2
let pinsC = b2 & 15             ‘ Mask the low nibble of b2 (%00001111)
high C.7                                ’ Make sure RS is high
pulsout C.6,1                       ‘ Pulse the enable pin to send data.
let pinsC = b1 & 15             ‘ Mask the low nibble of b1
high C.7                                ‘ Make sure RS is high
pulsout C.6,1                       ‘ Pulse the enable pin to send data.
return

wrins:
‘#########################################################
‘# Write Instruction to LCD – subprocedure, sends an instruction to the LCD       #
‘#########################################################

let b2 = b1 / 16                  ‘ Put high nibble of b1 into b2
let pinsC = b2 & 15            ’ Mask the low nibble of b2 (%00001111)
pulsout C.6,1                       ‘ Pulse the enable pin to send data.
let pinsC = b1 & 15             ‘ Mask the low nibble of b1
pulsout C.6,1                       ‘ Pulse the enable pin to send data.
high C.7                                ‘ Back to character mode
return

You can download the program file, Parallel LCD for 18M2 using C port by right clicking on it and selecting ‘save link as’ from the popup menu.