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

Restoring Data

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



Joined: 01 Feb 2018
Posts: 42

View user's profile Send private message

Restoring Data
PostPosted: Wed Oct 31, 2018 5:15 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Wed Oct 31, 2018 6:33 am     Reply with quote

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: 19215

View user's profile Send private message

PostPosted: Wed Oct 31, 2018 6:36 am     Reply with quote

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: 9097
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Oct 31, 2018 7:59 am     Reply with quote

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: 19215

View user's profile Send private message

PostPosted: Wed Oct 31, 2018 9:35 am     Reply with quote

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...
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