Hi all,
I am again seeking a little guidance on my "exploded" ADC design using the Teensy 3.2. I am using the Teensy as the control logic for my up/down counting type ADC, interfacing with the 74LS191 Synchronous Up/Down Binary counter.
It seems to function just fine counting up with a super-slow 1Hz clock-pulse using the code below. However, when the comparator is force switched to "Low" the counter makes 2-3 down counts then just oscillates between two values. Looking for some insight as after pouring over the code for hours hasn't yielded a solution yet.
Schematic:
Teensy Code:
Attempted down-count:
Successful up-count:
Control Logic from counter IC:
I am again seeking a little guidance on my "exploded" ADC design using the Teensy 3.2. I am using the Teensy as the control logic for my up/down counting type ADC, interfacing with the 74LS191 Synchronous Up/Down Binary counter.
It seems to function just fine counting up with a super-slow 1Hz clock-pulse using the code below. However, when the comparator is force switched to "Low" the counter makes 2-3 down counts then just oscillates between two values. Looking for some insight as after pouring over the code for hours hasn't yielded a solution yet.
Schematic:
Teensy Code:
Code:
#define HWSERIAL Serial1
const int numBits = 12; //Number of bits in ADC (resolution)
const int comparatorPin = 22; //Pin number of comparator out
const int clockPin = 23; //Pin number of external clock (square wave) input; expecting clock with 50% duty-cycle
const int enablePin = 19;
const int upDownPin = 18;
volatile bool clockState = 0; //Is clock high or low?
volatile bool isSameVal = false; //Is the value of the master count the same value as last cycle?
volatile bool upDown = 0; //Init up/down count to up (being low state, or false)
byte masterBinCountIn[12]; //Array to store binary value of the master count
byte inputPins[12]; //Array to store the numbers associated with the output pin numbers
int maxValue = 0; //Initialize max counter value to zero
bool comparatorState = 0; //Init comparator state
byte masterCount = 0; //Init master Count
void setup() {
Serial.begin(115200); //Begin Serial transmission (for troubleshooting)
HWSERIAL.begin(115200, SERIAL_8N1);
delay(5000);
defineInPins(); //Assign pin numbers for output pins
pinMode(comparatorPin, INPUT); //Comparator Pin
pinMode(clockPin, INPUT); //Clock-in pin
pinMode(enablePin, OUTPUT); //Default to up-count (both low)
pinMode(upDownPin, OUTPUT);
digitalWrite(enablePin, LOW);
digitalWrite(upDownPin, LOW);
maxValue = pow(2, numBits); //Set max counter value to max bit resolution. Ex. for 12 bits --> 4096
attachInterrupt(clockPin, doOnClock, RISING); //When clock is on a positive-edge execute doOnClock function
}
void loop() {
}
void defineInPins() { //Assign pin numbers to parallel binary out: Teensy 3.2 pin #2 --> #14
for(byte i = 2; i < (numBits + 2); i++) {
inputPins[(i-2)] = {i}; //Assign current pin this value
pinMode(i, INPUT); //Define pins as outputs
Serial.print(inputPins[(i-2)]);
}
}
void doOnClock() {
Serial.println("clock pulsed!");
binaryToPins(); //Get current binary count
masterCount = convertBinToDec(); //Convert to decimal value
comparatorState = digitalRead(comparatorPin); //Check comparator state
Serial.print("comparator state: ");
Serial.print(comparatorState);
Serial.println();
Serial.print("master count is: ");
Serial.print(masterCount);
Serial.println();
HWSERIAL.println(masterCount);
if(comparatorState == 1) { //Check if comparator is HIGH
if(masterCount == (maxValue - 1)) { //Check if current count value is at "ceiling"
//Do nothing
}
else { //Else, increment count up
upDown = 0;
toggleClk(upDown); //Control counter IC
}
}
else if(comparatorState == 0) { //Check if comparator is LOW
if(masterCount == 0) { //Check if current count value is at "floor"
//Do nothing
}
else {
upDown = 1;
toggleClk(upDown); //Control counter IC
}
}
}
byte convertBinToDec() {
byte result = 0;
for(int i = 0; i < numBits; i++) {
result |= masterBinCountIn[(11-i)] << i;
}
return result;
}
void binaryToPins() {
for(int e = 0; e < numBits; e++) {
masterBinCountIn[(11-e)] = digitalRead(inputPins[e]); //Read our master binary value to output pins
Serial.print(masterBinCountIn[e]);
}
Serial.println();
}
void toggleClk(bool hiOrLo) {
if(hiOrLo == 1) {
if(isSameVal == false) {
//count down from up
digitalWrite(enablePin, HIGH);
delayMicroseconds(100);
digitalWrite(upDownPin, HIGH);
delayMicroseconds(50);
digitalWrite(enablePin, LOW);
Serial.println("down count");
isSameVal = true;
}
else {
//do nothing
Serial.println("Do nothing!");
Serial.println(isSameVal);
}
}
else if(hiOrLo == 0) {
//count up from down
if(isSameVal == true) {
digitalWrite(enablePin, HIGH);
delayMicroseconds(100);
digitalWrite(upDownPin, LOW);
delayMicroseconds(50);
digitalWrite(enablePin, LOW);
Serial.println("up count");
isSameVal = false;
}
else {
//Do nothing
}
}
}
Attempted down-count:
Successful up-count:
Control Logic from counter IC: