Arduino 101 Curie Timer One Interrupt

With this tutorial you learn to use one of the timers available in the microcontroller on the 101.

With this tutorial you learn to use one of the timers available in the microcontroller. The library uses Timer 1 and this tutorial shows how to set up an interrupt at variable intervals to toggle the on-board LED. The interrupt is the only way to exit from a delay() function and it is used in this sketch to show how a delay instruction - that apparently stops the flow of the code execution - is temporarily exited to toggle the LED, then it is continued from where it was interrupted. The final effect is a 10 seconds blinking block with the same blinking rate, incremented every 10 seconds in four steps.

Hardware Required

The Circuit

genuino101fzz

image developed using Fritzing. No additional hardware is needed to use this tutorial.

Software Essentials

Libraries

CurieTimerOne.h is the library that provides access to the Timer 1 of the microcontroller. This library allows to set up the number of microseconds that the timer counts before it asserts an interrupt. The interrupt can be configured to call a specific function - the callback function - and each interrupt increments a counter. The same library is used to generate a PWM signal with duty cycle and period length fully customizable.

Functions

timedBlinkIsr() - is the function to which the sketch execution is passed when the interrupt is asserted. It toggles the state of the LED on D13 using the toggle variable that is changed using the "!" (NOT) operator at each call.

Code

The code is written with a serial monitor feature that can be toggled commenting or uncommenting

#define SERIAL_PORT_LOG_ENABLE 1
.

1/*
2
3 Sketch: Timer1Interrupt.ino
4
5 This sketch demonstrates the usage of the Curie Timer One Library.
6
7 It uses timer-1 to blink the onboard LED, pin 13, at different
8
9 intervals (speed) in four steps.
10
11 You can see the time interval and the number of interrupt counted
12
13 in 10 seconds if you keep serial logging active, but this may require
14
15 a MASTER_RESET to reprogram the board.
16
17 Blinking of the LED will start only when you open the Serial Monitor
18
19 unless you comment the "#define SERIAL_PORT_LOG_ENABLE 1"; don't
20
21 forget to uncomment "CurieTimerOne.restart(time);"
22
23 created by Intel
24
25 Modified 14 March 2016
26
27 by Simone Majocchi
28
29 This example code is in the public domain.
30
31*/
32
33#include "CurieTimerOne.h"
34
35// Comment the following statement to disable logging on serial port.
36#define SERIAL_PORT_LOG_ENABLE 1
37
38const int oneSecInUsec = 1000000; // A second in mirco second unit.
39
40bool toggle = 0; // The LED status toggle
41int time; // the variable used to set the Timer
42
43void timedBlinkIsr() // callback function when interrupt is asserted
44{
45
46 digitalWrite(13, toggle);
47
48 toggle = !toggle; // use NOT operator to invert toggle value
49}
50
51void setup() {
52
53#ifdef SERIAL_PORT_LOG_ENABLE
54
55 Serial.begin(115200); // initialize Serial communication
56
57 while (!Serial); // wait for the serial monitor to open
58#endif
59
60 // Initialize pin 13 as an output - onboard LED.
61
62 pinMode(13, OUTPUT);
63}
64
65void loop() {
66
67 for (int i = 1; i < 9; i = i * 2) {
68
69 // We set a blink rate of 1000000, 500000, 250000, 125000 microseconds
70
71 time = oneSecInUsec / i; // time is used to toggle the LED is divided by i
72
73 CurieTimerOne.start(time, &timedBlinkIsr); // set timer and callback
74
75#ifdef SERIAL_PORT_LOG_ENABLE
76
77 Serial.print("The blink period: ");
78
79 Serial.println(time);
80#endif
81
82 delay(10000); // 10 seconds of delay, regularly 'interrupted' by the timer interrupt
83
84#ifdef SERIAL_PORT_LOG_ENABLE
85
86 Serial.print("Total number of ticks in 10 seconds: ");
87
88 Serial.println(CurieTimerOne.rdRstTickCount()); // Reads and Resets tick count
89
90 Serial.println("----");
91#endif
92
93 // Uncomment the following line if the serial logging is disabled
94
95 // CurieTimerOne.restart(time); // Restarts Timer
96
97 }
98}

Last revision 2016/03/13 by SM

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.

Suggest Changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.