piclist 2007\03\08\154903a >
Thread: float to byte - byte to float in C
www.piclist.com/techref/microchip/languages.htm?key=c
face picon face BY : William Couture email (remove spam text)



On 3/8/07, Phillip Coiner <spamBeGonepcoinerTakeThisOuTspamspam_OUTgpssource.com> wrote:
>
> Hi All
>
> I need to store some float types in the EEPROM of a PIC in my C18 code.

You can reference the individual bytes in a float by either using a union:
  union float_bytes
     {
     float value;
     unsigned char bytes[sizeof(float)];
     } myvar;

  myvar.value = 123.456;
  for (i = 0; i < sizeof(float); i++)
     {
     write_eeprom_byte(ee_base+i,myvar.bytes[i]);
     }

and read it by:

  for (i = 0; i < sizeof(float); i++)
     {
     myvar.bytes[i] = read_eeprom_byte(ee-base+i);
     }

Or you can use a pointer:

  unsigned char *p;
  float myval;
  myval = 123.456;
  p = (unsigned char *)&myval;
  for (i = 0; i < sizeof(float); i++, p++)
     write_eeprom_byte(ee_base+i,*p);

and read it by

  p = (unsigned char *)&myval;
  for (i = 0; i < sizeof(float); i++, p++)
     *p = read_eeprom_byte(ee_base+i);

Bill

--
Psst...  Hey, you... Buddy...  Want a kitten?  straycatblues.petfinder.org
<8ffe5b0c0703081247r1bae0250s19e5e96d4743fca6@mail.gmail.com> 7bit

In reply to: <000b01c761a1$5dfa1d50$7701a8c0@mygpssource.net>
See also: www.piclist.com/techref/microchip/languages.htm?key=c
Reply You must be a member of the piclist mailing list (not only a www.piclist.com member) to post to the piclist. This form requires JavaScript and a browser/email client that can handle form mailto: posts.
Subject (change) float to byte - byte to float in C

month overview.

new search...