Sunday, March 28, 2010

ATtiny2313 Four Digit Clock

After my foray into TTL logic, I feel like I've earned back the right to start using a C compiler again.  I've already done enough clock builds based on the Arduino environment, so I figured it was time to go another layer down, and toss all the nice Arduino libraries out the window.  Spring break left me with plenty of time to work only on software, since I left all of my tools and parts in Davis.  The hardware for the clock was finished during finals week, with meant I only had to take it and my laptop home for the break.

This clock reads just as a normal digital clock does: hours and minutes, with a PM indicator.  The brains of the clock is the ATtiny2313, which comes in at 2kB of flash program space, and 128 bytes of RAM.  This is plenty for this application, since it only needs to download three bytes worth of time from the real time clock chip, and then display them on the seven segment displays.


Note that flicker on the display in the video is an artifact of the video, and isn't visible in person.

Source code.
 From a software side, the first challenge was multiplexing the four digits of the display.  This was the first time I have done anything with interrupts, but once I got an LED to blink, extending it to switch digits on and off 300 times a second was fairly trivial.

Next was talking to an RTC.  I originally tried using a DS1306 SPI RTC, since SPI is a significantly simpler protocol to implement in software.  For some reason beyond me, I couldn't get my DS1306 to talk to me, so I downloaded Atmel application note AVR310, "Using the USI module as a I2C master."  This comes with a complete I2C library, which meant I could use my trusty DS1307 once again, but left me unsatisfied with reusing someone else's code.  This meant I spent most of my time on this project simply reimplementing their library how I wanted it.  My version does seem to compile to a few dozen bytes larger than theirs, but I feel like the code is much easier to read.  The entire program comes in under 1300 bytes anyways, so it all fits on the ATtiny2313 in the end.

Nothing too exciting from a hardware side.  I used a 7447 BCD to seven segment decoder because I was quickly running out of pins on the ATtiny2313.  I also got tired of swapping the 2313s from the breadboard to my miniPOV3 for reprogramming, so I scrounged a serial port to break out the pins I needed for my DASA programmer.  I have a real programmer in the mail, so these problems are soon to be a thing of the past.

Parts list:
  • 1 ATtiny2313 ($2.47 Digikey)
  • 1 momentary push button($0.23 Digikey)
  • 1 74LS47 BCD to seven segment decoder (Good luck finding somewhere to buy that)
  • 1 DS1307 RTC ($3.74 Digikey)
  • 1 32.768kHz quartz crystal ($0.32 Digikey)
  • 1 4 DIP switch
  • 4 2N3906 PNP transistors ($0.36 Digikey)
  • 4 1k resistors
  • 8 100Ω resistors
  • 2 4.7k resistors for the I2C lines
  • 1 3.5 digit 7 segment display (Bought mine locally at Halted)
  • Optional serial port and 3 4.7k resistors for in circuit programming


  1. Thx for that! I was asking google for more than a week for that solution. Let's see how it works!

  2. After some small modifications; it works!
    Thx again!

  3. Very cool Kenneth! Thank you for taking time to share all of your projects w/ us denizens of the cyber world, especially during those times when you had such little spare time.

    I had purchased the RTC DS1337 specifically because of its badass name "1337" , com'on, thats just tailor made for h4x0rz wit M4d Sk1lLZ. However, I have found the 1307 to be easier to work with , having less demands for extra passives than the 1337.