PDA

View Full Version : need eeprom to save values



queenidog
- 5th July 2014, 00:04
I'm using a 12F675 to control a servo that swings back and forth like a pendulum. When the power goes off and comes back on, the servo resets to limits then starts to work on the narrower range I've specified.

I want to use the EEPROM to save the values when the power goes off. (This servo is part of a really big project that gets turned on or off by one main switch).

How do I detect the impending power loss and trigger the EEPROM to save the values? Then on power restoration I read the values.

Or is a simpler way just to put stops on the servo until the program in the 675 "get going"? Or some kind of delay for the servo power?

longpole001
- 5th July 2014, 02:16
why not save the range to eeprom and just use them on powerup ?

longpole001
- 5th July 2014, 03:27
http://www.avrfreaks.net/index.php?name=PNphpBB2&file=printview&t=57055&start=20

just a quick google , may be worth a look

mackrackit
- 6th July 2014, 02:09
Use a FET to control the power supply, the FET will be controlled by the PIC.

When you give the PIC the command to kill the FET jump to a routine to write whatever is needed to the EEPROM, then kill the FET.

This thread has a couple schematics and discussion that should help.
http://www.picbasic.co.uk/forum/showthread.php?t=19293

aratti
- 6th July 2014, 07:21
If you have a spare ADC pin available, than you can place a diode (1N4148 for example) and an electrolitic capacitor on the Vdd line to your pic. With the ADC you will monitor the power supply and when you detect a low ( you will establish the safe value) you will jump to the saving routine and write to eeprom all the necessary value the pic will need at start.

An example of what I mean can be seen in the attached hand drawing.

Cheers

Al.

queenidog
- 7th July 2014, 13:09
why not save the range to eeprom and just use them on powerup ?

I don't think that will work, but will try. I say "may not work" because in the program itself, I start off by going to the lower limit, ie "my lower limit". I do not swing the servo to ITS limits, but rather a subset in-between. My problem is mostly a startup problem, starting the servo only after the program is running and no slamming it to either of the stops.

queenidog
- 7th July 2014, 13:10
been there. good info.

queenidog
- 7th July 2014, 13:11
thanks a lot for this. Good link.

Amoque
- 7th July 2014, 14:22
... I want to use the EEPROM to save the values when the power goes off. (This servo is part of a really big project that gets turned on or off by one main switch).

How do I detect the impending power loss and trigger the EEPROM to save the values? Then on power restoration I read the values.

I do not believe this is possible, unless there is some other indication from the system of an impending power loss (or I misunderstand the nature of your inquiry). Two ideas come to mind, either use an alternate power supply to power the PIC after shut-down and "detect" the loss of main power to activate the write or, write the values routinely in the main program and start from them on power up each time.

Archangel
- 7th July 2014, 16:45
I do not believe this is possible, unless there is some other indication from the system of an impending power loss (or I misunderstand the nature of your inquiry). Two ideas come to mind, either use an alternate power supply to power the PIC after shut-down and "detect" the loss of main power to activate the write or, write the values routinely in the main program and start from them on power up each time.You isolate the PIC power from the power supply with a diode. Place a large cap on the PIC side of that diode to power the PIC when the power fails, and interrupt the PIC & write the eeprom when power failure is sensed on the other side of the diode. Very possible. HTH

Amoque
- 8th July 2014, 14:16
You isolate the PIC power from the power supply with a diode. Place a large cap on the PIC side of that diode to power the PIC when the power fails, and interrupt the PIC & write the eeprom when power failure is sensed on the other side of the diode. Very possible. HTH

Yes, thank you. For some reason I assumed the OP was suggesting that the PIC would be without power - in such a case writing to EEprom is clearly impossible. A battery or capacitor is an ideal solution - providing power AFTER the main is switched off. Yours (and others?) suggestions to provide power via a cap is a very efficient answer - allowing, with careful design - the ability to control the shutdown and perhaps even position the servo in a non-destructive position before exhausting the supply. Thanks again!

Dave
- 8th July 2014, 18:04
Please let me ask an obvious question. Why does the data have to be stored when power is taken away? Why can't it be stored then it is actually changed?

HenrikOlsson
- 9th July 2014, 06:20
Hi,
If you go the ADC route I suggest putting a voltage divider in between Vdd and the ADC input. Otherwise you're overdriving the ADC input, the internal protection diode from the input to Vdd may be forward biased (depending on its actual Vf compared to the 1N4148 or whatever).

If the PIC has a comparator you may want to use that instead of the ADC because then you can set it up to trip an interrupt when the comparator output toggles - may be better than continously sampling the ADC. (The voltage divider still applies though.)

/Henrik.

queenidog
- 10th July 2014, 00:16
Thanks all for responding. All good ideas that I will be sure to use in the future.

I looked back at my project, which was to automate a 1/87 scale (HO train scale) dump truck so it's box would dump, reset, then dump again. For this I was using a servo with a sweep motion of about 90 degrees, not the full range of the servo (180 deg from stop to stop). This worked well except when I would shut off power and power-up. The servo would immediately jump to one of the stops, which was out of range of my sweep, then the program would run and everything would be fine. If the dump truck box had been connected, the servo would have lifted the entire truck off the layout. This is why I was wondering how I could save the last value I sent to the servo, power off, and when power came back on again, the servo would know where to start. (I put the initial value at the beginning of my program but this did not work: the servo still swung to one of the limits. In other words it did this BEFORE the program could actually run).

So...employing the KISS principle (Keep it Simple Stupid), I decided there was a MUCH simpler way. I was so happy to be controlling servos, my judgement became cloudy. My solution is to use a simple DC gear motor under PWM control (easy with a PIC 12F1501), and operate the truck box with a crankshaft coming off the motor, basically an eccentric. Voila! Smooth operation, total speed control (and lots of torque thanks to PWM), no extra electronics required.

All the ideas you gave me guys are still useful and I will use them in the future. I ain't done with servos.