View previous topic :: View next topic |
Author |
Message |
Guest
|
Communication between 2 PIC |
Posted: Tue Feb 09, 2010 1:53 pm |
|
|
Hi guys
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
|
|
Posted: Tue Feb 09, 2010 2:16 pm |
|
|
What is 'A' ? How is it declared ? Is it an 'int32' value or is it a 'float' ? |
|
|
Guest
|
|
Posted: Tue Feb 09, 2010 2:43 pm |
|
|
sorry i forgot that.
A is float ;
info is long ;
k and string is char string ; |
|
|
Guest
|
Re: Communication between 2 PIC |
Posted: Tue Feb 09, 2010 2:46 pm |
|
|
Anonymous wrote: | Hi guys
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.
it will be printf("info: %s",STRING) ; not printf("info: %s",A) |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Feb 09, 2010 3:46 pm |
|
|
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
|
|
Posted: Wed Feb 10, 2010 4:18 pm |
|
|
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.
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... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Feb 10, 2010 4:32 pm |
|
|
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
|
|
Posted: Thu Feb 11, 2010 2:54 am |
|
|
What does #include <input.c> have in it ? |
|
|
barisdogg
Joined: 09 Feb 2010 Posts: 3
|
|
Posted: Thu Feb 11, 2010 2:04 pm |
|
|
Thank you for your effort
But the same problem still exist
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 input.c has this function.for this reason i had to add it |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Feb 11, 2010 2:18 pm |
|
|
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
|
|
Posted: Thu Feb 11, 2010 2:35 pm |
|
|
yeay
for(a=0;a<1000000000;a++)
{
printf("thank you so much PCM )
}
fuses solved my problem
again thank you |
|
|
hayee
Joined: 05 Sep 2007 Posts: 252
|
|
Posted: Tue Mar 16, 2010 12:37 am |
|
|
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
|
real time |
Posted: Thu Mar 18, 2010 4:12 am |
|
|
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 |
|
|
|