Controlling 16x2 LCD module using VB6.

udaywali 0 Tallied Votes 543 Views Share

Here is some code that will help you connect a LCD module to Parallel port of a computer.
Almost any 16x2 LCD module should work. It has complete VB6 code. It works on Windows XP very well. I have used hwinterface.ocx from Logix4u.net that provides basic Inport and OutPort functionality (Thanks to them, made the work lot more fun).

Requirements:
a) Parallel Port connector
b) LCD Module
c) Power supply (5V, 50mA should be ok)
d) hwinterface.ocx downloadable from the net, free. (Visit logix4u.net for more info)

You will have to connect the LCD to Parallel port. Pin map is given below. You can get this info from several other locations on the net.

LCD - ParallelPort
RS - Pin 17
R/W - Gnd
E - Pin 1
D7-D0 - Pin8-Pin2

Other connections to LCD include back lit and contrast control. See LCD manual for details.

Copy the hwinterface.ocx to c:\windows\System32
From start menu, click on Run
In the text box key in the following
regsvr32 c:\windows\system32\hwinterface.ocx
You should get a message saying registered successfully.

Create a new project in VB6. Add a form.
Add required controls (text1, text2, text3, command1 and command2)
Add the code snippet as appropriate.

LPT port is generally located at 0x378. However, it may be moved to other ports as well. For example, on my laptop, it is located on 0x3BC. If in doubt, go to device manager and look for resources used by LPT.

Run the application (VB6)

Keyin the port number (in hex format itself). Click on command1 button to initialize the LCD. You should immediately see Welcome in text1 and ABCE... in text2. LCD module will also contain the exact same data displayed.

You can keyin any thing in text1 or text2. It is copied to LCD Module.

If in doubt, mail me. You can also contact me at udaywali@cquad.co.in

Happy interfacing!

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