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 CCS Technical Support

PIC 12F1840 EEPROM issue

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



Joined: 17 Nov 2016
Posts: 7

View user's profile Send private message

PIC 12F1840 EEPROM issue
PostPosted: Fri Nov 25, 2016 7:29 am     Reply with quote

Hello,

I am working with PIC12F1840 and have a problem with EEPROM.
I am trying to run a startup function only 3 times so i need a non-volatile variable.
I am trying to store a variable in EEPROM.
But it is not working.
Code:

/*
#rom  0x7FFF = {1}
#define ADDR 0x7FFF 
int8 stcnt =0;
*/

Code:

/*
stcnt = read_eeprom(ADDR);
   if(stcnt <= 0x02)
   {
      Startup();
      stcnt++;
      write_eeprom(ADDR,stcnt);
   }
*/

Here startup function is not getting executed.

Can someone help me with the issue ?
Thank you
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Fri Nov 25, 2016 8:07 am     Reply with quote

The EEPROM, is not 'at' address 0x7FFF. This is a 'trick' used in the hex file, when passing data to the programmer. Data stored at this address, is _programmed_ to the EEPROM.

Read the manual entry for read_eeprom:
Code:

value = read_eeprom (address)

address is an 8 bit or 16 bit int depending on the part

Reads a byte from the specified data EEPROM address. The address begins at 0 and the range depends on the part.


Note the last line. Address begins at 0....

When writing or reading the EEPROM, using the internal code, the address used, is the location _inside the EEPROM_. Not the 'pseudo address' used for programming.

So:
Code:

/*
#rom 0x7FFF = {1}
#define ADDR 0
int8 stcnt =0;
*/

/*
stcnt = read_eeprom(ADDR);
if(stcnt <= 0x02)
{
Startup();
stcnt++;
write_eeprom(ADDR,stcnt);
}
*/
nirmal1990



Joined: 17 Nov 2016
Posts: 7

View user's profile Send private message

PostPosted: Mon Nov 28, 2016 1:22 am     Reply with quote

Hi

Thank you for the reply.
I tried the changes but still it is not working.
The statement "if(stcnt <= 0x02) " is not getting true.

As per my understanding the default EEPROM value is 0x00 right.
so is there some other issues?

I use following fuses:
Code:
#fuses INTRC_IO,PUT,BROWNOUT,NOMCLR,NOPROTECT,NOLVP,NOWDT
Does this have an impact ?

Thank you.
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Mon Nov 28, 2016 2:20 am     Reply with quote

No.
The default erased EEPROM content is 0xFF

The line you showed:
#rom 0x7FFF = {1}

Would set it to 1, if you have this in your code.
nirmal1990



Joined: 17 Nov 2016
Posts: 7

View user's profile Send private message

PostPosted: Mon Nov 28, 2016 2:53 am     Reply with quote

Hi,

I did not understand this

in CCS help its mentioned
#rom 0x2100={1,2,3,4,5,6,7,8}
so it is also a method to insert data right.

so is it ok if i dont include this statement, since iam using read_eeprom and write_eeprom functions ?.
Also i am not sure of the data eeprom address iam using.

#rom 0x7FFF = {1}
this statement writes 1 to 0x7FFF or to all locations ?

Thank you
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Mon Nov 28, 2016 3:03 am     Reply with quote

First, I assumed you had checked the programming address as 0x7F00 to 0x7FFF. It may not be, and I'd have to check the programming data sheet to know. It is always far safer to use:

#ROM int8 getenv("EEPROM_ADDRESS") = {0x00, ....}

This will write to the defined programming address for the EEPROM on any particular chip.

It writes sequentially starting at the first byte. So:

{1} will just write 1 to the first byte

{1,2,3,4} will write 1 to the first byte, 2 to the second etc..

Different chips use different addresses. 0x2100, 0x7F00, 0x 7F000 etc..

If you wanted to use the last byte in a 256 byte EEPROM, then your write_eeprom instruction would use 0xFF as the address, and the code to initialise the EEPROM should use:

#ROM int8 (getenv("EEPROM_ADDRESS")+0xFF) = {0x00}
nirmal1990



Joined: 17 Nov 2016
Posts: 7

View user's profile Send private message

PostPosted: Mon Nov 28, 2016 3:17 am     Reply with quote

Hi

That was helpful, still i have a doubt.
if i use
Code:
#ROM int8 getenv("EEPROM_ADDRESS") = {0x00}

this means i write 0 to the first location right.
So every time when the PIC is switched ON it writes 0 to first location.

But i want a non-volatile variable
as i want my startup function to be run only for 3 times (ie 3 ON OFF cycles). So I want to store number of sartups in eeprom first location.

So if i assign 0 to first location, on every PIC ON OFF cycle it will execute startup function. Which is a problem.

So do you know any solution ?

Thank you
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Mon Nov 28, 2016 4:18 am     Reply with quote

No.

What is in the #ROM statement, is only written when the chip is programmed.
nirmal1990



Joined: 17 Nov 2016
Posts: 7

View user's profile Send private message

PostPosted: Mon Nov 28, 2016 4:30 am     Reply with quote

Thank you,
It worked.
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