 |
 |
View previous topic :: View next topic |
Author |
Message |
PROMOD
Joined: 01 Feb 2018 Posts: 42
|
Restoring Data |
Posted: Wed Oct 31, 2018 5:15 am |
|
|
I have written a program for Single Phase AC Energy meter using CCS C Compiler. It is a correct programming to measure energy. The problem is that the program can't save data. As a result after every power interruption it starts measuring electricity newly.
How can I write a program that can save the current information of energy measurement and can restore previous data after a power interruption? |
|
 |
dluu13
Joined: 28 Sep 2018 Posts: 395 Location: Toronto, ON
|
|
Posted: Wed Oct 31, 2018 6:33 am |
|
|
EDIT: shows how much I know XD. You should probably listen to Ttelmah instead
Depends on how much info you want to save. If it's just a few numbers, you should be able to write it into eeprom when you take a measurement. Then when you lose power, you can load the value from eeprom as part of init routine.
If you want to keep a running log, then it's probably better to write to an SD card.
Last edited by dluu13 on Wed Oct 31, 2018 6:39 am; edited 1 time in total |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19730
|
|
Posted: Wed Oct 31, 2018 6:36 am |
|
|
You need to add circuitry to tell your PIC the power is failing, before it actually goes fully off. Key is that if (for instance) your PIC has a few hundred uF capacitor on it's supply line, and the supply is being fed from the AC in, then you can detect when this goes off, _before_ the actual supply to the PIC starts to drop by very much. Furthermore you can know (from the size of the capacitor, and the typical consumption of the PIC), how long it'll be before the PIC loses power.
Then you can use the circuitry to trigger an interrupt to the PIC, and have this write the measured data to the EEPROM. This takes a few tens of mSec (depending how much has to be saved).
Now I think you are using the 4431?. If so, this does not give you a lot of margin (since this only works down to 4.2v). If the PIC circuitry draws a couple of mA (turn everything else off as soon as the supply failure is detected), then if you have to save 10bytes, you need to ensure the supply will stay above 4.2v, for perhaps 40mSec to write the data. Then just have the code going into a wait, which it exits if the power comes back on.
On reset, load the stored data.
Now you have to write it like this (on failure), otherwise you destroy the EEPROM. This memory has a limited 'write life'. Only warranted at 10000cycles. If you wrote 10 bytes every second (say), you would destroy the memory in 1000 seconds...
By only writing when the power actually goes off, you make it instead able to endure 1000 power off cycles. A lot. |
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9426 Location: Greensville,Ontario
|
|
Posted: Wed Oct 31, 2018 7:59 am |
|
|
How much 'current information' ? If only a few bytes the cheap,easy solution can be a DS1307 type RTC. It has battery backed RAM, unlimited writes, simple I2C. The 'bonus' is that it IS an RTC, can be used as a 'clock'.
However if you want 'data logging' , then either an SD card or Viniculum device will work. SD requires a large 'driver', V is simple 'RS232' writes, NO driver. If you do decide to 'data log', then store the data in CSV format. That way eXcel can 'autoload' the data into a spreadsheet for you.
Jay |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19730
|
|
Posted: Wed Oct 31, 2018 9:35 am |
|
|
The point Jay is making is very important.
A battery backed memory like this can be updated as often as you want. Every time a reading is taken, you can store the running totals so they are available next time.
There is a 'caveat' though. You still really do need the same ability to detect power failing, before it actually goes 'off' to ensure that a write is not started when the power may disappear... |
|
 |
|
|
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
|