' LCDAdvanced.bas ' By C.D.Odom on 3.27.06 ' chris_odom@georgeschool.org ' www.basicxandrobotics.com ' A somewhat advanced application to print text and numbers to ' NetMedia's 2x16 LCD screen using subprograms. This app also ' displays data from a Sharp IR range finder. See LCDSimple.bas ' for a more straightforward application. ' NB: Set the project's Maximum String Length to 64 characters. Option Explicit ' Global Variables ' ///// Pin Constants /////// Const LCDOutputPin as Byte = 20 ' RAMB pin 15 Const SensorPin as Byte = 13 ' Sharp IR Sensor in RAMB pin 8 ' ////// LCD Constants ///// ' LCD Control Constants ' The following are ASCII codes sent to the LCD. These are NOT pin numbers! Const Clear_LCD as Byte = 12 Const Set_Cursor as Byte = 17 Const Contrast as Byte = 19 Const BackLight as Byte = 20 ' Power Backlight with any RAMB power pin ' LCD Queue Info Dim Com3In(1 to 15) as Byte ' Create an input data array for the LCD input queue Dim Com3Out(1 to 40) as Byte ' Create an output data array for the LCD output queue ' ////// Main /////////// Public Sub Main() Dim Message as String Dim ScrollMessage as String Dim MessageLength as Integer Dim SensorValue as Integer Dim i as Integer Dim R as Byte, C as Byte Dim Column as Byte Dim RND_Brightness as Byte Call InitializeLCD ' Print 1-line text message to LCD screen Message = "BasicX Rocks!" Call DisplayOneLine(Message) Call Delay(2.0) ' Delay to read message ' Print 2-line text message to LCD screen Call DisplayTwoLine("Imagine the", "possibilities") Call Delay(2.0) ' Delay to read message ' Print 1-line text message at specified Row and Column Message = "Where am I?" Call DisplayRowCol(Message, 1, 3) Call Delay(2.0) ' Delay to read message ' Animation: watch the arrow bounce! For i = 1 to 2 ' Do it twice For C = 0 to 15 ' Column loop Call DisplayRowCol(">", 0, C) ' Display the right arrow Call Delay(0.25) ' A delay allows you to see the graphic Next For C = 15 to 1 Step -1 ' Column loop Call DisplayRowCol("<", 1, C) ' Display the left arrow Call Delay(0.25) ' A delay allows you to see the graphic Next Next ' Print scrolling message to LCD screen. ' String literals must be between 1 and 64 characters. ' Messages may be longer than 64 characters if you concatenate one or more strings. ' Try some of these messages: Message = "<--->" ' Message = "A" ' Message = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ' Message = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ... 1 2 3 4 5 6 7 8 9 10" ' Message = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" & "0123456789" & "abcdefghijklmnopqrstuvwxyz" MessageLength = Len(Message) For i = 0 to (MessageLength + 14) ' Scroll message to right-hand-side of screen then stop: If (i <= 15) Then Column = 15 - CByte(i) ScrollMessage = Mid(Message, 1, i+1) Else ' when displayed message becomes small: If (i > MessageLength) Then Column = 0 ScrollMessage = Mid(Message, (i-14), (MessageLength - (i - 15))) Else ' Scroll the middle of the message: Column = 0 ScrollMessage = Mid(Message, i-14, 16) End If End If Call DisplayRowCol(ScrollMessage, 0, Column) Call Delay(0.25) ' Recommended delay time Next ' Explode the screen! Output random LCD brightness levels. Call PutQueueStr(Com3Out, Chr(Clear_LCD)) For i = 1 to 100 RND_Brightness = CByte(255.0 * Rnd) Call PutQueueStr(Com3Out, Chr(BackLight) & Chr(RND_Brightness)) Call Delay(0.04) Next Call PutQueueStr(Com3Out, Chr(BackLight) & Chr(255)) ' Full brightness ' Continually print sensor values to computer and LCD screens Do SensorValue = GetADC(SensorPin) Debug.Print "Sensor Value = " & CStr(SensorValue) ' Print to computer Call DisplayValue(SensorValue) ' Print to LCD Call Delay(0.3) ' Delay to read value Loop End Sub ' /////////// InitializeLCD ////////// Public Sub InitializeLCD() ' Open the COM 3 Port Call OpenQueue(Com3In, 15) ' Create an input queue Call OpenQueue(Com3Out, 40) ' Create an output queue Call DefineCom3(0, LCDOutputPin, bx1000_1000) ' Define Com3 parameters Call OpenCom(3, 9600, Com3In, Com3Out) ' Inititalize Com3 port Call Delay(0.7) ' Allow the LCD time to power up ' Set the LCD's Contrast and Backlight Levels Call PutQueueStr(Com3Out, Chr(Contrast) & Chr(0)) ' 0 = High, 255 = Low Call PutQueueStr(Com3Out, Chr(BackLight) & Chr(255)) ' 0 = Dim, 255 = Bright End Sub ' /////////// DisplayOneLine ////////// Public Sub DisplayOneLine(ByVal MSG as String) ' Clear the screen, set cursor to top-left, and print simple one-line text message Call PutQueueStr(Com3Out, Chr(Clear_LCD)) Call PutQueueStr(Com3Out, Chr(Set_Cursor) & Chr(0) & Chr(0)) Call PutQueueStr(Com3Out, MSG) End Sub ' /////////// DisplayTwoLine ////////// Public Sub DisplayTwoLine(ByVal MSG1 as String, ByVal MSG2 as String) ' Clear the screen, set cursor, and print two-line text message Call PutQueueStr(Com3Out, Chr(Clear_LCD)) Call PutQueueStr(Com3Out, Chr(Set_Cursor) & Chr(0) & Chr(0)) Call PutQueueStr(Com3Out, MSG1) Call PutQueueStr(Com3Out, Chr(Set_Cursor) & Chr(1) & Chr(0)) Call PutQueueStr(Com3Out, MSG2) End Sub ' /////////// DisplayRowCol ////////// Public Sub DisplayRowCol(ByVal MSG as String, ByVal Row as Byte, ByVal Col as Byte) ' Clear the screen, set cursor to user-defined row and column, and then print message Call PutQueueStr(Com3Out, Chr(Clear_LCD)) Call PutQueueStr(Com3Out, Chr(Set_Cursor) & Chr(Row) & Chr(Col)) Call PutQueueStr(Com3Out, MSG) End Sub ' /////////// DisplayValue ////////// Public Sub DisplayValue(ByVal Value as Integer) ' Print text message and sensor data to LCD screen Call PutQueueStr(Com3Out, Chr(Clear_LCD)) Call PutQueueStr(Com3Out, Chr(Set_Cursor) & Chr(0) & Chr(2)) Call PutQueueStr(Com3Out, "Sensor Value") Call PutQueueStr(Com3Out, Chr(Set_Cursor) & Chr(1) & Chr(6)) Call PutQueueStr(Com3Out, CStr(Value)) End Sub