View previous topic :: View next topic |
Author |
Message |
evaradharaj
Joined: 15 Jan 2009 Posts: 60
|
Re: Serial Communication between Two PIC16f877a Microcontrol |
Posted: Thu Feb 04, 2010 12:41 am |
|
|
Hi,
I am transmitting the temperature data from one Microcontroller to other Microcontroller through serial port.
The temperature is in the form of Float value. I need to transmit that Float value to other Microcontroller. Here is a problem that, I sent the data bit by bit and it is not working correctly.
Is there any other provision in ccs compiler, to transmit the float value through serial port.
Thanks and Regards,
Varadharaj.E
My code:
Code: |
float temp; // for example take the value as 35.2568
long j,t,l;
int m,n;
unsigned char k[5]={0};
j=temp*1000;
k[0]=j%10;
t=j/10;
k[1]=t%10;
l=j/100;
k[2]=l%10;
m=j/1000;
k[3]=m%10;
k[4]=m/10;
putc[k[4]];
delay_ms(200);
putc[k[3]];
delay_ms(200);
putc[k[2]];
delay_ms(200);
putc[k[1]];
delay_ms(200);
|
Note: Is there any wrong in this procedure. Is there any other way to proceed. _________________ embedding innovation in engineers |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Feb 04, 2010 2:55 am |
|
|
This all depends on what you are sending the data to and how it is reading it.
If you can confirm that the data gets through correctly then your problem may be the float format that CCS and microchip use for PICs.
It is not IEEE standard.
The sign and exponent are in slightly different places.
CCS/microchip format
EEEEEEEE SMMMMMMM MMMMMMMM MMMMMMMM
IEEE
SEEEEEEE EMMMMMMM MMMMMMMM MMMMMMMM
Notics the sign S bit!
I use these 2 routines to convert:-
Code: |
// Convert c# float to Microchip float
void float_convert(int8 *fl) {
int8 sign_bit, carry_bit;
sign_bit = fl[0] & 0x80;
carry_bit = (fl[1] & 0x80) >> 7;
fl[0] = (fl[0] << 1) | carry_bit;
fl[1] = (fl[1] & 0x7F) | sign_bit;
}
// Convert Microchip float to c# float
void float_unconvert(int8 *fl) {
int8 sign_bit, carry_bit;
sign_bit = fl[1] & 0x80;
carry_bit = (fl[0] & 0x01) << 7;
fl[0] = (fl[0] >> 1) | sign_bit;
fl[1] = (fl[1] & 0x7F) | carry_bit;
}
|
|
|
|
hayee
Joined: 05 Sep 2007 Posts: 252
|
|
Posted: Thu Feb 04, 2010 3:56 am |
|
|
You can also use SPI to send data from one microcontroller to other if serial communication is not necessary. |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Feb 04, 2010 4:49 am |
|
|
I have to appologise, I did not read your post properly.
The way you are sending the float has many problems.
A better way to send the float is to send the bytes.
Code: |
float temp; // temp is a 32 bit float (4 bytes)
int8 *p;
temp = 123456.1234;
p = &temp; // p now points to the address of temp.
putc(p[0]); // Send the first byte
putc(p[1]); // Send the second byte
putc(p[2]); // Send the third byte
putc(p[3]); // Send the last byte
|
to receive the float
Code: |
float temp;
int8 *p;
p = &float;
p[0] = getc();
p[1] = getc();
p[2] = getc();
p[3] = getc();
|
my initial statement still stands. If you are sending the float to anything other than another pic you will have to consider converting the format. |
|
|
varadharaj Guest
|
Thanks @wayne |
Posted: Thu Feb 04, 2010 5:20 am |
|
|
Thank you Mr.Wayne for spending your valuable time to solve my problem.
I will try this method.
What is the good book for knowing the pointer operations ? So that i can use my coding using pointer. |
|
|
|