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

Pic24fj16ga004 - Saving data into program memory

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



Joined: 21 Dec 2011
Posts: 42

View user's profile Send private message

Pic24fj16ga004 - Saving data into program memory
PostPosted: Mon Jan 17, 2022 11:50 am     Reply with quote

dear eveyone,

My question is about saving data into memory program, as an eeprom.
My pic : 24fj16ga004.

I have found a lot of explain, but i'm already confused.

What I think:
1. define start address of my program on top of my code.
2. use command provided by CCS.
3. Configure my MPLAB / Pickit for do not erase the data place.

it's right ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon Jan 17, 2022 12:50 pm     Reply with quote

How much data do you need to store?.
How often do you need to update it?.

Easiest way is to use the virtual_eeprom driver supplied with the compiler.
Downside is that this is quite bulky, and needs a reasonably large amount
of the program memory.
It does though allow you to write single bytes just as to an EEPROM.
Trying to write the memory yourself, even using the supplied functions
is quite hard. You are dealing with three bytes of data every four bytes,
and have to erase an entire page to clear anything.
Microchip have an application note about emulating the behaviour of an
EEPROM in this memory, and it is this that the CCS driver emulates.
joseph20480



Joined: 21 Dec 2011
Posts: 42

View user's profile Send private message

PostPosted: Thu Jan 20, 2022 5:17 am     Reply with quote

My need is to store 3 16bits value. no more.
The value is like offset / setting of my system.

Sure, add a little I2C eeprom is one solution but add it for so little data seem to be luxury.

Have you an advice for me ? an example ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Thu Jan 20, 2022 6:34 am     Reply with quote

As I said, use the CCS driver.
You would need something like:
Code:

//your processor header files clock settings etc.
//Assuming an RS232 is also configured

#define VIRTUAL_EEPROM_8BIT_ADDY
#include "virtual_eeprom.c"

void main()
{
   union {
       int16 vals[3];
       BYTE bytes[];
   }demo_val = {1,2,3};
   int ctr;
   init_virtual_eeprom(); //start the driver

   while(TRUE)
   {
      if (virtual_eeprom_max_entries() ==0) //this is a diagnostic to tell you
      //if there is anything stored in the virtual EEPROM
      {
         //eeprom is empty so write some data.
         for (ctr=0;ctr<sizeof(demo_val);ctr++)
            write_virtual_eeprom(ctr,demo_val.bytes[ctr]);
      }
      //Now read the data that is in the EEPROM and print it.
      for (ctr=0;ctr<sizeof(demo_val);ctr++)
         demo_val.bytes[ctr]=read_virtual_eeprom(ctr);
         //read the stored bytes 
      //Now print the three 16bit values
      for (ctr=0;ctr<sizeof(demo_val)/2;ctr++)
         printf("Val %d is %d\n", ctr, demo_val.vals[ctr]);       
      delay_ms(1000:
   }
}


This shows writing three 16bit values to the virtual eeprom (only after
the chip is erased), and then reading these and displaying them.

For your chip the virtual EEPROM would start at 0x2400.

To explain, the erase pages are 512bytes in size. The chip has it's config
data at the top of the top page (so this cannot be erased), and 0x2BFF,
So the page below this is 0x2800, and the page below this is at 0x2400.
The driver needs two pages so it can copy stuff from one to the other
when an erase is needed.
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Jan 20, 2022 7:15 am     Reply with quote

If these 3 values are computed during running of the program,say calibrations of 'configurations', then you should also have 3 'default' values stored as well.
That way, when the program runs the first time 'bad things' don't happen.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Thu Jan 20, 2022 7:22 am     Reply with quote

Yes.
On systems I do with calibrations like this, they store a checksum for the
calibration values, and if the checksum is wrong, or the values fall outside
the legal range, they drop back to a 'master' calibration value. This is the
first one generated after the system is initialised, and is stored separately
from the 'in use' calibrations.
joseph20480



Joined: 21 Dec 2011
Posts: 42

View user's profile Send private message

PostPosted: Thu Jan 20, 2022 2:56 pm     Reply with quote

your reply is -always- rapid and clear.
thanks for all

it's works perfectly
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