Skip to content

Commit 98ee736

Browse files
author
SimonePDA
committed
Update CurieTimer1Interrupt.ino
Changed the style of the code according to Arduino's Tutorials guidelines.
1 parent 17eac79 commit 98ee736

File tree

1 file changed

+38
-26
lines changed

1 file changed

+38
-26
lines changed

libraries/CurieTimerOne/examples/CurieTimer1Interrupt/CurieTimer1Interrupt.ino

+38-26
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,71 @@
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
83
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);"
1015
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
1319
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"
1624

25+
// Comment the following statement to disable logging on serial port.
26+
#define SERIAL_PORT_LOG_ENABLE 1
1727

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
1933
{
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
2236
}
2337

2438
void setup() {
2539

2640
#ifdef SERIAL_PORT_LOG_ENABLE
2741
Serial.begin(115200);
28-
while(!Serial);
42+
while (!Serial); // wait for the serial monitor to open
2943
#endif
3044

3145
// Initialize pin 13 as an output - onboard LED.
3246
pinMode(13, OUTPUT);
3347
}
3448

3549
void loop() {
36-
unsigned int i, time = oneSecInUsec;
37-
38-
CurieTimerOne.start(time, &timedBlinkIsr);
3950

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
4255

4356
#ifdef SERIAL_PORT_LOG_ENABLE
4457
Serial.print("The blink period: ");
4558
Serial.println(time);
4659
#endif
4760

48-
delay(10000); // 10 seconds
61+
delay(10000); // 10 seconds of delay, regularly 'interrupted' by the timer interrupt
4962

5063
#ifdef SERIAL_PORT_LOG_ENABLE
5164
Serial.print("Total number of ticks in 10 seconds: ");
52-
Serial.println(CurieTimerOne.rdRstTickCount());
65+
Serial.println(CurieTimerOne.rdRstTickCount()); // Reads and Resets tick count
5366
Serial.println("----");
5467
#endif
55-
56-
CurieTimerOne.restart(time);
68+
// Uncomment the following line if the serial logging is disabled
69+
// CurieTimerOne.restart(time); // Restarts Timer
5770
}
5871
}
59-

0 commit comments

Comments
 (0)