PDA

View Full Version : Help with Serial I/O



PDT
08-17-2008, 10:21 AM
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

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

' -----[ 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

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

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


' -----[ 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

metron9
08-17-2008, 11:04 AM
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!

Unsoundcode
08-17-2008, 09:54 PM
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,··[STR·elmOutbuffer·\9]
DEBUG STR elmOutbuffer
DEBUG·"END"
END

or

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

or

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

FOR idx=0 to 8


DEBUG elmOutbuffer(idx)

NEXT
DEBUG·"END"
END

Jeff T.

PDT
08-18-2008, 08:04 PM
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?

PJ Allen
08-18-2008, 08:24 PM
GET. See, also, PUT.