AVR microseconds ticking. - Page 1
Forum section: electronics / general

author: firewalker
author_rank: Super Contributor
timestamp: April 16, 2015, 06:03:07 am
content: Every time I needed a tick counter in an AVR project, a tick every millisecond was enough. Timer/Counter0 with prescalled and pre-loading the TCNT0 with the appropriate value.Is a 10 microsecond tick doable on a 16 MHz timed AVR, with the same method?Alexander.

author: Psi
author_rank: Super Contributor
timestamp: April 16, 2015, 06:16:34 am
content: You can do it but at 100khz you only have ~80 instructions before the next tick.Assuming a 8mhz xtal.If your polling the timer in you main loop you will need super simple logic to keep up.If using an interrupt you will want to keep it super short. Like under 10 C statements. And keep main fast tooWhat are you using the tick for, if you are counting ticks of some external event to work out the duration, like pulse lengh, use the timer to sample the event itself

author: matseng
author_rank: Frequent Contributor
timestamp: April 16, 2015, 06:19:50 am
content: Can you have a 10uS interrupt? Yes - why not?But if it will actually work in your particular project depends on what you do in your interrupt handler. At 16MHz the processor can do at a maximum 160 instructions during a 10 uS period. If you need more processing that that you're out of luck. I've done it before, but that was just some simple stuff like incrementing a counter and flipping a gpio when a certain number was reached.

author: firewalker
author_rank: Super Contributor
timestamp: April 16, 2015, 06:34:12 am
content: It will be something like:volatile uint32_t tensofusecs;ISR(TIMER0_OVF_vect){uint32_t temp;temp = tensofusecs;temp++;tensofusecs = temp;TCNT0 += TIMER0_PRELOAD_VAL;}Translates to 25~30 instructions?Alexander.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: April 16, 2015, 06:48:14 am
content: uint32 inside an ISR? That's gonna be a lot longer... don't forget to add at least four pushes and four pops; or loading, adding and storing each byte in sequence. Just as bad either way.Using it as a prescaler so the big math is done every e.g. 1ms would allow using a uint8_t inside the heart of it.But really: why not leave the timer free running? You can atomically poll the timer itself at any time, and if it's set to increment every about 10us (8us would be the closest, 16MHz div 128 prescaler), you can poll it at any time to find the number of counts. And use the timer-clear interrupt for a slower 1ms heartbeat or whatever.The hardware is rich, use it to your advantage!Tim

author: firewalker
author_rank: Super Contributor
timestamp: April 16, 2015, 07:16:17 am
content: Free running with a prescaller of 64 the timer would tick every 4 usecs. I would generate an overflow interrupt every 1024 usecs. Close to 1 msec.Microseconds passed: (TCNT0 + OVERFLOWS*256)*4Correct?Alexander.

author: westfw
author_rank: Super Contributor
timestamp: April 16, 2015, 07:59:14 am
content: That's about how the "micros()" function on arduino works...Interrupt on overflows, micros = nOverflows*overflowtime + timerTicks*ticktime...

author: firewalker
author_rank: Super Contributor
timestamp: April 16, 2015, 08:15:25 am
content: Didn't knew Arduino was offering microseconds. I knew about millis().Took a quick look at the code. They are doing error correction for the millseconds. I guess this is the way to go.Thanks you guys.Alexander.

author: ralphd
author_rank: Frequent Contributor
timestamp: April 20, 2015, 03:10:16 pm
content: I wrote a timer interrupt counter routine that has a minimum execution time of 30 cycles (in the normal case of only incrementing the LSB). If you don't have a correction factor then you can knock 3 cycles off that. Interrupt dispatch/return overhead adds another 8 cycles (7 on AVRs with <=8KB flash).https://github.com/nerdralph/nerdralph/blob/master/avr/t32isr.SUsing a 8-bit counter with a prescaler of 8 on a 8Mhz avr the ISR will trigger every 2048 cycles and take < 2% CPU time. With LSB in the counter register and 32-bits for the overflow, you would have microsecond-accurate timing over a span of 12 days.Note with my code you need to reset the overflow counter before it reaches the end, otherwise it will tromp on the next byte in memory after the overflow counter. Another option is to make the overflow counter 48 bits, so it would be good for 2,128 years (if the AVR lasts that long!).

author: firewalker
author_rank: Super Contributor
timestamp: April 20, 2015, 03:13:22 pm
content: Nice!Alexander.