PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Nov 15, 2007 4:53 pm |
|
|
Here is a demo program that shows how to save floats in internal eeprom.
The code is based on this CCS FAQ article:
http://www.ccsinfo.com/faq.php?page=write_eeprom_not_byte
Code: |
#include <16F690.h>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, MCLR
#use delay(clock=8000000)
#use rs232(baud=9600, xmit=PIN_B7, rcv=PIN_B5, ERRORS)
void write_float_to_eeprom(int8 addr, float data)
{
int8 i;
for(i = 0; i < 4; i++)
write_eeprom(addr + i, *((int8*)&data + i) ) ;
}
//--------------------------------------
float read_float_from_eeprom(int8 addr)
{
int8 i;
float data;
for(i = 0; i < 4; i++)
*((int8*)&data + i) = read_eeprom(addr + i);
return(data);
}
//==========================
main()
{
float value1, value2, value3, result;
value1 = 1.234;
value2 = -4.567;
value3 = 567.89;
// Write the float values to internal eeprom.
// Floats take 4 bytes, so the eeprom addresses must
// be spaced 4 bytes apart, as shown below.
write_float_to_eeprom(0, value1);
write_float_to_eeprom(4, value2);
write_float_to_eeprom(8, value3);
// Read the floats from eeprom and display them.
result = read_float_from_eeprom(0);
printf("Addr 0: result = %7.3f \n\r", result);
result = read_float_from_eeprom(4);
printf("Addr 4: result = %7.3f \n\r", result);
result = read_float_from_eeprom(8);
printf("Addr 8: result = %7.3f \n\r", result);
while(1);
}
|
|
|