' ============================================================================= ' Program..... LCD_595.bas ' Author...... Jon Williams (jonwms@aol.com) ' Copyright... Copyright (c) 1999 Jon Williams ' ' Started..... 22 OCT 1999 ' Updated..... 19 NOV 1999 ' ============================================================================= ' -----[ Program Description ]------------------------------------------------- ' ' This BasicX module provides low-level support for connecting a ' character-based LCD to the BasicX through a 74x595 shift register. ' ' The connection is four-bit mode. It is up to the user to connect proper ' power, ground and contrast connections. It is not possible to read back ' from the LCD using this interface. ' ' Connections: ' NC = Not Connected ' PU = Pulled Up to +5V through 10K resistor ' ' BX-24 74HC595 LCD Notes ' ------- ------- ------- ---------------- ' 1 NC ' 2 6 (E) ' 3 4 (RS) ' 4 11 (D4) ' 5 12 (D5) ' 6 13 (D6) ' 7 14 (D7) ' 8 GND ' 9 NC ' 10 PU ' Pin 20 11 Shift clock ' Pin 18 12 Output latch ' 13 GND ' Pin 19 14 Shift data ' 15 NC ' 16 Vcc ' -----[ Revision History ]---------------------------------------------------- ' ' 22 OCT 1999 : Ported from Parallax PBASIC ' 27 OCT 1999 : Fine-tuned ' 19 NOV 1999 : Updated docs; reconfigured for BX-24 ' -----[ I/O Definitions ]----------------------------------------------------- Private Const LCD_clock As Byte = 20 ' pin connections to 74x595 Private Const LCD_data As Byte = 19 Private Const LCD_latch As Byte = 18 ' -----[ Subroutines ]--------------------------------------------------------- Public Sub LCDInit(ByVal multiLine As Byte) Dim temp As Byte Call PutPin(LCD_clock, bxOutputLow) ' setup connection pins Call PutPin(LCD_data, bxOutputLow) Call PutPin(LCD_latch, bxOutputLow) Call Sleep(0.25) ' allow LCD to self-init Call LCDPutByte(&H3, LCDcmd) Call Sleep(0.006) Call LCDPutByte(&H3, LCDcmd) Call Sleep(0.002) Call LCDPutByte(&H3, LCDcmd) Call LCDPutByte(&H2, LCDcmd) ' 4-bit interface If (multiLine = LCDMulti) Then Call LCDPutByte(&H28, LCDcmd) ' multi-line mode End If Call LCDPutByte(&HC, LCDcmd) ' disp on, crsr off, blink off Call LCDPutByte(&H6, LCDcmd) ' inc crsr, no disp shift Call LCDPutByte(LCDclear, LCDcmd) ' get ready to use End Sub ' ----------------------------------------------------------------------------- Public Sub LCDPutByte(ByVal theByte As Byte, ByVal mode As Byte) Dim temp As Byte temp = theByte And &HF0 ' grab the high nibble If mode = 1 Then ' set RS bit to mode temp = temp Or &H8 Else temp = temp And &HF7 End If temp = temp Or &H4 ' blip the E line Call ShiftOut595(temp) temp = temp And &HFB Call ShiftOut595(temp) temp = theByte * 16 ' grab the low nibble If mode = 1 Then ' set RS to mode temp = temp Or &H8 Else temp = temp And &HF7 End If temp = temp Or &H4 ' blip the E line Call ShiftOut595(temp) temp = temp And &HFB Call ShiftOut595(temp) End Sub ' ----------------------------------------------------------------------------- Private Sub ShiftOut595(ByVal theByte As Byte) ' Send data to LCD through 74x595 shift register Dim theBit As Byte Dim mask As Byte mask = bx10000000 ' start with MSB Do theBit = (theByte And mask) \ mask ' isolate the bit Call PutPin(LCD_data, theBit) ' spit it out Call PulseOut(LCD_clock, 0.0001, 1) ' clock the bit mask = mask \ 2 ' shift mask bit right Loop While (mask > 0) Call PulseOut(LCD_latch, 0.0001, 1) ' latch data into the 595 End Sub