Timers in assembly (Atmega328P)

Hi all,

I'm trying to get the hang of using timers in my AVR project. I decided that for my use the 16Bit timer would fit my needs perfectly, and using the datasheet I have figured out how to set the correct bits for the timer (TCCR1B) and the prescaler I wish to use (1024).

Now, I understand that as this is a 16Bit timer on an 8Bit controller, I have to read the high and low bytes separately - but I'm not sure how to do this.

Basically, I'm trying to set up a timer that can measure an arbitrary passage of time, but I'm not sure how to read out and compare the value of the timer once it is running. I'm very new to assembly so if anyone can offer any advice that'd be a great help.

Thanks in advance

Timers can be manipulated very easily in C, so no need for assembler unless it's just for its own sake. The C compiler handles accessing the 16-bit registers properly. See the datasheet, but basically: To do a 16-bit write, the high byte must be written before the low byte. For a 16-bit read, the low byte must be read before the high byte.

Dean Camera's "Newbie's Guide to AVR Timers" helped me tremendously:

Thanks for the quick reply Jack. I'll check out that link.

I'm keen to stick to assembly because its teaching me a lot more about how the hardware really works. I'd rather avoid the high-level abstraction if I can. I know most people are happy just to get a piece of code to do something, but I want to understand how and why it works, if that makes sense. Also the long-term goal of this is to produce video output, which assembly is of course almost certainly needed for (AFAIK).

I'd rather avoid the high-level abstraction if I can. I know most people are happy just to get a piece of code to do something, but I want to understand how and why it works, if that makes sense.

Sure it does. But, you could always code it in C, and use objdump, on the object file, to see what assembly language instructions that C code translated to.

Thanks Paul, that's a really neat idea :smiley:

triduino:
Now, I understand that as this is a 16Bit timer on an 8Bit controller, I have to read the high and low bytes separately - but I'm not sure how to do this.

Read the datasheet. It has a whole section on this... (section 15.3)