Both sides previous revisionPrevious revisionNext revision | Previous revision |
en:examples:timer:hardware_delay [2010/03/04 15:30] – priitj | en:examples:timer:hardware_delay [2020/07/20 09:00] (current) – external edit 127.0.0.1 |
---|
===== Theory ===== | ===== Theory ===== |
| |
The software delay is not the only method for creating breaks. The same can be done using timer. Timer is a hardware which counts up or counts down with a certain frequency. The clock frequency of the timer can be generated from microcontroller’s frequency or from some kind of other outside pace. The clock frequency can usually be divided – to get smaller frequency. This is done with prescaler. Important fact is that the fixed clock frequency timer’s value is related to the time linearly. The time can be calculated by multiplying the period of the clock frequency of the timer with the value of the timer | The software delay is not the only method for creating breaks. The same can be done using timer. Timer is a hardware which counts up or down with a certain frequency. The clock frequency of the timer can be generated from microcontroller’s frequency or from some kind of other outside pace. In general the clock frequency can be divided with a multiplier to reach a smaller frequency - this is done with prescaler. Important fact is that the fixed clock frequency timer’s value is linearly related to the time. The time can be calculated by multiplying the period of the clock frequency of the timer with the value of the timer. |
| |
[{{ :examples:timer:timer_counter.png?300|The events which come with the changes of AVR timer.}}] | [{{ :examples:timer:timer_counter.png?300|The events which come with the changes of AVR timer.}}] |
| |
AVR counters can be made informing about overflow of the counter or achieving compare mach. Overflow happens when the counter has the maximal possible value and it start all over again form 0. The controlling of achieving the value is done on the moment of growth of the counter’s value and it is done by comparing the new value with the value given by the user. On the occurrence of the event, the bits in the status indexes of the AVR are automatically set as high. | AVR counters can be made to inform about overflow of the counter or achieving compare mach. Overflow happens when the counter has the maximal possible value and the cycle starts all over again form 0. With reaching a pre set value during the moment of growth of the counter’s value it is compared to the value given by the user. On the occurrence of the event, the bits in the status indexes of the AVR are automatically set as high. |
| |
For generating a delay using a timer, it is sufficient when only the timer is set and waiting for the status bit to go high. Different from the software delay, the work of the timer is not depending on the compiler, it makes using them more reliable. On the other hand the setup of the AVR counter may be fairly troublesome, thanks to their diversity (or their complexity). Depending on the microcontroller’s timing signal, may happen that it will not divide exactly with the desired delay period and the delay will nit be accurate. | For generating a delay using a timer, it is only necessary to set the timer and waiting for the status bit to go high. Different from the software delay, the work of the timer is not depending on the compiler, which makes them more reliable. At the same time the diversity (or complexity) of the set-up of the AVR counter can be considered fairly troublesome. Depending on the microcontroller’s timing signal, may happen that it will not divide exactly with the desired delay period and the delay will not be accurate. |
| |
===== Practice ===== | ===== Practice ===== |
The program code below is a delay function basing on a timer, which is a little bit simplified. The principle of counting is the same as it is at software delay function – a desired amount of 1 ms long delays are produced. The delay is produced with an 8-bit ATmega 128 counter 0. It is calculated previously that at clock frequency 14,7456 Mhz the timing signal has to be divided at least 64 times, so that the counter would not reach to overflow in 1 ms. The value which that the counter must have so that the overflow occurs after 1 ms presented on the form of expression and the variable is //timer_start//. //F_CPU// is a constant in macro-language, which shows clock frequency in Hz. This clock frequency should be 25,6 at the moment but since we can not use fractions, the initial value will be 26. Unfortunately arises here a mistake in delay time, but it is fairly small (-1,7 μs). | The program code below is a delay function based on a timer, which is simplified a little bit. The principle of counting is the same as it is at software delay function – a desired amount of 1 ms long delays are produced. The delay is produced with an 8-bit ATmega 128 counter 0. It is calculated previously that at clock frequency 14,7456 Mhz the timing signal has to be divided at least 64 times, so that the counter would not reach to overflow in 1 ms. The value which the counter must have so that the overflow occurs after 1 ms is presented in the form of an expression and the variable is //timer_start//. //F_CPU// which is a constant in macro-language, that shows clock frequency in Hz. The clock frequency should be 25,6 at the moment but since fractions can not be used, the initial value will be set 26. Unfortunately here arises a mistake in delay time, however it is fairly small (-1,7 μs). |
| |
In the cycle takes place initialing of the counter and zeroing the flag of the overflow (by writing 1 into that). After which is waited until the counter counts to 256 from the initial value, i.e. to the overflow. At the moment of the overflow the flag goes high and the delay of 1 ms id happened. In the end of the function the timer is stopped. | In the cycle takes place initialing of the counter and zeroing the flag of the overflow (by writing 1 into that). Then is waited until the counter counts to 256 from the initial value, i.e. to the overflow. At the moment of the overflow the flag goes high and the delay of 1 ms has taken place. In the end of the function the timer is stopped. |
| |
| |
</code> | </code> |
| |
The following is similar program as it was in the example of the software delay. In the shorter 100 ms half-period the LED is lit and on the longer 900 ms half-period it is switched off. The result is that the LED is blinking after every second. Unfortunately, in this example the period isn’t exactly 1 second, because executing other functions of the program takes also time. For exact timing a 16-bit timer with interruptions must be used. | The following is a similar program to the example of the software delay. In the shorter 100 ms half-period the LED is lit and on the longer 900 ms half-period it is switched off. As the result the LED is blinking after every second. Unfortunately, in this example the period isn't precisely 1 second either, because executing other functions of the program takes also time. For exact timing a 16-bit timer with interruptions must be used. |
| |
<code c> | <code c> |
// | // |
// Demonstration program of harware delay of the Homelab. | // Demonstration program of hardware delay of the HomeLab. |
// The Program blinks LED for a moment after every ~1 second. | // The Program blinks LED for a moment after every ~1 second. |
// | // |