1

I am working on a proof-of-concept ADC to "explode" what's inside of an ADC chip for teaching purposes.

Currently, I have a sketch that uses a push-button to act as a clock-pulse that counts up (1, 2, 3, 4, ...) for every positive edge using the attachInterrupt() function.

The sketch works great to view the data in the serial monitor.

I am pairing the Arduino (Teensy 3.2) with a Raspberry Pi configured to read data off the Serial line to a text box.

After a good bit of troubleshooting it appears that the attachInterrupt() function corrupts the Serial buffer with garbage for every interrupt. For instance, if I use

Serial1.flush();
Serial1.begin(115200);

immediately after the interrupt and then send out my data using Serial.println() I get my expected decimal value at my output. However, without these lines I just get unicode garbage at my Serial output. See full "fixed" sketch code below:

My question is: why is this happening? I should not need to restart Serial every iteration of the clock.

#define HWSERIAL Serial1

const int numBits = 12;             //Number of bits in ADC (resolution)
const int comparatorPin = 14;       //Pin number of comparator out
const int clockPin = 15;            //Pin number of external clock (square wave) input; expecting clock with 50% duty-cycle
volatile int comparatorState = 0;   //Is comparator high or low?
volatile bool clockState = 0;       //Is clock high or low?
volatile int masterCount = 0;       //Our master count
volatile bool isSameVal = false;    //Is the value of the master count the same value as last cycle?

int outVal = 0;

int masterBinCount[12];     //Array to store binary value of the master count
byte outputPins[12];        //Array to store the numbers associated with the output pin numbers
int maxValue = 0;           //Initialize max counter value to zero

void setup() {
  Serial.begin(115200);       //Begin Serial transmission
  HWSERIAL.begin(115200, SERIAL_8N1);
  HWSERIAL.flush();
  delay(2000);

  defineOutPins(outputPins);  //Assign pin numbers for output pins

  pinMode(14, INPUT);   //Comparator Pin
  pinMode(15, INPUT);   //Clock-in pin

  maxValue = pow(2, numBits);   //Set max counter value to max bit resolution. Ex. for 12 bits --> 4096
}

void loop() {
  attachInterrupt(clockPin, doOnClock, RISING);   //When clock is on a positive-edge execute doOnClock function

  HWSERIAL.flush();
  HWSERIAL.begin(115200);

  if(isSameVal == false) {    //If the master count is NOT the same as the last cycle
    convertDecToBin();        //Convert the master count (dec) to binary
    binaryToPins();           //Send the binary value (via digitalWrite) to output pins
  }
  else {
    //Do nothing
  }
  delay(1000);
}

void defineOutPins(byte pinNos[]) {   //Assign pin numbers to parallel binary out: Teensy 3.2 pin #0 --> #11
  for(byte i = 0; i < numBits; i++) {
    pinNos[i] = {i};      //Assign current pin this value
    pinMode(i, OUTPUT);   //Define pins as outputs
  } 
}

void doOnClock() {
  comparatorState = digitalRead(comparatorPin);   //Check comparator state

  if(comparatorState >= 0.5) {            //Check if positive clock pulse
    if(masterCount == (maxValue - 1)) {   //Check if current count value is at "ceiling"
      //Do nothing
    }
    else {            //Else increment count up
      masterCount++;
    }
  }
  else if(comparatorState < 0.5) {    //Check if negative clock pulse
    if(masterCount == 0) {            //Check if current count value is at "floor"
      //Do nothing
    }
    else {
      masterCount--;    //Else decrment count down
    }
  }

  isSameVal = false;    //Set "finished" counting for this clock-pulse
}

void convertDecToBin() {
  for(int j = 0; j < numBits; j++) {
    int tempCnt = bitRead(masterCount, j);
    masterBinCount[numBits - (j+1)] = tempCnt;    //Populate the master binary count array in "reverse" order starting with the LSB at the highest array index
  }
}

void binaryToPins() {
  for(int e = 0; e < numBits; e++) {
    //digitalWrite(outputPins[e], masterBinCount[e]);   //Write our master binary value to output pins
    //Serial.print(masterBinCount[e]);                    //THIS LINE IS FOR TROUBLESHOOTING!!
  }
  isSameVal = true;       //Tell program we wrote the master count to the pins for this clock cycle

  outVal = masterCount;

  Serial.println(outVal);
  HWSERIAL.println(outVal);

  //Serial.print(" decimal: ");   //THIS LINE IS FOR TROUBLESHOOTING!!
  //Serial.print(masterCount);    //THIS LINE IS FOR TROUBLESHOOTING!!
  //Serial.println("");           //THIS LINE IS FOR TROUBLESHOOTING!!
}
1
  • not related to your problem .... the attachInterrupt() should be in setup() Commented Mar 23, 2019 at 18:41

1 Answer 1

1

I believe your issue is with initializing HWSERIAL.begin(115200); in void loop. I created this simple sketch to verify this. Firstly, I commented out the second initialization of Serial.begin, and you can see I was getting a perfect output to the serial monitor. Then I uncommented the line, re-uploaded the code to the Arduino, and you can see it didn't even make it through one print before it sent a bunch of garbled junk and effectively froze my serial port.

Remove HWSERIAL.begin(115200); from void loop and only set it in void setup.

Hope this helps!

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  Serial.begin(115200);
  Serial.print("Test");
  Serial.print(" - ");
  Serial.println(millis());
}

enter image description here

1
  • 1
    Thank you! I think that sorted things out. Commented Mar 22, 2019 at 19:32

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.