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 » microseconds realtimeclock

February 08, 2010
by lcruz007
lcruz007's Avatar

Hi, I was editing the realtimeclock project. And wanted to create a precise microseconds timer... However, it is NOT working for me, I am getting milliseconds, not microseconds. I am using a crystal of 16MHz for a more precise timer (I would get a multiple of 62.5ns that requires one instruction, 16 instructions = 1.00 us).

This is my code:

void realtimeclock() { // setup Timer0:

// CTC (Clear Timer on Compare Match mode)

// TOP set by OCR0A register

TCCR0A |= (1<<WGM01);

// clocked from CLK/8. 16MHz / 8 prescaler = 2MHz

TCCR0B |= (1<<CS01);

// set TOP to 1 ... 2 events 2000000/2 = 1000000 per second

OCR0A = 1;

// enable interrupt on compare event

// (16000000 / 8 )/ 2 = 1000000

TIMSK0 |= (1<<OCIE0A); }

What am I doing wrong here?


February 09, 2010
by N3Roaster
N3Roaster's Avatar

You've pointed out the problem yourself. You only have 16 clock cycles to respond to your interrupt. Let's take a look at the assembly for an interrupt call. First, you have a JMP to the start of your interrupt service routine. That uses 3 clock cycles. At the start of the ISR, it needs to push the content of any registers used to the stack in order to avoid clobbering whatever was going on at the time the interrupt occurred. Each PUSH uses 2 clock cycles. At the end of the ISR, it needs to pop values from the stack back into registers so the rest of your program can carry on as it was. Each of these is another 2 clock cycles. After that, the MCU has to go back to where it was in your program to resume program execution. The RETI instruction does that which uses another 4 clock cycles. You can probably see that those 16 clock cycles don't go very far. We've just handled getting to and from the ISR without actually doing anything and may well have already run out of time.

If you're using a Makefile based on one of the kit projects, there will be a build target for seeing the assembly code for yourself. Try typing make projectname.ass (substituting projectname as appropriate) and taking a look at that file. At the start of it you'll find a table showing the address where your interrupt handlers are located. Once you've found the entry for the relevant ISR, you can look at the code at that address. Section 31 of the ATmega168 datasheet has a table showing how many clock cycles each of those instructions takes. Count them up. You'll find that you're almost certainly over the limit.

February 09, 2010
by lcruz007
lcruz007's Avatar

Ok, I see... So what the heck can I do to have a timer which can have enough clock cycles to respond to my interrupt and at the same time have a precise timer that counts microseconds? lol

Because I think I need at least to increase a timer every 8 cycles of my crystal. :p

Post a Reply

Please log in to post a reply.

Did you know that multiple microcontrollers can communicate with each other? Learn more...