Non volitile storage

For E-blocks user to discuss using E-blocks and programming for them.

Moderators: Benj, Mods

Post Reply
echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times

Non volitile storage

Post by echase »

I need to store four 8 bit calibration parameters for my temperature sensors, even after a reset or power off. Is this what the EPROM component is for? If not how can I specify that these variables are to be stored in non volatile memory, separate from the programme memory, as they are not specified by my programme but entered using a menu? I assume all other variables are wiped to zero by a reset/power down.

I got confused about the EPROM component because when I added it, it had no connections to the PIC. Is it in fact representing an internal area of the PIC? Also is it supported in my Student/Home version of Flowcode? Apologies if all this is in the Help file for EPROM.

User avatar
Steve
Matrix Staff
Posts: 3433
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times

Post by Steve »

The EEPROM component is what you need - it allows access to the internal EEPROM area that a lot of PICmicros have.

Unfortunately, it is not included in the "student/home" version of Flowcode.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Post by Benj »

If you need a constant value then you can just assign a variable with a value at the start of your program.

EG VAR1 = 23

You will need to make sure that you do not change the value of the variable.

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times

Post by echase »

In my case I assign something like VAR1 = 23 at initial setup but then adjust it using a menu to trim it to the exact calibrated value. So the adjusted value will get lost upon reset unless I insert the trimmed value into the programme or use that EEPROM.

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times

EEPROM

Post by echase »

Having now upgraded to Professional I can use the EEPROM component. It talks of storing byte variables. What happens if they are integer variables though? Will it put them in 2 bytes? Do I need to do anything special to set it up?

If I write a variable to an EPROM location with EEPROMWrite(addr, data) command will it always write to that location even if I don’t repeat the EEPROMWrite(addr, data) before every time that variable is called?

Same question for ReadEEPROM(addr). Indeed do I have to use ReadEEPROM(addr) at all, as the program should know where the data is once an EEPROMWrite(addr, data ) is used?

User avatar
Steve
Matrix Staff
Posts: 3433
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times

Post by Steve »

The EEPROM component currently does not "know" about INT (or STRING) data types. To store an INT variable, you will need to break it into a high byte and a low byte and save it to two adjacent locations within the EEPROM memory.

As for the other questions, I'm not sure exactly what you mean. The <EEPROMWrite> macro takes 2 parameters - "addr" is the addrss within the EEPROM memory where the data is written, and "data" is a BYTE value or variable that is to be stored. If a variable is used, the value of the variable at the time of the macro call is stored. If the variable value subsequently changes within your program, the EEPROM value is not updated (unless you then call the <EEPROMWrite> macro again).

The reason for storing data into EEPROM is that the values stored will be retained even when the PICmicro is turned off.

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times

Post by echase »

Ah I understand. I need to either

1) Use variables in RAM as normal and then only store the values in EEPROM before shutting down. Need to trigger on something like the brownout limit to detect when to do this storage. Any suggestions of how to detect immanent shutdown other than by using a spare A/D input (run out of pins) to measure falling supply voltage? I have a battery backup so shutdown would be gradual.

2) Or store them in EEPROM every single time they change using the write command, which conceivably could exceed the read/write life of the EEPROM.

User avatar
Steve
Matrix Staff
Posts: 3433
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times

Post by Steve »

I'm not sure how to detect shut-down without using a free i/o pin.

I suppose your strategy for saving EEPROM data depends on the application you are designing and the data you are saving. If you can, please explain more about your program and I'll think about a possible solution.

One possible strategy would be to set up a timer interrupt and store your data every second (or any time period that is appropriate). Of course, you may not always be saving the latest set of data - but that may not be critical.

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times

Post by echase »

Data to be kept only changes every 30 mins and a 30 min loop is already set. So maybe I could save it every 30min. Loss of one 30min value is not critical.

Would it run for 10-20 years without exceeding EEPROM read/write life? If there is a crash or battery goes flat the system upon reboot would read the EPROM values.

How easy to split integers into 2 bytes? Is they a forum topic on this?

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Post by Benj »

Hello

It is very easy to split an int into two bytes.

First we have two byte variable BLow and BHigh

And an Int variable Int1

To convert from an Int to two bytes.

BLow = Int1 & 0xFF
BHigh = ( Int1 >> 8 ) & 0xFF

To covert back from two bytes to an Int

Int1 = BLow
Int1 = Int1 + ( BHigh << 8 )

User avatar
Steve
Matrix Staff
Posts: 3433
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times

Post by Steve »

If you are concerned about EEPROM life, you could build in a check within your program so that data written to the EEPROM is immediately read back to make sure the data has been transferred ok.

If it has not been transferred, your program could begin to use a different part of the EEPROM instead. You would need to update a specific address within the EEPROM so your program knows the new location to store data.

This technique will work if the amount of data stored is relatively low.

Another thing you could do to minimise data writes is to only write new data if the data has changed.

Post Reply