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

Communication between 2 PIC

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








Communication between 2 PIC
PostPosted: Tue Feb 09, 2010 1:53 pm     Reply with quote

Hi guys Smile
I have a problem with rs232 comm. between two 16f877.
My aim is first pic reads analog signals and does mathematical jobs finally sends float information to second pic throught TX pin. Before sending float I am using itoa() function to converting float into string data.

But at last step my problem occurs. When I try to send string, receiver pic do not recognize end of the string. I can't send enter signal. So receiver pic always wait for enter.

eg. code:

For transmiter pic
Code:
A=(info*0.0048828125)*100;
     itoa(A,10,string);
     printf("info: %s",A) ; (i also use puts(A) )


For receiver
Code:

  gets(k);
  printf(lcd_putc,"%s",k);

Eventually I gave up and send information character by character. But how can I send a string ?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Feb 09, 2010 2:16 pm     Reply with quote

What is 'A' ? How is it declared ? Is it an 'int32' value or is it a 'float' ?
Guest








PostPosted: Tue Feb 09, 2010 2:43 pm     Reply with quote

sorry i forgot that.

A is float ;
info is long ;
k and string is char string ;
Guest








Re: Communication between 2 PIC
PostPosted: Tue Feb 09, 2010 2:46 pm     Reply with quote

Anonymous wrote:
Hi guys Smile

eg. code:

For transmiter pic
Code:
A=(info*0.0048828125)*100;
     itoa(A,10,string);
     printf("info: %s",[b]STRING[/b]) ; (i also use puts([b]STRING[/b]) )



Eventually I gave up and send information character by character. But how can I send a string ?



and again sorry about my error. Very Happy
it will be printf("info: %s",STRING) ; not printf("info: %s",A)
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Feb 09, 2010 3:46 pm     Reply with quote

If you want to send a float, just use printf. Send a '\r' character at
the end to tell gets() that the string has ended. The CCS manual
tells you that the \r (carriage return) is required at the end:
Quote:

gets( )

Reads characters (using GETC()) into the string until a RETURN
(value 13) is encountered.


Example:
Notice the \r after the %f in the printf statement:
Code:

#include <18F452.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

//======================================
void main(void)
{
float A;

A = 1234.56;

printf("%f\r", A);

while(1);
}
 

Make sure that the array in your receiver PIC is large enough to hold
all the characters in the incoming string. Make the array be a few bytes
larger than you think you will need.

The gets() routine is not completely safe, because the string could be
much longer than the available array, and it could write bytes past the
end of the array. To solve this problem, you could use the get_string()
routine instead. It lets you set a limit on the maximum number of bytes
which are allowed to be placed in the array. get_string() is in this file:
Quote:

c:\program files\picc\drivers\input.c
barisdogg



Joined: 09 Feb 2010
Posts: 3

View user's profile Send private message MSN Messenger

PostPosted: Wed Feb 10, 2010 4:18 pm     Reply with quote

Thank you PCM ;
I tried your suggestion but still I can't send my information.

There is a example code for my aim.and the picture shows my result. Sad

For transmitter pic :
Code:

#include <16f877.h>
#device ADC=10
#use delay(clock=4000000)
#use fast_io(b)
#use rs232(baud=9600,xmit=pin_c6,rcv=pin_c7,stop=1,parity=N)
long bilgi;
float isi;

void main()

{ set_tris_a(0xff);
  set_tris_b(0x01);
  output_b(0x00);
   
  setup_adc(adc_clock_div_32);
  setup_adc_ports(all_analog); 
   
   while(1)
   { set_adc_channel(1);
     delay_us(20);
     bilgi=read_adc();
     isi=(bilgi*0.0048828125)*100; // When I was trying i also declared 'isi' such as 'isi=100;'
     //and send information.but there was no differance between results.
     printf("%f\r",isi);
     
    }
     delay_ms(1000);   
     
}


and for receiver pic :

