-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter.cpp
458 lines (405 loc) · 12.2 KB
/
counter.cpp
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*counter.cpp - Source file for SN74LV8154 IC driver
Copyright (c) 2020 Justin Holland. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
*/
#include "Arduino.h"
#include "counter.h"
#include "shiftreg.h"
#include <math.h>
void CounterIC::set_data_pins(uint8_t pins[8]) {
/*Data pins on SN74LV8154
*Only need to call this function when using the SN74LV8154
*parallel output pins
* Y0: Pin 19 [Data output bit 0 (LSB)]
* Y1: Pin 18 [Data output bit 1]
* Y2: Pin 17 [Data output bit 2]
* Y3: Pin 16 [Data output bit 3]
* Y4: Pin 15 [Data output bit 4]
* Y5: Pin 14 [Data output bit 5]
* Y6: Pin 13 [Data output bit 6]
* Y7: Pin 12 [Data output bit 7]
*/
Y0_pin = pins[0];
Y1_pin = pins[1];
Y2_pin = pins[2];
Y3_pin = pins[3];
Y4_pin = pins[4];
Y5_pin = pins[5];
Y6_pin = pins[6];
Y7_pin = pins[7];
}
void CounterIC::set_gate_pins(uint8_t gau, uint8_t gal) {
/*Gate A pins on SN74LV8154
* GAL: Pin 3 [Gate A lower byte; active-low puts lower byte of stored counter A on Y bus]
* GAU: Pin 4 [Gate A upper byte; active-low puts upper byte of stored counter A on Y bus]
*/
GAU_pin = gau;
GAL_pin = gal;
}
void CounterIC::set_gate_pins(uint8_t gau, uint8_t gal, uint8_t gbu, uint8_t gbl) {
/*Gate A and B pins on SN74LV8154
* GAL: Pin 3 [Gate A lower byte; active-low puts lower byte of stored counter A on Y bus]
* GAU: Pin 4 [Gate A upper byte; active-low puts upper byte of stored counter A on Y bus]
* GBL: Pin 5 [Gate B lower byte; active-low puts lower byte of stored counter B on Y bus]
* GBU: Pin 6 [Gate B upper byte; active-low puts upper byte of stored counter B on Y bus]
*/
GAU_pin = gau;
GAL_pin = gal;
GBU_pin = gbu;
GBL_pin = gbl;
}
void CounterIC::set_clear_pin(uint8_t cclr) {
/*CCLR pin on SN74LV8154
* CCLR: Pin 11 [Clock clear, async active-low clear for both counters]
*/
CCLR_pin = cclr;
}
void CounterIC::set_regclock_pin(uint8_t rclk) {
/*RCLK pin on SN74LV8154
* RCLK: Pin 7 [Register clock, rising edge stores counters into internal storage register]
*/
RCLK_pin = rclk;
}
void CounterIC::set_clkben_pin(uint8_t clkben) {
/*Clock B enable pin on SN74LV8154
* CLKBEN: Pin 9 [Clock B enable; active-low allows clocking for counter B; connect to RCOA for 32-bit counter]
*/
CLKBEN_pin = clkben;
}
void CounterIC::set_test_pins(uint8_t a) {
/*Test pin for Counter A on SN74LV8154
*Set this pin to a digital output pin on the Arduino to perform controlled testing
*of the counter; Connect to CLKA (Pin 1 on SN74LV8154)
*/
a_trig_pin = a;
}
void CounterIC::set_test_pins(uint8_t a, uint8_t b) {
/*Test pins for Counters A and B on SN74LV8154
*Set these pins to two digital output pins on the Arduino to perform controlled testin
*of counters A and B; Connect to CLKA (Pin 1 on SN74LV8154) and CLKB (Pin 2)
*/
a_trig_pin = a;
b_trig_pin = b;
}
void CounterIC::set_testA_freq(uint32_t fa) {
/*Set frequency in Hz for testing Counter A
*
*/
a_freq = fa;
}
void CounterIC::set_testB_freq(uint32_t fb) {
/*Set frequency in Hz for testing Counter B
*
*/
b_freq = fb;
}
void CounterIC::set_serial_conn(ShiftRegIC* s) {
/*Provide a pointer to a ShiftRegIC object that will allow data reads from
*the SN74LV8154 to be performed via a single serial wire connection to the Arduino
*/
serial_output = s;
}
void CounterIC::init() {
/*This function must be called just prior to entering the loop
*
*/
//Single 32-bit counter mode requires specific pin configuration
if (strcmp(mode, "single") == 0) {
_single = true;
//Single counter mode requires CLKBEN to be connected to RCOA
if (CLKBEN_pin != 255 || RCOA_pin != 255) {
Serial.println("fatal error: From CounterIC::init() -- cannot define CLKBEN or RCOA pins for single counter mode.");
while(1);
}
}
else {
_single = false;
}
//Initialize Gate pins
if (GAL_pin == 255 || GAU_pin == 255 || GBL_pin == 255 || GBU_pin == 255) {
Serial.println("fatal error: From CounterIC::init() -- all gate pins must be defined!");
while(1);
}
else {
pinMode(GAL_pin, OUTPUT);
pinMode(GAU_pin, OUTPUT);
pinMode(GBL_pin, OUTPUT);
pinMode(GBU_pin, OUTPUT);
digitalWrite(GAL_pin, HIGH);
digitalWrite(GAU_pin, HIGH);
digitalWrite(GBL_pin, HIGH);
digitalWrite(GBU_pin, HIGH);
}
//Initialize Register Clock
if (RCLK_pin != 255) {
pinMode(RCLK_pin, OUTPUT);
digitalWrite(RCLK_pin, LOW);
}
else {
Serial.println("fatal error: From CounterIC::init() -- register clock pin must be defined!");
}
//Initialize overflow status pin
if (RCOA_pin != 255) {
_overflow = true;
pinMode(RCOA_pin, INPUT_PULLUP);
}
else {
_overflow = false;
}
//Initialize Clock B enable pin
if (CLKBEN_pin != 255) {
_clkBenable = true;
_toggle = true;
pinMode(CLKBEN_pin, OUTPUT);
digitalWrite(CLKBEN_pin, LOW);
}
else {
_clkBenable = true;
_toggle = false;
}
//Initialize Shift Register connection, if present
if (serial_output != NULL) {
_shift = true;
if (Y0_pin != 255) {
Serial.print("warning: From CounterIC::init() -- pins defined for parallel data output will not be used since shift register object has been specified.");
}
}
else {
_shift = false;
if (Y0_pin == 255) {
Serial.println("fatal error: From CounterIC::init() -- data pins (Y0-Y7) must be defined!");
while(1);
}
}
//Initialize CCLR pin
if (CCLR_pin != 255) {
_clear = true;
pinMode(CCLR_pin, OUTPUT);
digitalWrite(CCLR_pin, HIGH);
}
else {
_clear = false;
}
//Initialize test pins
if (a_trig_pin != 255) {
_testA = true;
pinMode(a_trig_pin, OUTPUT);
digitalWrite(a_trig_pin, LOW);
testA_delay = (uint32_t) round((1.0/a_freq)*1E6);
_resetTimers = true;
}
else {
_testA = false;
}
if (b_trig_pin != 255) {
_testB = true;
pinMode(b_trig_pin, OUTPUT);
digitalWrite(b_trig_pin, LOW);
testB_delay = (uint32_t) round((1.0/b_freq)*1E6);
_resetTimers = true;
}
else {
_testB = false;
}
}
void CounterIC::update() {
/*This function must be called in the main loop if testing of Counters
*A/B is being performed. It can also be used to monitor for overflow condition
*on Counter A. Alternatively, overFlow() function can be called for this purpose
*/
_updateRunning = true;
tf_2 = micros();
tf_3 = micros();
//Handle timers for TestA and TestB signals
if (_testA || _testB) {
if (_resetTimers) {
t0_2 = micros();
t0_3 = micros();
tf_2 = micros();
tf_3 = micros();
_resetTimers = false;
}
}
if (_testA) {
if (_resetTimer2) {
t0_2 = micros();
tf_2 = micros();
_resetTimer2 = false;
}
if (tf_2 - t0_2 >= testA_delay/2) {
digitalWrite(a_trig_pin, !digitalRead(a_trig_pin));
_resetTimer2 = true;
}
}
if (_testB) {
if (_resetTimer3) {
t0_3 = micros();
tf_3 = micros();
_resetTimer3 = false;
}
if (tf_3 - t0_3 >= testB_delay/2) {
digitalWrite(b_trig_pin, !digitalRead(b_trig_pin));
_resetTimer3 = true;
}
}
//Handle Overflow status
if (_overflow) {
if (digitalRead(RCOA_pin) == LOW) {
Serial.println("Counter A is full!");
}
}
}
uint32_t CounterIC::readCounter(const char* ab) {
/*Read the value stored on the internal register of the SN74LV8154
*Argument: "A" for counter A
* "B" for counter B
*If a shift register object has been attached to this counter object
*it will automatically be used to retrieve the counter values. Otherwise,
*this function will attempt to read the parallel data pins from the
*SN74LV8154 by default.
*/
uint32_t data_out = 0x00;
if (_testA || _testB) {
if (!_updateRunning) {
Serial.println("fatal error: From CounterIC::readCounter() -- CounterIC::Update() must be called in loop");
while(1);
}
}
digitalWrite(RCLK_pin, HIGH);
delayMicroseconds(2);
digitalWrite(RCLK_pin, LOW);
//Read A counter
if (strcmp(ab, "A") == 0 || strcmp(ab, "a") == 0) {
digitalWrite(GAU_pin, LOW);
delayMicroseconds(2);
if (_shift) {data_out = serial_output->readByte(true);}
else {data_out = readDataPins();}
digitalWrite(GAU_pin, HIGH);
digitalWrite(GAL_pin, LOW);
delayMicroseconds(2);
if (_shift) {data_out = (data_out << 8) | serial_output->readByte(true);}
else {data_out = (data_out << 8) | readDataPins();}
digitalWrite(GAL_pin, HIGH);
}
//Read B counter
if (strcmp(ab, "B") == 0 || strcmp(ab, "b") == 0) {
if (_clkBenable) {
digitalWrite(GBU_pin, LOW);
delayMicroseconds(2);
if (_shift) {data_out = serial_output->readByte(true);}
else {data_out = readDataPins();}
digitalWrite(GBU_pin, HIGH);
digitalWrite(GBL_pin, LOW);
delayMicroseconds(2);
if (_shift) {data_out = (data_out << 8) | serial_output->readByte(true);}
else {data_out = (data_out << 8) | readDataPins();}
digitalWrite(GBL_pin, HIGH);
}
else {
Serial.println("warning: From CounterIC::readCounter() -- counter B is not enabled, cannot read.");
}
}
return data_out;
}
uint32_t CounterIC::readCounter_32bit() {
/*Read the 32-bit value stored on the internal register of the SN74LV8154
* This function can only be called when the IC is configured as a single 32-bit counter
* by either connecting the CLKBEN pin to the RCOA pin or toggling Counter B on using this
* library when an overflow occurs on Counter A.
*/
uint32_t ret = 0xFFFF;
if (_single) {
uint32_t high_byte = readCounter("A");
uint32_t low_byte = readCounter("B");
ret = (high_byte << 16) | low_byte;
}
else {
Serial.println("error: From CounterIC::readCounter_32bit() -- this function cannot be called for dual 16-bit counters configuration");
}
return ret;
}
void CounterIC::toggleCounterB() {
/*Toggles the state of the CLKBEN pin on the SN74LV8154
*
*/
if (_toggle) {
digitalWrite(CLKBEN_pin, !digitalRead(CLKBEN_pin));
if (digitalRead(CLKBEN_pin) == HIGH) {_clkBenable = false;}
else {_clkBenable = true;}
}
else {
Serial.println("error: From CounterIC::toggleCounterB() -- cannot toggle counter B since CLKBEN pin was not defined.");
}
}
bool CounterIC::overFlow() {
/*Returns the status of the overflow pin RCOA
*
*/
bool ret = false;
if (_overflow) {
if (digitalRead(RCOA_pin) == HIGH) {
ret = false;
}
else {
ret = true;
}
}
else {
Serial.println("error: From CounterIC::overFlow() -- this function cannot be called because RCOA pin was not defined");
}
return ret;
}
bool CounterIC::enabledCounterB() {
/*Returns ON/OFF status of Counter B
*
*/
bool ret = false;
if (_toggle) {
if (digitalRead(CLKBEN_pin) == HIGH) {
ret = false;
}
else {
ret = true;
}
}
else {
Serial.println("error: From CounterIC::enabledCounterB() -- this function cannot be called because CLKBEN pin was not undefined");
}
return ret;
}
void CounterIC::clearCounters() {
/*This function clears the values stored in the internal register in the SN74LV8154
*for both counters A and B.
*/
digitalWrite(CCLR_pin, LOW);
delayMicroseconds(2);
digitalWrite(CCLR_pin, HIGH);
}
uint32_t CounterIC::readDataPins() {
uint32_t val = 0x00;
uint32_t data_out = 0x00;
val = digitalRead(Y7_pin);
data_out = (data_out << 1) | val;
val = digitalRead(Y6_pin);
data_out = (data_out << 1) | val;
val = digitalRead(Y5_pin);
data_out = (data_out << 1) | val;
val = digitalRead(Y4_pin);
data_out = (data_out << 1) | val;
val = digitalRead(Y3_pin);
data_out = (data_out << 1) | val;
val = digitalRead(Y2_pin);
data_out = (data_out << 1) | val;
val = digitalRead(Y1_pin);
data_out = (data_out << 1) | val;
val = digitalRead(Y0_pin);
data_out = (data_out << 1) | val;
return data_out;
}