Presentation is loading. Please wait.

Presentation is loading. Please wait.

Callback TYWu.

Similar presentations


Presentation on theme: "Callback TYWu."— Presentation transcript:

1 Callback TYWu

2 Reference and Library Reference Download Library
Download Library 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 (on digital 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's happened?

13 Lab Lab Interrupt


Download ppt "Callback TYWu."

Similar presentations


Ads by Google