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.
I have not been able to find any examples using this feature. The ISR:
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()
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
}
}
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()