This is an old revision of the document!
Necessary knowledge: [HW] Controller module, [HW] User Interface Module, [AVR] Interrupts, [AVR] Counters/Timers, [LIB] Pins, [LIB] Delay, [LIB] Timers, [PRT] Software delay
The goal of these practical exercises is to demonstrate the usage of the interruptions on the example of the counters. The interrupts are program parts which are reacting on the events that occur in the microcontrollers. They are used usually for quick reacting to an event, but they can be used for completing several parallel processes, precisely timed action and saving power. For example, it is possible to make a LED blinking, which’s blinking frequency does not depend on what is happening in the program at the moment.
The following program shows how the counter is set up to make a interrupt. There are used 2 LED-s of the digital module in the program, the state of the red one’s is changed periodically with software delay. The green one’s state is changed when interrupts occur. There is a separate exercise about blinking LED with software delay and this is not explained here. The main goal is to explain the usage of the library of the counters and interrupts.
In the beginning of the program, the 16-bit counter/timer 1 is been set up with the function timer1_init_ctc. With this function the counter CTC clear timer on compare match is been set to the mode where the maximum value of the timer is not 216 – 1 but elective. In the present case the maximal value is set the value of the ICR1 index. The divider of the counter is 1024 and the value of ICR1 is 14400, so when the clock frequency is 14,7456 MHz, the period will be exactly one second. It is easy to calculate:
f = 14745600 Hz / 1024 / 14400 = 1
After allowing the interrupt to achieve the maximum value of the counter 1, allowing to make interrupt must be done at the global level also, that means in the microcontroller. For allowing global interrupts, there is the function sei and for forbidding function cli. For defining the program part of these functions and interrupts, there must be also a header file avr/interrupt.h included. The program part of the interrupt is defined with macro function ISR, which’s parameter is the name of the interrupt vector. The name of the vector of counter 1’s value achievement’s interrupt is TIMER1_CAPT_vect.
// // The Homelab's example of blinking LED which blinks due to counter interruptings. // For comparison to the LED blinking due to interrupts, // there is a software delay blinking LED working parallel. // #include <homelab/pin.h> #include <homelab/delay.h> #include <homelab/timer.h> #include <avr/interrupt.h> // // Determining the pins of the LED-s. // pin led_red = PIN(C, 5); 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); } }
Programmi käivitades on näha, et hoolimata sellest, mida mikrokontroller põhiprogrammis teeb, toimuvad katkestused ja roheline LED vilgub.
Kui programmil lasta töötada mõni minut, tuleb välja oluline aspekt, mida tarkvaralise viite harjutuses nii lihtsalt näha polnud. Kuigi punast LEDi vilgutavas tsüklis olev viide on 1000 ms, siis tegelik aeg, mis kulub iga tsükli täitmiseks, on natukese suurem. Põhjus on selles, et ka LED-i oleku muutmine, viite funktsiooni väljakutsumine ja tsükli täitmine võtavad protsessoril mõned taktid täitmise aega. Tulemusena jääb punase LED-i vilkumine rohelise LED-i vilkumisest pidevalt maha. Just sel põhjusel ei ole soovitatav ka kellaaja loendureid ja muid täpselt ajastatud tegevusi teha viitega, vaid loenduri katkestustega.