1

I'm trying to make a small clock but want to ditch the need for a separate RTC chip with battery backup. What I would like to do, instead, is keep track of the time completely on the ATMega using the watchdog timer to generate a roughly 1Hz signal to count the seconds with. Not super accurate, but good enough for what I need.

The key though is that I would like to still have a battery backup. So, what I need it to do is detect that it no longer has 5V power (probably from USB) and switch into low power mode (running off a 3V CR2032 or similar) only waking when the watchdog timer ticks in order to increment the seconds. I'm guessing also dynamically decrease the clock speed (switch to internal or something) to save power.

I'm a bit stumped... How would I actually do the hardware for this? I vaguely remember something about "diode ORing" the backup battery... no idea what that means. And bonus points for the software side of things.

Adam Haile
  • 1,603
  • 5
  • 31
  • 50

1 Answers1

2

Diode or-ring is right but here's a slight improvement for the hardware: -

enter image description here

When the 5V voltage is present it turns off both P channel MOSFETs and power is routed thru the schottky diode to the MCU. The MCU's input "knows" that is is connected to the 5V rail and this is important for your watchdog timer.

When the 5V disappears, the MOSFETs gate voltage falls to 0V/Ground and MOSFETs turn-on and apply 3V (with barely any voltage drop) to replace the 5V (which isn't there any more). You want minimum volt-drop on this power rail to keep the MCU running for longer when the 3V battery starts to fail.

Again, the MCU can detect that it should be using its watchdog because the IO pin is low. The 10k in series with the input is to offer over-current protection when the 5V supply is active and the power voltage reaching the chip may be somewhat less at 4.6V (due to the schottky diode).

It should be noted that the right-hand MOSFET is probably not needed in this application - I took the idea from a circuit that also protects against reverse polarity on the 3V input.

Software side - sorry, this answer is ineligible for bonus points.

Andy aka
  • 434,556
  • 28
  • 351
  • 777
  • Very nice, that's definitely a huge start. Any chance you know the implications of clock speed on doing this? My understanding is that 8HMz is max at 3V, but I'm not sure I can switch from external 16MHz to internal 8MHz quick enough when the power switches from 5V to battery. – Adam Haile Feb 18 '14 at 11:51
  • @AdamHaile If you have enough capacitance on the MCU power rail (after the schottky diode) you might "buy" enough time to do this while the voltage is still reasonably 4.5 volt ish - how long does it take. You'll need capacitance anyway to smooth-over the power changeover in the circuit above. – Andy aka Feb 18 '14 at 12:02
  • Ahh, true. I don't know about for changing clock speed (or entirely how possible that is :P) but I know that going into sleep mode is on the order of microSeconds. So I probably should be fine. – Adam Haile Feb 18 '14 at 12:03
  • @AdamHaile You can't change the CPU clock source at runtime (external crystal to internal RC), you can only change the prescaler value to slow down the clock http://playground.arduino.cc/Code/Prescaler#.UwNxQ3-i7lc – alexan_e Feb 18 '14 at 14:44
  • @alexan_e You should be able to change an external clock source if you have two oscillators. I'm not saying it's easy or desirable BTW. – Andy aka Feb 18 '14 at 15:05
  • @alexan_e I know that the ATMega is only supposed to be capable of 16MHz at 5V but only 8MHz at 3.3V. Does that still apply if it's 8MHz because of the prescaller? – Adam Haile Feb 18 '14 at 15:50
  • @Andyaka If you read the first comment it says `I'm not sure I can switch from external 16MHz to internal 8MHz` and I replayed that this can't be done at runtime. What you say about selection between two external clocks is something different. – alexan_e Feb 18 '14 at 16:39
  • @AdamHaile The [datasheet](http://www.atmel.com/pt/br/Images/doc2545.pdf) says (note 3, page 30) `If 8MHz frequency exceeds the specification of the device (depends on VCC), the CKDIV8 Fuse can be programmed in order to divide the internal frequency by eight. It must be ensured that the resulting divided clock meets the frequency specification of the device.` so my answer is yes, as long as the resulting frequency from the prescaler is withing range you are fine. – alexan_e Feb 18 '14 at 16:44
  • Cool :) Good to know... – Adam Haile Feb 18 '14 at 23:32
  • @Adam are you done with this question and answer now? – Andy aka Aug 15 '20 at 15:51