Stem 5 Arduino timer interrupt and external interrupt

  • Slides: 11
Download presentation
Stem 5 Arduino timer interrupt and external interrupt KH Wong Stem 4: Arduino and

Stem 5 Arduino timer interrupt and external interrupt KH Wong Stem 4: Arduino and Computer vision, v. 0. b 2 1

Overview • What is interrupt. • How to use timer interrupt? • Test 5.

Overview • What is interrupt. • How to use timer interrupt? • Test 5. 1 • How to use external interrupt? Stem 4: Arduino and Computer vision, v. 0. b 2 2

What is interrupt? • Main () • { • : • Doing something •

What is interrupt? • Main () • { • : • Doing something • Can happen anytime Depends on types of interrupts Phone rings _isr() //Interrupt service routine { (e. g. browsing) • : • } • Phone rings activate interrupt Phone rings • Timer interrupt: you may set up a timer to activate interrupt • External interrupt: you may use an external pin to activate interrupt when the external pin changes state. CEG 2400 12 SWI, and 14. init V 7 a some tasks (e. g. answer telephone) }//when finished, //goes back to main 3

Application examples • Regularly reading a sensor, or blinking an LED. Since the timer

Application examples • Regularly reading a sensor, or blinking an LED. Since the timer frequency is fixed, it is more accurate than blinking an LED by a time delay method. • Timer interrupt: Timer interrupts the CPU at a rate of 1 KHz. At each interrupt an timer interrupt service routine (timer_isr) will run to change the state of an LED. • When your computer is running, a key press will trigger an interrupt to input a character to your system • External interrupt: When a key is pressed (the key is attached to an external pin, so its state will change) an interrupt service routine (ext_isr) will be executed to read the keyboard. CEG 2400 12 SWI, and 14. init V 7 a 4

TEst 5. 1 Blink LEDs at different frequencies. • // https: //www. instructables. com/id/Arduino-Timer-Interrupts/

TEst 5. 1 Blink LEDs at different frequencies. • // https: //www. instructables. com/id/Arduino-Timer-Interrupts/ • //this enables all three Arduino timer interrupts. • //timer 0 will interrupt at 2 k. Hz, blinking an LED at pin 8 at 1 KHz • //timer 1 will interrupt at 1 Hz, blinking an LED at pin 13 at 0. 5 Hz • //timer 2 will interrupt at 8 k. Hz, blinking an LED at pin 9 at 4 KHz Stem 4: Arduino and Computer vision, v. 0. b 2 5

Setup test 5. 1. ino //https: //www. instructables. com/id/Arduino-Timer-Interrupts/ //this code will enable all

Setup test 5. 1. ino //https: //www. instructables. com/id/Arduino-Timer-Interrupts/ //this code will enable all three arduino timer interrupts. //timer 0 will interrupt at 2 k. Hz, blinking an LED at pin 8 at 1 KHz //timer 1 will interrupt at 1 Hz, blinking an LED at pin 13 at 0. 5 Hz //timer 2 will interrupt at 8 k. Hz, blinking an LED at pin 9 at 4 KHz //storage variables boolean toggle 0 = 0; boolean toggle 1 = 0; boolean toggle 2 = 0; void setup(){ //set pins as outputs pin. Mode(8, OUTPUT); pin. Mode(9, OUTPUT); pin. Mode(13, OUTPUT); cli(); //stop interrupts //set timer 0 interrupt at 2 k. Hz TCCR 0 A = 0; // set entire TCCR 2 A register to 0 TCCR 0 B = 0; // same for TCCR 2 B TCNT 0 = 0; //initialize counter value to 0 // set compare match register for 2 khz increments OCR 0 A = 124; // = (16*10^6) / (2000*64) - 1 (must be <256) // turn on CTC mode TCCR 0 A |= (1 << WGM 01); // Set CS 01 and CS 00 bits for 64 prescaler TCCR 0 B |= (1 << CS 01) | (1 << CS 00); // enable timer compare interrupt TIMSK 0 |= (1 << OCIE 0 A); //set timer 1 interrupt at 1 Hz TCCR 1 A = 0; // set entire TCCR 1 A register to 0 TCCR 1 B = 0; // same for TCCR 1 B TCNT 1 = 0; //initialize counter value to 0 // set compare match register for 1 hz increments OCR 1 A = 15624; // = (16*10^6) / (1*1024) - 1 (must be <65536) // turn on CTC mode TCCR 1 B |= (1 << WGM 12); // Set CS 12 and CS 10 bits for 1024 prescaler TCCR 1 B |= (1 << CS 12) | (1 << CS 10); // enable timer compare interrupt TIMSK 1 |= (1 << OCIE 1 A); //set timer 2 interrupt at 8 k. Hz TCCR 2 A = 0; // set entire TCCR 2 A register to 0 TCCR 2 B = 0; // same for TCCR 2 B TCNT 2 = 0; //initialize counter value to 0 // set compare match register for 8 khz increments OCR 2 A = 249; // = (16*10^6) / (8000*8) - 1 (must be <256) // turn on CTC mode TCCR 2 A |= (1 << WGM 21); // Set CS 21 bit for 8 prescaler TCCR 2 B |= (1 << CS 21); // enable timer compare interrupt TIMSK 2 |= (1 << OCIE 2 A); sei(); //allow interrupts • }//end setup Stem 4: Arduino and Computer vision, v. 0. b 2 6

ISR Interrupt service routines of test 5. 1. ino ISR(TIMER 0_COMPA_vect){//timer 0 interrupt ISR(TIMER

ISR Interrupt service routines of test 5. 1. ino ISR(TIMER 0_COMPA_vect){//timer 0 interrupt ISR(TIMER 2_COMPA_vect){ //2 k. Hz toggles pin 8 //timer 1 interrupt 8 k. Hz toggles p 9 //generates pulse wave of frequency 2 k. Hz/2 = 1 k. Hz //generates pulse wave of // (takes 2 cycles for full wave- toggle high then low) //frequency 8 k. Hz/2 = 4 k. Hz if (toggle 0){ // (takes two cycles for full digital. Write(8, HIGH); //wave-toggle high toggle 0 = 0; //then toggle low) } else{ if (toggle 2){ digital. Write(8, LOW); digital. Write(9, HIGH); toggle 0 = 1; toggle 2 = 0; } } } else{ ISR(TIMER 1_COMPA_vect){//timer 1 inter. 1 Hz toggles p 13 digital. Write(9, LOW); //generates pulse wave of frequency 1 Hz/2 = 0. 5 k. Hz toggle 2 = 1; // (takes 2 cycles for full wave- toggle high then low) } if (toggle 1){ } digital. Write(13, HIGH); toggle 1 = 0; } else{ void loop(){ digital. Write(13, LOW); //do other things here • toggle 1 = 1; } } Stem 4: Arduino and Computer vision, v. 0. b 2 7 }

Test 5. 1 code (full code) https: //www. instructables. com/id/Arduino-Timer-Interrupts/ • Test 5. 1:

Test 5. 1 code (full code) https: //www. instructables. com/id/Arduino-Timer-Interrupts/ • Test 5. 1: Timer interrupt • //timer interrupts • //by Amanda Ghassaei • //June 2012 • //https: //www. instructables. com/id/Arduino-Timer-Interrupts/ • /* • * This program is free software; you can redistribute it and/or modify • * it under the terms of the GNU General Public License as published by • * the Free Software Foundation; either version 3 of the License, or • * (at your option) any later version. • */ • //timer setup for timer 0, timer 1, and timer 2. • //For arduino uno or any board with ATMEL 328/168. . diecimila, duemilanove, lilypad, nano, mini. . . • //https: //www. instructables. com/id/Arduino-Timer-Interrupts/ • //this code will enable all three arduino timer interrupts. • //timer 0 will interrupt at 2 k. Hz, blinking an LED at pin 8 at 1 KHz • //timer 1 will interrupt at 1 Hz, blinking an LED at pin 13 at 0. 5 Hz • //timer 2 will interrupt at 8 k. Hz, blinking an LED at pin 9 at 4 KHz • //storage variables • boolean toggle 0 = 0; • boolean toggle 1 = 0; • boolean toggle 2 = 0; Stem 4: Arduino and Computer vision, v. 0. b 2 8

Test 5. 2 External interrupt test • Interrupt arises whenever the normal flow of

Test 5. 2 External interrupt test • Interrupt arises whenever the normal flow of a program has to be halted temporarily to another routine. • For example to serve an interrupt (IRQ) for a hardware key press input • The LED will toggle (change state: from high to low, or low to high) when the push button is depressed once. Arduino Computer CEG 2400 12 SWI, and 14. init V 7 a 9

Test 5. 2 external interrupt code https: //www. allaboutcircuits. com/technical-articles/using-interrupts-on-arduino/ The resistor (10 K)

Test 5. 2 external interrupt code https: //www. allaboutcircuits. com/technical-articles/using-interrupts-on-arduino/ The resistor (10 K) and capacitor (10 u. F) attached to the push button are used to minimize the denounce problem • const int button. Pin = 2; • const int led. Pin = 13; • int button. State = 0; • void setup() { • Serial. begin(9600); • pin. Mode(led. Pin, OUTPUT); • // init input pin: • pin. Mode(button. Pin, INPUT); • //interrupt ISR vector • attach. Interrupt(0, pin_ISR, CHANGE); • } The 100 Ohm resistor is used to limit current flow void loop() { // Nothing here! while (1) { Serial. println(“Main loop 1"); delay(1000); Serial. println(“Main loop 2"); delay(1000); } } void pin_ISR() { button. State = !button. State; digital. Write(led. Pin, button. State); Serial. println("INTERRUPTED"); Serial. println(button. State); } Stem 4: Arduino and Computer vision, v. 0. b 2 10

Test 5. 2 External interrupt Full code • //pin 2 -----10 k Ohm--->5 V

Test 5. 2 External interrupt Full code • //pin 2 -----10 k Ohm--->5 V • // 10 u. F--- / on-off sw 1 • // --- | • // | | • // GND • //Pin 13 ---100 Ohm -->|--GND • // variables will change: • const int button. Pin = 2; // the number of the pushbutton pin • const int led. Pin = 13; // the number of the LED pin • // variables will change: • //volatile int button. State = 0; • void setup() { Pull up resistor | | LED // variable for reading the pushbutton status • Serial. begin(9600); • // initialize the LED pin as an output: • pin. Mode(led. Pin, OUTPUT); • // initialize the pushbutton pin as an input: • pin. Mode(button. Pin, INPUT); Stem 4: Arduino and Computer vision, v. 0. b 2 11