Doing something once a day with minimum battery usage

matherp

Senior Member
Hi

Is there a best practice approach for a single picaxe (no other supporting circuitry) to run a bit of code once a day whilst minimising battery usage? Timing accuracy within 2-3 minutes per day would be fine.

The code could easily be run on a 08M(2) but if one of the bigger chips would make life easier that would be fine.

My current thought would be to use a 20X2 (happen to have some spare) running at "setfreq k31", then I would use "settimer" to wake up occasionally from a "doze 0" and also use "disablebod" and "disconnect" to further reduce power usage

If my calcs are correct, a minor tick on a 20X2 at K31 occurs every 0.007813 seconds (1/clock freq/256) and therefore if I settimer with 65536-(300/0.007813)=27136 I should wake up from my doze every five minutes - confirmation appreciated :)

Any better ideas with lower power usage appreciated

Thanks

Peter
 

nick12ab

Senior Member
If my calcs are correct, a minor tick on a 20X2 at K31 occurs every 0.007813 seconds (1/clock freq/256) and therefore if I settimer with 65536-(300/0.007813)=27136 I should wake up from my doze every five minutes - confirmation appreciated :)
That is correct.
 

lbenson

Senior Member
You might check out this thread:

http://www.picaxeforum.co.uk/showthread.php?8353-Low-Power-Battery-Backup-Reference-Design

It's about an 08M which has been running for 4+ years on 3 AA batteries, checking for an input which will never again come, and blinking every 7 seconds as an "I'm alive" signal. With a timer added, it could do something daily (although I'm not certain what kind of accuracy it would have).

For lowest power, sleep may be better than a low clock.

It looks like you already have all the pieces in mind. Try testing your wake-up timer with a piezo buzzer to see how close to 5 minutes you come.
 

matherp

Senior Member
Why doesn't this work?

I've tried a test program on my AXE091 and the timing isn't correct.

The code is:

Code:
#picaxe 20X2
setfreq k31
disablebod
disconnect
high c.0
low c.0
settimer 27136
do
	doze 0
	high c.0
	low c.0
loop
which I think should flash the LED every 5 minutes but it does it about every 40 seconds - any ideas?
Is there something odd about using the timer with K31 frequency?

Also tried it at K250 with "settimer 6942" which I think should give 1 minute but results in 7.5 seconds.

Technical - is there a bug here or am I missing the plot as usual?

Thanks

Peter
 
Last edited:

nick12ab

Senior Member
I've tried a test program on my AXE091 and the timing isn't correct.

The code is:

Code:
#picaxe 20X2
setfreq k31
disablebod
disconnect
high c.0
low c.0
settimer 27136
do
    doze 0
    high c.0
    low c.0
loop
which I think should flash the LED every 5 minutes but it does it about every 40 seconds - any ideas?
Is there something odd about using the timer with K31 frequency?
Does it work when you:
  • use the setintflags command after the settimer command?
  • Don't use doze?
 

matherp

Senior Member
Works without Doze

The following code works fine for a ten second period so it looks like doze is getting woken up prematurely

Code:
#picaxe 20X2
setfreq k31
disablebod
disconnect
high c.1
low c.1
setintflags %10000000,%10000000
settimer 64256
timer=65535
do
'debug
loop
interrupt:
high c.1
low c.1
toflag = 0
setintflags %10000000,%10000000 ; re-activate interrupt
timer=65535
return
 

AllyCat

Senior Member
Timing accuracy within 2-3 minutes per day would be fine.
Hi Peter,

I don't think you're going to get close to that with the PICaxe's internal clock. The "Electrical Specifications" section of the data sheet is still "Preliminary" (and like some Beta software may remain so for some years) but the clock frequency is only quoted to around +/- 7%, but can be calibrated to within 1%. The "31kHz" might be worse (as far as I can see it's not specified) so IMHO you're very unlikely to get much better than within say 1%, and that represents almost 15 minutes a day.

But the solution is quite simple, 32.768 kHz crystals are ubiquitous, small, remarkably cheap and can be added to most PICaxes (if the two required pins are available - I think one is the download pin on the 8M2). If you can't find a crystal with low/no shipping costs then maybe "cannibalise" an old (non-mains) electric clock, or even buy a new (alarm) clock (or two) from a "Pound Shop". You could even use the one second "ticks" from the clock PCB onto an interrupt/polled pin, but that might be considered to exceed your "no supporting circuitry" request.

In terms of the software, I'm not sure if it matters whether you use "Sleep/Nap" (which uses the watchdog timer hardware) and/or the 31kHz clock because the data sheet seems to imply that they both use the same 31kHz base hardware. However, the watchdog timer (sleep/nap) might always use the internal clock as a "clock fail" measure and thus be less accurate.

Cheers, Alan.
 

srnet

