You are on page 1of 7

LCD

Dim BasePort As Integer

Dim ctrl As Integer


'control port is active low.
'For LCD following lines are used
'control.0 = nStrobe 'Assigned to E
'control.1 = nAutoLF 'Not assigned
'control.2 = nInit 'Not assigned
'control.3 = nSelPrinter 'Assigned to RS

Dim Staus As Integer 'unused for LCD


'Stat.3 = nError
'Stat.4 = Select
'Stat.5 = PaperOut
'Stat.6 = nAck
'Stat.7 = Busy

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Public Function Hex2Long(hexstr As String)

Dim i As Integer
Dim c As Long
Dim d As Integer

hexstr = UCase(hexstr)
c=0
For i = 1 To Len(hexstr)

c = c * 16
d = Asc(Mid(hexstr, i, 1))
If d >= &H30 And d <= &H39 Then
d = d - &H30
Else
d = d - &H41 + 10
End If
c=c+d
Next

Hex2Long = c

End Function
Private Sub LCDInit()

ctrl = 1 'E should be zero (ctrl.0 = 1)


RS_Low
LCDStrobe &H30
Sleep 50
LCDStrobe &H30
Sleep 1
LCDStrobe &H30
Sleep 1

RS_Low
LCDStrobe &H38 '8 bit interface. for 4 bit interface, use &H28
Sleep 1
LCDStrobe &H8 ' Display off
Sleep 1
LCDStrobe &H1 'clear screen
Sleep 1
LCDStrobe &H6 'Increment, no shift
Sleep 1
LCDStrobe &HE 'display on, show cursor, no blink

RS_Low
If (ctrl Mod 2) = 0 Then
ctrl = ctrl + 1 'E=0, or ctrl.0 = 1
End If

End Sub

Private Sub LCDStrobe(ByVal dbyte As Integer)

Dim i As Integer

'Initially, E should be zero that is ctrl.0 = 1


i = ctrl Mod 2
If i = 0 Then
ctrl = ctrl + 1
End If
'RS at required value and E = 0
Hwinterface1.OutPort BasePort + 2, ctrl

'force E to high (ctrl.0 to low)


ctrl = ctrl - 1
Hwinterface1.OutPort BasePort + 2, ctrl
microSleep 10 'at least 4 microsecs

Hwinterface1.OutPort BasePort, dbyte


microSleep 5
'Toggle E down (ctrl.0 to high) to transfer data
ctrl = ctrl + 1
Hwinterface1.OutPort BasePort + 2, ctrl
microSleep 10

Hwinterface1.OutPort BasePort, 0 'clear the data

End Sub

Private Sub LCDWrite(ByVal dbyte As Integer, ByVal line As Integer, ByVal pos As Integer)
'dbyte is ascii char to be shown
'line is 1 or 2
'pos is 1 to 16

Dim addr As Integer


Dim data As Integer

dbyte = dbyte Mod 128


pos = (pos - 1) Mod 16

If line = 1 Then
addr = &H80 + pos
ElseIf line = 2 Then
addr = &HC0 + pos
Else
addr = &H80 + pos
End If

'command - DDRam address


RS_Low
LCDStrobe addr

'data - Data to be displayed


RS_High
LCDStrobe dbyte

End Sub

Private Sub microSleep(n As Integer)


'n > 0, n is in micro seconds, approximately

Dim i As Integer
Dim j As Integer
Dim c As Integer

If n <= 0 Then
n=1
ElseIf n > 1000 Then
n = 1000
End If

For i = 1 To n
For j = 1 To 100
'change this 100 to higher value if your system is very fast
'100 was ok on a Celeron 1.8GHz CPU
'You may need to change only if LCD does not function well.
c = c 'dummy.
Next
Next

End Sub

Private Sub RS_High()

'RS-x-x-E: 1-0-0-0
ctrl = ctrl Mod 8 'make bit four zero (inverted)

End Sub

Private Sub RS_Low()

'control port is active low.


'also, there is hardware inversion on this bit
'So invert all bits

'RS-x-x-E: 0-0-0-0
'keep other bits unchanged
ctrl = ctrl Mod 8
'make control.3 high
ctrl = ctrl + 8

End Sub

Private Sub Command1_Click()

Dim i As Integer
Dim c As Integer
Dim s As String

LCDInit
Sleep 10

s = "Welcome"
For i = 1 To Len(s)
c = Asc(Mid(s, i, 1))
LCDWrite c, 1, i
Next
Text1 = s

s = ""
c = &H41 'Ascii A
For i = 1 To 16
LCDWrite c, 2, i
s = s & Chr(c)
c=c+1
Next
Text2.Text = s

End Sub

Private Sub Command2_Click()


s = Text1.Text
s = Left(s, 16)
If Len(s) < 16 Then
s = s & Space(16 - Len(s))
End If

For i = 1 To Len(s)
c = Asc(Mid(s, i, 1))
LCDWrite c, 1, i
Next

s = Text2.Text
s = Left(s, 16)
If Len(s) < 16 Then
s = s & Space(16 - Len(s))
End If
For i = 1 To Len(s)
c = Asc(Mid(s, i, 1))
LCDWrite c, 2, i
Next

End Sub

Private Sub Form_Load()


Dim s As String

BasePort = &H3BC 'Normally, LPT1 is at 0x378


s = GetSetting("C-Quad", "LCD", "BasePort", "378")
BasePort = Hex2Long(s)

End Sub

Private Sub Text1_Change()

Dim i As Integer

s = Left(Text1.Text, 16)
If Len(s) < 16 Then
s = s & Space(16 - Len(s))
End If

For i = 1 To Len(s)
LCDWrite Asc(Mid(s, i, 1)), 1, i
Sleep 10
Next

End Sub

Private Sub Text1_LostFocus()


If Len(Text1.Text) > 16 Then
Text1.Text = Left(Text1.Text, 16)
End If

End Sub

Private Sub Text2_Change()

Dim i As Integer

s = Left(Text2.Text, 16)
If Len(s) < 16 Then
s = s & Space(16 - Len(s))
End If

For i = 1 To Len(s)
LCDWrite Asc(Mid(s, i, 1)), 2, i
Sleep 10
Next

End Sub

Private Sub Text2_LostFocus()


If Len(Text2.Text) > 16 Then
Text2.Text = Left(Text2.Text, 16)
End If
End Sub
Private Sub Text3_LostFocus()

BasePort = Hex2Long(Text3.Text)
SaveSetting "C-Quad", "LCD", "BasePort", Text3.Text

End Sub

You might also like