View previous topic :: View next topic |
Author |
Message |
ThomasC
Joined: 09 Oct 2007 Posts: 62
|
Want to record total on-time of pic16f690 |
Posted: Wed Dec 26, 2007 5:17 pm |
|
|
I already have the Real Time Clock function thanks to Neutone.
I would like the PIC to save the total time that it is on into eeprom. That means for it to load the total elapsed time when it turns on and when it turns off, it adds its current on time with the total and saves it. When it turns on again it repeats the same process.
I just need an idea, I can come up with the code myself. Thanks as usual guys! |
|
|
ECACE
Joined: 24 Jul 2006 Posts: 94
|
|
Posted: Wed Dec 26, 2007 6:10 pm |
|
|
Others may have a better way to to this, but one way may be to...
Since you have RTC working, just save the time to an address in EEPROM. When you power up, initialze the RTC and save it in an address that you will call startup time. Everytime you then update the time, save that time to another address called current time. Subtract the two and you have your uptime.
Keep in mind though that you will be repeatedly be rewriting to the same address. Over time, this could will wear out the EEPROM (Depends on how often it is updated and how long it is up for. You have to check the datasheets for that part and see how many times you can write to a location.) If that poses a problem, you could setup a bank of EEPROM, say 10 addresses, that you write the current time to, then each time you update the time, you write FF to the previous location and the current time to the next address up. This way you are not using the same address over and over. You roll the time to a different address each time. Then when you want to look up the current time, you look at the EEPROM bank you set up, you are going to look for addresses that are NOT FF. Then you know you have the time. Just an idea. You need to be sure you keep track of where you last wrote to so you don't write to a location that doesn't really exist for your device.
I would start smaller though. Get your code so you can aquire the uptime, then do the address rolling later on. Make sure the uptime works first. Good luck. _________________ A HW Engineer 'trying' to do SW !!! Run!!! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Dec 26, 2007 6:13 pm |
|
|
You're describing a data logger which measures elapsed time.
Questions:
1. Is this the only function of this PIC ? Or is it doing other things ?
2. Does the PIC control any external circuits ?
3. Is power completely removed from the PIC ?
Is it controlled by a mechanical power switch ?
4. Does the PIC have an external EEPROM chip connected to it ?
If so, what is the part number ?
5. Is the circuit already designed and the board already built ? |
|
|
crystal_lattice
Joined: 13 Jun 2006 Posts: 164
|
PIC Up Time |
Posted: Thu Dec 27, 2007 5:46 am |
|
|
A very easy method of doing this is by using a RTC which has an elapsed seconds counter and a clock halt bit (The 1 wire DS1904 comes to mind).
When you start the PIC, enable the RTC, when you stop the PIC, disable the RTC. That way you never have to worry about saving any of the values for a "Lifetime on" counter. If you want a session timer then you take reading before/after and do the math
Kind Regards |
|
|
ThomasC
Joined: 09 Oct 2007 Posts: 62
|
|
Posted: Thu Dec 27, 2007 4:11 pm |
|
|
eace - That's a good idea. I will mess around with that and see how it works. I am only required to save the time every 10 minutes or so; it doesn't have to be extremely accurate. BTW, your signature is funny. I am also a hardware engineer trying to do software.
PCM programmer wrote: | You're describing a data logger which measures elapsed time.
Questions:
1. Is this the only function of this PIC ? Or is it doing other things ?
2. Does the PIC control any external circuits ?
3. Is power completely removed from the PIC ?
Is it controlled by a mechanical power switch ?
4. Does the PIC have an external EEPROM chip connected to it ?
If so, what is the part number ?
5. Is the circuit already designed and the board already built ? |
1. No the PIC will have at least 4 more functions besides keeping time.
2. It will serve as an oscillator for the external circuits.
3. Power is completely removed from the PIC; it is controlled by a mechanical power switch.
4. The PIC doesn't have an external EEPROM chip connected to it.
5. Yes, the circuit is already designed and a prototype is built.
crystal_lattice - the circuit is already designed and I think it would be easiest not to add more parts. That is a good idea though....simple.
Last edited by ThomasC on Fri Dec 28, 2007 8:51 am; edited 3 times in total |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
|
Posted: Thu Dec 27, 2007 4:57 pm |
|
|
If the pic continues to run when the circuit is not operating its easy. You don't need to backup to EEPROM. While the circuit is operating, increment the elapsed time in the RTC timer interrupt.
If you are actually going to power the pic on and off load the elapsed time on power up from the EEPROM. While the circuit is operating, increment the elapsed time in the RTC timer interrupt. The math is much easier to keep up with if the value is always accurate. Forget about adding old and new elapsed times. If you can save the elapsed time just before powering off do that. If you can't, save the elapsed time once per 30 minute and you should be safe to avoid burning out the EEPROM location for about 8 years of elapsed time. |
|
|
ThomasC
Joined: 09 Oct 2007 Posts: 62
|
|
Posted: Fri Dec 28, 2007 3:50 pm |
|
|
Where should I write the save function so it automatically does it when it hits a certain time? Within the TIMER1_isr() function perhaps?
Thx. |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
|
Posted: Sat Dec 29, 2007 5:27 pm |
|
|
ThomasC wrote: | Where should I write the save function so it automatically does it when it hits a certain time? Within the TIMER1_isr() function perhaps?
Thx. |
Writes to EEPROM take about 5mS per write. I would suggest you set a flag in the interrupt to trigger a function in Main to do the write. I consider it to be a "Best Practice" to keep long duration code out of interrupts. It might work just fine as your code is written now but it could be a problem as your code grows. It would save you having to rewrite later if you put it in main in the first place. |
|
|
|