This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| fr:examples:timer:periodic_interrupt [2010/03/09 17:02] – sdeniaud | fr:examples:timer:periodic_interrupt [2020/07/20 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Interruption périodique ====== | ||
| + | //Necessary knowledge: [HW] [[en: | ||
| + | |||
| + | // | ||
| + | |||
| + | ===== Théorie ===== | ||
| + | |||
| + | Le but de ces exercices pratique est de montrer l' | ||
| + | |||
| + | ===== Pratique ===== | ||
| + | |||
| + | Le programme suivant montre comment initialiser un compteur pour utiliser une interruption. On utilisera deux LEDs du module numérique dans le programme, l' | ||
| + | |||
| + | Au départ, le compteur 16-bits est initialisé grâce à la fonction '' | ||
| + | |||
| + | f = 14745600 Hz / 1024 / 14400 = 1 | ||
| + | |||
| + | After allowing the interrupt to achieve the maximum value of the counter 1, an interrupt must be allowed also at the global level, that means in the microcontroller. For allowing global interrupts, there is a function //sei// and for forbidding //cli//. A header file // | ||
| + | |||
| + | |||
| + | <code c> | ||
| + | // | ||
| + | // The Homelab' | ||
| + | // For comparison to the LED blinking due to interrupts, | ||
| + | // there is a software delay blinking LED working parallel. | ||
| + | // | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | // | ||
| + | // Determining the pins of the LED-s. | ||
| + | // | ||
| + | pin led_red | ||
| + | pin led_green = PIN(C, 3); | ||
| + | |||
| + | // | ||
| + | // Interruption | ||
| + | // | ||
| + | ISR(TIMER1_CAPT_vect) | ||
| + | { | ||
| + | // Changing the state of the green LED. | ||
| + | pin_toggle(led_green); | ||
| + | } | ||
| + | |||
| + | // | ||
| + | // Main program. | ||
| + | // | ||
| + | int main(void) | ||
| + | { | ||
| + | // Seting the pins of the LED-s as outputs. | ||
| + | pin_setup_output(led_red); | ||
| + | pin_setup_output(led_green); | ||
| + | |||
| + | // Seting the timer up in the CTC mode. | ||
| + | timer1_init_ctc( | ||
| + | TIMER1_PRESCALE_1024, | ||
| + | TIMER1_CTC_TOP_ICR); | ||
| + | |||
| + | // The maximal value of the timer is 14400, which | ||
| + | // makes the length of the period 1 s. | ||
| + | // Formula: 14,7456Mhz / 1024 = 14400 | ||
| + | timer1_set_input_capture_value(14400); | ||
| + | |||
| + | // Allowing interruption of achieving the value. | ||
| + | timer1_input_capture_interrupt_enable(true); | ||
| + | |||
| + | // Allowing global interruption. | ||
| + | sei(); | ||
| + | |||
| + | // Endless loop. | ||
| + | while (true) | ||
| + | { | ||
| + | // Software delay 1000 ms. | ||
| + | sw_delay_ms(1000); | ||
| + | |||
| + | // Change of the state of the red LED. | ||
| + | pin_toggle(led_red); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | At the start of the program it is seen that regardless of what the microcontroller is doing in the main program, the interrupts are taking place and the green LED is blinking. | ||
| + | |||
| + | If we let the program to work for a couple of minutes, an important aspect occurs, that was not so easily noticeable during software delay exercise. Although the delay in red LED blinking is 1000 ms, the actual time for completing the full cycle is a little bit longer. This is because, | ||