elapsed timer?

Status
Not open for further replies.

The writing of 0xffs and 0x55s is a test to do during development to work out the size of capacitor needed.

The idea is that you only ever write the time to EEPROM when the power fails. The rest of the time it is in RAM.

Mike.
 
Last edited:
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.

Mike.
 

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.

Mike.
 
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
 
Last edited:
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.

Mike.
 
Last edited:
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.
 
Futz,

Why not try the writing 0x55s after power down detection to see how long you have. LCDs tend to take very little power so you may be OK.

Mike.
 
Mike,

Would moving the wire to the output side of the diode (cathode) protect the PIC pin if the power leads were reversed?
 

Attachments

  • PowerDetect.jpg
    12.7 KB · Views: 121
Last edited:
Mike,

Would moving the wire to the output side of the diode (cathode) protect the PIC pin if the power leads were reversed?

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.

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

That's why Schottky diodes are popular 0.2V drop average ie 1N5817.
 
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.
 
Last edited:
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.
 
Last edited:
Ha! Works like a charm! Pull the wire and it's able to write 124 bytes of EEPROM before running out of juice.

Now to test the same thing on the actual clock...

Here's the test code:
Code:
#include "eep.h"

void main(void)
{
	int x;
	trisa=1;
	adcon0=0b10000001;
	adcon1=0b00001110;
	eecon1.EEPGD=0;				//clear EEPGD - point to EEPROM data memory
	eecon1.WREN=1;				//enable writes
	for(x=0;x<256;x++){			//erase EEPROM to $ff
		while(eecon1.WR);
		eeadr=x;
		eedata=0xff;
		eecon2=0x55;
		eecon2=0xaa;
		eecon1.WR=1;
	}
	eecon1.WREN=0;
	while(1){
		delay_us(20);
		adcon0.GO=1;
		while(adcon0.GO_DONE);
		if(adresh<0xe0){
			eecon1.EEPGD=0;
			eecon1.WREN=1;
			for(x=0;x<256;x++){			//write $55's to EEPROM
				while(eecon1.WR){}
				eeadr=x;
				eedata=0x55;
				eecon2=0x55;
				eecon2=0xaa;
				eecon1.WR=1;
			}
			eecon1.WREN=0;
		}
	}
}

And the header file:
Code:
#include <system.h>
#include <stdio.h>
#pragma CLOCK_FREQ 18432000
#pragma DATA _CONFIG, _WDT_OFF & _HS_OSC & _LVP_OFF
 
Last edited:
Musings

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.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…