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

probem with rs232 communication

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







probem with rs232 communication
PostPosted: Fri Dec 08, 2006 7:52 am     Reply with quote

my project require 2 pic to communicate serially i write this code for pic1
Code:

#include <16f877.h>
#fuses XT,NOWDT
#use delay(clock=4000000)
#use rs232(baud=9600,xmit=PIN_c6,RCV=PIN_c7)


void main(){

int i;


int y[9]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};

delay_ms(10000);
for(i=0;i<9;++i){
putc(y[i]);}
}


the pic 2 code is
Code:

#include <16f877.h>
#fuses XT,NOWDT
#use delay(clock=4000000)
#use rs232(baud=9600,xmit=PIN_c6,RCV=PIN_c7)
int x[9];
int y[9];
int i=0;
int m;
int d;

#INT_RDA

void int_sra(){

               for(i=0;i<9;++i)
                               {
               y[i]=getc();
                               }

              }
void main(){
int m;
int d;

enable_interrupts(global);
enable_interrupts(INT_RDA);
set_tris_b(0x00);
set_tris_d(0x00);
do    {
      output_b(0x00);
      delay_ms(5000);
      for(m=0;m<9;++m){
                        x[m]=y[m];
                        d=x[m];
                        output_b(d);
                        delay_ms(1000);
                      }
      }
while(1);



}


the pics communicate correctly but at the beginning i received garbage please how can i solve this problem??
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Dec 08, 2006 11:51 am     Reply with quote

At a minimum, you need to add the items shown in bold below.
Quote:

#include <16f877.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600,xmit=PIN_c6,RCV=PIN_c7,ERRORS)

The Brownout fuse will hold the PIC in reset, until the supply voltage
gets up to about 4.0v. The PUT fuse will add an additional delay,
to allow time for the crystal oscillator to start running properly.
The NOLVP fuse will prevent random lockups. The ERRORS parameter
will clear the UART receiver if it locks up, due to character overruns.
doneaxe



Joined: 22 Nov 2006
Posts: 6

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Fri Dec 08, 2006 1:22 pm     Reply with quote

thanx alot pcm programmer i did wht you say pic2 received the data without garbage at the begining but still i have a problem the first element of the recieved array is not the first elment of the sent array so if i send 1 2 3 4 5 6 i will receive 3 4 5 6 1 2 . i think there is a problem when i move the content of the array in the interrupt to the array in the main how can i ensure the two arrays are the same
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Dec 08, 2006 1:54 pm     Reply with quote

You need to add a while(1); statement before the end of main()
in your transmitter, as shown in bold below:
Quote:

for(i=0;i<9;++i)
{
putc(y[i]);
}

while(1);
}
Ttelmah
Guest







PostPosted: Fri Dec 08, 2006 3:40 pm     Reply with quote

Also, the delay in the receive, needs to be longer than the one in the send. At present, what happens, is that the receive pauses for half a second, and then starts looping, and printing 'null' characters, until the data arrives. By the time the first character has actually arrived, it has already looped a few times, so the first characters are missed.
Do it slightly differently:
Code:

int1 have_chars=FALSE;

#INT_RDA
void int_sra(){
    y[i++]=getc();
    if (i==9) {
        //tell main program, data has arrived
        have_chars=true;
        //reset counter in case anything else arrives
        i=0;
    }
}

//Then in main, get rid of the first delay, and just have:
   do {
      output_b(0x00);
      //This will wait till the characters have arrived
      while(have_chars==false) ;
      for(m=0;m<9;++m) {
          //Why these copies????
          x[m]=y[m];
          d=x[m];
          output_b(d);
          delay_ms(1000);
      }
   } while(1);


Best Wishes
doneaxe



Joined: 22 Nov 2006
Posts: 6

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Sat Dec 09, 2006 10:53 am     Reply with quote

thank you very much with your aid the problem is solved again thank yuo very much
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