I have a clock!
So last night/today, I worked on getting the clock part of my AVR working. Here's the basic rundown.
The ATmega48A has what's called an asynchronous timer. A timer usually works by simply incrementing a number on a fixed interval of time and doing something special when that number reaches a certain value. Most of the time, the timer will just increment every time the processor's clock ticks. An asynchronous timer is special in that it can clock along at an entirely different pace.
While the processor is chugging along at 8MHZ, my clock timer is clicking along at 32.768kHz. How do I know this? Because I have it connected to a 32,768Hz clock crystal. Why such an odd number? Lemme explain.
So, let's say we increment our number at the 32.768kHz speed. The counter is an 8 bit number which means it will overflow after 256 clock ticks. That makes my overflow event happen at a rate of 128 times per second. That seems kind of fast. Luckily, there's something called a prescaler that can be set in the AVR's code. The prescaler sets how many clock ticks the chip should ignore before incrementing its counter by one. If you set this prescaler to 128, the clock will increment exactly once every second.
Clock crystals are incredibly accurate, so this dude should have no trouble keeping time.
Now what about power? Surely, I can't afford to be running a micro controller at 8MHz all the time. Luckily, the ATmega48A has the ability to enter a Power-Save mode where it will shut down all unnecessary actions and keep just the counter running. The idea is to have the chip wake up every second just for a millisecond or so and then go back into Power-Save mode for the remainder of the second. My actual code will have the chip waking up to update the time, but for now, I just have it blink an LED.
Here's a video of it in action. It only draws 1 microamp! (I had to actually unplug the programmer which was drawing 15 microamps)
The LED is extremely dim in the last part, because the multimeter will not allow enough current to pass when it's measuring on the 200 microamp scale.
The only thing I have left to look at as far as the clock goes is adding some load capacitors to my quartz crystal. These caps are necessary for keeping a crystal extremely accurate, so I'll need to figure out what's required before I trust my watch not to lose time.