en:examples:timer:periodic_interrupt
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:examples:timer:periodic_interrupt [2010/03/04 19:29] – priitj | en:examples:timer:periodic_interrupt [2020/07/20 12: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 ===== | ||
| - | The goal of these practical exercises | + | The goal of this chapter |
| ===== Practice ===== | ===== Practice ===== | ||
| - | The following program shows how the counter is set up to make a interrupt. There are used 2 LED-s of the digital | + | 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 |
| - | In the beginning of the program, the 16-bit counter/ | + | The following shows the use of interrupts of the xmega controller. |
| - | f = 14745600 | + | period |
| - | + | ||
| - | After allowing the interrupt to achieve the maximum value of the counter | + | |
| + | 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> | <code c> | ||
| - | // | + | // HomeLab III example of blinking LED with counter |
| - | // 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 29: | Line 28: | ||
| #include < | #include < | ||
| - | // | + | // Interruption |
| - | // Determining | + | ISR(TCE1_OVF_vect) |
| - | // | + | { |
| - | pin led_red | + | // Changing |
| - | pin led_green = PIN(C, 3); | + | 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(&TCE1, 31249); | ||
| + | |||
| + | // 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 | // 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) | ||
| { | { | ||
| - | // Seting | + | // Setting |
| - | 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 59: | 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); | + | |
| - | } | + | |
| } | } | ||
| </ | </ | ||
| - | At the start of the program it can be seen that regardless of what the microcontroller is doing in the main program, the interrupts are happening | + | 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 aspect occurs that we just did not see during software delay exercise. Although in the cycle which blinks the red LED is the delay 1000 ms, the actual time for completing the full cycle is a little bit longer. It is because, | ||
en/examples/timer/periodic_interrupt.1267723778.txt.gz · Last modified: (external edit)
