NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » RTC 7 Segment Elapse Timer

March 10, 2014
by Ralphxyz
Ralphxyz's Avatar

Kinda slow, so maybe someone would like to help me with an Elapse Timer project.

I was thinking of using the Nerdkits RTC, but will have to hunt down the working code, I know I had it working a couple of years ago.

I remember Humberto had said in the instructions that the RTC would eventually lose some time but would that really effect an elapse timer, I might have a thousand hours of run time.

Anyone have some Seven Segment LED code I could use.

I still am not much of a programmer, haven't touched the stuff for a couple of years now but I still get these projects I'd like to do.

I got really burnt out on my Water Curtain, remember that project.

So I need a Elapse timer with a 4 digit Seven Segment Display (maybe even a 5 or 6 segment display).


March 11, 2014
by Rick_S
Rick_S's Avatar

For your display driver, you might want to try something like THIS 8 digit 7 segment display driven by a MAX7219. It lets you control 8 digits with spi so you don't have to mess with multiplexing a display. Alternately, Sparkfun has a 4 Digit 7 segment display that uses an ATMEGA328 as the controller. It can be controlled via TTL Serial, I2c, or SPI.

The timer code shouldn't be too hard to get up and running for you, but using a dedicated RTC module with either a DS1307 or the DS3234 Breakout type board would be more accurate.

I too haven't programmed in quite a while, have to brush back up on it. I need to get some project to work on. Life's just been so busy and this great hobby kinda took a back seat. Even though I still have a love for it, and TONS of parts I purchased.

Good luck with your project Ralph, hope I gave you some food for thought anyway.


March 11, 2014
by Ralphxyz
Ralphxyz's Avatar

Here are some MAX7219 LED modules I picked up off ebay.

And here, I believe, is the Nerdkit RTC code, the download from the project page does not give a full download:

// realtimeclock1.c
// for NerdKits with ATmega168

#define F_CPU 14745600

#include <stdio.h>
#include <stdlib.h>

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <inttypes.h>

#include "../libnerdkits/delay.h"
#include "../libnerdkits/lcd.h"
#include "../libnerdkits/uart.h"


