This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:avr:timers [2010/11/29 08:19] – allan.pettai | en:avr:timers [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Counters/ | ====== Counters/ | ||
- | Counters, which in some sense can be called timers, are one of the most important sub-functions of a microcontroller. These enable to precisely time processes, generate signals and count events. A counter converts the number of input cycles to a binary value using an array of triggers. The maximum number of counted cycles depends on the length of this array and this is marked by the length of the binary code. AVR has 8- and 16-bit counters. If a timer has reached its maximum value (255 in 8-bit and 65535 in 16-bit counters), the next cycle will generate an overflow and the counter resets back to 0. A counter' | + | Counters, which in some sense can be called timers, are one of the most important sub-functions of a microcontroller. These enable to precisely time processes, generate signals and count events. A counter converts the number of input cycles to a binary value using an array of triggers. The maximum number of counted cycles depends on the length of this array, and this is marked by the length of the binary code. AVR has 8- and 16-bit counters. If a timer has reached its maximum value (255 in 8-bit and 65535 in 16-bit counters), the next cycle will generate an overflow and the counter resets back to 0. A counter' |
===== Counter' | ===== Counter' | ||
Line 42: | Line 42: | ||
</ | </ | ||
- | The counter in this example will not generate the interrupt in exactly 10 ms though, in order to do that it would require giving the counter a decimal value and this is not possible. To achieve a precise interval between the interrupts, both the prescaler value and the initial value of the counter have to be chosen so that their division results in an exact number. This is not always possible, especially with 8-bit counters as their value range is quite small. To achieve a more precise or larger interval, a 16-bit counter can be used. | + | The counter in this example will not generate the interrupt in exactly 10 ms, though, in order to do that it would require giving the counter a decimal value, and this is not possible. To achieve a precise interval between the interrupts, both the prescaler value and the initial value of the counter have to be chosen so that their division results in an exact number. This is not always possible, especially with 8-bit counters as their value range is quite small. To achieve a more precise or larger interval, a 16-bit counter can be used. |
</ | </ | ||
Line 70: | Line 70: | ||
// The result is valid only if the counter | // The result is valid only if the counter | ||
- | // has not overflown | + | // has not overflowed |
if (!(TIFR & (1 << TOV1))) | if (!(TIFR & (1 << TOV1))) | ||
{ | { | ||
Line 111: | Line 111: | ||
===== Signal Generating ===== | ===== Signal Generating ===== | ||
- | More complex counters can generate a signal, in addition to timing the length of one. For this purpose the counter has an output compare unit and a compare match output unit. The output compare unit has registers with the same bit-width as the counter and the values of these registers are compared to the value of the counter while it is running. An interrupt can be generated and special pins' values can be changed each time the counter' | + | More complex counters can generate a signal, in addition to timing the length of one. For this purpose the counter has an output compare unit and a compare match output unit. The output compare unit has registers with the same bit-width as the counter and the values of these registers are compared to the value of the counter while it is running. An interrupt can be generated and special pins' values can be changed each time the counter' |
In some signal generating modes, the counter' | In some signal generating modes, the counter' | ||
Line 123: | Line 123: | ||
<box 100% round # | <box 100% round # | ||
- | Task: using an 8MHz ATmega128, generate two speed regulating servo motor signals. Use pin PB5 (OC1A) to generate a pulse width of 1 ms and pin PB6 (OC1B) to generate pulse width of 2 ms. | + | Task: Using an 8MHz ATmega128, generate two speed regulating servo motor signals. Use pin PB5 (OC1A) to generate a pulse width of 1 ms and pin PB6 (OC1B) to generate pulse width of 2 ms. |
<code c> | <code c> |