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 07:57] – heikopikner | en:examples:timer:periodic_interrupt [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Periodic interrupt ====== | ====== Periodic interrupt ====== | ||
- | //Necessary knowledge: | + | //Necessary knowledge: |
+ | [HW] [[en: | ||
+ | [AVR] [[en: | ||
+ | [LIB] [[en: | ||
+ | [LIB] [[en: | ||
===== Theory ===== | ===== Theory ===== | ||
Line 15: | Line 19: | ||
period = (32000000 Hz / 1024 / 1) - 1 = 31249 | period = (32000000 Hz / 1024 / 1) - 1 = 31249 | ||
- | In the beginning | + | After allowing |
- | f = 14745600 Hz / 1024 / 14400 = 1 | + | <code c> |
+ | // HomeLab III example of blinking LED with counter interrupt | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
- | 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 | + | // Interruption |
+ | ISR(TCE1_OVF_vect) | ||
+ | { | ||
+ | // Changing | ||
+ | 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> | <code c> | ||
- | // | + | // The HomeLab |
- | // The HomeLab' | + | |
- | // For comparison to the LED blinking due to interrupts, | + | |
- | // there is a software delay blinking LED working parallel. | + | |
- | // | + | |
#include < | #include < | ||
#include < | #include < | ||
Line 33: | Line 78: | ||
#include < | #include < | ||
- | // | ||
- | // Determining the pins of the LEDs. | ||
- | // | ||
- | pin led_red | ||
- | pin led_green = PIN(C, 3); | ||
- | |||
- | // | ||
// Interruption | // Interruption | ||
- | // | ||
ISR(TIMER1_CAPT_vect) | ISR(TIMER1_CAPT_vect) | ||
{ | { | ||
- | // Changing the state of the green LED. | + | // Changing the state of the green LED |
pin_toggle(led_green); | pin_toggle(led_green); | ||
} | } | ||
- | // | + | // Main program |
- | // Main program. | + | |
- | // | + | |
int main(void) | int main(void) | ||
{ | { | ||
- | // Setting the pins of the LEDs as outputs. | + | // Setting the pins of the LEDs as outputs |
- | pin_setup_output(led_red); | + | |
pin_setup_output(led_green); | pin_setup_output(led_green); | ||
- | // Seting the timer up in the CTC mode. | + | // Seting the timer up in the CTC mode |
timer1_init_ctc( | timer1_init_ctc( | ||
TIMER1_PRESCALE_1024, | TIMER1_PRESCALE_1024, | ||
Line 63: | Line 97: | ||
// The maximal value of the timer is 14400, which | // The maximal value of the timer is 14400, which | ||
- | // makes the length of the period 1 s. | + | // makes the length of the period 1 s |
// Formula: 14,7456Mhz / 1024 = 14400 | // Formula: 14,7456Mhz / 1024 = 14400 | ||
timer1_set_input_capture_value(14400); | timer1_set_input_capture_value(14400); | ||
- | // Allowing interruption of achieving the value. | + | // Allowing interruption of achieving the value |
timer1_input_capture_interrupt_enable(true); | timer1_input_capture_interrupt_enable(true); | ||
- | // Allowing global interruption. | + | // Allowing global interruption |
sei(); | sei(); | ||
- | // Endless loop. | + | // Endless loop |
- | while (true) | + | while (1){ } |
- | { | + | |
- | // Software delay 1000 ms. | + | |
- | sw_delay_ms(1000); | + | |
- | + | ||
- | // Change of the state of the red LED. | + | |
- | pin_toggle(led_red); | + | |
- | } | + | |
} | } | ||
</ | </ | ||
Line 87: | Line 114: | ||
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. | 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, | + |