Senior Member
But the solution is quite simple, 32.768 kHz crystals are ubiquitous, small, remarkably cheap and can be added to most PICaxes (if the two required pins are available - I think one is the download pin on the 8M2)
If your referring to PICAXE support for an external resonator (the Setfreq EMxx command) that is only available on the 28 and 40 pin PICAXES.
 

nick12ab

Senior Member
But the solution is quite simple, 32.768 kHz crystals are ubiquitous, small, remarkably cheap and can be added to most PICaxes (if the two required pins are available - I think one is the download pin on the 8M2).
Unfortunately this can only be done on the 28 and 40 pin PICAXEs. The others can only use internal oscillators. It's been covered on the forum before but I can't find it - something to do with the fuses which are set when the PICAXEs are programmed at the factory.
 

AllyCat

Senior Member
Hi,

Oh dear! I am sometimes saddened by how much the lovely original PIC hardware is "crippled" by the PICaxe OS.:( Fortunately my own ultra low power project is using an equivalent of the "one second tick" approach above so is not dead in the water yet.

But Hippy seems to know about lots of undocumented possibilities, might the M2s work any differently?

Cheers, Alan.
 

matherp

Senior Member
eclectic

Thanks for the references but these all assume full power working. My objective is to try and spend as much time as possible in a low power mode. Sleep and nap turn off the chips peripherals including timers so I'm left with doze but am struggling to make it work properly - or at least as I expect:)

Best Regards

Peter
 

AllyCat

Senior Member
Thanks for the references but these all assume full power working.
Hi Peter,

But "full power" working with a 31/32kHz clock can be only tens of microamps, or less.

There certainly is the issue of the shared external clock/SI pin on 8M devices, but at least 18X devices seem suitable and those are quite old threads which can't include M2 devices. I can't comment on any possible limitations of internal "fuses" but you should be able to turn off nearly all of the internal power-consuming hardware by poking suitable values, if direct PICaxe commands (like NAP, DISABLEBOD, etc.) don't exist.

Cheers, Alan.
 

matherp

Senior Member
OK

I have a program now working. It transpires that the doze 0 is woken at a rate 8 times the rate at which the timer variable increments as the internal hardware interrupts seem to be based on a divide by 32 pre-scalar to the timer rather than the divide by 256 implied by the manual - the picaxe firmware is presumably then adding these up in sets of 8. Given this the setting for settimer for 1 minute wake events from doze at K31 clock speed would be 65536-(60/(1/(32768/32))) = 4096 if the 20X2 internal clock was perfectly accurate.

However, at K31 processing takes place very slowly so the doze needs to be shortened to allow for the code in the loop and the settimer value also needs tuning for the variability of the chips internal oscillator. For my example program with my chip 5050 seems about right.
Code:
#picaxe 20X2
setfreq k31
disablebod
disconnect
dirsc=255
dirsb=255
dirsa=1
outpinsa=0
outpinsb=0
outpinsc=0
w0=0
high b.0
low b.0
settimer 5050 'tune this number to get accurate timing for 1 minute loop
do
	doze 0
	inc w0
	if w0=1440 then gosub daily
	outpinsb=b0 'allows external visibility of the time for tuning accuracy
loop
daily:
w0=0
return
Next job is to do some current measurements
 
Last edited:

chipwich

Member
I don't think you're going to get close to that with the PICaxe's internal clock. The "Electrical Specifications" section of the data sheet is still "Preliminary" (and like some Beta software may remain so for some years) but the clock frequency is only quoted to around +/- 7%, but can be calibrated to within 1%.
My experience writing a software-only clock for the PICAXE is that the PICAXE drifted at least 20 minutes/day, until software compensation is used. Better than the 7% tolerance stated (and my testing doesn't cover varying temperatures, voltages, or production lots), but certainly not anywhere close to 3 minutes/day. Project writeup at http://corticalcafe.com/picaxe_simplest_clock.html .

Depending on your timing requirements, you may need an external component (RTC or Xtal).
 

g6ejd

Senior Member
I've done something similar, but using a DS1307 and I get 1-sec/day accuracy/drift, which I correct with a software routine, so-far it has reduced the drift down to 1-sec in 30-days, so I'm pleased with that. The routine to correct time is not perfect, but it's adequate for my purposes.
 

fernando_g

Senior Member
As a fact, I think that I'm seeing a similar RTC correction technique used on a commercial logger which logs environmental parameters over extended periods of time. The unit goes completely to sleep, waking up only to take a sample and save it..

When I download the samples from internal memory, I can see that if I had set up -say- to log each 1/2 hour, I get 329 readings per week, instead of 336...this means an average of 47 logs per day. This is confirmed by the timestamp which appears to be drifting also.

However, every day at 00.00 hours the clock is re-synchronized, and timekeeping after 1 month appears to be off about a second or so.

In addition to the 4 AA batteries, the equipment uses a 3V lithium cell, which the manual clearly indicates is the clock backup.
 
Top