' *** LCDtest(16X2).bas ************** 2009.09.09 ************** Ron Hackett *** ' ' Program runs on a Picaxe-14M @ 8MHz - sends data to a 44780-type 8x2 LCD. ' Because we are using DB5...DB2 rather than DB7...DB4, high nibbles need to ' be divided by 4 (and low nibbles multiplied by 4) to place on correct pins. ' ' ****************************************************************************** ' *** Constants *** symbol cmd = 0 ' used to set up for "command" byte type symbol txt = 1 ' used to set up for "text" byte type symbol enable = 1 ' LCD enable pin connected to PICAXE output1 symbol RSin = outpin0 ' LCD RegSel pin connected to PICAXE output0 ' *** Variables *** symbol char = b1 ' character to be sent to LCD symbol index = b2 ' used as counter in For-Next loops symbol RSbit = b3 ' used to set up for cmd/txt byte type symbol temp = b4 ' used to manipulate char without destruction ' *** Directives *** '#com2 #picaxe 14M ' *** Data *** data 0,(" PICAXE-14M LCD ") ' store the text in EEPROM memory - LCD line 1 data 16,(" Serial Display ") ' store the text in EEPROM memory - LCD line 2 ' *** Begin Main Program ******************************************************* setfreq m8 gosub Init ' initialize LCD do for index = 0 to 15 ' output data to 1st row of LCD read index, char ' read character from EEPROM gosub OutByte ' send character to LCD next index char = 192 ' instruction: go to start of 2nd row gosub OutByte ' send instruction to LCD for index = 16 to 31 ' output data to 2nd row of LCD read index, char ' read character from EEPROM gosub OutByte ' send character to LCD next index wait 1 ' pause 1/2 sec (8 MHz) char = 1 gosub OutByte ' clear display and go home wait 1 ' pause 1/2 sec (8 MHz) loop ' *** End Main Program - Subroutines Follow ************************************ ' *** Init Subroutine *** Init: pins = 0 ' clear all output lines pause 200 ' pause 200 mS for LCD initialization pins = 12 ' (48/4=12) set to 4-bit operation pulsout enable,1 ' send data pause 10 ' pause 10 mS pulsout enable,1 ' send again pulsout enable,1 ' send again (necessary in 4-bit mode) pins = 8 ' (32/4=8) set to 4-bit operation pulsout enable,1 ' send data. pulsout enable,1 ' send again (necessary in 4-bit mode) pins = 32 ' (128/4=32) set to 2 line operation pulsout enable,1 ' send data char = 12 ' turn off cursor gosub OutByte ' send instruction to LCD return ' *** OutByte Subroutine *** OutByte: if char<32 OR char>127 then RSbit = cmd ' set up for command byte else RSbit = txt ' set up for text byte endif temp = char / 4 ' because we're using DB5...DB2 temp = temp & %00111100 ' mask for DB5...DB2 pins = temp ' place high nibble of char onto pins RSin = RSbit ' RSin = 0 for command or 1 for text pulsout enable,1 ' send data temp = char * 4 ' because we're using DB5...DB2 temp = temp & %00111100 ' mask for DB5...DB2 pins = temp ' place low nibble of char onto pins RSin = RSbit ' RSin = 0 for command or 1 for text pulsout enable,1 ' send data return