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 support@ccsinfo.com

How to write to EEPROM?

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



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Nov 15, 2007 4:53 pm     Reply with quote

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);
}
 
ThomasC



Joined: 09 Oct 2007
Posts: 62

View user's profile Send private message

PostPosted: Fri Nov 16, 2007 9:35 am     Reply with quote

Thank you man. It works! Laughing
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