View previous topic :: View next topic |
Author |
Message |
muiratinas
Joined: 13 Oct 2010 Posts: 16
|
serial com. problem |
Posted: Fri Dec 31, 2010 7:02 am |
|
|
I tried to send data from pc to pic via RS232. I used ccs c serial input output monitor to send data and I send ASCII data as
if data is name, "Mark NX"
if data is time, "12:13 TX"
"X" means data receiving is over and N indicates that data is a name, "T" indicates data is time.
What is wrong with this code :/?
Code: | #include <18F67J60.h>
#use delay(clock=25M)
#fuses NOWDT, NODEBUG, HS, NOIESO, NOFCMEN, PRIMARY, ETHLED
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS)
#int_rda
void serial_isr() {
if(BUFFER_SIZE>i)
{
mybuffer[i]=getc();
i=i+1;
}
}
void main(void)
{
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
while(TRUE)
{
if(88==mybuffer[i]) //
{
//printf(lcd_putc,"\f");
if(mybuffer[i-1]==78) //N
{
printf("%s",mybuffer);
i=0;
}
if(mybuffer[i-1]==84) //T
{
}
}
}
} |
|
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Fri Dec 31, 2010 9:39 am |
|
|
Where do you declare "i" ?? If the compiler is not aware that that variable is a global (can't remember if you have to also declare it volitile?), it can "help" you and decide that since it is not changing, there is no reason to re-read it from memory (I got helped that way several years ago - couldn't figure out why it never went true - the compiler had optimized it away for me). Don't know if it still works that way, but that was what I ran into a while back anyway.
mikey _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Fri Dec 31, 2010 10:26 am |
|
|
A couple of other problems:
If the buffer gets full, the ISR stops reading the incoming characters. The interrupt will therefore re-set instantly when the ISR exits, and the ISR will be called again. Hung chip.... Your ISR must read the character whenever it is called. If there is not space in the buffer, just don't save it.
Same comment about the definition of 'mybuffer', as for 'i'.
Use character escapes. Makes things much less likely to go wrong:
Code: |
if(mybuffer[i-1]=='N')
|
etc...
Also 'beware'. If you accidentally receive the 'X', as the first character in mybuffer, you will be testing a character in front of the buffer on the subsequent tests....
Best Wishes |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Tue Jan 04, 2011 11:54 am |
|
|
When you are finished receiving your data from the PC you need to reset your value of 'i' or 'i' will continue to grow in size each time you receive a new character until the BUFFER evaluation no longer enters your if() statement. Something like testing to see if a character is a 'carriage return' which would signify then end of entering your data. If a CR is detected then set 'i' back to whatever value you need it to be. That way it won't continue to grow.
Ronald |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Wed Jan 05, 2011 2:46 am |
|
|
Also, once you have got some correct data, make sure you put a null termination char at the correct point in mybuffer to turn it into a string BEFORE printing it.
As it stands you will have errors in your output. |
|
|
|