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 [2015/11/13 07:57] heikopikneren: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: [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]]//
  
 ===== Theory ===== ===== Theory =====
Line 15: Line 19:
 period = (32000000 Hz / 1024 / 1) - 1 = 31249 period = (32000000 Hz / 1024 / 1) - 1 = 31249
  
-In the beginning of the program, the 16-bit counter/timer 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 216 – 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 14400so when the clock frequency is 14,7456 MHz, the period will be exactly one secondIt is easy to calculate with following formula: +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/interrupt.h must be included for defining the program part of these functions and interrupts. The program part of the interrupt is defined with macro function ISR, which parameter is the name of the interrupt vector. In this set-up the vector of counter 1’s maximum value achievement interrupt is TCE1_OVF_vect. In additionto allow the global interrupta different priority interrupts should be enabled one by oneusing the xmega PMIC.CTRL register.
  
-f = 14745600 Hz 1024 14400 = 1 +<code c> 
 +// HomeLab III example of blinking LED with counter interrupt 
 +#include <homelab/pin.h> 
 +#include <homelab/delay.h> 
 +#include <homelab/timer.h> 
 +#include <avr/interrupt.h>
  
-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 //sei// and for forbidding //cli//. A header file //avr/interrupt.h// must be included for defining the program part of these functions and interrupts. The program part of the interrupt is defined with macro function ISR, which parameter is the name of the interrupt vector. In this set-up the vector of counter 1’s maximum value achievement interrupt is //TIMER1_CAPT_vect//.  +// 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/1024/[aeg] - 1 = periood
 + // 32000000 / 1024 / 1 - 1 = 31249
 + TC_SetPeriod(&TCE1, 31249);
 +
 + // Setting the clock of timer E1 (F_CPU/1024)
 + TC1_ConfigClockSource(&TCE1, TC_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> <code c>
-// +// The HomeLab II example of blinking LED with counter interrupt
-// The HomeLab'example of blinking LED which blinks due to counter interruptings. +
-// For comparison to the LED blinking due to interrupts,  +
-// there is a software delay blinking LED working parallel. +
-//+
 #include <homelab/pin.h> #include <homelab/pin.h>
 #include <homelab/delay.h> #include <homelab/delay.h>
Line 33: Line 78:
 #include <avr/interrupt.h> #include <avr/interrupt.h>
  
-// 
-// Determining the pins of the LEDs. 
-// 
-pin led_red   = PIN(C, 5); 
-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); +
- }+
 } }
 </code> </code>
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,  the change of the LED’s state, callout of the function of the delay and completion of the cycle are demanding some clock cycles of time for the processor. As a result the blinking of the red LED is always behind from the blinking of the green LED. This is why  it is not advised to design clocks and other precise actions with delay but with interruptions of the counter.+
en/examples/timer/periodic_interrupt.1447401424.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