void realtimeclock_setup() {
  // setup Timer0:
  // CTC (Clear Timer on Compare Match mode)
  // TOP set by OCR0A register
  TCCR0A |= (1<<WGM01);
  // clocked from CLK/1024
  // which is 14745600/1024, or 14400 increments per second
  TCCR0B |= (1<<CS02) | (1<<CS00);
  // set TOP to 143
  // because it counts 0, 1, 2, ... 142, 143, 0, 1, 2 ...
  // so 0 through 143 equals 144 events
  OCR0A = 143;
  // enable interrupt on compare event
  // (14400 / 144 = 100 per second)
  TIMSK0 |= (1<<OCIE0A);

// the_time will store the elapsed time
// in hundredths of a second.
// (100 = 1 second)
// note that this will overflow in approximately 248 days!
// This variable is marked "volatile" because it is modified
// by an interrupt handler.  Without the "volatile" marking,
// the compiler might just assume that it doesn't change in 
// the flow of any given function (if the compiler doesn't
// see any code in that function modifying it -- sounds 
// reasonable, normally!).
// But with "volatile", it will always read it from memory 
// instead of making that assumption.
volatile int32_t the_time;

  // when Timer0 gets to its Output Compare value,
  // one one-hundredth of a second has elapsed (0.01 seconds).

int main() {

  // init lcd
  FILE lcd_stream = FDEV_SETUP_STREAM(lcd_putchar, 0, _FDEV_SETUP_WRITE);

  // init serial port
  FILE uart_stream = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
  stdin = stdout = &uart_stream;

  // turn on interrupt handler

  while(1) {
    fprintf_P(&lcd_stream, PSTR("%16.2f sec"), (double) the_time / 100.0);

  return 0;

Now I would have to just grab the the countdown code and make it SPI output instead of LCD. Also associate a Start and Stop button. This will be a challenge hope I can make the time to do it, it would be fun.

I have your I2C RTC project somewhere, I have not touched it for a couple of years though. Do you have the link for that project? I never did figure out how to use this for anything but the time I think there are some built in timer functions.


March 11, 2014
by Noter
Noter's Avatar

Definitely you will be happier with an RTC chip like the DS3232 for a long timeout period. The DS3232 has an alarm function that you could set for an interrupt when the time comes.

I think you will also get more done in an arduino environment. You will find many examples/samples and not have to do much programming on your own to get things going. I've played quite a bit with arduino over the last year and it really is very easy compared to the nerdkits bare bones approach where you read the spec and write all the code. Just for fun I put the arduino bootloader on my nerdkit chip a few months ago and tried their DS3232 sample sketch and it worked great. All together it took about 15 minutes to have it up and running. Took only a few more minutes to hookup and read the temp from several DS18B20 temp sensors using another sample sketch. Likewise with a DHT11 sensor. Had them all working in less than an hour just using sample code. I can see why arduino is so popular, especially among hobbyists that don't have strong programming skills.

March 12, 2014
by Rick_S
Rick_S's Avatar

Looks like my RTC clock page has been eaten up by the crash here... You may find it on your wayback machine link. However, I'd have to agree with Paul, if you want to go the easier proven route use your arduino. I'm sure you can find libraries for not only the DS3232 (or any other real time clock IC for that matter), but also the MAX7219 modules you got on ebay. That will get all the more complicated programming done and get your project up and running more quickly.

March 12, 2014
by JimFrederickson
JimFrederickson's Avatar

If your intent is for a "true interval timer", that does not have any need to know the "time-of-day", that does not begin/end it's function at a specified time of day, and the timing/measuring interval is less than 1 hour then I wouldn't bother with the RTC.

An RTC is nice, and it has it's place, but it will add a significant percentage of cost to your project.

Temperature changes, power from the power supply, quality of the crystal, quality of the resistors, quality of the capacitors can all affect the accuracty of your time-base. (Not so much now, but years ago many electronic watches actually took into account the constancy of your body temperature to help keep their time accurate.)

I have been a longtime fan of Sparkfun. Not only for their "Products", but their information.

One of the things that people don't pay attention to is that nearly all "Sparkfun Products" have their schematics and software source available for them as well. (So you can use that information to help in your design if you do your own from scratch, or just for ideas on how certain things can be done.)

There are "SOME" of their 4 Digit 7 Segment displays, and kits with displays, rely on PWM to limit current across the LED Segments. So that would need to be taken into account too if you were using their schematics, or altering their software.

On the schematic the display that Rick linked to does seem to have current limiting resistors.

The brain of that kit is the Mega328. So just "add a socket" and you could use the Supplied Mega328 with the display, or you could program your own using your ISP and then that board could probably serve as the hardware for your project.

Sparkfun Clock Kit

There is also the above Sparkfun kit too!

You can look at the software source, and there are some push buttons on their board already. (NOTE: This kit is one that uses PWM to limit current to the LED's so if you make your own "custom code" that needs to be taken into account.)

You could develope your code using your Nerdkit Mega328 with the LCD and then once you have your RTC/Interval Timing Code you could then adapt that code to the LED 7 Segment Display.

March 13, 2014
by Ralphxyz
Ralphxyz's Avatar

Thanks everyone, nice to have some conversations.

My elaspe timer will be runnings for months or more.

The LEDs will only be lit on button press for 30 seconds (more code).

This project isn't on my critical list but would be nice for the future.

I am still trying to get my head around 3D printing, I actually am close to getting my first 3D printer working.


March 13, 2014
by Noter
Noter's Avatar

I like Sparkfun but seldom buy from them anymore because their price is at least double the eBay or Mouser price for the same or equivalent item. However, I still reference their open source and schematics on occasion and it's always fun to browse their breakout boards and new stuff.

March 14, 2014
by Rick_S
Rick_S's Avatar

Pretty much the same here, Sparkfun is fun to browse, but too overpriced on most items for my taste. I do love their business model though.

March 15, 2014
by JimFrederickson
JimFrederickson's Avatar

Don't forget to update us on your 3d Printer...

I have been VERY CURIOUS... (And I am sure others are as well...)

Post a Reply

Please log in to post a reply.

Did you know that the sparks present in motors can be dangerous for your microcontroller and other electronics? Learn more...