Shop OBEX P1 Docs P2 Docs Learn Events
Help with Serial I/O — Parallax Forums

Help with Serial I/O

PDTPDT Posts: 2
edited 2008-08-18 13:24 in BASIC Stamp
Hi all,

New BS user here, trying to get up to speed and was hoping some of the experts could point me in the right direction. I'm using the BS2e and trying to interface to an ELM327. My goal right now is just to get basic communications working by receiving the power-on text from the ELM and displaying it to an LCD. I'm using the LCD Terminal AppMod & the Super Carrier Board. I can write strings to the LCD no problem. I can communicate with the ELM327 when wired to the serial port on my computer.

However, when I try to get some text back to the stamp I'm not getting the results I expect. Here is the simplest code that I could reduce to. It just sits and waits for data received via the input pin. Whatever string comes back, I would like to print to the LCD as ASCII.

Right now when I power up the ELM (which causes it to send the string "ELM327 v1.2") I either get the word "DATA" or all black pixels on the LCD. Where am I going wrong?

Thanks in advance!


' {$STAMP BS2e}
' {$PBASIC 2.5}

E               PIN     1                       ' LCD Enable (1 = enabled) 
RS              PIN     3                       ' Reg Select (1 = char) 
LcdBusOut       VAR     OUTB 

' -----[noparse][[/noparse] Variables ]------------------------------------------------------- 
addr            VAR     WORD                    ' address pointer 
char            VAR     BYTE                    ' character sent to LCD 
elmOutbuffer    VAR        BYTE(10)        

' -----[noparse][[/noparse] Initialization ]-------------------------------------------------- 
Initialize: 
  NAP 5                                         ' let LCD self-initialize 
  DIRL = %11111110                              ' setup pins for LCD 
LCD_Init: 
    LcdBusOut = %0011                           ' 8-bit mode 
    PULSOUT E, 3 : PAUSE 5 
    PULSOUT E, 3 : PAUSE 0 
    PULSOUT E, 3 : PAUSE 0 
    LcdBusOut = %0010                           ' 4-bit mode 
    PULSOUT E, 3 
    char = %00101000                            ' 2-line mode 
    GOSUB LCD_Command 
    char = %00001100                            ' on, no crsr, no blink 
    GOSUB LCD_Command 
    char = %00000110                            ' inc crsr, no disp shift 
    GOSUB LCD_Command 

' -----[noparse][[/noparse] Program Code ]---------------------------------------------------- 
Main: 

DEBUG "waiting for serin",CR
SERIN 8, 84,  [noparse][[/noparse]STR elmOutbuffer \9]
addr = elmOutbuffer(0)
GOSUB LCD_Put_String
DEBUG "END"
END


' -----[noparse][[/noparse] Subroutines ]----------------------------------------------------- 
' Writes stored (in DATA statement) zero-terminated string to LCD 
' -- position LCD cursor 
' -- point to 0-terminated string (first location in 'addr') 
LCD_Put_String: 
  DO 
    READ addr, char 
    IF (char = 0) THEN EXIT 
    GOSUB LCD_Write_Char 
    addr = addr + 1 
  LOOP 
  RETURN 

' Send command to LCD 
' -- put command byte in 'char' 
LCD_Command:                                    ' write command to LCD 
    LOW RS 
    GOTO LCD_Write_Char 
' Write character to current cursor position 
' -- but byte to write in 'char' 
LCD_Write_Char:                                 ' write character to LCD 
    LcdBusOut = char.HIGHNIB                    ' output high nibble 
    PULSOUT E, 3                                ' strobe the Enable line 
    LcdBusOut = char.LOWNIB                     ' output low nibble 
    PULSOUT E, 3 
    HIGH RS                                     ' return to character mode 
  RETURN


Comments

  • metron9metron9 Posts: 1,100
    edited 2008-08-17 04:04
    I don't have a BS2e so i could be totally wrong here but the statement doesn't seem right.

    addr = elmOutbuffer(0)

    Are you not trying to set addr as the address of elmOutbuffer? That statement is copying the first character of the array. If it was for example ascii code 64 your routine to output the array would start at memory location 64 and not the memory address of your array.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think Inside the box first and if that doesn't work..
    Re-arrange what's inside the box then...
    Think outside the BOX!
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2008-08-17 14:54
    Hi PDT, the code is basically right, your main problem is the use of the READ instruction. READ is used in conjuction with WRITE to read and write to the EEPROM memory. ElmOutBuffer is a variable array held in RAM not EEPROM. Try simplifying the main routine to verify the serial string nefore you try to write to the LCD

    Main:

    DEBUG·"waiting·for·serin",CR
    SERIN·8,·84,··[noparse][[/noparse]STR·elmOutbuffer·\9]
    DEBUG STR elmOutbuffer
    DEBUG·"END"
    END

    or

    DEBUG·"waiting·for·serin",CR
    SERIN·8,·84,··[noparse][[/noparse]STR·elmOutbuffer·\9]
    DEBUG elmOutbuffer(0),elmOutbuffer(1),elmOutbuffer(2),elmOutbuffer(3),.......etc
    DEBUG·"END"
    END

    or

    DEBUG·"waiting·for·serin",CR
    SERIN·8,·84,··[noparse][[/noparse]STR·elmOutbuffer·\9]

    FOR idx=0 to 8


    DEBUG elmOutbuffer(idx)

    NEXT
    DEBUG·"END"
    END

    Jeff T.
  • PDTPDT Posts: 2
    edited 2008-08-18 13:04
    Hi, Thanks for your advice so far. I'm only using READ in the LCD routines which I grabbed directly from the example code, provided in the data sheets for the LCD. Jeff, if I'm understanding you correctly that subroutine would not work for a ram variable? If so, how should I go about modifying it? What's the ram equivalent of READ?
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2008-08-18 13:24
    GET. See, also, PUT.
Sign In or Register to comment.