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

serial com. problem

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



Joined: 13 Oct 2010
Posts: 16

View user's profile Send private message

serial com. problem
PostPosted: Fri Dec 31, 2010 7:02 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Dec 31, 2010 9:39 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Dec 31, 2010 10:26 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jan 04, 2011 11:54 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Jan 05, 2011 2:46 am     Reply with quote

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.
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