Tuesday, February 9, 2016

Avr clock

Wall clock made of avr mcu and led display using internal timer


The main goal was to try numeric LED display with MCU, so to make it a little bit more interesting I decided to create a clock that shows time in digital and analogue way. I used Atmega16L MCU because I needed 26 I/O pins (12 for numeric display, 12 for 2mm LEDs and 2 for buttons). The clock has got two buttons - one for adding hours and one for minutes. 2mm LEDs around edge shows seconds while numeric display inside shows hours and minutes.


I bought my display from ebay without any data sheet, but it is almost the same for all kinds of displays. You can easily find the right combination of pins. Four 7-segment digits are controlled via 12 I/O pins. Each digit is made of 8 parts (7 for number, 1 for dot). The other four pins are ground for each number. So to light up 4 different digits you have to toggle between all of them quickly.


A timer is a simple counter. The ATmega16 has two 8 bit and one 16 bit timer. The 8 bit counter (uses 8 registers) can count up to 2^8 = 255 while the 16 bit counter (uses 16 registers) can count up to 2^16 = 65 536. After reaching the maximum value for counter it simply overflows back to zero.

Lets say the MCU works on 1MHz frequency, that means it does 1 000 000 ticks per second, but the counters can only count up to 255 (8 bit timer) or 65 536 (16 bit timer) then it overflows. To set the counter properly you have to set the limit (prescaler) and also setup bits for right mode. All of the necessary configuration can be found in data sheet.

I used 16 bit timer with CTC mode (clear time on compare - when reached predefined value it is automatically set back to zero) with external interruption. The main thread is used for logic and redrawing while second thread (external interrupt) is used for counting time. There is a nice youtube tutorial for avr timers and also a avr timer calculator can be handy.


Using an internal timer might not be a best solution for a precise counter, because the error can be up to few percentage. So the clocks will lose its accuracy very soon. To avoid this error you have to use an external timer (e.g. Crystal Oscillator - more to come later on).


So I decided to use an external timer DS1307 RTC (real time clock) module. I've found a RTC library, removed buttons and added the timer.

the numbers have better visibility in the dark than in light room



Post a Comment