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 18:08] – 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 |
| - | ===== Praktika | + | ===== Practice |
| - | Järgnev programm näitab, kuidas seadistada loendurit katkestust tekitama. Programmis on kasutusel | + | The following program shows how the counter is set up to make an interrupt. There are 2 LEDs of the Digital |
| - | Programmi alguses toimub | + | The following shows the use of interrupts of the xmega controller. In the beginning of the program, the 16-bit counter/timer E1 has been set up. First, the timer period will be set, so the maximum value of the count function TC_SetPeriod. The divider of the counter is 1024 and the value of period is 31249, so when the clock frequency is 32 MHz, the period will be exactly one second. It is easy to calculate with following formula: |
| - | f = 14745600 | + | period |
| - | Pärast loendur | + | After allowing the interrupt to achieve the maximum value of the counter |
| <code c> | <code c> | ||
| - | // | + | // HomeLab III example of blinking |
| - | // Kodulabori loenduri katkestusega vilkuva | + | |
| - | // Võrdluseks katkestusega vilkuvale LED-ile | + | |
| - | // töötab paralleelselt ka tarkvaralise viitega vilkuv LED. | + | |
| - | // | + | |
| #include < | #include < | ||
| #include < | #include < | ||
| Line 28: | Line 28: | ||
| #include < | #include < | ||
| - | // | + | // Interruption |
| - | // LED-ide viikude määramine | + | ISR(TCE1_OVF_vect) |
| - | // | + | { |
| - | pin led_red | + | // Changing the state of the green LED |
| - | 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(&TCE1, TC_CLKSEL_DIV1024_gc); | ||
| + | // 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 |
| - | // Katkestus | + | |
| - | // | + | |
| ISR(TIMER1_CAPT_vect) | ISR(TIMER1_CAPT_vect) | ||
| { | { | ||
| - | // Rohelise | + | // Changing the state of the green LED |
| pin_toggle(led_green); | pin_toggle(led_green); | ||
| } | } | ||
| - | // | + | // Main program |
| - | // Põhiprogramm | + | |
| - | // | + | |
| int main(void) | int main(void) | ||
| { | { | ||
| - | // LED-ide viikude väljundiks seadmine | + | // Setting the pins of the LEDs as outputs |
| - | pin_setup_output(led_red); | + | |
| pin_setup_output(led_green); | pin_setup_output(led_green); | ||
| - | // Taimeri seadistamine | + | // Seting the timer up in the CTC mode |
| timer1_init_ctc( | timer1_init_ctc( | ||
| TIMER1_PRESCALE_1024, | TIMER1_PRESCALE_1024, | ||
| TIMER1_CTC_TOP_ICR); | TIMER1_CTC_TOP_ICR); | ||
| - | // Taimeri maksimaalne väärtus | + | // The maximal value of the timer is 14400, which |
| - | // teeb perioodi pikkuseks 1s | + | // makes the length of the period 1 s |
| - | // Valem: 14,7456Mhz / 1024 = 14400 | + | // Formula: 14,7456Mhz / 1024 = 14400 |
| timer1_set_input_capture_value(14400); | timer1_set_input_capture_value(14400); | ||
| - | // Väärtuse saavutamise katkestuse lubamine | + | // Allowing interruption of achieving the value |
| timer1_input_capture_interrupt_enable(true); | timer1_input_capture_interrupt_enable(true); | ||
| - | // Globaalne katkestuste lubamine | + | // Allowing global interruption |
| sei(); | sei(); | ||
| - | // Lõputu tsükkel | + | // Endless loop |
| - | while (true) | + | while (1){ } |
| - | { | + | |
| - | // Tarkvaraline paus 1000 millisekundit | + | |
| - | sw_delay_ms(1000); | + | |
| - | + | ||
| - | // Punase LED oleku muutmine | + | |
| - | pin_toggle(led_red); | + | |
| - | } | + | |
| } | } | ||
| </ | </ | ||
| - | Programmi käivitades on näha, et hoolimata sellest, mida mikrokontroller põhiprogrammis teeb, toimuvad katkestused ja roheline | + | 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. |
| - | 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, | ||
en/examples/timer/periodic_interrupt.1267718916.txt.gz · Last modified: (external edit)
