This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:avr:timers [2010/07/26 17:43] – corrected typos Wember | 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' |
- | In the default mode, a counter does nothing more than count sequential numbers | + | In the default mode, a counter does nothing more than continually |
<box 100% round # | <box 100% round # | ||
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. |
</ | </ | ||
- | ==== External | + | ==== External |
It is also possible to use an external clock source as a counter' | It is also possible to use an external clock source as a counter' | ||
- | ==== Timing | + | ==== Timing |
Since the counters allow timing operations, more complex AVR microcontrollers have an option to time specific events on a hardware level. This part of the counter is called an input capture unit. There is a choice between two events: the logical change in the value of a special input pin or in the value of the analog comparator result. If the selected event occurs, the counter' | Since the counters allow timing operations, more complex AVR microcontrollers have an option to time specific events on a hardware level. This part of the counter is called an input capture unit. There is a choice between two events: the logical change in the value of a special input pin or in the value of the analog comparator result. If the selected event occurs, the counter' | ||
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 103: | Line 103: | ||
</ | </ | ||
- | The program fires an interrupt each time a rising front occurs in the external signal. During the interrupt, the counter is checked for overflows - this can happen if the frequency of the signal is below 122 Hz (8 MHz / 2< | + | The program fires an interrupt each time a rising front occurs in the external signal. During the interrupt, the counter is checked for overflows - this can happen if the frequency of the signal is below 122 Hz (8 MHz / 2< |
</ | </ | ||
- | Catching events and registering the time it took for them to occur can also be resolved at the software level. It is possible to use external or other interrupts and read the value of the counter during these events. The hardware-level event catching is meant to run independently | + | Catching events and registering the time it took for them to occur can also be resolved at the software level. It is possible to use external or other interrupts and read the value of the counter during these events. The hardware-level event catching is meant to run independently |
- | ===== Signal | + | ===== Signal |
- | 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' |
- | The counters and the signal generating modes using them are one of the most complex peripheral modules in an AVR. Writing about all of them here would be a huge waste of time and typically there is no need to know everything | + | The counters and the signal generating modes using them are one of the most complex peripheral modules in an AVR. Writing about all of them here is beyond the scope of this text, and typically there is no need to know all aspects |
==== Pulse Width Modulation ==== | ==== Pulse Width Modulation ==== | ||
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> |