PDA

View Full Version : HSERIN String reading



louislouis
- 18th December 2016, 22:21
Hello all,
I need a little help with HSERIN and STRING reading.
I try to realise a simple car actual speed reading via OBDII with ELM327 chip.
The chip communicates via AT commands, and has a serial output. Comm speed is 38400 baud.
I hooked up 16F690 MCU with 20MHz Xtal, TX and RX pins to UART and try to communicate with HSEROUT and HSERIN.
My problem is, I cannot get back correct data string.
For example if sending string "ATI" the answer must be ">ATI ELM327 v1.5"
This all work fine in serial monitor on PC, but on PIC I don't get back correct data. The data is like: "-_gfh~*1.5u]" or something like this.

I think I am doing something wrong around HSERIN command. Can me someone explain how to do that on correct way?
Thanks. Louis.

Here is my sample code:


#CONFIG
cfg = _HS_OSC
cfg&= _WDT_ON
cfg&= _PWRTE_OFF
cfg&= _MCLRE_OFF
cfg&= _CP_OFF
cfg&= _CPD_OFF
cfg&= _BOD_ON
cfg&= _IESO_ON
cfg&= _FCMEN_ON
__CONFIG cfg
#ENDCONFIG

DEFINE OSC 20 ; Use a 20MHz xtal

DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
DEFINE HSER_BAUD 38400
DEFINE HSER_CLROERR 1 ' Clear overflow automatically

Define LCD_DREG PORTC
Define LCD_DBIT 0
Define LCD_RSREG PORTC
define LCD_RSBIT 4
define LCD_EREG PORTC
define LCD_EBIT 5
define LCD_BITS 4
define LCD_LINES 2
define LCD_COMMANDUS 2000
define LCD_DATAUS 50

ANSELH = 0 ; Set RB6 (TX) pin to digital
ANSEL = 0 ; Set all digital
TRISA = %011100 ; Set RA5,RA4,RA3 input ; RA2,RA1,RA0 output
TRISB = %0011 ; Set RB4,RB5 input ; RB6,RB7 output
TRISC = %00000000 ; Set RC0,RC3,RC4,RC5,RC6, RC1,RC2,RC7 output
CM1CON0 = 0 ; Disable comparator 1
CM2CON0 = 0 ; Disable comparator 2
OPTION_REG.7=1 ; Disable weak pull ups

mai var byte
Lcdout $fe, 1
pause 200

main:
hserout ["ATI",13,10]
pauseus 300
HSerin 2000,main,[STR mai\16] ;>ati ELM327 v1.5

pause 2000
Lcdout $fe, 1
lcdout str mai
goto main

richard
- 18th December 2016, 23:05
this will help , but maybe the serial data will need to be inverted
the pic chip expects the data line to idle " high"



mai var byte[16]
Lcdout $fe, 1
pause 200

main:
hserout ["ATI",13,10]
pauseus 300
HSerin 2000,main,[STR mai\16] ;>ati ELM327 v1.5

pause 2000
Lcdout $fe, 1
lcdout str mai\16

Dave
- 19th December 2016, 12:18
Looking at the garbled data I would first look at the interface levels and states. When I used one of these OBD interface chips quite a few years ago it was designed to be connected DIRECTLY to the PC. That means the signals are already inverted. To interface it to a PIC you will need to Re-invert the TX and RX signals before connecting it to the PIC. Look at the data sheet. I used an ELM322D OBD(VPW) device.

louislouis
- 19th December 2016, 23:16
Some success comes after applying Richard's changes, thaks. Now I can read strings from ELM chip.

to: Dave, I don't need to Re-invert the TX and RX signals because the ELM327 is an PIC18F2480 MCU
and I connected the UART pins directly to PIC16F690 UART pins.
I think it's OK.