This thread is related to previous threads
Sub micro timing and
Teensyduino access to counting cpu cycles
I've been using the teensy 3.1 to measure an external time interval. I use a very stable 100 Hz signal applied to 2 pins, one for the rising edge and one for the falling edge. The program counts cpu cycles to measure the time. The teensy runs at 48 MHz and the interval is 5 msec, 240000 cpu cycles. I measure an average of 239975 in my test. I use the average value to see how much each measurement differs from the average. I first ran the program below with the test for "test <-5 || test > 5" commented out and collected about 2000 data points. Then I re-ran it with the test for about 200000 total data points. I used 2 separate tests to reduce the number of data points that got recorded. Of the 200000 total points, about 1200 got recorded. I combined the results, scaling the second set to account for the longer run. The frequency distribution is in the table below. I realize it's probably hard to follow.
Most of the data is/are very close to the average. I noticed that one group of measurements is far removed from the others. In 200000 measurements, nothing falls between -25 cpu cycles and -100 cpu cycles from the average. The table shows 2 for the range -103 to -135. That is actually 1 from the run of 2000 and 100 from the run of 200000 (scaled by 0.01, 2000/200000). 100 cpu cycles is about 2 usec.
Maybe more than one thing is going on.
I don't know how to adjust the priority of my 2 interrupts. That might tighten the distribution of the points between 17 and -25 in the table. I expect some jitter due to the asynchronous signal.
Is something else causing the infrequent (100 in 200000 measurements) errors of about 100 cpu cycles?
Thanks,
Bob
bucket total
17 0.01
15 0.04
13 0.14
11 1.6
9 3.08
7 9.06
6 0.02
5 27
3 478
1 846
-1 382
-3 204
-5 0
-7 0.11
-9 0.11
-11 0.07
-13 0.06
-15 0.11
-17 0.1
-19 0.07
-20 0.07
-21 0.16
-22 0.09
-23 1.09
-24 0.05
-25 0.09
nothing between -26 and -102
-103 to -135 2
Sub micro timing and
Teensyduino access to counting cpu cycles
I've been using the teensy 3.1 to measure an external time interval. I use a very stable 100 Hz signal applied to 2 pins, one for the rising edge and one for the falling edge. The program counts cpu cycles to measure the time. The teensy runs at 48 MHz and the interval is 5 msec, 240000 cpu cycles. I measure an average of 239975 in my test. I use the average value to see how much each measurement differs from the average. I first ran the program below with the test for "test <-5 || test > 5" commented out and collected about 2000 data points. Then I re-ran it with the test for about 200000 total data points. I used 2 separate tests to reduce the number of data points that got recorded. Of the 200000 total points, about 1200 got recorded. I combined the results, scaling the second set to account for the longer run. The frequency distribution is in the table below. I realize it's probably hard to follow.
Most of the data is/are very close to the average. I noticed that one group of measurements is far removed from the others. In 200000 measurements, nothing falls between -25 cpu cycles and -100 cpu cycles from the average. The table shows 2 for the range -103 to -135. That is actually 1 from the run of 2000 and 100 from the run of 200000 (scaled by 0.01, 2000/200000). 100 cpu cycles is about 2 usec.
Maybe more than one thing is going on.
I don't know how to adjust the priority of my 2 interrupts. That might tighten the distribution of the points between 17 and -25 in the table. I expect some jitter due to the asynchronous signal.
Is something else causing the infrequent (100 in 200000 measurements) errors of about 100 cpu cycles?
Thanks,
Bob
bucket total
17 0.01
15 0.04
13 0.14
11 1.6
9 3.08
7 9.06
6 0.02
5 27
3 478
1 846
-1 382
-3 204
-5 0
-7 0.11
-9 0.11
-11 0.07
-13 0.06
-15 0.11
-17 0.1
-19 0.07
-20 0.07
-21 0.16
-22 0.09
-23 1.09
-24 0.05
-25 0.09
nothing between -26 and -102
-103 to -135 2
Code:
/*
Teensy 3.1, Arduino 1.61 and Teensyduino 1.21
*/
const int pin2 = 2;
const int pin3 = 3;
unsigned long longCount = 0;
long test;
volatile int riseFlag = 0;
volatile int fallFlag = 0;
unsigned long pulseWidth = 0;
unsigned long fallEdge = 0;
unsigned long riseEdge = 0;
void setup() {
ARM_DEMCR |= ARM_DEMCR_TRCENA;
ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA;
pinMode(pin2, INPUT);
pinMode(pin3, INPUT);
Serial.begin(9600);
attachInterrupt(pin2, fall_ISR, FALLING); // set flag high on falling edge
attachInterrupt(pin3, rise_ISR, RISING); // set flag high on rising edge
}
void loop() {
longCount++;
fallFlag = 0;
while (!fallFlag) { // wait for falling edge
}
fallEdge = ARM_DWT_CYCCNT;
riseFlag = 0;
while (!riseFlag) {
}
riseEdge = ARM_DWT_CYCCNT;
pulseWidth = riseEdge - fallEdge;
test = pulseWidth - 239975; // 24000 based on 100 Hz, 50% and 48 MHz. 239975 average of 450 readings
if (test < -5 || test > 5) { // span of -5 to 5 in 450 readings not counting exceptions
Serial.print(longCount);
Serial.print(" ");
Serial.print(test);
Serial.print(" ");
Serial.println(pulseWidth);
}
delay(100);
}
//-------------------------------------
void fall_ISR() {
fallFlag = 1;
}
//-------------------------------------
void rise_ISR() {
riseFlag = 1;
}