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
ThomasC



Joined: 09 Oct 2007
Posts: 62

View user's profile Send private message

How to write to EEPROM?
PostPosted: Thu Nov 15, 2007 2:55 pm     Reply with quote

I've done searches, looked at the examples and drivers directories, and looked at the CCS manual, but I do not understand how to write to EEPROM. What address or location do I use? I have a PIC16F690 and PCM. Thanks!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Nov 15, 2007 3:22 pm     Reply with quote

Here is a demo program for accessing internal eeprom in the 16F690.
It shows how to write and read to the eeprom with the built-in CCS
functions, write_eeprom() and read_eeprom(). It also shows how to
initialize the eeprom to specific values when the PIC is programmed
by a programmer. This is done with a #rom statement.

Here is the program output:
Quote:

Start

00 01 02 03 04
Wrote 0x55 to eeprom address 10
Read 55

Wrote 0xAA to eeprom address 10
Read AA

Done

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)

// These eeprom locations will be written when the
// PIC is programmed with the ICD2 programmer.
#rom 0x2100 = {0, 1, 2, 3, 4}  // 0x2100 is for 16F-series PICs

//==========================
main()
{
int8 i;

printf("Start\n\r\n\r");

// Display eeprom that was written with the ICD2.
for(i = 0; i < 5; i++)
    printf("%x " read_eeprom(i));

printf("\n\r");

// Write 0x55 to eeprom at run-time and read it back.
write_eeprom(10, 0x55);
printf("Wrote 0x55 to eeprom address 10\n\r");
printf("Read %X\n\r", read_eeprom(10));
printf("\n\r");

// Write 0xAA to eeprom at run-time and read it back.
write_eeprom(10, 0xAA);
printf("Wrote 0xAA to eeprom address 10\n\r");
printf("Read %X\n\r", read_eeprom(10));

printf("\n\rDone\n\r");

while(1);
 
}
 
ThomasC



Joined: 09 Oct 2007
Posts: 62

View user's profile Send private message

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

Thanks that example helped a lot. I was able to get the same output as you. But I do not understand the 0x55 part. What if I want to store a variable into eeprom instead of 0x55 and then read and display that?

I tried my variable called "CurrentTemp" Which is about -255.05, and it prints the value read 86. Many thanks.
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