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 » How to create a timer?

February 27, 2012
by lnino
lnino's Avatar

Hi at all.

I am back with a new issue on a project I am working at.

In my if branch I am waiting für some input from the user. When the input has happend the timer should start counting. When the input is over the program continous with the else branch.

In the else branch the timer stops.

Afterwards I want to know how many seconds passed by between the if and else branch.

No Real Code:

      while(1) {

             if (userinput = true)

                // User enters text

             // Output timer_value

How can I realise that? I have really no clue how to do that with my atmega168. Which registers do I have to use where and how in the code?

I know how to get the user input. This is not the problem. I don't know how to get the timer working.

Thanks for your help.

February 28, 2012
by Rocket_Man_Jeff
Rocket_Man_Jeff's Avatar

Hi there.

I'm not sure if you've already read through this but there's an example in the tutorials section of this website that makes a timer/clock using the frequency crystal. If I remember correctly this example uses interrupts to count time elapsing. I'm not sure if it's exactly what you're looking for, but certainly worth a look if you haven't done so already. You could probably find a way to make that work. There are some more crude ways to measure elapsed time too. How accurate does the timer need to be?


February 28, 2012
by lnino
lnino's Avatar

The Counter doent have to be very acurate. I am counting from Zero to 3 Seconds. I Need following steps 0 0,5 1 2 3 Seconds.

I dont Need milliseconds or numbers with float.

I will have a Look at The Example when i am at home.

February 28, 2012
by Rocket_Man_Jeff
Rocket_Man_Jeff's Avatar

Yeah, definitely take a look at the example as it's one of the better ways to handle accurate time keeping without extra hardware. The reason I ask how accurate you need the clock to be is that there are some simpler (one line) ways to keep time, but they often fall into the 'bad programming' category.

The simplest way I know of is just add a 1ms delay line into a 'for', 'do while', etc... loop and increment a counting variable on each iteration. This is a less than ideal approach for a number of reasons - 1) your MCU will be locked up during the delay command. 2) Any other processes or commands will cause potential lag in the counting process leading to eventual creep. 3) You'd probably need to somehow check or calibrate the timer so that one second being counted by the MCU is in fact equal to one second on your watch.

Maybe it's a big deal, maybe it's not - it depends on the application. If I'm measuring shock wave properties in a detonation tube this method certainly wouldn't be accurate enough to give me accurate results. It's up to you to figure out what works best.


March 06, 2012
by lnino
lnino's Avatar

After some tests and studies I think for my application a simple counter would be accurate enough.

When my if brach is true a counter counts up. Simply solved by counter++.

I have connected the nerdkit LCD display to see the incremention. The counter counts fast.

How can I calculate how many counts per second happen? Is it possible to calculate this with the value of the crystal (14.7456MHz)?

This would be the solution for my project. I only have to know how many counts per second occur.

Thanks for your reply.

March 06, 2012
by Ralphxyz
Ralphxyz's Avatar

Inino, that is a incrementer not a counter!! With counter++ you are incrementing counter by one with each hit.

You need to study the specsheet for counters and of course search the forum for "counter".

You need to "count" the number of times a second your if branch is true and for that you have to read the specsheet.


March 06, 2012
by pcbolt
pcbolt's Avatar

Hi Inino -

To do what you are suggesting is certainly possible. In theory, an instruction is executed every clock cycle (in our case one per 70 nsec). The next question you'd then need to answer is how many instructions are executed in each loop of your code. These are "machine code" instructions not "C code" instructions so you have to dig into the assembly files (*.ass files) to see how the compiler created the machine code. This would give a close idea of how much time has elapsed. The "delay" function Rocket_Man_Jeff mentioned does this by just executing 9 NOP (i.e. "no operation") statements in a row and calls that 1 micro-second (uS). This was found by experiment since the looping instructions themselves need to be taken into account. Long story short, for the resolution you want, I'd go with Jeff's idea. If you want better resolution, call the delay_us function and count with a uint32_t type variable. If you want the best resolution, watch the "Realtime Clock" video on the tutorial page then download the source code for that. Once you master that, you won't have problems with timing anymore.

March 07, 2012
by Rick_S
Rick_S's Avatar

One thing, pcbolt, while what you said was mostly accurate, there is something you may not be aware of. I've seen you post a couple of times how "In theory, an instruction is executed every clock cycle (in our case one per 70 nsec)." While this is true for some instructions, many take two or more clock cycles. Just a few commonly used ones would be:

Setting bits in a register takes two, multiplication functions take two, returning from subroutines takes four, etc...

If you are curious about this, there is a full list of all the assembler instructions and exactly how many clock cycles each takes near the end of the datasheet under "Instruction Set Summary"


Post a Reply

Please log in to post a reply.

Did you know that NerdKits make a great parent/child activity? Learn more...