This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| fr:examples:timer:hardware_delay [2010/03/09 15:35] – sdeniaud | fr:examples:timer:hardware_delay [2020/07/20 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Temporisation matérielle ====== | ||
| + | |||
| + | //Necessary knowledge: [HW] [[en: | ||
| + | |||
| + | // | ||
| + | |||
| + | ===== Théorie ===== | ||
| + | |||
| + | La temporisation matérielle n'est pas la seule façon de réaliser des pauses. La même chose peut tout à fait être réalisée par un //timer//. Le //timer// est un matériel (??) qui compte dans un sens ou dans l' | ||
| + | |||
| + | [{{ : | ||
| + | |||
| + | 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' | ||
| + | |||
| + | Pour réaliser une temporisation en utilisant un //timer//, il suffit d' | ||
| + | |||
| + | ===== Pratique ===== | ||
| + | |||
| + | 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' | ||
| + | |||
| + | Dans le cycle on retrouve l' | ||
| + | |||
| + | |||
| + | <code c> | ||
| + | // | ||
| + | // Hardware delay in milliseconds. | ||
| + | // | ||
| + | void hw_delay_ms(unsigned short count) | ||
| + | { | ||
| + | // Calculating the initial value of the timer. | ||
| + | register unsigned char timer_start = 256 - F_CPU / 1000 / 64; | ||
| + | |||
| + | // Starting the timer. | ||
| + | timer0_init_normal(TIMER0_PRESCALE_64); | ||
| + | |||
| + | // Counting the variable of the delay to the 0. | ||
| + | while (count-- > 0) | ||
| + | { | ||
| + | // Initializing the timer. | ||
| + | timer0_set_value(timer_start); | ||
| + | |||
| + | // Zeroing the overflow flag. | ||
| + | timer0_overflow_flag_clear(); | ||
| + | |||
| + | // Waiting for overflow. | ||
| + | while (!timer0_overflow_flag_is_set()) | ||
| + | { | ||
| + | asm volatile (" | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // Zeroing the overflow flag. | ||
| + | timer0_overflow_flag_clear(); | ||
| + | |||
| + | // Stoping the timer. | ||
| + | timer0_stop(); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Le programme suivant est un exemple similaire de temporisation logicielle. Dans la demi-période la plus petite de 100ms la LED s' | ||
| + | |||
| + | <code c> | ||
| + | // | ||
| + | // Demonstration program of harware delay of the Homelab. | ||
| + | // The Program blinks LED for a moment after every ~1 second. | ||
| + | // | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | // | ||
| + | // Determining the pin of the Test LED. | ||
| + | // | ||
| + | pin debug_led = PIN(B, 7); | ||
| + | |||
| + | // | ||
| + | // Main program. | ||
| + | // | ||
| + | int main(void) | ||
| + | { | ||
| + | // Setting the pin of the LED as output. | ||
| + | pin_setup_output(debug_led); | ||
| + | |||
| + | // Endless loop. | ||
| + | while (true) | ||
| + | { | ||
| + | // Lighting the LED. | ||
| + | pin_clear(debug_led); | ||
| + | |||
| + | // Hardware delay for 100 milliseconds. | ||
| + | hw_delay_ms(100); | ||
| + | |||
| + | // Switch off of the LED. | ||
| + | pin_set(debug_led); | ||
| + | |||
| + | // Hardware delay for 900 milliseconds. | ||
| + | hw_delay_ms(900); | ||
| + | } | ||
| + | } | ||
| + | </ | ||