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 08:48] 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 66: Line 70:
  
 f = 14745600 Hz / 1024 / 14400 = 1  f = 14745600 Hz / 1024 / 14400 = 1 
- 
-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//.   
- 
  
 <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 81: 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 111: 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 135: 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.1447404489.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