Code:
#include <16f877.h>
#device ADC=10
#use fast_io(b)
#use delay(clock=4000000)
#include <lcd.c>
#use rs232(baud=9600,xmit=pin_c6,rcv=pin_c7,stop=1,parity=N)
#include <input.c>
#define BUF_SIZE  25
char k[BUF_SIZE];



void main()
{    set_tris_a(0xff);
     set_tris_b(0x01);
     output_b(0x00);
     lcd_init();
     lcd_putc("\fbilgi:");
 while(1)
 
     {
       get_string(k,BUF_SIZE); // I also use gets(k) but result was same
       printf(lcd_putc, "%s", k);
       delay_ms(1000);
     }
}


finally my result :



In contrast this time Lcd shows something.

I'm very grateful for your replies... Smile
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Feb 10, 2010 4:32 pm     Reply with quote

An important way to troubleshoot a problem is to simplify your program.
You can "fake up" the data, for example. You don't have to use your
complex and possibly buggy data generation code. Just give it some
faked up data. In the program below, I provide some representative
data, which is 1.234.

Also, I think some versions of the CCS compiler don't work properly with
just "%f" as the format string. So give it the width and precision fields.
Make sure the width is at least 1 greater than the precision field.
Notice in the code below that I give it "%7.3f". Try this for the
transmitter code. If it doesn't work, then post your compiler version.
Code:

#include <16F877.h>
#fuses XT, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

//==============================
void main()
{
float isi;

while(1)
  {
   isi = 1.234;
   printf("%7.3f\r",isi);
   delay_ms(1000);   
  }     
}

----------
Edit: Added #fuses statement.


Last edited by PCM programmer on Thu Feb 11, 2010 2:19 pm; edited 1 time in total
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Thu Feb 11, 2010 2:54 am     Reply with quote

What does #include <input.c> have in it ?
barisdogg



Joined: 09 Feb 2010
Posts: 3

View user's profile Send private message MSN Messenger

PostPosted: Thu Feb 11, 2010 2:04 pm     Reply with quote

Thank you for your effort Smile

But the same problem still exist Sad

Like my last try LCD shows 'bilgi:' but nothing afther that.

My CCS version is PCWHD 4.093

I'm using windows 7 so older versions can't run at my platform.



@Wayne_

I'm using get_string() for get string Very Happy input.c has this function.for this reason i had to add it
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Feb 11, 2010 2:18 pm     Reply with quote

Use a shorter program for the receiver code. Example:
Code:

#include <16F877.h>
#fuses XT, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#include <input.c>
#include <lcd.c>

#define BUF_SIZE  25
char k[BUF_SIZE];

//======================================
void main()
{
lcd_init();

lcd_putc("\fbilgi:");

get_string(k, BUF_SIZE);
printf(lcd_putc, "%s", k);

while(1);
}


I think this is a Proteus project, so that's why you didn't put a #fuses
statement in your code. I don't know if Proteus ignores the fuses.
So to be safe, I added #fuses statements to both test programs.
barisdogg



Joined: 09 Feb 2010
Posts: 3

View user's profile Send private message MSN Messenger

PostPosted: Thu Feb 11, 2010 2:35 pm     Reply with quote

yeay Very Happy Very Happy

for(a=0;a<1000000000;a++)
{
printf("thank you so much PCM )
}

fuses solved my problem Smile

again thank you Very Happy
hayee



Joined: 05 Sep 2007
Posts: 252

View user's profile Send private message

PostPosted: Tue Mar 16, 2010 12:37 am     Reply with quote

barisdogg can you post your final working code of both transmitter and receiver side.
Can I apply same technique for transmitting and receiving data using rs-485 signaling?
evaradharaj



Joined: 15 Jan 2009
Posts: 60

View user's profile Send private message Visit poster's website

real time
PostPosted: Thu Mar 18, 2010 4:12 am     Reply with quote

Is your code working in real time?

If its so, then please share the circuit diagram. In Proteus its working. But in real time its not working.
_________________
embedding innovation in engineers
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