|
|
View previous topic :: View next topic |
Author |
Message |
doneaxe1 Guest
|
probem with rs232 communication |
Posted: Fri Dec 08, 2006 7:52 am |
|
|
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
|
|
Posted: Fri Dec 08, 2006 11:51 am |
|
|
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
|
|
Posted: Fri Dec 08, 2006 1:22 pm |
|
|
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
|
|
Posted: Fri Dec 08, 2006 1:54 pm |
|
|
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
|
|
Posted: Fri Dec 08, 2006 3:40 pm |
|
|
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
|
|
Posted: Sat Dec 09, 2006 10:53 am |
|
|
thank you very much with your aid the problem is solved again thank yuo very much |
|
|
|
|
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
|