Interfacing a Basic Stamp 2 and a HD44780 LCD (Part 3)

Well as promised, I’m finally posting the code. I know it’s been a while and the code still isn’t technically complete, but I’m hoping it can help at least a few people just getting started. There is no warranty on this code and if you ask for support I may not get back to you. This code is also meant to work with the wiring scheme I have developed in my previous posts. The actual code appears after the jump.

' =========================================================================
' File...... Parallel_LCD driver.bs2
' Purpose... this file will drive a paralell LCD based on a Hitachi 44780 Controller
' {$STAMP BS2}
' {$PBASIC 2.5}
' -----[ Program Description ]---------------------------------------------
' This file provides the necessary routines to drive a parallel LCD display
' This code works with the BS2, BS2e and BS2sx
' -----[ I/O Definitions ]-------------------------------------------------
'These assignments are for pins that will act as controls for the LCD display.
'They are set in an arbitrary order and must be changed during if your lcd is not plugged into the same pins
E PIN 5 ' Enable Pin For LCD
RW PIN 4 ' R/W Pin For LCD
RS PIN 3 ' LCD Register Select
' 0 = Instruction, 1 = Text
'Data pin definitions, these should correspond to where you have the Controller connected for pins D0-D7
DB7 PIN 15
DB6 PIN 14
DB5 PIN 13
DB4 PIN 12
DB3 PIN 11
DB2 PIN 10
DB1 PIN 9
DB0 PIN 8
' -----[ LCD Size ]-------------------------------------------------
Rows VAR Byte
Columns VAR Byte
Rows=4
Columns=20
' -----[ Variables ]-------------------------------------------------------
char VAR Byte ' Character To Send To LCD
index VAR Word ' Character Pointer
temp VAR Byte ' Temp Variable
' -----[ EEPROM Data ]-----------------------------------------------------
DATA " http://Heapsorandom.wordpress.com " ' Message To Send To LCD
' -----[ Main Code ]-------------------------------------------------
Main:
DIR6=%1 'use pin 6 as a visual init flag
OUT6=%1
HIGH 6
GOSUB InitStamp
GOSUB InitLCD
LOW 6

FOR temp = 0 TO 20 ' 28 Characters
' IF temp = 19 THEN ' Check For End Of Line
' GOSUB Next_Line ' Jump To Next Line
' ENDIF
READ temp, char ' Read Next Character From EEPROM
GOSUB SendText ' Send Character To LCD Display
NEXT
GOSUB DCBon
END

' -----[ Initialization Code ]-------------------------------------------------
InitStamp:
DIR5 = %1
DIR4 = %1
DIR3 = %1 ' Set LCD To Write Mode
OUTH = %00000000 ' Set All Output Low
DIRH = %11111111 ' Set I/O Direction
LOW E
LOW RW
LOW RS
RETURN
InitLCD:
PAUSE 200
GOSUB Set8BitWidth ' Reset The LCD
PULSOUT E,1 ' send a pulse on the Enable Pin
PAUSE 10
PULSOUT E,1
PAUSE 10
PULSOUT E,1
PAUSE 10
GOSUB DCBoff ' Turn On Cursor and blink
GOSUB EntryModeIncNoShift ' Set Auto-Increment
GOSUB ClearDisplay ' Clears LCD
RETURN

' -----[ LCD Control Subroutines ]-------------------------------------------------

' [Set4BitWidth] Sets the bus size to 4 data bits in width
Set4BitWidth:
LOW RS
IF Rows=1 THEN 'sets for instruction mode if RS=0
OUTH = %00100000 'Assumes 1/8 or 1/11 Duty (1 line) and the font is 5x7 Dots
ELSE
OUTH = %00101000
ENDIF
PULSOUT E,1
RETURN

' [Set8BitWidth] Sets the bus size to 8 data bits in width
Set8BitWidth:
LOW RS
IF Rows=1 THEN
OUTH = %00110000 'Assumes 1/8 or 1/11 Duty (1 line) and the font is 5x7 Dots
ELSE
OUTH = %00111000
ENDIF
PULSOUT E,1
RETURN

' [Clear Display] clears the display and sets the cursor to home position (address 0)
ClearDisplay:
LOW RS
OUTH = %00000001
PULSOUT E,1
PAUSE 2 'It takes the display 1.64 mS to execute the command
RETURN

' [CursorHome]Returns cursor to home position (address 0). Also returns display being shifted to the original position. DDRAM contents remains unchanged.
CursorHome:
LOW RS
OUTH = %00000010
PULSOUT E,1
PAUSE 2 'It takes the display 1.64 mS to execute the command
RETURN

' [DCBon]Sets the display on, cursor on and blinks the cursor
DCBon:
LOW RS
OUTH = %00001111
PULSOUT E,1
RETURN

' [DCBoff]Sets the display off, cursor off
DCBoff:
LOW RS
OUTH = %00001000
PULSOUT E,1
RETURN

' [DConBoff]Sets the display on, cursor on, but doesn't blink the cursor
DConBoff:
LOW RS
OUTH = %00001110
PULSOUT E,1
RETURN

' [DonCBoff]Sets the display on, cursor on, but doesn't blink the cursor
DonCBoff:
LOW RS
OUTH = %00001100
PULSOUT E,1
RETURN

' [ShftRight]Sets the display to Display Shift and the direction is right
ShiftRight:
LOW RS
OUTH = %00011100
PULSOUT E,1
RETURN

' [ShiftLeft]Sets the display to Display Shift and the direction is right
ShiftLeft:
LOW RS
OUTH = %00011000
PULSOUT E,1
RETURN

' [CursorLeft]Sets the display to Display Shift and the direction is right
CursorLeft:
LOW RS
OUTH = %00010000
PULSOUT E,1
RETURN

' [CursorRight]Sets the display to Display Shift and the direction is right
CursorRight:
LOW RS
OUTH = %00010100
PULSOUT E,1
RETURN

' [EntryModeIncShift]
EntryModeIncShift:
LOW RS
OUTH = %00000111
PULSOUT E,1
RETURN

' [EntryModeIncNoShift]
EntryModeIncNoShift:
LOW RS
OUTH = %00000110
PULSOUT E,1
RETURN

' [EntryModeDecShift]
EntryModeDecShift:
LOW RS
OUTH = %00000101
PULSOUT E,1
RETURN

' [EntryModeDecNoShift]
EntryModeDecNoShift:
LOW RS
OUTH = %00000100
PULSOUT E,1
RETURN

'-----------------------------[LCD Data Subroutines]-------------------------------
SendText:
HIGH RS
OUTH = char ' Send Low Nibble
PULSOUT E,1
PAUSE 100
RETURN

About these ads

4 Responses to Interfacing a Basic Stamp 2 and a HD44780 LCD (Part 3)

  1. sampan says:

    your articles are interesting and so useful for me. Thank you for sharing great information

  2. vaclav says:

    Nice, but…
    I am looking for a sample code to read data from the Hitachi HD44780 controller (DDRAM) working in 4 bit mode.
    The manual I have does not really explains it well or even saiz if it si posible in 4 bit mode.

  3. knob and tube replacement…

    [...]Interfacing a Basic Stamp 2 and a HD44780 LCD (Part 3) « Heaps-o-Random[...]…

  4. Divorce Solicitor Campbelltown…

    [...]Interfacing a Basic Stamp 2 and a HD44780 LCD (Part 3) « Heaps-o-Random[...]…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: