Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:examples:timer:periodic_interrupt [2010/02/08 12:48] mikk.leinien:examples:timer:periodic_interrupt [2020/07/20 09:00] (current) – external edit 127.0.0.1
Line 1: Line 1:
-====== Perioodiline katkestus ======+====== Periodic interrupt ======
  
-//Vajalikud teadmised[HW] [[en:hardware:homelab:controller]], [HW] [[en:hardware:homelab:digi]], [AVR] [[en:avr:interrupts]], [AVR] [[en:avr:timers]], [LIB] [[en:software:homelab:library:pin]], [LIB] [[en:software:homelab:library:delay]], [LIB] [[en:software:homelab:library:timer]], [PRT] [[en:examples:timer:software_delay]]//+//Necessary knowledge 
 +[HW] [[en:hardware:homelab:digi]],   
 +[AVR] [[en:avr:interrupts]], [AVR] [[en:avr:timers]], \\ 
 +[LIB] [[en:software:homelab:library:pin]],   
 +[LIB] [[en:software:homelab:library:timer]]//
  
-===== Teooria =====+===== Theory =====
  
-Käesoleva praktilise harjutuse eesmärk on demonstreerida katkestuste kasutamist loendurite näitelKatkestused on mikrokontrolleris esinevatele sündmustele reageerivad programmilõigudKatkestusi kasutatakse tavaliselt kiiresti sündmusele reageerimisekskuid neid võib kasutada ka mitme paralleelse protsessi täitmisekstäpselt ajastatud tegevuseks ning voolu säästmiseksNäiteks on katkestuste abil võimalik panna vilkuma LED, mille vilkumise sagedus ei sõltu sellestmis programmis parasjagu toimub+The goal of this chapter is to demonstrate the usage of the interrupts on the example of the countersThe interrupts are program parts which are reacting to the events taking place in the microcontrollersThey are usually used for quick response to an eventbut they can also be used for completing several parallel processesprecisely timed action and saving powerFor example, it is possible to make a LED blinking using interruptionsso that blinking frequency does not depend on what is happening in the program at the moment. When the interrupt occur then main program execution stopped and the interrupt priority check of the interrupt vector table happenafter that the program of the interrupt function has been executed. While the interrupt program will be executed then the main program execution continues on the state where it left off.
  
-===== Praktika =====+===== Practice =====
  
-Järgnev programm näitab, kuidas seadistada loendurit katkestust tekitamaProgrammis on kasutusel digitaalse mooduli LED-i, millest punase olekut muudetakse perioodiliselt tarkvaralise viitega ja rohelinemille olekut muudetakse katkestuse tekkimiselTarkvaralise viitega LED-i vilgutamise kohta on olemas eraldi harjutus ja seda siinkohal selgitatud polePõhieesmärk on selgitada loendurite teegi ja katkestuste kasutamist+The following program shows how the counter is set up to make an interruptThere are LEDs of the Digital i/o module in the programthe state of the red LED is changed periodically with software delaythe state of the green LED is changed when interrupts occurThere is a separate exercise for blinking LED with software delay and it is not explained hereThe main goal is to explain the usage of the library of the counters and interrupts
  
-Programmi alguses toimub 16-bitise loendur/taimer 1 seadistamine funktsiooniga //timer1_init_ctc//. Selle funktsiooni abil seatakse loendur CTC (inglise keeles //clear timer on compare match//) režiimikus taimeri maksimaalseks väärtuseks pole mitte 2<sup>16</sup> - 1, vaid see on valitav. Antud juhul määratakse maksimaalseks väärtuseks ICR1 registri väärtusLoenduri jaguriks on 1024 ja ICR1 väärtuseks 14400mille tulemusena 14,7456 Mhz taktsageduse puhul on loenduri perioodiks täpselt 1 sekundSeda on lihtne arvutada valemist:+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 upFirst, the timer period will be setso the maximum value of the count function TC_SetPeriodThe divider of the counter is 1024 and the value of period is 31249so when the clock frequency is 32 MHzthe period will be exactly one secondIt is easy to calculate with following formula:
  
-14745600 Hz / 1024 / 14400 = 1+period (32000000 Hz / 1024 / 1) - 1 = 31249
  
