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