Shop OBEX P1 Docs P2 Docs Learn Events
4x20 display sample code — Parallax Forums

4x20 display sample code

Aaron WallAaron Wall Posts: 31
edited 2009-03-30 02:04 in BASIC Stamp
Does any one have a sample program with a stamp controlling the 4x20 display in the Accessory page? I learn much better by tinkering with code than reading through the manual.

Thanks,
Aaron

Comments

  • SRLMSRLM Posts: 5,045
    edited 2009-02-23 06:31
    Nearly every product Parallax sells has sample code available on the product page. Take a look there.
  • Aaron WallAaron Wall Posts: 31
    edited 2009-02-23 15:46
    That was the first place I looked. They have 3 pdf files on there, but I didn't see anything about sample code.
  • sylvie369sylvie369 Posts: 1,622
    edited 2009-02-23 15:49
    Hmm. On the product page for the 4x20 LCD I see
    Parallax web gremlins said...
    Downloads:
    Parallax Serial LCD Documentation v2.0 (.pdf)
    Javelin Stamp Example Code (.zip)
    BASIC Stamp 1 and 2 Example Codes (.zip)
    SX/B Serial LCD Demo (.SXB)

    The BS1/2 .zip file contains several files of sample code. In addition, the .pdf has useful little code snippets - all I needed to figure the thing out - plus links to sample code.
  • Aaron WallAaron Wall Posts: 31
    edited 2009-02-24 05:12
    Upon further investigation, there are two 4x20 displays on the list. I'm referring to the one with the keypad interface. The display design is more what I'm looking for.
  • FranklinFranklin Posts: 4,747
    edited 2009-02-26 02:39
    It looks like the code Sylvie pointed to will work for either.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • Steve88WSteve88W Posts: 2
    edited 2009-03-02 03:19
    You can use my code to learn from.
    I did not buy mine from Parallax, but it is a serial 4X20 LCD with keypad conrtoller built in so the code should work.

    ' Serial LCD Display project
    ' Steve White 2009
    ' LCD Module with I2C/Serial Interface and Keypad Control
    ' My examples have my output pin1 from the microcontroller to the RX pin on the LCD
    ' Make sure you have a fresh battery for the Basic Stamp!
    

    ' Visit http:\\www.stevesgeekprojects.blogspot.com for pictures of my LCD setup.
    

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    

        'COMMANDS
    'Prefix Command Parameter Description
    '$FE    $00    null      No operation
    '$FE    $01    1 Byte    Changing the I2C Slave Address
    '$FE    $02    1 Byte    Changing BAUD Rate
    '$FE    $03    1 Byte    Set Backlight Brightness
    '$FE    $04    1 Byte    Set Contrast
    '$FE    $05    None      Save Splash/Startup Screen
    '$FE    $06    None      Display Firmware Version Number
    '$FE    $07    None      Display Serial Baud Rate
    '$FE    $08    None      Display I2C Address
    '$FE    $09    Null      No operation
    '$FE    $0A    None      Turn ON Display
    '$FE    $0B    None      Turn Off Display
    '$FE    $0C    2 bytes   Set Cursor Position
    '$FE    $0D    None      HOME Cursor
    '$FE    $0E    None      Turn ON Underline Cursor
    '$FE    $0F    None      Turn Off Underline Cursor
    '$FE    $10    None      Move Cursor Left One Space
    '$FE    $11    None      Move Cursor Right One Space
    '$FE    $12    None      Turn ON Blinking Cursor
    '$FE    $13    None      Turn Off Blinking Cursor
    '$FE    $14    None      Clear Screen
    '$FE    $15    Variable  Print String
    '$FE    $16    1 Byte    Init Horizontal Bar Graph
    '$FE    $17    4 bytes   Draw Horizontal Bar Graph
    '$FE    $18    None      Init Vertical Bar Graph
    '$FE    $19    4 bytes   Draw Vertical Bar Graph
    '$FE    $1A    9 bytes   Load Custom Characters
    '$FE    $1B    None      READ Keypad
    

      'Brightness/Contrast
    'Parameter     Length   Description
    '[noparse][[/noparse]brightness]  1 Byte   Set backlight brightness (0 – 250)      $FE, $03
    '[noparse][[/noparse]contrast]    1 byte   Set LCD contrast (0 – 100)              $FE, 04
    

    'Set Cursor Position  $FE $0C [noparse][[/noparse]col] [noparse][[/noparse]row]  (0-3) AND col (0-19)
    'Example:   SEROUT 1, 84, 25, [noparse][[/noparse]$FE, $0C, 2, 1, "text"]
    '= transmit On PIN1, 9600 Baud, 25 type speed, row 3, col 2, LCD shows "text" On screen
    

    'Clear Screen $FE $14
    'Example: SEROUT 1, 84, [noparse][[/noparse]$FE $14]   where 1 is the pin and 84 is the baud rate
    

    '<<<<<<<<<<<<<<<<< BEGIN TEXT CODE >>>>>>>>>>>>>>>>>>>>>>
    PAUSE 2000    'I like to give the LCD 2 seconds to initialize
    SEROUT 1, 84, [noparse][[/noparse]$FE, $0A]   'Ensures that the display is on
    PAUSE 100
    SEROUT 1, 84, [noparse][[/noparse]$FE, $14]    'Ensures that the screen is cleared
    PAUSE 100
    SEROUT 1, 84, [noparse][[/noparse]$FE, $03, 150] 'Sets the brightness (0-250)  *currently at 150
    PAUSE 200
    SEROUT 1, 84, 85, [noparse][[/noparse]"Hello World!"]    'Some text sent to the LCD, defaults to row 0, col 0
    PAUSE 1500
    SEROUT 1, 84, 25, [noparse][[/noparse]$FE, $0C, 1, 0, "This LCD is awesome"]  'Moves the cursor to row 2, col 1
    PAUSE 1000
    SEROUT 1, 84, 25, [noparse][[/noparse]$FE, $0C, 3, 1, "and easy to use"]      'Moves the cursor to row 4, col 2
    PAUSE 1500        'waits 1.5 seconds
    SEROUT 1, 84, [noparse][[/noparse]$FE, $14]  'Clears the screen
    SEROUT 1, 84, 95, [noparse][[/noparse]$FE, $0C, 1, 2, "SLOW TEXT HERE"]     'the "95" is the type speed  (higher value = slower)
    PAUSE 4000        'waits 4 seconds
    SEROUT 1, 84, [noparse][[/noparse]$FE, $14]  'Clears the screen
    PAUSE 200
    '<<<<<<<<<<<<<<<<< END TEXT CODE >>>>>>>>>>>>>>>>>>>>>>
    

    '<<<<<<<<<<<<<<<<< BEGIN ASCII ART CODE >>>>>>>>>>>>>>>
    SEROUT 1, 84, [noparse][[/noparse]$FE, $0C, 0, 2,"    _     _ "]              'this area shows an ASCII image
    SEROUT 1, 84, [noparse][[/noparse]$FE, $0C, 1, 2,"  o' ),=.( `o"]             'Check the manual to make sure the
    SEROUT 1, 84, [noparse][[/noparse]$FE, $0C, 2, 2,"     (o-o)"]                'characters you are using are included on
    SEROUT 1, 84, [noparse][[/noparse]$FE, $0C, 3, 2,"-ooO--(_)--Ooo-"]           'the LCD carrier board chip
    PAUSE 2000
    '<<<<<<<<<<<<<<<<< END ASCII ART CODE >>>>>>>>>>>>>>>>>
    

    '<<<<<<<<<<<<<<<<< BEGIN GRAPH CODE >>>>>>>>>>>>>>>>>>>
    ' Ok, let's draw a Vertical Bar Graph
    SEROUT 1, 84, [noparse][[/noparse]$FE, $14]    'Ensures that the screen is cleared
    PAUSE 30
    SEROUT 1, 84, 95, [noparse][[/noparse]$FE, $0C, 1, 3, "Vertical Bars"]
    PAUSE 1500     '1.5 second pause
    SEROUT 1, 84, [noparse][[/noparse]$FE, $14]    'Ensures that the screen is cleared
    PAUSE 30
    ' Step One: Initialize the vertical bar graph code to the LCD  Command: $FE, $18
    SEROUT 1, 84, [noparse][[/noparse]$FE, $18]
    PAUSE 10
    ' Step Two: Tell the LCD where you want the bars  Command:  $FE $19 [noparse][[/noparse]Row 0-3][noparse][[/noparse]Col 0-19][noparse][[/noparse]Bar Height 1-4][noparse][[/noparse]Total Pixel Height 1-32]
    SEROUT 1, 84, [noparse][[/noparse]$FE, $19, 3, 0, 4, 32]    'this is a full bar on the left side of the screen
    PAUSE 1000     '1 second pause
    SEROUT 1, 84, [noparse][[/noparse]$FE, $19, 3, 2, 3, 20]    'adding another 2 1/2 character tall bar with a 1 col space between
    PAUSE 1000     '1 second pause
    SEROUT 1, 84, [noparse][[/noparse]$FE, $19, 3, 4, 1, 8]    'adding one more bar, 1 box high and two col space from last
    PAUSE 300
    SEROUT 1, 84, [noparse][[/noparse]$FE, $19, 3, 10, 1, 8]
    PAUSE 200
    SEROUT 1, 84, [noparse][[/noparse]$FE, $19, 3, 12, 3, 20]
    PAUSE 100
    SEROUT 1, 84, [noparse][[/noparse]$FE, $19, 3, 14, 4, 32]
    PAUSE 3000     '3 second pause
    

    ' Ok, let's draw a Horizontal Bar Graph
    SEROUT 1, 84, [noparse][[/noparse]$FE, $14]    'Ensures that the screen is cleared
    PAUSE 30
    SEROUT 1, 84, 95, [noparse][[/noparse]$FE, $0C, 1, 2, "Horizontal Bars"]
    PAUSE 1500     '1.5 second pause
    SEROUT 1, 84, [noparse][[/noparse]$FE, $14]    'Ensures that the screen is cleared
    PAUSE 30
    ' Step One: Initialize the Horizontal bar graph code to the LCD  Command: $FE, $16 (type = 0 for solid graph, 1 &#8211; for line graph)
    SEROUT 1, 84, [noparse][[/noparse]$FE, $16, 0]
    PAUSE 10
    ' Step Two: Tell the LCD where you want the bars  Command:  $FE $17 [noparse][[/noparse]Row][noparse][[/noparse]Col][noparse][[/noparse]Length in total bars][noparse][[/noparse]PixelColEnd]
    SEROUT 1, 84, [noparse][[/noparse]$FE, $17, 0, 0, 20, 100]    'this is a full bar on the top of the screen
    PAUSE 1000     '1 second pause
    SEROUT 1, 84, [noparse][[/noparse]$FE, $17, 1, 2, 10, 45]    '2nd row 3 cols over and 45 total pixels long (the 10 shows the max characters it can take up to)
    PAUSE 1000     '1 second pause
    SEROUT 1, 84, [noparse][[/noparse]$FE, $17, 3, 4, 4, 17]    '4th row, 5th col, 4 max character spaces, 17 pixels long
    PAUSE 3000     '3 second pause
    '<<<<<<<<<<<<<<<<< END GRAPH CODE >>>>>>>>>>>>>>>>>>>>>>
    

    '<<<<<<<<<<<<<<<<< BEGIN SCROLL CODE >>>>>>>>>>>>>>>>>>>>>>
    MessageStart DATA @ 2, "Scrolling Message  "         ' Scrolling Message + two spaces
    MessageEnd   DATA
    

    cursorStartROW    VAR     Byte                  ' First character location
    cursorStartCOL    VAR     Byte                  ' First character location
    head           VAR     Byte                  ' Start of displayed text
    tail           VAR     Byte                  ' End of displayed text
    pointer        VAR     Byte                  ' EEPROM address pointer
    character      VAR     Byte                  ' Stores a character
    

    SEROUT 1, 84, [noparse][[/noparse]$FE, $0A]   'Ensures that the display is on
    PAUSE 10
    SEROUT 1, 84, [noparse][[/noparse]$FE, $14]    'Ensures that the screen is cleared
    PAUSE 10
    

    cursorStartROW = 1                 ' Rightmost character in window
    cursorStartCOL = 19                ' Right side of start col
    head = 0                                     ' Initialize head and tail
    tail = 0                                     ' of message
    

    DO WHILE tail < (MessageEnd - MessageStart)  ' Scrolling loop
    

      SEROUT 1, 84, [noparse][[/noparse]$FE, $0C, cursorStartROW, cursorStartCOL]    ' Place window's right
      SEROUT 1, 84, [noparse][[/noparse]$FE, $0B]                        ' Turn off the display
      PAUSE 40                                  ' Let characters fade away
    

      FOR pointer = head TO tail                 ' Print new characters
        READ pointer + MessageStart, character
        SEROUT 1, 84, [noparse][[/noparse]character]
      NEXT
    

      SEROUT 1, 84, [noparse][[/noparse]$FE, $0A]                        ' Turn display back on
      PAUSE 120                                  ' Give the characters some time
    

      cursorStartCOL = cursorStartCOL - 1 MIN 1      ' MIN (number) where number = stop column
      tail = tail + 1                            ' Increment tail pointer
    

      ' Increment head pointer if tail pointer > window width.
      IF tail > (22) THEN head = head + 1 ELSE head = 0       '(22) = length of text area
    

    LOOP                'Loops until condition is met
    PAUSE 2000          '2 second pause
    

    '<<<<<<<<<<<<<<<<< END SCROLL CODE >>>>>>>>>>>>>>>>>>>>>>
    

    '<<<<<<<<<<<<<<<<< BEGIN BLINK TEXT CODE >>>>>>>>>>>>>>>>>>>>>>
    'To blink text is easy
    blink VAR Byte              'create the variable
    FOR blink = 1 TO 20
    SEROUT 1, 84, [noparse][[/noparse]$FE, $14]    'Ensures that the screen is cleared
    PAUSE 200
    SEROUT 1, 84, [noparse][[/noparse]$FE, $0C, 1, 3, "BLINKING TEXT"]
    PAUSE 200
    blink = blink +1          'when the count reaches 20, the loop stops and goes to the next line
    NEXT
    PAUSE 2000    '2 second pause
    

    '<<<<<<<<<<<<<<<<< END BLINK TEXT CODE >>>>>>>>>>>>>>>>>>>>>>
    

    'Good Bye message
    SEROUT 1, 84, [noparse][[/noparse]$FE, $14]    'Ensures that the screen is cleared
    PAUSE 20
    SEROUT 1, 84, 5, [noparse][[/noparse]$FE, $0C, 0, 0, "Thanks for watching!"]
    PAUSE 20
    SEROUT 1, 84, 5, [noparse][[/noparse]$FE, $0C, 1, 0, "For more info, go to"]
    PAUSE 20
    SEROUT 1, 84, 50, [noparse][[/noparse]$FE, $0C, 2, 0, "stevesgeekprojects."]
    PAUSE 20
    SEROUT 1, 84, 50, [noparse][[/noparse]$FE, $0C, 3, 6, "blogspot.com"]
    PAUSE 4000     '3 second pause
    SEROUT 1, 84, [noparse][[/noparse]$FE, $14]    'Ensures that the screen is cleared
    PAUSE 20
    SEROUT 1, 84, 5, [noparse][[/noparse]$FE, $0C, 1, 5, "Good Bye!"]
    PAUSE 2000
    

    ' Now for some cleanup
    SEROUT 1, 84, [noparse][[/noparse]$FE, $0B]   'Turns the text display off (not the backlight)
    PAUSE 20
    

    '<<<<<<<<<<<<<<<<< BEGIN BACKLIGHT FADE CODE >>>>>>>>>>>>>>>>>>>>>>
    ' These two variables are used to dim the LCD backlight
    fade VAR Byte
    brightness VAR Byte
    brightness = 150                 'this was set at the begining of this code but it's now assigned to the variable
    FOR fade = 0 TO brightness                         'Fades the LCD backlight to zero
    SEROUT 1, 84, [noparse][[/noparse]$FE, $03, Brightness - fade] 'Sets the brightness (0-250)  *fades to 0 from whatever level it was
    NEXT
    PAUSE 10
    '<<<<<<<<<<<<<<<<< END BACKLIGHT FADE CODE >>>>>>>>>>>>>>>>>>>>>>
    

    ' I noticed that the backlight is never truly 'off' but you can control that with another output pin from the microcontroller
    

    END     'Puts the Basic Stamp into low power mode
    


    If you have any questions, let me know.
  • ozarkshermitozarkshermit Posts: 14
    edited 2009-03-29 22:04
    Steve:

    I have used some of your example code for the subject backpack, and it was very helpful.
    One thing I have not been able to get to work is custom characters. I've tried every combination I can think of, but no success. A line or two of sample code would be appreciated.

    That is a great backpack, by the way. I found it very easy to use.

    Thanks. . . .

    Ken
  • Steve88WSteve88W Posts: 2
    edited 2009-03-30 02:04
    You'll need to read the manual for the LCD screen closely.

    Here's an excerpt from my manual.
    Load Custom Characters
    Syntax hexadecimal 0xFE 0x1A [noparse][[/noparse]addr][noparse][[/noparse]d0 … d7]
    Parameter Length Description
    [noparse][[/noparse]addr][noparse][[/noparse]d0 … d7] 9 bytes Load custom characters,
    [noparse][[/noparse]addr] 1 byte – custom character address from 0 to 7,
    [noparse][[/noparse]d0 … d7] 8 bytes – custom character pattern bit map
    14
    Description: LCD module has space for 8 custom characters. Each custom character is 5 pixels
    wide by 8 pixels high.
    The [noparse][[/noparse]addr] parameter indicates which custom character is defining, and must have a value from 0
    to 7.
    Following the [noparse][[/noparse]addr] parameter are 8 bytes that define the custom character. Bits 0 to 4 each byte
    byte will each define a pixel character.
    Example: The bit map for character ‘X’.
    Bit 7 6 5 4 3 2 1 0 Hex
    Byte 1 0 0 0 1 0 0 0 1 0x11
    Byte 2 0 0 0 0 1 0 1 0 0x0A
    Byte 3 0 0 0 0 0 1 0 0 0x04
    Byte 4 0 0 0 0 1 0 1 0 0x0A
    Byte 5 0 0 0 1 0 0 0 1 0x11
    Byte 6 0 0 0 0 0 0 0 0 0x00
    Byte 7 0 0 0 0 0 0 0 0 0x00

    Byte 8 0 0 0 0 0 0 0 0 0x00

Sign In or Register to comment.