This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:examples:timer:periodic_interrupt [2015/11/13 10:46] – heikopikner | en:examples:timer:periodic_interrupt [2020/07/20 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Periodic interrupt ====== | ||
| + | |||
| + | //Necessary knowledge: | ||
| + | [HW] [[en: | ||
| + | [AVR] [[en: | ||
| + | [LIB] [[en: | ||
| + | [LIB] [[en: | ||
| + | |||
| + | ===== Theory ===== | ||
| + | |||
| + | The goal of this chapter is to demonstrate the usage of the interrupts on the example of the counters. The interrupts are program parts which are reacting to the events taking place in the microcontrollers. They are usually used for quick response to an event, but they can also be used for completing several parallel processes, precisely timed action and saving power. For example, it is possible to make a LED blinking using interruptions, | ||
| + | |||
| + | ===== Practice ===== | ||
| + | |||
| + | The following program shows how the counter is set up to make an interrupt. There are 2 LEDs of the Digital i/o module in the program, the state of the red LED is changed periodically with software delay, the state of the green LED is changed when interrupts occur. There is a separate exercise for blinking LED with software delay and it is not explained here. The main goal is to explain the usage of the library of the counters and interrupts. | ||
| + | |||
| + | The following shows the use of interrupts of the xmega controller. In the beginning of the program, the 16-bit counter/ | ||
| + | |||
| + | period = (32000000 Hz / 1024 / 1) - 1 = 31249 | ||
| + | |||
| + | After allowing the interrupt to achieve the maximum value of the counter 1, an interrupt must be allowed at the global level, which means over the entire microcontroller. The global interrupts can be enabled by function sei and forbidding with cli. A header file avr/ | ||
| + | |||
| + | <code c> | ||
| + | // HomeLab III example of blinking LED with counter interrupt | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | // Interruption | ||
| + | ISR(TCE1_OVF_vect) | ||
| + | { | ||
| + | // Changing the state of the green LED | ||
| + | pin_toggle(led_green); | ||
| + | } | ||
| + | |||
| + | // Main program | ||
| + | int main(void) | ||
| + | { | ||
| + | // Setting the pins of the LEDs as outputs | ||
| + | pin_setup_output(led_green); | ||
| + | |||
| + | // Setting the period of timer E1 | ||
| + | // F_CPU/ | ||
| + | // 32000000 / 1024 / 1 - 1 = 31249 | ||
| + | TC_SetPeriod(& | ||
| + | |||
| + | // Setting the clock of timer E1 (F_CPU/ | ||
| + | TC1_ConfigClockSource(& | ||
| + | // Setting timer E1 to the normal operating mode | ||
| + | TC1_ConfigWGM(& | ||
| + | |||
| + | // Enabling high-priority overflow interruptions | ||
| + | TC1_SetOverflowIntLevel(& | ||
| + | |||
| + | // Enabling high-priority interruptions | ||
| + | PMIC.CTRL |= PMIC_HILVLEN_bm; | ||
| + | // Enabling global interruption | ||
| + | sei(); | ||
| + | |||
| + | // Endless loop | ||
| + | while (1) { } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | Example of interrupt is quite different between ATmega series (in this example ATmega2561) controllers, | ||
| + | |||
| + | In the beginning of the program, the 16-bit counter/ | ||
| + | |||
| + | f = 14745600 Hz / 1024 / 14400 = 1 | ||
| + | |||
| + | <code c> | ||
| + | // The HomeLab II example of blinking LED with counter interrupt | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | // Interruption | ||
| + | ISR(TIMER1_CAPT_vect) | ||
| + | { | ||
| + | // Changing the state of the green LED | ||
| + | pin_toggle(led_green); | ||
| + | } | ||
| + | |||
| + | // Main program | ||
| + | int main(void) | ||
| + | { | ||
| + | // Setting the pins of the LEDs as outputs | ||
| + | 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 (1){ } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | 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. | ||
| + | |||