1 / 25

Callback

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.

battlem
Download Presentation

Callback

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Callback TYWu

  2. 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

  3. 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.

  4. 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 }

  5. Template (Cont’d) void callback() {   //callback statements } void loop() { // your program here... }

  6. Example #include "TimerOne.h"  unsigned long time; void setup() {   Timer1.initialize(500000);           Timer1.attachInterrupt(callback); Serial.begin(9600); time = millis(); }

  7. Example (Cont’d) void callback() { Serial.println(millis() - time);   time = millis(); } void loop() { }

  8. 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)

  9. attachInterrupt() • The table below shows the available interrupt pins on various boards.

  10. 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.

  11. 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; }

  12. attachInterrupt() If pin 2 changes its value…. What'shappened?

  13. Lab • Lab Interrupt

  14. 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.

  15. 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.

  16. ATtiny85 • SREG

  17. ATtiny85 • PCMSK

  18. 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.  

  19. 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){}.

  20. ATtiny85 • One port

  21. 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

  22. ATtiny85 • Sketch

  23. ATtiny85 • The attachInterrupt function • attachInterrupt(pin, ISR, mode)

  24. ATtiny85 • attachInterrupt(0, blink, RISING);

  25. Reference • http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-2586-AVR-8-bit-Microcontroller-ATtiny25-ATtiny45-ATtiny85_Datasheet.pdf

More Related