-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path74hc165_serializer_test.ino
148 lines (130 loc) · 4.19 KB
/
74hc165_serializer_test.ino
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* This sketch is used to test the 8-Bit parallel in/serial out shift register IC 74HC165.
* Copyright (c) 2021, maehw; for details check LICENSE
*
* The specific use case and pinout has been intended for for testing the
* "splitflap classic controller" shield in combination with the magnetic switches (hall-effect sensors);
* see also: https://github.com/scottbez1/splitflap
*
* Holding magnets near the sensors should toggle the output from high to low.
*/
/* I/O configuration */
#if defined(__AVR_ATmega32U4__) /* working with an Arduino Leonardo here */
/* SCK and MISO pins are usually used for SPI communication,
* but here "bitbanged" as GPIOs.
*/
#define CLK_PIN (15) /* "SPI_CLOCK"/SCK */
#define QH_PIN (14) /* "SENSOR_DATA_OUT"/MISO */
#define nLD_SH_PIN ( 5) /* "SENSOR_LATCH" */
#endif
/**
* The serial input pin is usually pulled low (resistor to GND),
* but could also be daisy chained between other ICs and then pulled low,
* or be controlled by a uC and therefore be defined as an output pin here.
*/
//#define SER_IN_PIN () /* "SENSOR_DATA_IN"; usually pull-up resistor to GND but could also be daisy chained between other ICs and then pulled low, or be controlled here */
#define NUM_INPUTS (8) /* Number of inputs to load and shift out serially; one IC has 8 input pins (labelled A..H);
* pins are shifted out in the order H..A, i.e. in order to get A's value you must read at least 8 inputs */
#define CLK_PERIOD_HALF_US (50) /* Approximate (minimum) half clock period (low/high duration) */
#define READ_DELAY_MS (200) /* Delay between consecutive reads ("pause") */
#define SERIAL_BAUDRATE (115200)
#define SERIAL_PRINT_WELCOME (1)
#define SERIAL_PRINT_HEADER (1)
#define SERIAL_SYMBOL_HIGH ("H") /* could be any string, e.g. "high", "1", "-", ... */
#define SERIAL_SYMBOL_LOW ("L") /* could be any string, e.g. "low", "0", "_", ... */
const char sHeader[] = "HGFEDCBA"; /* Name of input pins */
uint32_t g_nValue = 0; /* The binary value which has been serialized (newest bit is stored at the LSB; value is continuously shifted to the left) */
void setup()
{
/* I/O configuration */
pinMode(nLD_SH_PIN, OUTPUT);
pinMode(CLK_PIN, OUTPUT);
#ifdef SER_IN_PIN
pinMode(SER_IN_PIN, OUTPUT);
#endif
digitalWrite(nLD_SH_PIN, HIGH);
digitalWrite(CLK_PIN, HIGH);
#ifdef SER_IN_PIN
pinMode(SER_IN_PIN, LOW);
#endif
pinMode(QH_PIN, INPUT);
/* pause before final start */
delay(2000);
Serial.begin(SERIAL_BAUDRATE);
#if SERIAL_PRINT_WELCOME
Serial.println("74HC165 Serializer Test");
Serial.println("-----------------------");
#endif
}
void clock_cylce()
{
digitalWrite(CLK_PIN, LOW);
delayMicroseconds(CLK_PERIOD_HALF_US);
digitalWrite(CLK_PIN, HIGH);
delayMicroseconds(CLK_PERIOD_HALF_US);
}
void reset_value()
{
g_nValue = 0;
}
void read_serial_in()
{
g_nValue = g_nValue << 1;
int sensorVal = digitalRead(QH_PIN);
if( HIGH == sensorVal )
{
g_nValue |= 1;
}
}
void output_value()
{
/* header (with device numbers and pins names) */
#if SERIAL_PRINT_HEADER
for( int k = 0; k < NUM_INPUTS; k++ )
{
if( k % 8 == 0 )
{
Serial.print(" ");
Serial.print(k/8, HEX);
}
Serial.print( sHeader[k % 8] );
}
Serial.println();
#endif
/* values: H=1, L=0 */
for( int k = NUM_INPUTS-1, m = 0; k >= 0; k--, m++ )
{
if( m % 8 == 0 )
{
Serial.print(" ");
}
if(g_nValue & (1 << k))
{
Serial.print(SERIAL_SYMBOL_HIGH);
}
else
{
Serial.print(SERIAL_SYMBOL_LOW);
}
}
Serial.println();
}
void loop()
{
reset_value();
/* cycle new data in */
digitalWrite(nLD_SH_PIN, LOW); /* parallel load */
clock_cylce();
digitalWrite(nLD_SH_PIN, HIGH); /* shift mode */
read_serial_in(); /* stable at transition high -> low */
/* shift in H..A for every device */
for( int k = 0; k < NUM_INPUTS-1; k++ )
{
clock_cylce();
read_serial_in(); /* stable at transition high -> low */
}
clock_cylce(); /* one additional clock cycle */
/* dump new value, wait and repeat */
output_value();
delay(READ_DELAY_MS); /* no clock cycling during the given period */
}