NetMedia 2X16 Serial LCD Interface with Parallax Basic Stamp 2


Program LCD_1.BS2

' Program LCD_1.BS2
'
' Parallax Basic Stamp 2
'
' Illustrates the use of various features of the NetMedia 2X16 LCD with BackLight.
'
'
'  BS2 				    Serial LCD
'	
'  P7 (term 12) ------------------ RX
'                        +5 ------ +5	(To power LCD)
'                        GRD ----- GND
'                        +5 ------ LEDSV+  (LED Back Light - 135 mA at full backlight
'
' Initalizes LCD by setting the geometry to a 16X2 LCD and resets LCD.  Illustrates various 
' backlight and contrast settings.  Illustrates how to modify the horizonntal tab size and 
' use of the tab.  Illustrates how to change the cursor settings and erase text using the 
' backspace.  Shows use of the vertical tab and turning off and turning on the display while
' retaining the data on the screen.
'
' This routine does not illustrate the definition and the display of special characters.
'
' copyright, Peter H. Anderson, Baltimore, MD, Nov, '02

      N9600 CON $4000 + 84	' Define Baud Rate as 9600 Inverted
      TXPin CON 7
      
      N Var Byte

      NN Var Byte		' gerneral purpose variable used in Subs
      X Var Byte		' X and Y are used to transfer data to Subs
      Y var Byte

Main:

      GoSub LCDInit

      ' backlight demonstration
      Y = 0
      GoSub LCDClrLine		' clear line 0
      
      SerOut TxPin, N9600, 10, ["BackLight Demo"]
      
      Y = 1
      GoSub LCDClrLine		' clear line 1
      
      For N = 0 to 25
         X = 10 * N		' backlight value
         GoSub LCDSetBackLight
         SerOut TXPin, N9600, 10, ["Backlight = ", Dec X]	' display back light value
         Pause 1000
         Y = 1			' row 1
         GoSub LCDClrLine
      Next   

      GoSub LCDClrScr
      X = 100
     	GoSub LCDSetBacklight	' set the backlight for something that pleases
      
      ' contrast demo

      Y = 0
   	GoSub LCDClrLine		

      SerOut TxPin, N9600, 10, ["Contrast Demo"]
      
      Y = 1
      GoSub LCDClrLine		' clear row 1
      
      For N = 0 to 25
         X = 10 * N		' contrast value
         GoSub LCDSetContrast
         SerOut TXPin, N9600, 10, ["Contrast = ", Dec X]	' display contrast value
         Pause 1000
         Y = 1			' row 1
         GoSub LCDClrLine
      Next   

      GoSub LCDClrScr

      ' tab demo

      X = 0
      GoSub LCDSetContrast	' set contrast for something comfortable
 	SerOut TxPin, N9600, 10, ["Tab Demo"]
      Pause 2000
   
      X = 4
      GoSub LCDSetTab		' set tab to four positions
      GoSub LCDClrScr
   
   	For N = 1 to 4		' now display some numbers separated by tabs
	   SerOut TXPin, N9600, 10, [Dec (N+8)]
         GoSub LCDTab
      Next

      X = 1
      GoSub LCDClrLine		' locate to second line
  
	X = 2
	GoSub LCDSetTab		' set tab to 2

	For N = 1 to 8
	   SerOut TXPin, N9600, 10, [Dec (N)]
         GoSub LCDTab   
      Next   
   
      Pause 2000

      GoSub LCDClrScr

      ' cursor demo

      SerOut TXPin, N9600, 10, ["Cursor Demo"]
    
      For N = 0 to 3
          X = 1
          GoSub LCDClrLine	' clear line 1
          X = N
          GoSub LCDSetCursorStyle
          SerOut TXPin, N9600, 10, ["Cursor Style ", Dec X]
          Pause 2000
      Next
   
      X = 0
      GoSub LCDSetCursorStyle		' turn off cursor

      ' demo of misc items - vertical tab and display on and off

      GoSub  LCDClrScr
      SerOut TXPin, N9600, 10, ["Misc Stuff"]

      Pause 3000
     
      ' now, slowly erase it

      X = 1		' number of backspaces
      GoSub LCDBackSpaces
      SerOut TXPin, N9600, 10, [" "]

      For N = 2 to 10
	   X = 2	' two back spaces
         GoSub LCDBackSpaces
         SerOut TXPin, N9600, 10, [" "]
         Pause 500
      Next
   
   GoSub LCDClrScr
   
   For N = 1 to 32
       SerOut TXPin, N9600, 10, [($21 - 1 + N)]
      If (N & $01) = 0 Then Main1
          X = 1
          GoSub LCDBackSpaces	' if N is odd 
