|
|
View previous topic :: View next topic |
Author |
Message |
jeremiah
Joined: 20 Jul 2010 Posts: 1351
|
|
Posted: Sat Apr 21, 2012 1:17 pm |
|
|
TRIS is handled automatically by the compiler unless you invoke a command to disable that. I tend to prefer to let it handle the TRIS so I don't have to. |
|
|
wordizlife
Joined: 08 Mar 2012 Posts: 38 Location: Canada
|
|
Posted: Sat Apr 21, 2012 4:17 pm |
|
|
I fixed a few errors that I found in in the code i'm using to transmit data.
Code: |
void write_buffer(void)
{
char ch_str[20] = "ATZ\r"; // puts character in string array
int chcount = strlen(ch_str);// counts # of characters
if (tempc==0x3E)
{
for (i=0; i<=chcount;i++)
{
printf("%s" ,ch_str[i]);
}
}
}
|
What this code does is it inserts the characters "ATZ\r" in the char ch_string[20] array, counts the # of characters in the array, look at each letter in the array, checks that tlast received character was ">" and prints it to the TX output with the printf function starting with the first one and ending with chcount.
I hooked up my USBee device and there are no activity on the TX pin at all...What am I doing wrong?
Updated Version of my main code:
Code: |
/********** Included Files ***********/
#include <main.h>
#include "D:\Winter_2012\Project_2\flex_lcd.c"
#include <stdio.h>
/********** Required Registers **********/
#byte TRISA = 0x85
#byte PORTA = 0x05
#byte TRISB = 0x86
#byte PORTB = 0x06
#byte TRISC = 0x87
#byte PORTC = 0x07
/********** Required Bits **********/
#bit A5 = 0x5.5 // Used to power on ELM323
/********** Declared Variables **********/
int tempc;
int32 i;
int32 chcount;
/********** Declared Arrays **********/
char ch_str[20];
/********** Declared Functions **********/
read_buffer(void);
void reset_elm(void);
void write_buffer(void);
#int_rda
void serial_isr() {
int t;
buffer[next_in]=getc();
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()
{
enable_interrupts(int_rda);
#if defined(__PCD__)
enable_interrupts(intr_global);
#else
enable_interrupts(global);
#endif
LCD_init(); // Initialize LCD
lcd_putc("\f*****\n");
lcd_putc("Project 2 - 2012");
delay_ms(5000); // wait 5 seconds
/********** Turn ELM327 On **********/
TRISA = 0x00; // Set Pot D as outputs
A5 = 1; // Turns on ELM327
/********** Display ELM Version **********/
lcd_init();
lcd_putc("\fOBD Reader\n");
read_buffer();
delay_ms(2000);
/********** Reset ELM **********/
reset_elm();
while(1);
}
/********** Read Buffer Function **********/
read_buffer(void) {
do {
if (bkbhit) {
tempc=bgetc();
lcd_putc(tempc);
if (tempc==0x3E)
return tempc;
}
} while (TRUE);
}
/********** Send Data to ELM Function **********/
void write_buffer(void)
{
chcount = strlen(ch_str);
if (tempc==0x3E)
{
for (i=0; i<=chcount;i++)
{
printf("%s" ,ch_str[i]);
}
}
}
/********** Reset ELM Function **********/
void reset_elm(void)
{
lcd_init();
ch_str[20] = "ATZ\r";
write_buffer();
delay_ms(500);
//lcd_putc("\fOBD Reset\n");
if (tempc==0x3E)
read_buffer();
}
|
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sun Apr 22, 2012 10:54 am |
|
|
A major bug is that you are declaring the array ch_str two times, once as a local array and once global. The data you write to the local array is not going to be available to the write_buffer() function that is reading from the global array.
Looking at your code I see many minor problems caused by you not being very precise in writing the code. You are lucky the CCS compiler lets you get away with many of these but in doing so it also hides some possible bugs that you are not aware of.
For example: Code: | read_buffer(void);
void reset_elm(void);
void write_buffer(void); | Here for two functions you have defined the return type but for one function you have left it out. Why? Now it defaults to returning an int, this is correct as you do return an integer, but it is not explicitly written in the code. Besides, the return value is always 0x3E and you don't use it so could as well be made into a void function.
I also recommend you to add the #case directive to the start of your code. This makes the CCS compiler to be case sensitive; sometimes annoying but it ensures you write consistent capitalizations.
Have another look at Jeremiah's comment about the CCS compiler handling the I/O settings for you, this would save you several lines of hard coded memory addresses. In the manual this is in the section for '#USE STANDARD_IO'.
Code: | #if defined(__PCD__)
enable_interrupts(intr_global);
#else
enable_interrupts(global);
#endif
| This piece of code shows you have copied code from another project without having a clue as to what you are doing. PCD is the compiler for the PIC24 processor series. This piece of code makes it easy to use the same code for all the PIC16/PIC18/PIC24 processors, but as you have hard coded addresses for the PIC16 processors you've lost the whole concept of portability.
Simplify your code by removing the unneeded #-lines.
Code: | }while(bgetc() != 0x3e); //0x3e = > | I like 'self explaining' code. With a small change your code is easier to read and doesn't need comments any more: Code: | }while(bgetc() != '>'); |
And a similar example: Code: |
#bit A5 = 0x5.5 // Used to power on ELM323
...
A5 = 1; // Turns on ELM327 | Can be simplified to: Code: | #define ELM327_POWER PIN_A5
...
output_high(ELM327_POWER); |
|
|
|
|
|
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
|