CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

ELM to PIC communication VIA RS232

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
littlet05



Joined: 28 Jun 2009
Posts: 4

View user's profile Send private message

ELM to PIC communication VIA RS232
PostPosted: Sun Jun 28, 2009 1:38 pm     Reply with quote

I have been unsuccessful at receiving data from the ELM 327 OBDII chip via RS232. I know the hardware is ok because I have a PIC Basic Pro program written that works ok...

The code that would reset the elm (send it the characters ATZ and the \r character aka 0x0d in hex) in pic basic that is functional reads

Code:

RESETELM:
SERout PORTC.6,T9600,["AT Z",13,10]
SERIN2 PORTC.7,84,5000,RESETELM,[SKIP 5,STR elmRead\12]


In CCS C, I have been able to send the ATZ command successfully (my board has the confirmation I/O wired to LED's for the ELM), and the ELM also sends back the reply, which is normally:

Quote:

ELM327 v1.3a

>


I was never able to get the #INT_RDA interrupt to work, or kbhit polling, or using fgetc... it just hangs in the fgetc function.

I'm using MPLAB v8.30 and CCS Compiler 4.3.0.217 and a PICKIT2

Below is me taking the EX_SISR.C file and tailoring it to my application (unsuccessfully).

Code:


#include <18F4620.h>
#device adc=10                                                   
#fuses H4,NOWDT,NOPROTECT,NOLVP,MCLR


#use delay(clock=40000000)                                 
#use fast_io(d)   
      
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, stream=ELM)

#include <lcd_flex.c>
                  
#define ELMRTS       PIN_C2
#define ELMBUSY    PIN_C1
#define ELMSLEEP   PIN_A3

#define BUZZER       PIN_D2
#define SLEEP       PIN_D7
#define BUFFER_SIZE 32

BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;

char msg[4];


void beep(int N, int delay)
{
   for(;N>0;N=N-1)
   {
      output_toggle(BUZZER);
      delay_us(delay);
   }
}

#int_rda
void serial_isr() {
   int t;
   output_high(SLEEP);//attempt to shut off LCD backlight to signal isr entry   
   buffer[next_in]=fgetc(ELM);
   t=next_in;
   next_in=(next_in+1) % BUFFER_SIZE;
   if(next_in==next_out)
     next_in=t;           // Buffer full !!
}

#define bkbhit (next_in!=next_out)

BYTE bgetc() {
   BYTE c;
 
   while(!bkbhit) ;
   c=buffer[next_out];
   next_out=(next_out+1) % BUFFER_SIZE;
   return(c);
}

void main() {
output_low(SLEEP); //turns on lcd
output_low(ELMSLEEP);//turns on ELM
output_high(ELMRTS);//ELM is allowed to perform tasks

   set_tris_a(0x00);
   set_tris_b(0x00); 
   set_tris_c(0x02);//01000010, ELMBUSY is input
   set_tris_d(0x00);
   set_tris_e(0x00);

   enable_interrupts(int_rda);
   #if defined(__PCD__)
   enable_interrupts(intr_global);
   #else
   enable_interrupts(global);
   #endif
   output_low(SLEEP);
   delay_ms(500);
   

   lcd_init();   
   lcd_putc(0x0c);//Clear the LCD
   lcd_gotoxy(1,1);
   printf(lcd_putc,"Hello");
   lcd_gotoxy(1,2);

               // The program will delay for 10 seconds and then display
               // any data that came in during the 10 second delay

   do {
        delay_ms(2000);
      msg[0]='A';msg[1]='T';msg[2]='Z';msg[3]=0x0d; //RESET ELM
      
      
      fprintf(ELM,"%c",msg[0]);
      fprintf(ELM,"%c",msg[1]);
      fprintf(ELM,"%c",msg[2]);
      fprintf(ELM,"%c",msg[3]);
      
      
      lcd_putc(0x0c);//Clear the LCD
      lcd_gotoxy(1,1);
      printf(lcd_putc,"sent");
      delay_ms(4000);

      while(bkbhit)
        printf(lcd_putc,"%c",bgetc() );
   } while (TRUE);
}



Any help or insight would be much appreciated.

Thanks,

JT
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jun 28, 2009 2:02 pm     Reply with quote

Quote:
#use fast_io(d)

set_tris_a(0x00);
set_tris_b(0x00);
set_tris_c(0x02);//01000010, ELMBUSY is input
set_tris_d(0x00);
set_tris_e(0x00);

Get rid of all this TRIS stuff. Let the compiler set the TRIS for you.
If you use the CCS i/o functions (which you are doing), the compiler
will handle the TRIS for you.

Quote:
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS, stream=ELM)

Add ERRORS to the #use rs232() statement. This will prevent the UART
from locking up if you don't read incoming characters often enough.
littlet05



Joined: 28 Jun 2009
Posts: 4

View user's profile Send private message

PostPosted: Sun Jun 28, 2009 2:36 pm     Reply with quote

I made those two corrections and when I compile I get the warning:

>>> Warning 202 "V2_ELM_SERIAL.c" Line 11(5,8): Variable never used: rs232_errors

I guess I have to catch these errors upon waiting for data on the rcv pin?

And now the LCD won't illuminate, which leads me to believe that output_low(SLEEP) is not toggling.

Shouldn't I assume the TRIS was necessary?
littlet05



Joined: 28 Jun 2009
Posts: 4

View user's profile Send private message

PostPosted: Sun Jun 28, 2009 2:39 pm     Reply with quote

My mistake, I forgot to comment the #use fast_io(d) upon commenting the tris statements... it seems to spit back some data over rs232.


THANK YOU!

JT
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group