sounds intriguing and I understand the cap idea to supply power to the pic for a few seconds after power failure but I just dont get the rest of the idea
surely if I'm writing to the eeprom while the program is running each of those writes count, and the extra one at power up and off so thats an extra 2 writes?
what happens to the clock timer data when the eeprom is written all 0Xff at power up, or 0X55 at power failure?
ahh the mist clears
hmm now thats given me more to think about lol
only write to eeprom on power failuer or say every half an hour as a backup, or save code space and use the 1 wire timer both are good ideas, the 1wire possibly easier to implement for a newbie like me, but the internal eeprom saves an extra component although I'd have to get a cap! the only problem I'm left with is how does the pic detect power loss and then write the value to the eeprom
Take the power line right after the V reg through a resistor to a pin on the PIC, poll that pin often to see when the state change. You can also use the PORTB Interrupt on change feature.
Ahh...
I feed one if the input pins via a pullup resistor
And take the 5v from regulator and feed it via a suitable cap to VDD pin of the pic, that should give enough the power to keep the pic working for around 5 seconds which should be plenty of time to poll the port and write to the eeprom, I's simple and I like it
Only problem now is which do I do lol
DS would be simper in terms of code, no timers to run, using pic timer would be cheaper
Take the power line right after the V reg through a resistor to a pin on the PIC, poll that pin often to see when the state change. You can also use the PORTB Interrupt on change feature.
You have the right idea but it must be before the diode. You're also right that it needs to be an interrupt unless you are absolutely sure that your code cannot be tied up for more than a few milli seconds.
Ahh...
I feed one if the input pins via a pullup resistor
And take the 5v from regulator and feed it via a suitable cap to VDD pin of the pic, that should give enough the power to keep the pic working for around 5 seconds which should be plenty of time to poll the port and write to the eeprom, I's simple and I like it
Only problem now is which do I do lol
DS would be simper in terms of code, no timers to run, using pic timer would be cheaper
The cap should be on the highest voltage (12V, I assume) and then feed a regulator. This gives an amount of time for you to spring clean (batten down the hatches etc.)
You only need the pic to work for 16mS for you to write the elapsed time. For tolerance sake you would ensure your pic was powered for at least double that.
If you need help with the circuit or code then I am happy to help.
THanks Mike, thats a great help, and if you dont mind i'll take you up on your offer of some help with the circuit please
What I'd like to do IF possible is power the pic for 1 full second after power failure, or if possible 2
All the pic is going to do is monitor 2 temp's off 2 X ds18b20's check to see if they are within a set temperature range (both different temps) and IF the temp is within these ranges activae 2 relays via 2 ds2405 1 wire devices.
obviously at the same time be running some kind of timer circuit (THAT ONLY runs when BOTH temp sensors are at the predetermined ranges) i.e. when both relays are activated, that will start counting at 00 hours, 00 minutes and 00 seconds, incrementing once a second write the time to the eeprom say once every 15 minutesas I'm only expecting a maximum run time of 2 hours per day so that should last quite a while.
Also if the power fails the pic gets an interupt ( no idea about interupts yet) and writes the timer value to the eeprom.
On power up the pic reads the eeprom time stored and displays it as hours minutes and seconds on the lcd for a few seconds, then it does it's job of monitering the temperatures and once the temps have been reached the ds2405's switch the relays once more and the timer start counting from the last value stored in the eeprom.
Hope I've made that clear, I'm wondering if it would help if I told you exactly what the circuit will be doing no the car.
Think I said this already but in case I didnt, the pic is a 16f88
If you put a diode feeding a capacitor that subsequently feeds your 5V reg, you will have enough time after the circuit is switched off to write the time to EEPROM. You will of course have to monitor the power line before the diode with a pic input.
Excellent! I had been wondering if that would work for my clock/thermometer. Since you say will work, I'll get going on it. The interface has me bogged down at present with multiple timers and half a dozen interrupt sources to get playing nice together. It works, but not as well as it should yet. The code is just about as inelegant as it can be at present.
I've attached a diagram of how it would be connected. C1 is the capacitor that will keep it powered and the resistors should give an output of 5V when power is applied (8k + 5k). To power it for a second or two is a little ambitious, to power a single LED will take around 1000uF so to power a relay or two will take a very big capacitor. I was envisaging something like 30mS with nothing powered except the pic so that 4 EEPROM locations could be written.
With these longer requirements, could you just leave it switched on all the time and 2 seconds after you detect power down go into sleep mode.
I've attached a diagram of how it would be connected. C1 is the capacitor that will keep it powered and the resistors should give an output of 5V when power is applied (8k + 5k). To power it for a second or two is a little ambitious, to power a single LED will take around 1000uF so to power a relay or two will take a very big capacitor. I was envisaging something like 30mS with nothing powered except the pic so that 4 EEPROM locations could be written.
Hmm... I'd have to maybe have that power loss sensing pin trigger an interrupt that would shut off everything (the LCD!!!) quick and then write to EEPROM.
It would protect the pic but would give less time. The idea is the capacitor remains charged after the supply is cut, moving the connection would delay the detection of the power off.
If you are worried about that, put another diode in front of the current one and connect the wire between them.. also keep the forward voltage drop with diodes in mind. For each diode you put in the line the V will drop by about 0.6V.
If you are worried about that, put another diode in front of the current one and connect the wire between them.. also keep the forward voltage drop with diodes in mind. For each diode you put in the line the V will drop by about 0.6V.
On a car you can expect about 14V when running and a bit over 12 when the engine is off. During starting the voltage can drop well below 12 and the uC will think the power is going down.
If that is a problem run a wire from the key side of the cranking circuit to uC and ignore the voltage level when the engine is starting.
I slapped together a test power supply with a 4700uF cap on my 16F876 breadboard just to see how it would work. With seven LEDs and the 876 running I pull the power wire out and it takes approximately one second before the LEDs go out. I have to write some code to sense power failure and write data to EEPROM now. I think it should work fine.
The power supply at the left end is not powered. It serves as a connector for the power & ground rails and provides one more LED.
When I was working on the magic switchboard I intended to use the ADC to detect if the bulbs were out of circuit. Mike/Pommie (i think) showed me that I could do without the ADC.
Could the same be true here? You are not interested in the voltage in an analog sense. Only true above or false below. By setting up the voltage divider correctly you should be able to get the 1 to zero transition to happen at the voltage you are interested in.
If it works it would be handy on a uC with no ADC. Not having to wait on the ADC would give you just a bit more time.