Shop OBEX P1 Docs P2 Docs Learn Events
Coding data from basic stamp 2 to LCD display — Parallax Forums

Coding data from basic stamp 2 to LCD display

ad9409ad9409 Posts: 8
edited 2013-12-12 15:50 in BASIC Stamp
Ok, so I ran a sample code that runs from the basic stamp 2 to an LCD screen and it worked. The code used the function DATA to display onto the LCD screen.

So for example, it had a lot of coding lines but then used, DATA "Hello world", and using that, the LCD displayed "Hello world".
Then I wanted to display data that I gathered from an ADC that was connected to a sensor. When I used the function DEBUG...., the correct information displayed on the PC. But then when I replace DEBUG with DATA, the program is giving an error saying that it was expecting a constant.

What do I need to do to display the data gathering from the ADC to display on the LCD screen?

The LCD that I am using is LCM1602A which I believe is similar to the HD44780

Thanks

Comments

  • Hal AlbachHal Albach Posts: 747
    edited 2013-12-10 21:00
    In PBasic DATA writes constants to EEPROM storage during program load, WRITE will store a constant, variable, or expression in an EEPROM location. READ will transfer the contents of a particular location in EEPROM to a variable. In order to display the ADC data you have to move it from wherever it is stored and given to the LCD handler. Are you using a BS2 or BS2P? The BS2 does not have a built in LCD function, the BS2P does. Can you post the code you are using to display the ADC readings on the LCD? When copying the code to this forum place code in square brackets at the beginning and /code in square brackets at the end to preserve formatting.
  • davejamesdavejames Posts: 4,047
    edited 2013-12-10 22:36
    ad9409 wrote: »
    What do I need to do to display the data gathering from the ADC to display on the LCD screen?

    If the LCD has a serial interface, then SEROUT can be used with any version of the Stamp. If it has a parallel interface, then something else would be required such as the use of a version "P" Stamp as Mr. Albach mentioned.

    BTW - DEBUG is just a special form of SEROUT that sends data to the Stamp editor debug window.
  • ad9409ad9409 Posts: 8
    edited 2013-12-11 07:19
    I'm using a parallel LCD with a BS2 but I am able to display a message on the lcd . But when I want to display the data, I can't.
    I will post the code a little later today
  • davejamesdavejames Posts: 4,047
    edited 2013-12-11 08:12
    ad9409 wrote: »
    I will post the code a little later today

    ...any chance of a schematic also?
  • ad9409ad9409 Posts: 8
    edited 2013-12-11 09:09
    This is the code I used to display on the LCD screen. The line that says DATA under EEPROM Data displays that message in quotes. I got this code from parallax.
    ' ========================================================================= ' File...... Parallel_LCD_2X16.bs2 
    ' Purpose... Parallel LCD Display Demo 
    ' Author.... Parallax, Inc. 
    ' E-mail.... support@parallax.com 
    ' {$STAMP BS2} 
    ' {$PBASIC 2.5} 
    ' -----[ Program Description ]---------------------------------------------
     
    ' This program demonstrates using a Hitachi-compatible Parallel LCD Display 
    ' This code works with the BS2, BS2e and BS2sx 
    ' -----[ I/O Definitions ]------------------------------------------------- 
    E PIN 0 ' Enable Pin For LCD 
    RW PIN 2 ' R/W Pin For LCD 
    RS PIN 3 ' LCD Register Select 
     ' 0 = Instruction, 1 = Text 
    
    ' -----[ Variables ]------------------------------------------------------- 
    char VAR Byte ' Character To Send To LCD 
    inst VAR char ' Induction To Send To LCD 
    index VAR Word ' Character Pointer 
    temp VAR Byte ' Temp Variable 
    
    ' -----[ EEPROM Data ]----------------------------------------------------- 
    DATA "Hello, this is the LCD demo." ' Message To Send To LCD 
    
    ' -----[ Initialization ]-------------------------------------------------- 
    Initialize: 
     LOW RW ' Set LCD To Write Mode 
     OUTS = 00000000000000 ' Set All Output Low 
     DIRS = 00000011111111 ' Set I/O Direction 
     GOSUB Init_Lcd ' Initialize The LCD Display 
    
    ' -----[ Program Code ]---------------------------------------------------- 
    Main: 
     FOR temp = 0 TO 27 ' 28 Characters 
     IF temp = 15 THEN ' Check For End Of Line 
     GOSUB Next_Line ' Jump To Next Line 
     ENDIF 
     READ temp, char ' Read Next Character From EEPROM 
     GOSUB Send_Text ' Send Character To LCD Display 
     NEXT 
    END 
    
    ' -----[ Subroutines ]----------------------------------------------------- 
    Init_Lcd: 
     PAUSE 200 
     OUTS = 110000 ' Reset The LCD 
     PULSOUT E,1 ' Send Command Three Times 
     PAUSE 10 
     PULSOUT E,1 
     PAUSE 10 
     PULSOUT E,1 
     PAUSE 10 
     OUTS = 100000 ' Set To 4-bit Operation 
     PULSOUT E,1 
     Inst = 101000 ' Function Set (2-Line Mode) 
     GOSUB Send_Inst 
     Inst = 001110 ' Turn On Cursor 
     GOSUB Send_Inst 
     Inst = 000110 ' Set Auto-Increment 
     GOSUB Send_Inst 
     Inst = 000001 ' Clears LCD 
     GOSUB Send_Inst 
     Inst = 14 ' Set Cursor To Underline 
     GOSUB Send_Inst 
     RETURN 
    Send_Inst: 
     LOW RS ' Set Instruction Mode 
     OUTB = Inst.HIGHNIB ' Send High Nibble 
     PULSOUT E,1 
     OUTB = Inst.LOWNIB ' Send Low Nibble 
     PULSOUT E,1 
     HIGH RS ' Set LCD Back To Text Mode 
     RETURN 
    Send_Text: 
     OUTB = Char.HIGHNIB ' Send High Nibble 
     PULSOUT E,1 
     OUTB = char.LOWNIB ' Send Low Nibble 
     PULSOUT E,1 
     PAUSE 100 
     RETURN 
    Next_Line:
     
     Inst = 128+64 ' Move Cursor To Line 2 
     GOSUB Send_Inst 
     RETURN
    

    I have another code that helps get the data information that I need:
     CS   PIN  10   ' chip select  
      DX   PIN  11   ' data out
      CLK  PIN  12   ' clock
      result  VAR BYTE  ' result of ADC conversion
    
    
      DO
        LOW CS                              ' enable CS
        SHIFTIN DX,CLK,MSBPOST,[result\9]   ' read temperature and store in result
        HIGH CS                             ' disable CS
    
    
        DEBUG "Temp = ",DEC result*/$080,"F",CR    ' display temp ($080 is scaling of 1/2 for Vref = 1.28v)
        PAUSE 1024
      LOOP
    

    I tried adding this code above and it works but since i'm using DEBUG, it displays it on the PC. I assumed DATA would display it on the LCD screen but that is not the case. The program gives me an error that it is expecting a constant where DEC is in the DEBUG.

    And I do not have a a schematic to go along with it, sorry about that.
  • Hal AlbachHal Albach Posts: 747
    edited 2013-12-11 14:04
    " I assumed DATA would display it on the LCD screen but that is not the case."

    The only thing DATA can do is store the constant or string of constants that follows it to the Stamp's EEPROM memory during program loading. It cannot write to the LCD, it just writes to EEPROM, nothing else.

    When the above program is loaded into the Stamp, PBasic catches the DATA statement and places what follows into EEPROM storage. In your program under the heading '

    [ EEPROM DATA ]
    is the line DATA "Hello, this is the LCD demo." ' Message to send to LCD.
    PBasic takes whatever is between the quotation marks and places it into EEPROM memory. Since a starting location was not specified the location defaulted to location 0 in EEPROM. Further down in your program in Main: you have a FOR loop which extracts one location at a time from EEPROM and places it into a variable called char. This is done by the statement READ temp, char. temp is used as an index variable that points to which location to READ from the EEPROM. The next statement GOSUB send_Text goes to a sub-routine called send_Text that takes the contents of the variable char and sends it to the LCD, then returns to Main: for more until the FOR loop is complete.
    You cannot substitute the command DEBUG with DATA. All that will get you is an error.

    In your code for the ADC you have defined a BYTE variable named result. Yet your program shifts in 9 bits from the ADC into result, which holds only 8 bits. Maybe this is what you want, I don't know.
    In order to display the ADC result on the LCD you will have to copy result into char and then call the send_Text subroutine. But first you will have to scale result in order to display the correct number.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2013-12-11 14:58
    I have an example for writing both data and variables to the Parallel LCD on my home PC. After work I will go find and post the examples here for you. (2 hours).
  • SavageCircuitsSavageCircuits Posts: 247
    edited 2013-12-11 23:09
    Okay, here are the example programs I promised. These examples print data to the Parallel LCD from DATA statements but also handle variables, one with leading zero-stripping on numbers, one without and there's a third one I haven't looked at in awhile. I hope these help.

    -=<[Chris]>=-
  • SapphireSapphire Posts: 496
    edited 2013-12-12 15:50
    Hal,

    The 9-bit shift is required by the ADC0831 because it doesn't send out the data until the second clock pulse. The leading zero bit is simply shifted out of the result.

    The scaling by 1/2 for a Vref of 1.28 is to put the LM34 data in range for the Temp display. Since Vref is 1.28, an input voltage from the LM34 of 1.27v will generate an ADC output of 255. Dividing by two yields 127 which is 127°F.

    Look at Chris' examples. You'll need to pull each digit out of the result using the DIG operator and adjust it by ASCII "0" - zero - to make it show on the display.
    ...
    result = result / 2  ' simpler way to divide by two
    
    FOR i = 2 TO 0     ' 3 digits
      text = result DIG i + '0'  ' get each digit of the result (left to right)
      GOSUB Display_Text   ' your subroutine to display a character on the LCD - the character is text (a byte)
    NEXT
    ...
    
Sign In or Register to comment.