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 » Running the 2nd clock

June 06, 2010
by carlhako
carlhako's Avatar

Hi

I have put together a simple temp sensor that uses the first clock @ 900hz to multiplex 3 7seg displays.

I want to run a 2nd clock to keep rough time. This will allow me to work out some averages, 24hr, 12hr etc.

Does anybody have some sample code that runs a 2nd clock, or could point in the right direction. Ive tried working it out using the data sheet its still a little over my head.

Thanks

Carl

June 06, 2010
by mrobbins
(NerdKits Staff)

mrobbins's Avatar

Hi Carl,

The ATmega168 and ATmega328P have three timer/counter modules, named Timer/Counter 0, 1, and 2. Our Crystal Real Time Clock video shows the use of Timer/Counter 0, but this can be modified for one of the other timers if you wish. Watch that video before you try to move it to a different timer.

For example, to modify it to run on Timer/Counter #2, you would have to make these changes:

1) Change the name of the interrupt handler from SIGNAL(SIG_OUTPUT_COMPARE0A) to the new notation ISR(TIMER2_COMPA_vect).

2) Change all of the register names and settings in the realtimeclock_setup function. Maybe something like:

// set mode CTC, with max as OCR2A
TCCR2A |= (1<<WGM21);
// set prescale CLK/1024
TCCR2B |= (1<<CS22) | (1<<CS21) | (1<<CS20);
// count 144 times (0 to 143)
OCR2A = 143;
// enable interrupt on compare match
TIMSK2 |= (1<<OCIE2A);

The only thing that really changed from Timer/Counter0 is that different bits were required to set the clock prescaler to CLK/1024.

Hope that helps!

Mike

June 07, 2010
by carlhako
carlhako's Avatar

Thanks Mike :)

Exactly what I was after. I have not tried it yet, I'm currently soldering it all together onto a project board. That code looks like it will pretty much work if I copy and paste it.

Thanks again!

Carl

June 14, 2010
by banerjen
banerjen's Avatar

From where do I get to know about all the names of the different interrupt handlers?

Nandan.

June 14, 2010
by mrobbins
(NerdKits Staff)

mrobbins's Avatar

Hi Nandan,

Take a look at this table. However, you still have to match this to the microcontroller datasheet in order to figure out which is which!

Mike

June 14, 2010
by banerjen
banerjen's Avatar

Thanks a lot.

Nandan.

Post a Reply

Please log in to post a reply.

Did you know that you can make a capacitive proximity sensor with some aluminum foil and paperclips? Learn more...