-Pärast loendur maksimaalse väärtuse saavutamise katkestuse lubamist tuleb katkestuse tekkimine lubada ka globaalseltehk siis üle kogu mikrokontrolleriGlobaalseks katkestuste lubamiseks on funktsioon //sei// ja keelamiseks //cli//Nende funktsioonide ja katkestuste programmilõigu defineerimiseks peab programmi kaasama ka //avr/interrupt.h// päisefailiKatkestuse programmilõik defineeritakse makrofunktsiooniga //ISR//mille parameetriks on katkestuse vektori nimiLoendur väärtuse saavutamise katkestuse vektori nimi on //TIMER1_CAPT_vect//.+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 microcontrollerThe global interrupts can be enabled by function sei and forbidding with cli. A header file avr/interrupt.h must be included for defining the program part of these functions and interruptsThe program part of the interrupt is defined with macro function ISR, which parameter is the name of the interrupt vectorIn this set-up the vector of counter 1’s maximum value achievement interrupt is TCE1_OVF_vect. In addition, to allow the global interrupt, a different priority interrupts should be enabled one by one, using the xmega PMIC.CTRL register.
  
 <code c> <code c>
-// +// HomeLab III example of blinking LED with counter interrupt
-// Kodulabori loenduri katkestusega vilkuva LED näide. +
-// Võrdluseks katkestusega vilkuvale LED-ile +
-// töötab paralleelselt ka tarkvaralise viitega vilkuv LED. +
-//+
 #include <homelab/pin.h> #include <homelab/pin.h>
 #include <homelab/delay.h> #include <homelab/delay.h>
Line 28: Line 28:
 #include <avr/interrupt.h> #include <avr/interrupt.h>
  
-// +// Interruption 
-// LED-ide viikude määramine +ISR(TCE1_OVF_vect) 
-// +{ 
-pin led_red   PIN(C5); + // Changing the state of the green LED 
-pin led_green = PIN(C3);+ 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/1024/[aeg] - 1 periood 
 + // 32000000 / 1024 / 1 - 1 = 31249 
 + TC_SetPeriod(&TCE131249); 
 + 
 + // Setting the clock of timer E1 (F_CPU/1024) 
 + TC1_ConfigClockSource(&TCE1TC_CLKSEL_DIV1024_gc); 
 + // Setting timer E1 to the normal operating mode 
 + TC1_ConfigWGM(&TCE1, TC_WGMODE_NORMAL_gc); 
 + 
 + // Enabling high-priority overflow interruptions 
 + TC1_SetOverflowIntLevel(&TCE1,TC_OVFINTLVL_HI_gc); 
 + 
 + // Enabling high-priority interruptions 
 + PMIC.CTRL |= PMIC_HILVLEN_bm; 
 + // Enabling global interruption 
 + sei(); 
 + 
 + // Endless loop 
 + while (1) { } 
 +
 +</code> 
 + 
 + 
 +Example of interrupt is quite different between ATmega series (in this example ATmega2561) controllers, because the timers, compared to the xmega series controllers, are also different. 
 + 
 +In the beginning of the program, the 16-bit counter/timer 1 has been set up with the function //timer1_init_ctc// With this function the counter CTC //clear timer on compare match// has been set to the mode where the maximum value of the timer is not 2<sup>16</sup> - 1 but can be selected. In this case the maximum value is set to equal 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 with following formula:  
 + 
 +f = 14745600 Hz / 1024 / 14400 = 1  
 + 
 +<code c> 
 +// The HomeLab II example of blinking LED with counter interrupt 
 +#include <homelab/pin.h> 
 +#include <homelab/delay.h> 
 +#include <homelab/timer.h> 
 +#include <avr/interrupt.h>
  
-// +// Interruption
-// Katkestus +
-//+
 ISR(TIMER1_CAPT_vect) ISR(TIMER1_CAPT_vect)
 { {
- // Rohelise LED oleku muutmine+ // 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 CTC režiimi + // 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 14400 mis + // 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); +
- }+
 } }
 </code> </code>
  
-Programmi käivitades on nähaet hoolimata sellest, mida mikrokontroller põhiprogrammis teeb, toimuvad katkestused ja roheline LED vilgub.+At the start of the program it is seen that regardless of what the microcontroller is doing in the main programthe 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, 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. 
en/examples/timer/periodic_interrupt.1265633306.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0