01 – How to connect a LCD to a PICAXE?

One of the very first questions that students ask is: How do I connect a LCD to a PICAXE microcontroller? In this article we are going to answer that by connecting a 16×2 LCD display to a PICAXE-18M2 microcontroller with a direct connection. It is also possible to use an additional circuit to serialise the operation and it is then possible to send data to a LCD via one output pin but for now we are going to look at the direct connection method using 4 output pins to transfer a nibble at a time.

An LCD display is probably the handiest option to output information to the real world in an understandable manner. LED’s also provide some sort of indication but nothing beats an LCD display. With an LCD one can also implement menus with a few buttons.

The PICAXE-18M2 is ideal to connect a LCD to and you still have another port and two of port B input/output pins to do something else with. The code for this is given in the PICAXE manual 3 but I am still going to repeat it here but with a complete circuit diagram to eliminate any uncertainty. This example uses the top 4 I/O pins of port B that is B.4, B.5, B.6 and B.7 to transfer a nibble of data. Sometimes one has to use the bottom 4 I/O pins because of other restrictions on the I/O ports. For example if one wants to use the I2C interface for a real-time clock, port B can’t be used on the PICAXE-18M2 because the the I2C scl is on port B.4 and it can’t be shared with the LCD. How do one use the bottom half of the port I/O pins for the LCD? You have to convert the thee subprocedures for the LCD. In the next example I have done just that and you can download the code for it.


 

The circuit diagram is shown below with the audio programming socket and the two programming resistors of 22k and 10k and a LCD connected to port B.

The basic code to send text to the LCD is given below. It is not important to know how the initiation (init), write character (wrchr) and write instruction (wrins) subprocedures works to use them. They are based on the LCD manufacturers instructions and more detail can be obtained from the LCD data sheet.

‘##########################################################
‘# 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 B For LCD                                                                                                     #
‘# B.7 – 14 (DB7)                                                                                                               #
‘# B.6 – 13 (DB6)                                                                                                              #
‘# B.5 – 12 (DB5)                                                                                                              #
‘# B.4 – 11 (DB4)                                                                                                              #
‘# B.2 – 4 (RS)                                                                                                                  #
‘# B.3 – 6 (E)                                                                                                                    #
‘########################################################

let pinsB = 0              ‘ Clear all output lines
let b3 = 0                    ‘ Reset variable b3
let dirsB = 252         ‘ Set pins 2-7 as output lines (Stamp only).
pause 200                  ‘ Wait 200 ms for LCD to reset.
let pinsB = 48           ‘ Set to 8-bit operation.
pulsout B.3,1            ‘ Send data by pulsing ‘enable’
pause 10                     ‘ Wait 10 ms
pulsout B.3,1            ‘ Send data again
pulsout B.3,1            ’ Send data again
let pinsB = 32           ‘ Set to 4-bit operation.
pulsout B.3,1            ’ Send data.
pulsout B.3,1            ‘ Send data again.
let pinsB = 128         ‘ Set to two line operation
pulsout B.3,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 pinsB = b1 & 240             ‘ Mask the high nibble of b1 into b2.
high B.2                                   ‘ Make sure RS is high
pulsout B.3,1                          ’ Pulse the enable pin to send data.
let b2 = b1 * 16                      ‘ Put low nibble of b1 into b2.
let pinsB = b2 & 240             ’ Mask the high nibble of b2
high B.2                                   ’ Make sure RS is high
pulsout B.3,1                          ‘ Pulse enable pin to send data.
return

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

let pinsB = b1 & 240             ‘ Mask the high nibble of b1 into b2.
pulsout B.3,1                          ’ Pulse the enable pin to send data.
let b2 = b1 * 16                      ’ Put low nibble of b1 into b2.
let pinsB = b2 & 240             ‘ Mask the high nibble of b2
pulsout B.3,1                          ‘ Pulse enable pin to send data.
high B.2                                   ‘ Back to character mode
return

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