Thanks for complete code - wrongly assumed there were two competing IntervalTimer()'s.
SEE POST #3 for the ANSWER!
Code:
code re-ordering moving .end() up to top
: needed change marked in testinner( )
Gives this correct result, because the duration of the _isr code allows the timer to trigger a second time creating a PENDING interrupt before the .end() is called:
Code:
C:\T_Drive\tCode\FORUM\iTimerChange\iTimerChange.ino Jan 16 2023 02:14:56
test clock 10 1
A INdelay us=12.156667 Delta=2.393333 B IN CYC=2973357490 Delta us=1.620000
DONE
test clock 10 1
A INdelay us=12.183333 Delta=2.393333 B IN CYC=3693429474 Delta us=1.646667
DONE
CHANGE _u2
test clock 10 11
A INdelay us=12.210000 Delta=12.393333 B IN CYC=1018533438 Delta us=1.673333
DONE
test clock 10 11
A INdelay us=12.190000 Delta=12.393333 B IN CYC=1798604574 Delta us=1.653333
DONE
CHANGE _u2
test clock 10 1
A INdelay us=12.170000 Delta=2.393333 B IN CYC=2878676086 Delta us=1.633333
DONE
test clock 10 1
A INdelay us=12.190000 Delta=2.393333 B IN CYC=3418747174 Delta us=1.653333
DONE
Wrote this edit since no scope here:
Code:
#include "Arduino.h"
#include <digitalWriteFast.h>
#include <limits.h>
const int PinB = 11;
const int busyPin = 1;
const int led = 13;
#define RCVLEN 256
char rcvbuffer[RCVLEN];
uint16_t nrcvbuf = 0;
IntervalTimer testTimer;
unsigned int test_u1 = 10;
unsigned int test_u2 = 1;
volatile uint32_t tiA = 0;
volatile uint32_t toA = 0;
volatile uint32_t tdA = 0;
uint32_t tiB = 0;
uint32_t toB = 0;
void testinner( ) {
// testTimer.end( ); // Change suggested in Post #3: uncomment this and comment out the .end() below!
tiA = ARM_DWT_CYCCNT;
digitalWriteFast(PinB, HIGH);
delayMicroseconds(1);
digitalWriteFast(PinB, LOW);
digitalWriteFast( busyPin, LOW );
delayMicroseconds(test_u2);
testTimer.end( );
toA = ARM_DWT_CYCCNT;
tdA = toA - tiA;
}
void testclock( ) {
tiB = ARM_DWT_CYCCNT;
digitalWriteFast(PinB, HIGH);
delayMicroseconds(1);
digitalWriteFast(PinB, LOW);
digitalWriteFast( busyPin, HIGH );
testTimer.begin( testinner, test_u1 );
toB = ARM_DWT_CYCCNT;
}
char *startsWith( char *s, const char *key ) {
int n = strlen(key);
if ( !strncmp( s, key, n ) ) {
return s + n;
}
return 0;
}
char *parseUint( char *s, unsigned int *u ) {
unsigned long int l;
char *p = s;
l = strtoul( s, &p, 0 );
if ( (p > s) && (l <= UINT_MAX) ) {
*u = (unsigned int) l;
return p;
}
return 0;
}
void setup() {
pinMode(led, OUTPUT);
pinMode(busyPin, OUTPUT);
pinMode(PinB, OUTPUT);
digitalWriteFast(busyPin, LOW);
digitalWriteFast(PinB, LOW);
Serial.begin(115200);
while (!Serial && millis() < 4000 );
Serial.println("\n" __FILE__ " " __DATE__ " " __TIME__);
delay(100);
}
void loop() {
uint16_t nlen = 0;
char *pc;
char c;
if (0 != tdA) {
Serial.printf( "A INdelay us=%f Delta=%f B IN CYC=%lu Delta us=%f \n", (tiA - tiB) / 600.0, tdA / 600.0, tiB, (toB - tiB) / 600.0 );
tdA = 0;
}
while ( Serial.available() ) {
c = Serial.read();
if ( c ) {
if ( iscntrl( c ) ) {
nlen = nrcvbuf;
rcvbuffer[nrcvbuf] = 0;
nrcvbuf = 0;
break;
}
else if ( nrcvbuf || !isspace(c) ) {
rcvbuffer[nrcvbuf++] = c;
}
if ( nrcvbuf >= RCVLEN ) {
Serial.println( (char *)"Error: buffer overflow" );
nrcvbuf = 0;
}
}
}
if ( nlen > 0 ) {
if ( (pc = startsWith( rcvbuffer, "test clock")) ) {
unsigned int test_u2B = 0;
(pc = parseUint( pc, &test_u1 ) ) && (pc = parseUint( pc, &test_u2B ) );
if (test_u2 != test_u2B) Serial.println( "\tCHANGE _u2 " );
test_u2 = test_u2B;
Serial.print( "test clock ");
Serial.print( test_u1 );
Serial.print( " " );
Serial.println( test_u2 );
testclock();
delayMicroseconds(100);
if (0 != tdA) {
Serial.printf( "\tA INdelay us=%f Delta=%f B IN CYC=%lu Delta us=%f \n", (tiA - tiB) / 600.0, tdA / 600.0, tiB, (toB - tiB) / 600.0 );
tdA = 0;
}
}
else {
Serial.print( "unknown command //" );
Serial.print( (char *) rcvbuffer );
Serial.println( "//" );
}
nlen = 0;
Serial.println( "DONE" );
}
delay(100);
}
It shows with above code and belated .end(): After the time change the NEXT interval timing is based on the PRIOR setting due to pending interrupt.
Code:
C:\T_Drive\tCode\FORUM\iTimerChange\iTimerChange.ino Jan 16 2023 02:08:25
test clock 10 1
A INdelay us=12.110000 Delta=2.361667 B IN CYC=201008866 Delta us=1.640000
DONE
test clock 10 1
A INdelay us=12.063333 Delta=2.361667 B IN CYC=741080842 Delta us=1.593333
DONE
test clock 10 1
A INdelay us=12.130000 Delta=2.361667 B IN CYC=1161151654 Delta us=1.660000
DONE
CHANGE _u2
test clock 10 11
A INdelay us=12.110000 Delta=12.361667 B IN CYC=1621353706 Delta us=1.640000
DONE
test clock 10 11
A INdelay us=2.116667 Delta=12.361667 B IN CYC=3721424826 Delta us=1.646667
DONE
test clock 10 11
A INdelay us=2.136667 Delta=12.361667 B IN CYC=3326528318 Delta us=1.666667
DONE
CHANGE _u2
test clock 10 1
A INdelay us=2.076667 Delta=2.361667 B IN CYC=1866730070 Delta us=1.606667
DONE
test clock 10 1
A INdelay us=12.136667 Delta=2.361667 B IN CYC=2286800586 Delta us=1.666667
DONE
test clock 10 1
A INdelay us=12.083333 Delta=2.361667 B IN CYC=2706871366 Delta us=1.613333
DONE