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 support@ccsinfo.com

pic to pic serial communication

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



Joined: 26 Jan 2011
Posts: 31

View user's profile Send private message

pic to pic serial communication
PostPosted: Tue Feb 21, 2012 5:10 am     Reply with quote

I trying to communicate b/w two pic 16f877a via serial. The first pic sends a number and it has to be received by the 2nd pic. The two pic's were configured using UART and sending the value through hardware USART to the 2nd pic. This data is further sent to the pc via software USART. But i am getting some garbage value on the hyper terminal.Can anyone please help?
mateeb4u@gmail.com



Joined: 07 Feb 2012
Posts: 19
Location: pakistan

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

Baud rate
PostPosted: Tue Feb 21, 2012 5:37 am     Reply with quote

Check about there Baud Rate

Baud must be same
temtronic



Joined: 01 Jul 2010
Posts: 9160
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Feb 21, 2012 6:48 am     Reply with quote

1) post compiler version...

2) post small version of compilable code .

3) Are you using MAX232 chips between PICs and PC ?

4)Have you tried a 'loopback test' to confirm hyperterminal/PC are working right?Baudrate,databits,stopbits,flow control,etc.

5)Have you added 'errors' to the USE RS232(...) options, if using the hardware UART in the PICs?
ratheeshbr



Joined: 26 Jan 2011
Posts: 31

View user's profile Send private message

PostPosted: Tue Feb 21, 2012 8:45 am     Reply with quote

Compiler version 3.36
Ya I am using MAX232 on Both sides
I first checked the transmitter side by connecting it to the PC hyper terminal. Its working.

At the reliever side I am using both hardware and software serial . I tried to print some data to PC through software serial and its also working.

And also Baud Rate , 9600 at both ends.

My doubt is whether I have to make any conversion?

I haven't used errors. I will check it now.

Thanks for the reply
ratheeshbr



Joined: 26 Jan 2011
Posts: 31

View user's profile Send private message

Transmit program
PostPosted: Tue Feb 21, 2012 8:54 am     Reply with quote

Code:
#include<16F877A.h>
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N, xmit=PIN_C6,rcv=PIN_C7,bits=8)
#include<String.h>
#include<pic.h>   
void main()
{
   while(1)
   {
      printf("rain");
   }
}
ratheeshbr



Joined: 26 Jan 2011
Posts: 31

View user's profile Send private message

Receive program
PostPosted: Tue Feb 21, 2012 8:55 am     Reply with quote

Code:
#include<16F877A.h>
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N, xmit=PIN_C6,rcv=PIN_C7,bits=8)
#include<String.h>
#include<pic.h>   

void main()
{


   int i;
   char a[5];
trisd=0x00;
   RD0=RD1=RD2=RD3=RD4=RD5=RD6=RD7=0;
   while(1)
{
   char pwd[]="rain";
   for(i=0;i<4;i++)
   {
      a[i]=getchar();
   }
   if(Strcmp(a,pwd)==0)
   {
      RD0=1;
      delay_ms(2000);
      RD0=0;
      delay_ms(2000);
   }
   else
   {
      
      RD1=1;
      delay_ms(2000);
      RD1=0;
      delay_ms(2000);
   }
}


}
dyeatman



Joined: 06 Sep 2003
Posts: 1923
Location: Norman, OK

View user's profile Send private message

PostPosted: Tue Feb 21, 2012 12:57 pm     Reply with quote

What are you expecting to see as an indication that you received something
(good or bad) on the receive PIC? All you are doing is assigning a value to
a variable.
_________________
Google and Forum Search are some of your best tools!!!!
Ttelmah



Joined: 11 Mar 2010
Posts: 19326

View user's profile Send private message

PostPosted: Tue Feb 21, 2012 3:44 pm     Reply with quote

In fact, the receive chip will hang after the first pass.

The transmit sends 'rain' continuously. 960 characters per second, without pause.
The receive function, waits for four characters, and then _does not null terminate the string_. So will not match the required test value. As soon as it fails, the 4 seconds of delay, will have 3840 characters arriving without being handled. UART will therefore lock....

So, you need _ERRORS_ in the receive code.
You need to have a delay in the transmit code that it _more_ than those in the receive, or you will never have any chance of keeping synchronisation (or send a line feed after the message, and synchronise to this).
You need to add the null termination to the string before testing.


Best Wishes
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Tue Feb 21, 2012 4:03 pm     Reply with quote

To emphasize a point Ttelmah makes, when you send "rain" continuously like that, the Stop bit of 'n' will be immediatly followed by the Start bit of 'r' and the receiver UART will have a very hard time synchronizing to the incoming barrage of bits.

It would help a lot to leave a character wide gap (1ms) between messages so the UART knows when to start.
_________________
The search for better is endless. Instead simply find very good and get the job done.
ratheeshbr



Joined: 26 Jan 2011
Posts: 31

View user's profile Send private message

PostPosted: Tue Feb 21, 2012 4:25 pm     Reply with quote

Thanks for the replies . I will try with delay and errors. and will reply back
ratheeshbr



Joined: 26 Jan 2011
Posts: 31

View user's profile Send private message

RX pgm
PostPosted: Tue Feb 21, 2012 5:39 pm     Reply with quote

Now here is my code : Receiver
Code:
#include<16F877A.h>
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N, xmit=PIN_C6,rcv=PIN_C7,errors,bits=8,stream=s1)
#use rs232(baud=9600,parity=N, xmit=PIN_D0,rcv=PIN_D1,errors,bits=8,stream=s2)
#include<String.h>
#include<pic.h>   

void main()
{


   int i;
   char a[5];
trisd=0x00;
   RD0=RD1=RD2=RD3=RD4=RD5=RD6=RD7=0;
   fprintf(s2,"SoftSerial");
      for(i=0;i<4;i++)
   {
      a[i]=0;
      
   }
   while(1)
{
   char pwd[]="rain";
   if(!kbhit())
   {
   for(i=0;i<4;i++)
   {
      a[i]=getchar();
      
      
   }
a[i]='\0';   

fprintf(s2,a);
}
   if(Strcmp(a,pwd)==0)
   {   fprintf(s2,"SoftSerial2");
      RD0=1;
      RD1=0;
delay_ms(800);
   }
   else
   {
      fprintf(s2,"SoftSerial failed");
      delay_ms(800);
      RD1=1;
      
   }
}


}
ratheeshbr



Joined: 26 Jan 2011
Posts: 31

View user's profile Send private message

PostPosted: Tue Feb 21, 2012 5:40 pm     Reply with quote

Transmit:

Code:
#include<16F877A.h>
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N, xmit=PIN_C6,rcv=PIN_C7,bits=8)
#include<String.h>
#include<pic.h>   
void main()
{
   while(1)
   {
      printf("rain");
      delay_ms(2);
   }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19326

View user's profile Send private message

PostPosted: Wed Feb 22, 2012 3:21 am     Reply with quote

You are still missing the fundamental 'point' about timing.
Assume both chips start together.

The 'transmit' chip starts sending 'rain' immediately.
Your receive chip, now sits and sends 'SoftSerial', taking 9.6mSec to do so.
By this time, the transmit chip will have sent 'rain rai'.
The hardware receive buffer will contain the 'a', and the 'i' (probably), so you receive 'ainr' when you start to look for the string.

Your slave needs to _synchronise_. It needs to actually scan the incoming data either looking for a 'marker' character you add to say 'end of message', or the time break that is now present, and only then start looking for the message The receive code needs to be looking for the time delay, or you need to add a marker character, and look for this (typically line feed, or ASCII STX).

Best Wishes
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