SER_LCD.Bas
'
' Illustrates many capabilities of the K221 serial LCD interface
'
' Configures the interfacing LCD for 4X20 geometry.  This need only be done one time as
' this is saved in the K221 EEPROM
'
' Clears the LCD using the "?f" command.
' Writes some text.  Note the new line function "?n"
' Displays the user defined characters using commands ?0, ?1, etc
' Moves cursor to line 1, col 00 using commands "?y1" and "?x00"
' Clears lines 1, 2 and 3 using "?l" command.  Note "?j" is used for down cursor.
'
' Positions cursor at line 1 and displays the values of N and Word in decimal and in Hexadecimal.
'
' Defines new user defined characters consisting of no vertical lines, 1 vertical line,
' etc through 5 vertical lines.
'
' A bar scaled to 0 through 25 is displayed on line 3.  This might be used to graphically
' display a quantity.
'
' The PWM duty cycle is then set to 25 steps over the range of 00 to 250 using the ?B command.
' The duty cycle is also displayed on the LCD.
'
' The program then continually sets the two general purpose outs to 10 and 01 using the ?H and
' ?L commands.
'
' Written by.... Peter H Anderson, Baltimore, MD, Feb, '04

   Symbol N = B2
   Symbol OWord = W0
   Symbol OByte = B3

   					'These variables are used in Sub BarDemo
   Symbol NN = B8
   Symbol LeftOver = B9
   Symbol Num_5 = B10
   Symbol J = B11
   					'These variables are used in Sub 												'SerOutByteHex
   Symbol X = B8

   ' Variables used in PWMDemo
   Symbol Duty = B9

   High 0					' Be sure Tx Pin is idle for some time
   Pause 5000				' wait for PICAXE LCD to boot

   SerOut 0, T2400, ("?G420")			' configure LCD as 4 X 20
   SerOut 0, T2400, ("?f")			' clear the LCD and home the cursor

   Pause 100
   SerOut 0, T2400, ( "    PICAXE LCD?n")		' note new line


   SerOut 0, T2400, ("www.phanderson.com?n")

   SerOut 0, T2400, ("?0?1?2?3?4?5?6?7?n")		' display special characters

   Pause 2000				' pause to admire

   SerOut 0, T2400, ("?y1?x00")			' position cursor at beginning of row 1

   SerOut 0, T2400, ("?l?j?l?j?l?y1")		' clear lines 1, 2 and 3 and start at line 1
   					' note the use of down cursor command
   OWord = 12345

   For N = 0 to 25
      SerOut 0, T2400, ("?x00?y1")			' locate cursor to beginning of line 1

      SerOut 0, T2400, (#N)			' display in decimal
      SerOut 0, T2400, ("?t")

      OByte = N
      GoSub SerOutByteHex     			' display in hex
      SerOut 0, T2400, ("?t")


      SerOut 0, T2400, (#OWord, "?t")		' display a word in decimal

      GoSub SerOutWordHex			' and in hex
      SerOut 0, T2400, ("?n")

      OWord = OWord + 1

      SerOut 0, T2400, ("?g")			' beep
      Pause 1000
   Next

   SerOut 0, T2400, ("?D00000000000000000")	' define special characters
   Pause 200				' delay to allow EEPROM to program

   SerOut 0, T2400, ("?D11010101010101010")
   Pause 200

   SerOut 0, T2400, ("?D21818181818181818")
   Pause 200

   SerOut 0, T2400, ("?D31c1c1c1c1c1c1c1c")
   Pause 200

   SerOut 0, T2400, ("?D41e1e1e1e1e1e1e1e")
   Pause 200

   SerOut 0, T2400, ("?D51f1f1f1f1f1f1f1f")
   Pause 200


   SerOut 0, T2400, ("?c0")			' no cursor


   SerOut 0, T2400, ("?y3?x00?l")			' cursor to beginning of line 3 and clear line

   GoSub BarDemo

   Pause 200

   GoSub PWMDemo

   Pause 200


AGAIN:   					' continually bring outputs 5 & 4 hi and lo
   SerOut 0, T2400, ("?H5?L4")
   SerOut 0, T2400, (".")
   Pause 500
   SerOut 0, T2400, ("?L5?H4")
   SerOut 0, T2400, ("!")
   Pause 500
   GoTo AGAIN

BarDemo:

   For N = 0 to 25				' increasing bar
      SerOut 0, T2400, ("?y3?x00")
      NN = 4 * N
      Num_5 = NN / 5
      LeftOver = NN % 5
      For J = 1 to Num_5
         SerOut 0, T2400, ("?5")
      Next
      LeftOver = LeftOver + 48			' convert to a character
      SerOut 0, T2400, ("?", LeftOver)
   Next

   For N = 0 to 25				' decreasing bar
      SerOut 0, T2400, ("?y3?x00?l")
      NN = 4 * N
      NN = 100 - NN
      Num_5 = NN / 5
      LeftOver = NN % 5
      For J = 1 to Num_5
         SerOut 0, T2400, ("?5")
      Next

      LeftOver = LeftOver + 48			' convert to a character
      SerOut 0, T2400, ("?", LeftOver)
   Next

   Return

PWMDemo:
    SerOut 0, T2400, ("?f")			' clear the LCD
    SerOut 0, T2400, ("  PWM Duty Demo?n")
    For N = 0 to 25
       Duty = 10 * N
       SerOut 0, T2400, ("?l")
       OByte = Duty
       GoSub SerOutByteHex			' display the PWM in hex

       SerOut 0, T2400, ("?B")			' backlight control
       GoSub SeroutByteHex
       Pause 200
    Next

    SerOut 0, T2400, ("?f")			' clear the LCD
    SerOut 0, T2400, ("?B00")			' set PWM to 0

    Return


SerOutByteHex:
    X = OByte / 16				' high nibble
    X = X + 48				' add the character '0'

    If X <= 57 Then SerOutByteHex_1
       X = X + 7				' it is alphabetic; A, B, C, D, E, F

SerOutByteHex_1:
    SerOut 0, T2400, (X)

    X = Obyte % 16				' low nibble
    X = X + 48				' add the character '0'

    If X <= 57 Then SerOutByteHex_2
       X = X + 7				' it is alphabetic; A, B, C, D, E, F

SerOutByteHex_2:
    SerOut 0, T2400, (X)

    Return

SerOutWordHex:

    OByte = OWord / 256
    GoSub SerOutByteHex
    OByte = OWord % 256
    GoSub SerOutByteHex

    Return
