-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOutputExpander8.ino
More file actions
93 lines (79 loc) · 2.22 KB
/
Copy pathOutputExpander8.ino
File metadata and controls
93 lines (79 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//////////////////////////////////////////////////////////////////////
// OptimizedGPIO Library Example
//
// Using an 8-bit serial-in-parallel-out serial shift register chip
// as an Output Expander by bit-banging the control signals using
// the OptimizedGPIO Library.
// It compares the speed of the OptimizedGPIO version with the version
// using digitalWrite(). The OptimizedGPIO version is about 10x faster.
//
// mumanchu + muman.ch, 2026.03.31
// https://github.com/mumanchu/OptimizedGPIO
#include "OutputExpander8.h"
OutputExpander8 expander;
// Pins connected to the shift register chip
#define DATA_PIN 2
#define CLOCK_PIN 3
#define STROBE_PIN 4
#define SHIFT_PIN 5
// The number of times around the timing loop
#define LOOP_COUNT 100000
void setup()
{
Serial.begin(115200);
// delay to give you time to open the serial monitor
delay(5000);
Serial.println("\n\rStarted...\n\r");
Serial.flush();
if (!expander.begin(DATA_PIN, CLOCK_PIN, STROBE_PIN)) {
Serial.println("FATAL ERROR: expander.begin() failed");
Serial.flush();
while (1)
yield();
}
}
void loop()
{
unsigned long t;
unsigned long elapsedTime;
char buf[128];
// time the timing loop so we can subtract it
t = micros();
for (volatile long i = 0; i < LOOP_COUNT; ++i) {
;
}
unsigned long loopTime = micros() - t;
// if the chip's SHIFT OUT pin is connected to an INPUT, we can run the test
#ifdef SHIFT_PIN
if (!expander.shiftOutTest(SHIFT_PIN)) {
Serial.println("FATAL ERROR: expander.shiftOutTest() failed");
Serial.flush();
while (1)
yield();
}
Serial.println("expander.shiftOutTest() passed!");
#endif
// time writeByte(), uses OptimizedGPIO
byte b = 0;
t = micros();
for (volatile long i = 0; i < LOOP_COUNT; ++i) {
expander.writeByte(b);
b = ~b;
}
elapsedTime = micros() - t - loopTime;
sprintf(buf, "writeByte() = %lu microseconds/byte",
elapsedTime / LOOP_COUNT);
Serial.println(buf);
// time writeByteSlow(), uses digitalWrite()
b = 0;
t = micros();
for (volatile long i = 0; i < LOOP_COUNT; ++i) {
expander.writeByteSlow(b);
b = ~b;
}
elapsedTime = micros() - t - loopTime;
sprintf(buf, "writeByteSlow() = %lu microseconds/byte",
elapsedTime / LOOP_COUNT);
Serial.println(buf);
Serial.println();
}