Using ADC enableCompareRange

Status
Not open for further replies.

brianmichalk

Active member
Teensy 3.2

I have a project where I've been using an ISR triggered when a DIO pin toggled state. This works well, but in the field, the optical sensor degrades over time and the digital sensor supplies a voltage that eventually does not pass through the required digital threshold. The proper solution is to modify the circuit board, but I want to make a software solution in the interim.

I want to use the analog comparator and implement my own threshold for that pin.

Code:
void Sensor::setup(){
  pinMode(SENSOR_PIN, INPUT);
  adc->setAveraging(8);
  adc->setResolution(8);
  adc->setConversionSpeed(ADC_VERY_LOW_SPEED);
  int i = adc->analogRead(SENSOR_PIN, ADC_0); // get initial state
  if (i > THRESHOLD) {  // already high, set up for low toggle event 
    adc->enableCompareRange(0,THRESHOLD-DEADBAND,true,true,ADC_0);
  } else {
    adc->enableCompareRange(THRESHOLD+DEADBAND,255,true,true,ADC_0);
  }
  adc->enableInterrupts(ADC_0);
  adc->analogRead(SENSOR_PIN, ADC_0);
}

I have not been able to find any examples using this feature. The ISR:
Code:
void adc0_isr(void) {
  int i = adc->adc0->readSingle();  // capture and reset interrupt
  toggledUp = i > THRESHOLD;
  if (toggledUp) {
    adc->enableCompareRange(0,THRESHOLD-DEADBAND,true,true,ADC_0);  // reset for a transition to low
  } else {
    adc->enableCompareRange(THRESHOLD+DEADBAND,255,true,true,ADC_0);// reset for a transition to high
  }  
}
My question is this:
If I am in the ISR, and I call readSingle(), this will clear the interrupt. If the condition of the pin has not changed, will this ISR be called immediately again, or does the pin voltage have to pass out of the range and back into the compare range? My thinking is the former. And if so, the proper sequence would be:
disableInterrupts()
disableCompareRange()
readSingle()
enableCompareRange()
enableInterrupts()
 
Teensy 3.2 has 3 analog comparators (CMP0, CMP1, CMP2), each with an 6-bit DAC to generate a comparison value. There is a programmable hysteresis (5-30 mV), extensive filtering options and each has an own interrupt (selectable rising, falling or both). I believe there are 9 pins on T3.2 that can be used as inputs (3, 4, 9, 11, 12, 23, 27, 28, A14 / DAC0 out). So if you haven't used up those pins, you could just put a jumper wire...
 
Teensy 3.2 has 3 analog comparators (CMP0, CMP1, CMP2), each with an 6-bit DAC to generate a comparison value. There is a programmable hysteresis (5-30 mV), extensive filtering options and each has an own interrupt (selectable rising, falling or both). I believe there are 9 pins on T3.2 that can be used as inputs (3, 4, 9, 11, 12, 23, 27, 28, A14 / DAC0 out). So if you haven't used up those pins, you could just put a jumper wire...

Thanks, but I wasn't planning on using an actual voltage comparator, but a register compare. I hope I did not mis-understand. But still, if I clear the interrupt, will it immediately reenter if the conditions are the same? I suppose I'll find out soon enough when I get my changes in to test.
 
Status
Not open for further replies.
Back
Top