Main1: 
      GoSub LCDVertTab
      Pause 300
   Next      

   For N = 1 to 10		' flash LCD on and off
      X = 0
      GoSub LCDOnOff
      Pause 300
      X = 1
      GoSub LCDOnOff
      Pause 300
   Next

   GoSub LCDClrScr		' sign off
   X = 4
   GoSub LCDSetTab		' set tab size to 4

   For N = 1 to 4
      SerOut TXPin, N9600, 10, ["73"]
      GoSub LCDTab
   Next

   Y = 1
   GoSub LCDClrLine
   SerOut TXPin, N9600, 10, ["phanderson.com"]

   Pause 5000

   GoTo Main
      
LCDInit:
   GoSub LCDSetGeometry
   GoSub LCDReset
   Return
  
LCDReset:
   SerOut TxPin, N9600, 10, [14]
   Pause 1000
   Return

LCDSetGeometry:
   SerOut TxPin, N9600, 10, [15, 16, $80, $c0, $80, $80]
   Return
 
LCDSetBackLight:	' value is passed in X
   SerOut TxPin, N9600, 10, [20, X]
   Return
   
LCDSetContrast:	' value is passed in X
   SerOut TxPin, N9600, 10, [19, X]
   Return

LCDClrScr:
   SerOut TxPin, N9600, 10, [12]
   Return

LCDNewLine:
   SerOut TxPin, N9600, 10, [10]
   Return

LCDCR:
   SerOut TxPin, N9600, 10, [13]
   Return

LCDTab:
   SerOut TxPin, N9600, 10, [9]
   Return

LCDVertTab:
   SerOut TxPin, N9600, 10, [11]
   Return

LCDBackSpaces:	' value in X, NN is general purpose byte   
   For NN = 1 to X
      SerOut TxPin, N9600, 10, [8]  
   Next
   Return

LCDSetTab:  ' Val passed in X
   X = (X ^ $FF) + 1	' takes twos compliment
   SerOut TxPin, N9600, 10, [16, X]
   Return

LCDSetCursorPos:  ' Y is Row, X is col
   SerOut TxPin, N9600, 10, [17, Y, X]
   Pause 100
   Return   

LCDClrLine:  ' Y is Row, X is Col, NN is general purpose Byte
   X = 0	'  Col 0
   GoSub LCDSetCursorPos	' start at the beginning of the line

   For NN = 0 to 15
      SerOut TxPin, N9600, 10, [" "]     
   Next
   X = 0
   GoSub LCDSetCursorPos ' leave the cursor at the beginning of the line
   Return

LCDSetCursorStyle:   ' Val X  in the range of 0 - 3
   ' X = 0, 1 No cursor, X = 2 solid, X = 3 blinking
   SerOut TxPin, N9600, 10, [21, ($0c+X)]   
   Return

LCDOnOff:   ' Val X   0 = off, 1 = on
   If X <> 0 Then LCDOnOff1
      SerOut TxPin, N9600, 10, [21, $08]
      GoTo LCDOnOff2
LCDOnOff1:
      SerOut TxPin, N9600, 10, [21, $0c]
LCDOnOff2:   
    Return