| Both sides previous revisionPrevious revisionNext revision | Previous revision |
| pt:examples:timer:delay [2015/12/14 12:40] – artica | pt:examples:timer:delay [2020/07/20 09:00] (current) – external edit 127.0.0.1 |
|---|
| ~~PB~~ | <pagebreak> |
| ====== Espera ====== | ====== Espera ====== |
| |
| </code> | </code> |
| |
| Although it seems that the LED blinks in every 1 second, the time is actually a little bit longer, because the callouts of LED’s and delay functions are taking a couple of clock rates of the microcontroller | Embora pareça que o indicador pisca a cada segundo, o tempo é na verdade um pouco mais, porque as chamadas das funções de espera e do LED tomam um par de ciclos de relógio do microcontrolador. |
| |
| 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 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). | O código que se segue é uma função de espera com base num temporizador um pouco simplificado. O princípio de contagem é o mesmo da função de espera de software - uma quantidade desejada de esperas de 1 ms são produzidas. O atraso é produzido com um contador 0 de 8 bits. É calculado anteriormente que à frequência do relógio de 14,7456 MHz, o sinal de sincronismo tem de ser dividido, pelo menos, 64 vezes, de modo a que o contador não chegue ao overflow em 1 ms. O valor que o contador deve ter para que o overflow ocorra após 1 ms é apresentado sob a forma de uma expressão sendo a variável //timer_start//. //F_CPU// que é uma constante na macro-linguagem, mostra a freqüência de relógio em Hz. A frequência de relógio deve ser 25,6 no momento, mas uma vez que fracções não podem ser usadas, o valor inicial será estabelecido em 26. Infelizmente aqui surge um erro no tempo de espera, sendo no entanto, bastante pequeno (-1,7 μs). |
| |
| 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. | No ciclo ocorre a inicialização do contador e a da flag de overflow (que será 1 quando ocorre). De seguida, espera-se até que o contador atinja 256 a partir do valor inicial, ou seja, o overflow. No momento do overflow a flag de overflow passa a high e a espera de 1 ms teve já lugar. No final da função do temporizador é parado. |
| |
| |
| </code> | </code> |
| |
| Referenced delay function uses a timer library whose source code for ATmega controller looks like the following: | A função de espera referenciada usa uma biblioteca de temporizador cujo código-fonte para o controlador ATmega é parecida com o seguinte: |
| |
| <code c> | <code c> |
| </code> | </code> |
| |
| 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. | O que se segue é um programa semelhante ao exemplo de espera de software. Nos meios-períodos mais curtos de 100 ms, o LED é iluminado e nos meios-períodos mais longos é desligado. Como resultado o LED pisca a cada segundo. Infelizmente, neste exemplo, o período não é precisamente um segundo também, porque a execução de outras funções do programa leva tempo também. Para uma temporização exata, um temporizador de 16 bits com interrupções deve ser utilizado. |
| |
| <code c> | <code c> |