260 likes | 349 Views
Callback. TYWu. Reference and Library. Reference http://www.arduino.cc/playground/Code/Timer1 Download Library http://code.google.com/p/arduino-timerone/downloads/detail?name=TimerOne-v9.zip&can=2&q = Unzip the downloaded file in …arduino-1.0.4libraries TimerOne. TimerOne.h.
E N D
Callback TYWu
Reference and Library • Reference • http://www.arduino.cc/playground/Code/Timer1 • Download Library • http://code.google.com/p/arduino-timerone/downloads/detail?name=TimerOne-v9.zip&can=2&q= • Unzip the downloaded file in …arduino-1.0.4\libraries\TimerOne
TimerOne.h • initialize(period) • You must call this method first to use any of the other methods. You can optionally specify the timer's period here (in microseconds), by default it is set at 1 second. Note that this breaks analogWrite() for digital pins 9 and 10 on Arduino.
Template #include "TimerOne.h" void setup() { Timer1.initialize(500000); // initialize timer1, and set a 1/2 second period Timer1.attachInterrupt(callback); // attaches callback() as a timer overflow interrupt }
Template (Cont’d) void callback() { //callback statements } void loop() { // your program here... }
Example #include "TimerOne.h" unsigned long time; void setup() { Timer1.initialize(500000); Timer1.attachInterrupt(callback); Serial.begin(9600); time = millis(); }
Example (Cont’d) void callback() { Serial.println(millis() - time); time = millis(); } void loop() { }
attachInterrupt() • attachInterrupt() • Specifies a function to call when an external interrupt occurs. • Replaces any previous function that was attached to the interrupt. • Most Arduino boards have two external interrupts: numbers 0 (ondigital pin 2) and 1 (on digital pin 3)
attachInterrupt() • The table below shows the available interrupt pins on various boards.
attachInterrupt() • Syntax attachInterrupt(interrupt, function, mode) attachInterrupt(pin, function, mode) (Arduino Due only) • interrupt: the number of the interrupt (int) • function: the function to call when the interrupt occurs; this function must take no parameters and return nothing. This function is sometimes referred to as an interrupt service routine. • mode: defines when the interrupt should be triggered. Four contstants are predefined as valid values: • LOW to trigger the interrupt whenever the pin is low, • CHANGE to trigger the interrupt whenever the pin changes value • RISING to trigger when the pin goes from low to high, • FALLING for when the pin goes from high to low.
attachInterrupt() • Example int pin = 13; volatile int state = LOW; void setup() { pinMode(pin, OUTPUT); attachInterrupt(0, blink, CHANGE); } void loop() { digitalWrite(pin, state); } void blink() { delay(1000); state = !state; }
attachInterrupt() If pin 2 changes its value…. What'shappened?
Lab • Lab Interrupt
ATtiny85 • GIMSK • Bit 6 (INT0): External Interrupt Request 0 Enable • When the INT0 bit is set (one) and the I-bit in the Status Register (SREG) is set (one), the external pin interrupt is enabled.
ATtiny85 • Bit 5 (PCIE): Pin Change Interrupt Enable When the PCIE bit is set (one) and the I-bit in the Status Register (SREG) is set (one), pin change interrupt is enabled. Any change on any enabled PCINT[5:0] pin will cause an interrupt. The corresponding interrupt of Pin Change Interrupt Request is executed from the PCI Interrupt Vector. PCINT[5:0] pins are enabled individually by the PCMSK0 Register. • The Interrupt Sense Control0 bits 1/0 (ISC01 and ISC00) in the MCU Control Register (MCUCR) define whether the external interrupt is activated on rising and/or falling edge of the INT0 pin or level sensed. • Activity on the pin will cause an interrupt request even if INT0 is configured as an output. The corresponding interrupt of External Interrupt Request 0 is executed from the INT0 Interrupt Vector.
ATtiny85 • SREG
ATtiny85 • PCMSK
ATtiny85 • We need to use a mask to say which pins we want triggering this Pin Change ISR. • Just like with the ATMEGA328 example, we do this by setting the bits in the PCMSK register. • Bits 0 through 5 controller the 6 pins that can be used for outputs.
ATtiny85 • A 1 in that bit means an interrupt can occur on that pin, while a 0 means it cannot. • Remember that whenever any of the pins are triggered it will call the same ISR. • Finally, you need to write the ISR function. • Because there is only one port on this chip, there is only one function you need to worry about and it is called ISR(PCINT0_vect){}.
ATtiny85 • One port
ATtiny85 • #include "avr/interrupt.h"; • sei(); • Enables interrupts: set SREG Bit 7 • cli(); • Disabble interrupts • ISR(PCINT0_vect) • PCINT0_vect:Pin Change Interrupt Request 0
ATtiny85 • Sketch
ATtiny85 • The attachInterrupt function • attachInterrupt(pin, ISR, mode)
ATtiny85 • attachInterrupt(0, blink, RISING);
Reference • http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-2586-AVR-8-bit-Microcontroller-ATtiny25-ATtiny45-ATtiny85_Datasheet.pdf