Both sides previous revisionPrevious revisionNext revision | Previous revision |
fr:examples:timer:hardware_delay [2010/03/09 13:36] – sdeniaud | fr:examples:timer:hardware_delay [2020/07/20 09:00] (current) – external edit 127.0.0.1 |
---|
[{{ :examples:timer:timer_counter.png?300|Les évenements qui correspondent au changement du //timer// AVR.}}] | [{{ :examples:timer:timer_counter.png?300|Les évenements qui correspondent au changement du //timer// AVR.}}] |
| |
Les compteurs 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. | Les compteurs AVR peuvent être utilisés pour informer d'un franchissement de limite ou pour atteindre une valeur de comparaison. Le franchissement de la limite est atteint lorsque la valeur du compteur arrive à sa valeur maximale, dans ce cas le cycle redémarre à 0. Pendant l'incrémentation du compteur, chacune des valeurs du compteur sont comparées à la valeur objectif pré-configurée par l'utilisateur. Lorsque ces conditions sont remplies, le bit des indexes de statut correspondants sont automatiquement configurés en position haute. |
| |
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. | Pour réaliser une temporisation en utilisant un //timer//, il suffit d'initialiser le //timer// et d'attendre que le bit de statut passe en position haute. Cela est différent dans le cas de la temporisation logiciel, le travail du //timer// ne dépend pas du compilateur, ce qui le rend plus fiable. 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 ===== | ===== Pratique ===== |
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). 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. | Le code du programme ci-dessous est une fonction de temporisation basée sur un //timer// simplifié. Le principe de comptage est le même que celui d'une fonction de temporisation logicielle – on fabrique une temporisation de longueur 1ms. La temporisation est réalisée avec un compteur 0 de ATmega 128 avec 8-bits. Il à été calculé précédemment que la fréquence de l'horloge est de 14,7456 Mhz le signal de temps doit être divisé 64 fois, donc le compteur ne pourra pas dépasser la limite de 1ms. La valeur que doit prendre le compteur est présenté sous la forme d'une expression dont la variable est ''timer_start''. ''F_CPU'' qui est une constante en langage macro, renvoi la fréquence de l'horloge en Hz. La fréquence de l'horloge doit être 25,6 pour le moment mais depuis qu'on ne peut plus utiliser les valeurs décimales, la valeur initiale sera 26. Malheureusement il survient une erreur dans le temps de la temporisation, qui peut être cependant négligeable (-1,7 μs). |
| |
| Dans le cycle on retrouve l'initialisation du compteur et la mise à zéro de la limite (en inscrivant 1). Puis il attend que le compteur atteigne la valeur 256 en partant de la valeur initiale, i.e. par rapport à la limite. A ce moment la balise prend une valeur haute et 1 ms est passée. A la fin de la fonction le //timer// est arrêté. |
| |
| |
</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. | Le programme suivant est un exemple similaire de temporisation logicielle. Dans la demi-période la plus petite de 100ms la LED s'allume et dans la plus grande de 900ms elle s'éteint. Ainsi la LED clignote toutes les secondes. Malheureusement, dans cet exemple la période n'est pas précisément d'une seconde, parce que l'exécution des fonction du programme prend aussi du temps. Pour un timing tout à fait exact on doit utiliser un //timer// 16-bits avec interruptions. |
| |
<code c> | <code c> |