' Hitachi HD44780 LCD controller chip in 4-bit mode Const HD44780_DB3 As Byte = 9 'Most significant bit Const HD44780_DB2 As Byte = 10 Const HD44780_DB1 As Byte = 11 Const HD44780_DB0 As Byte = 12 'Least significant bit Const HD44780_ENABLE As Byte = 13 'to pin 6 'Const HD44780_READWRITE As Byte = 14 '1=Read 0=Write, to pin 5 Const HD44780_REGSELECT As Byte = 15 '1=Data 0=Instruction, to pin 4 Const LCDREAD As Byte = 1 Const LCDWRITE As Byte = 0 Const LCDDATA As Byte = 1 Const LCDINSTRUCTION As Byte = 0 Sub Main() Delay (0.5) 'in case of rescue Call PutPin(26,0) ' green led on Call LCDInit() Call PutPin(26,1) ' green led off End Sub 'Main Sub LCDInit() Call PutPin(HD44780_ENABLE, bxOutputLow) Call PutPin(HD44780_REGSELECT, bxOutputLow) ' Call PutPin(HD44780_READWRITE, bxOutputLow) Call Sleep(0.050) 'for LCD chip startup Call LCDPut4Bits ( bx00110000 ) 'function set (assumes 8-bit at startup) Call PulseOut(HD44780_ENABLE, 0.00005, bxOutputHigh) Call Sleep(0.005) Call LCDPut4Bits ( bx00110000 ) 'function set (assumes 8-bit at startup) Call PulseOut(HD44780_ENABLE, 0.00005, bxOutputHigh) Call Sleep(0.00015) Call LCDPut4Bits ( bx00110000 ) 'function set (assumes 8-bit at startup) Call PulseOut(HD44780_ENABLE, 0.00005, bxOutputHigh) Call LCDPut4Bits ( bx00100000 ) 'Now actually sets to 4-bit mode Call PulseOut(HD44780_ENABLE, 0.00005, bxOutputHigh) Call LCDFunctionSet () 'sets the complete function set Call LCDDisplayOFF () Call LCDClearDisplay () Call LCDEntryModeSet () Call LCDDisplayON () End Sub Sub LCDFunctionSet8 () '8-bit, 2 lines, no-scroll, blinking cursor Call LCDSendCommand ( bx00111000 ) End Sub Sub LCDFunctionSet () '4-bit, 2 lines, no-scroll, blinking cursor Call LCDSendCommand ( bx00101000 ) End Sub Sub LCDDisplayON () Call LCDSendCommand ( bx00001111 ) End Sub Sub LCDDisplayOFF () Call LCDSendCommand ( bx00001000 ) End Sub Sub LCDEntryModeSet () Call LCDSendCommand ( bx00000110 ) End Sub Sub LCDClearDisplay () Call LCDSendCommand ( bx00000001 ) Call Sleep(0.0016) End Sub Sub LCDReturnHome () Call LCDSendCommand ( bx00000010 ) Call Sleep(0.0016) End Sub ' Left-pads bWidth string with spaces. Sub LCDPrintValueField(ByVal sSingle As Single, ByVal bDecimal As Boolean, ByVal bWidth As Byte) Dim bLength As Byte Dim sTmp As String If bDecimal = True Then sTmp = CStr(CLng(sSingle * 10.0)) Else sTmp = CStr(CLng(sSingle)) End If bLength = CByte(Len(sTmp)) If bDecimal = True Then bLength = bLength + 1 ' for decimal point End If Do While (bWidth - bLength) > 0 ' print right padding Call LCDSendCharacter( CByte(Asc(" ")) ) bWidth = bWidth -1 Loop If bDecimal = True Then Call LCDPrintString ( Mid(sTmp, 1, CInt(bLength - 2)) ) Call LCDSendCharacter( CByte(Asc(".")) ) Call LCDPrintString ( Mid(sTmp, CInt(bLength-1), 1) ) Else Call LCDPrintString (sTmp) End If End Sub Sub LCDPrintString (ByVal sString As String) Dim bLength As Byte Dim bCount As Byte Dim sChar As String * 1 bLength = CByte(Len(sString)) For bCount = 1 To bLength sChar = Mid(sString, CInt(bCount), 1) Call LCDSendCharacter( Asc(sChar) ) Next End Sub Sub LCDGotoRowCol (ByVal bRow As Byte, ByVal bCol As Byte) Call LCDSetDDRAMAddr ( bRow * 64 + bCol ) End Sub Sub LCDSetDDRAMAddr (ByVal bAddr As Byte) Call LCDSendCommand ( bx10000000 Or bAddr) 'bAddr is 7-bits End Sub Sub LCDSendCommand (ByVal bByte As Byte) Call PutPin(HD44780_REGSELECT, LCDINSTRUCTION) ' Call PutPin(HD44780_READWRITE, LCDWRITE ) Call PutPin(HD44780_ENABLE, bxOutputLow) Call LCDPutByte ( bByte ) Call PulseOut(HD44780_ENABLE,0.00005, bxOutputHigh) Call Sleep(0.00005) End Sub Sub LCDSendCharacter (ByVal bByte As Byte) Call PutPin(HD44780_REGSELECT, LCDDATA) ' Call PutPin(HD44780_READWRITE, LCDWRITE ) Call PutPin(HD44780_ENABLE, bxOutputLow) Call LCDPutByte ( bByte ) Call PulseOut(HD44780_ENABLE,0.00005, bxOutputHigh) Call Sleep(0.00005) End Sub ' 4-bit interface - ENABLE pulse expects WRITE mode Sub LCDPutByte (ByVal bByte As Byte) Dim bMask As Byte Call LCDPut4Bits (bByte) Call PulseOut(HD44780_ENABLE,0.00005, bxOutputHigh) Call LCDPut4Bits (bByte * bx00010000) End Sub Sub LCDPut4Bits (ByVal bByte As Byte) 'msb's only Dim bMask As Byte Dim bCount As Byte bMask = bx10000000 For bCount = 0 to 3 'Put most significant bits first Call PutPin(HD44780_DB3 + bCount, ((bByte And bMask)\bMask)) bMask = bMask \ 2 Next End Sub