News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Counter

Started by Amod, Mar 22, 2021, 07:46 PM

Previous topic - Next topic

Amod

I made a 10 digit pulse counter on lcd. I want to save counter values on power cut off and start it again when power resumes from where it was saved in eeprom.

Stephen Moss

I would help people steer you in the right direction if you had indicated if your were intending to use internal or external EEPROM, if internal were the Edata, Eread & Ewrite sections of the manual not sufficient to help you?

rick.curl

Either way, you will probably have to add a big capacitor so when the power goes out you can detect loss of power and sill have time to write the memory before power goes down completely.

-Rick

Amod

Iam using internal eeprom.It is not possible to write counter values each time. So I wanted to only write them in eeprom when power goes down.

RGV250

How fast are the pulses, more to the question, how accurate do you need to be.
You could write once per minute and also write to different locations of the EEPROM so one area is not constantly written to.

Bob

m.kaviani

Use a diode in series with your power line . Place at least 2200uf cap after diode to support the circuit for seconds.
Now place a voltage divider before the diode and a transistor to make voltage detector circuit . Connect to external interrupt pin and at the interrupt routine
Save your counter value.

Amod

Kaviani, a circuit diagram will be helpful. Thanks

m.kaviani

hi Amod,
during my test with these values, it gave me about 1.5 seconds to drop voltage after breaking the switch.
when the charge of the capacitor goes below 4.7V the transistor switch off and the interrupt output activated.

m.kaviani

Amod, if you want the accurate circuit to 4.9V use single opamp like lm358.

top204

#9
Unfortunately, you will not be able to resume from where it lost power. Even if you saved the Call Stack contents (18F devices only), all RAM will be cleared that contains the variable values, and SFRs will be reset. You would need to save "all" RAM and SFR contents, then re-load them when the device powers up again.

Even I would struggle with that, because some SFRs are write only and cannot be read, so their contents cannot be saved. You would need to have shadow variables for all the SFRs being used, and copy what is written to the SFRs in the shadow variables. On an external EEPROM, it may be possible to write everything if the timing before critical shutdown, allows it.

I created a similar mechanism in the 1980s for the ZX Spectrum, Atari 800 and Commodore 64 computers, that saved all of RAM and created a start-up program in the screen RAM that re-loaded all the RAM back as if the computer had never been switched off, up to 64K of RAM. It was used to copy games on to floppy disks (MicroDrive for the ZX Spectrum) with a single button press, without me having to go into the machine code and remove the protection methods they had then. :-) I did the machine code altering for the challenge and the fun of it, when I was a young-un. :-) But, it was ripped off by a company in Fulham, when I showed them it, and explained it to them when they showed and interest in marketing it. I, again, got nothing in writing and foolishy relied on trust!

If you have a device that contains an HLVD peripheral, it is possible to fire an interrupt when the voltage drops to a certain level, and the interrupt routine could save some important things before the voltage gets below a level, then shut down the microcontroller itself. You will need a large capacitor to store the voltage to the microcontroller, so it has time to do certain things.
 

m.kaviani

Hi Les,
he can save the values of the counter when the detector circuit senses the voltage dropping and store it at EEPROM.
after power up the circuit in the header of the program, he can read the internal EEPROM.