Turn-key PCB assembly services in prototype quantities or low-volume to mid-volume production runs

Implementing precise single shots with AVR timer

Microcontroller timers are meant to count clock cycles in hardware and there are many ways and configurations where timers are handy. First of all timers are meant to work purely in hardware without utilizing CPU, secondly timers help generate waveforms like PWM, trigger events, count time between events and so on. But there is one use that in most cases is missed and not documented – precise single shots that allow to generate only single pulse shot with precise length. And this method doesn’t require CPU to be involved – just set and forget.

single_timer_shots

The idea of this method lies in using fast PWM (refer to datasheet for more info) mode a bit differently. Normally in this mode timer works by counting to TOP value, somewhere in between there is a MATCH value where timer outputs HIG and drops to LOW when TOP is counted. This way there is a PWM signal generated. Josh came up with idea that in this case we can generate only single pulse by setting MATCH value above the TOP and set initial timer counter value above TOP. This way timer counts and when finds MATCH value it triggers pulse to HIGH level and keeps it until counter reaches MAX value and pulse drops to LOW. Next time when timer counts from zero to TOP it never reaches MATCH value and so no more pulses are generated. This way you get total control of your single shots that length can vary depending on initial settings. Pulse length can be as short as 63ns and there is no CPU involved. There is a C demo for that if you would like to experiment.

[..Source link..]

Bookmark the permalink.

Leave a Reply