View previous topic :: View next topic |
Author |
Message |
tEaM
Joined: 29 Nov 2013 Posts: 26
|
EEPROM emulation on PIC24FJ |
Posted: Mon Mar 23, 2015 4:28 am |
|
|
Hi guys,
I need save a counter in a PIC24FJ in memory not volatile. This value is only changed 1x per day, just do not have endurance problems.
Does anyone have a stable code that makes E2PROM emulation these PICs?
Regards |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1345
|
|
Posted: Mon Mar 23, 2015 6:38 am |
|
|
Well you need to set aside some space for the compiler to avoid:
Code: |
#org start_address,stop_address {}
|
This needs to start on a page boundary and be the size of a page (see your particular chip's data sheet since you didn't supply a specific part number...there are a ton of PIC24's). If it isn't, at best it won't correctly work and at worst you will overwrite program space and crash your PIC.
After that you simply need to call write_program_memory() to write bytes (must be written in multiples of 4 bytes with the MSB always 0...so 24 bits of space. Each 4 bytes takes up 2 addresses from write_program_memory().
Code: |
unsigned int32 value;
//no larger than 24 bits
value = <whatever you want to save> & 0x00FFFFFF;
write_program_memory(&value,start_address,4);
|
That's the simplest method. The most complex is implementing the Application note that the microchip website provides.
Some other simple/medium complexity methods:
https://www.ccsinfo.com/forum/viewtopic.php?t=52680&highlight=pic24+eeprom+program+programme+memory |
|
|
tEaM
Joined: 29 Nov 2013 Posts: 26
|
|
Posted: Mon Mar 23, 2015 8:07 am |
|
|
Sorry, is the PIC24FJ128GA202.
It is possible to only use the "write_program_memory" function? It is not necessary to delete the page? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19499
|
|
Posted: Mon Mar 23, 2015 8:26 am |
|
|
The write_program_memory function _automatically erases the page, if you write to the first address in a page_. |
|
|
ELCouz
Joined: 18 Jul 2007 Posts: 427 Location: Montreal,Quebec
|
|
|
tEaM
Joined: 29 Nov 2013 Posts: 26
|
|
Posted: Mon Mar 23, 2015 12:21 pm |
|
|
I need your help to choose the "start_address" and "stop_address".
I can choose any address at the end of the flash? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19499
|
|
Posted: Mon Mar 23, 2015 1:58 pm |
|
|
You need to avoid the top four words of the program memory (so the top page), since these contain the flash config words.
So _two_ pages below the top of the program memory. Each page is 2KB (in address terms), but contains 1536 bytes of memory. So 0x14800 as a start address. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19499
|
|
Posted: Tue Mar 24, 2015 3:43 am |
|
|
I'll point you to the thread on writing the program memory on the 18F26J53 (currently only a couple of links from this one). I've just posted a demo program in this, of how to handle the memory. Most of what this involves is the same on your chip (except that you have to handle the re-arrangement of the data because every fourth byte is missing, and tweak the values to allow for the 32bit write size (the code uses 16bit).
I have used a more sophisticated version of this on the PIC24's. |
|
|
|