-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShiftregKeypad.ino
More file actions
101 lines (81 loc) · 2.05 KB
/
Copy pathShiftregKeypad.ino
File metadata and controls
101 lines (81 loc) · 2.05 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
94
95
96
97
98
99
100
101
#define DATA_PIN 10
#define CLOCK_PIN 9
#define PLOAD_PIN 8
#define INT_PIN 2
#define BYTES_VAL_T unsigned int
#define PULSE_WIDTH_USEC 5
#define POLL_DELAY_MSEC 1
#define DATA_WIDTH 8
BYTES_VAL_T pinValues;
BYTES_VAL_T oldPinValues;
void setup() {
Serial.begin(9600);
/* Initialize our digital pins...
*/
pinMode(PLOAD_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(DATA_PIN, INPUT);
digitalWrite(CLOCK_PIN, LOW);
digitalWrite(PLOAD_PIN, HIGH);
getKeypadData();
attachInterrupt(0, getKeypadData, RISING);
}
void getKeypadData() {
pinValues = read_shift_regs();
display_pin_values();
oldPinValues = pinValues;
}
BYTES_VAL_T read_shift_regs() {
long bitVal;
BYTES_VAL_T bytesVal = 0;
/* Trigger a parallel Load to latch the state of the data lines,
*/
digitalWrite(PLOAD_PIN, LOW);
delayMicroseconds(PULSE_WIDTH_USEC);
digitalWrite(PLOAD_PIN, HIGH);
/* Loop to read each bit value from the serial out line
of the SN74HC165N.
*/
for (int i = 0; i < DATA_WIDTH; i++)
{
bitVal = digitalRead(DATA_PIN);
/* Set the corresponding bit in bytesVal.
*/
bytesVal |= (bitVal << ((DATA_WIDTH - 1) - i));
/* Pulse the Clock (rising edge shifts the next bit).
*/
digitalWrite(CLOCK_PIN, HIGH);
delayMicroseconds(PULSE_WIDTH_USEC);
digitalWrite(CLOCK_PIN, LOW);
}
return (bytesVal);
}
void display_pin_values() {
Serial.print("Pin States:\r\n");
for (int i = 0; i < DATA_WIDTH; i++) {
Serial.print(" Pin-");
Serial.print(i);
Serial.print(": ");
if ((pinValues >> i) & 1) {
Serial.print("HIGH");
} else {
Serial.print("LOW");
}
Serial.print("\r\n");
}
Serial.print("\r\n");
}
void loop() {
// /* Read the state of all zones.
// */
// pinValues = read_shift_regs();
//
// /* If there was a chage in state, display which ones changed.
// */
// if (pinValues != oldPinValues) {
// Serial.print("*Pin value change detected*\r\n");
// display_pin_values();
// oldPinValues = pinValues;
// }
// delay(POLL_DELAY_MSEC);
}