bungernaut
Member
I am trying to measure capacitance with a LC oscillator. I'm using a timer interrupt at 1Hz to display the measured value, and an attached interrupt on my oscillator output (~1MHz) on digital pin 20 to count pulses for the LC frequency.
The sketch works, but occasionally it appears to miss a call from the timer interrupt, causing the calculation to be off by almost exactly a factor of 2X frequency I expect, making me think the timer interrupt call was missed. I have set the timer interrupt priority to 0, the highest by my understanding so I would expect that what's happening is the timer interrupts occurs during a pulse input and it's just missed?
Circuit diagram and code (also below): https://github.com/bungernut/Teensy_LCCapMeter/blob/main/LCCapMeter.ino
It would be ok to have a few extra pulses counted but why does the timer interrupt not always get called?
What is the best way to run two interrupts where one is high rate and the other is high priority?
Is there a better way to count pulses without an interrupt that would not interfere with the timer interrupt?
The sketch works, but occasionally it appears to miss a call from the timer interrupt, causing the calculation to be off by almost exactly a factor of 2X frequency I expect, making me think the timer interrupt call was missed. I have set the timer interrupt priority to 0, the highest by my understanding so I would expect that what's happening is the timer interrupts occurs during a pulse input and it's just missed?
Circuit diagram and code (also below): https://github.com/bungernut/Teensy_LCCapMeter/blob/main/LCCapMeter.ino
It would be ok to have a few extra pulses counted but why does the timer interrupt not always get called?
What is the best way to run two interrupts where one is high rate and the other is high priority?
Is there a better way to count pulses without an interrupt that would not interfere with the timer interrupt?
Code:
volatile uint32_t counts;
IntervalTimer myTimer;
double capF;
const double indH = 0.001;
const double twoPi = 6.283185;
void setup() {
Serial.begin(115200);
/* //myTimer.begin(function, microseconds); */
myTimer.priority(0);
myTimer.begin(dispCounts, 1000000); // run every 1 seconds
pinMode(20, INPUT);
attachInterrupt(digitalPinToInterrupt(20), doCount, FALLING);
}
void doCount()
{
counts++;
}
void dispCounts()
{
capF = 1.0 / (pow( (double)counts/1000000.0 * twoPi ,2) * indH) ;
Serial.println(capF);
counts = 0;
}
void loop()
{
while (1);
}
Last edited: