Ideally there is a CORES LIB fix in the waiting here … but … can you show code that does that?
When I was coding before the p#21 code it was tried with delayMicroseconds() as high as 105 and it was somehow not triggering the expected clearing reads … so the above was done.
And timing that discard code takes :: DISCARD INITIAL READS #=2 takes us #=249
Code:
FreqMeasure.begin();
{
[B]uint32_t tmeas = micros();[/B]
while (!FreqMeasure.available());
int yy = 0;
while ( yy < 2 ) { // removed dupe .available() same timing
if ( FreqMeasure.available()) {
FreqMeasure.read();
yy++;
}
}
[B]tmeas = micros() - tmeas;[/B]
Serial.print("\t\t DISCARD INITIAL READS #=");
Serial.print(yy);
Serial.print("\t takes us #=");
Serial.println(tmeas);
}
>> PERHAPS it is longer because this follows - not just on the first begin()?:
Code:
FreqMeasure.end();
FreqMeasure.begin();
these are the codes i am testing with lately
after you tie the two input pins together per the comments near top of code, apply sq wave 0-3.3 or 0.1-2.8 v fin there
t4 10k series each pin then tie together to gen
tlc direct to pin 13 and 2k to pin 16 from gen
these resistances allow you to safely unplug from usb while gen still running (another reason i like external gen) then re-plug
usb and see if numbers ok power on (cold) start, and i have also been doing sketch-upload to see if numbers ok warm start
these codes exercise both fmeas and fcount but if you have not made the posted edits to
two of the FreqCount lib files the t4 compile will crash - just comment everything related
to FreqCount and you will be ok. i am testing both the same way and in the same order
that my finished app does things
both these pgms have a define at the top where you choose tlc or t4 and it sets up several things according to that
including 3 dummy Fmeas for tls and 2 for t4. one dummy cycle for Fcount no matter which cpu
40 microsec delay after begin fmeas no matter which cpu
all of my testing so far has been tlc 48mhz smallest t4 600mhz faster
does two FreqMeas in a row and then FreqCount once - i have been doing most testing with 990 hz and 9950 hz
the first pgm does that and prints
the second pgm is similar but computes and prints the "overhead"time for FreqMeas and then for FreqCount fixes
maybe i am miscalculating from the elapsed microseconds
code is not clean or elegant - a) i am a duffer programmer and b) when i am chasing a funny i never clean anything
up til the funny is quashed. please excuse all of the unused variables
here is the first pgm:
Code:
// diag for fmeas and fcount
// set cpu for either tlc or t4
// t4 tie pin 22 to pin 9
// tlc tie pin 13 to pin 16
// ************************************
// note that both tlc and t4 only meas
// freq same both times if a) you delay
// 40 microsec after .. begin and b)
// there are two dummy ..avail ..read
// blobs before the two real ones
// note that freq count has a similar
// issue and one dummy read should be
// done before measurements
// ************************************
// this diag code does part of what the big
// pgm does which is freqmeas, freqmeas,
// many times freqcount all on the same
// signal
// data serial print order is fmeas1,fmeas2,fcount
// fcount is the 5th of 16 count measurements to
// keep the output small
// there are many unused left over variables
// from the pgm this was extracted from
#define cpu 1 // 0 for tlc 1 for t4
#include <FreqCount.h>
#include <FreqMeasure.h>
unsigned int data1,data2,data3;
unsigned short int tbit,sdelay,i,fe;
unsigned short int lim1,lim2,lim3;
unsigned short int lim4,lim5;
volatile unsigned short int cnt;
volatile unsigned int ary[45];
volatile unsigned short int lc;
volatile unsigned short int pdta;
volatile unsigned short int rp1,rp2;
volatile unsigned short int wrd0r,wrd1r,wrd2r;
volatile unsigned short int shft;
volatile unsigned short int wc;
volatile unsigned short int adr;
volatile unsigned short int fc;
volatile unsigned short int flag;
volatile unsigned short int mc;
volatile unsigned short int vc;
volatile unsigned int fm;
volatile unsigned int pe2;
volatile unsigned short int nm,fmd,fmpin,cpin;
volatile unsigned short int c8;
void setup(){
// tbit may be 20 or 40 or 80 or 140 or 200
tbit=80;
if(tbit== 80) sdelay=80;
if(tbit== 80) lim1=29;
if(tbit== 80) lim2=51;
if(tbit== 80) lim3=81;
if(tbit== 80) lim4=128;
if(tbit== 80) lim5=193;
// next two lines allow different intermeasurement
// delay on the fmeas blobs - sometimes 10 (ms)
// gives slightly less jumping betweem x 2x on the
// t4 but that is a lot of wasted time
if(cpu==0) fmd=1;
if(cpu==1) fmd=1;
if(cpu==0) fmpin=13;
if(cpu==1) fmpin=22;
if(cpu==0) cpin=16;
if(cpu==1) cpin= 9;
vc=1;
adr=1;
// in pin for fmeas
pinMode(fmpin, INPUT);
// in pin for fcount
pinMode(cpin, INPUT);
pinMode( 5, OUTPUT);
Serial.begin(9600);
digitalWrite(5, LOW);
delay(2000);
digitalWrite(5,HIGH);
}
void loop(){
mc=0;
lmc:
ary[mc]=0;
++mc;
if(mc<44)goto lmc;
Serial.println("begin");
// wait for pin 22 (fmeas) to change
wc=0;
wtchg:
rp1=digitalReadFast(fmpin);
delayMicroseconds(5);
rp2=digitalReadFast(fmpin);
delayMicroseconds(5);
if (rp1==rp2) goto wtchg;
Serial.println("change@in - start fmeas");
nm=0;
fm=0;
if (tbit== 80) delay(20);
FreqMeasure.begin();
delayMicroseconds(40);
// dummy meas 1
delay(fmd);
fmea:
nm = FreqMeasure.available();
if (!nm) goto fmea;
fm = FreqMeasure.read();
// dummy meas 2
delay(fmd);
fmeas:
nm = FreqMeasure.available();
if (!nm) goto fmeas;
fm = FreqMeasure.read();
if(cpu==1) goto skp3;
// dummy meas 3
delay(fmd);
fmeas7:
nm = FreqMeasure.available();
if (!nm) goto fmeas7;
fm = FreqMeasure.read();
skp3:
et1=etime;
// real meas 1
delay(fmd);
fmeasa:
nm = FreqMeasure.available();
if (!nm) goto fmeasa;
fm = FreqMeasure.read();
data1=fm;
// real meas 2
delay(fmd);
fmeas2:
nm = FreqMeasure.available();
if (!nm) goto fmeas2;
fm = FreqMeasure.read();
delay(fmd);
data2=fm;
FreqMeasure.end();
Serial.println("now start count 16 times");
delay(sdelay);
if (cpu == 1)FreqCount.begin(sdelay*1000);
if (cpu == 0)FreqCount.begin(sdelay* 1);
lc=0;
// meas cts per x ms 16 times
// and store msmnts in ary[]
lc=0;
loop:
if (!FreqCount.available()) goto loop;
Serial.print("*");
cnt = FreqCount.read();
++lc;
ary[lc]=cnt;
if (lc < 16) goto loop;
FreqCount.end();
Serial.println(" ");
// data3=ary[5];
// diag prints
Serial.println(data1); //f meas 1 should be aprox same each time
Serial.println(data2); //f meas 2 should be same as 1
// these 3 should be the same each time
fe=2;
data3=ary[fe];
Serial.println(data3);
data3=ary[fe+1];
Serial.println(data3);
data3=ary[fe+2];
Serial.println(data3);
Serial.println(" ");
delay(5000);
}
here is the second pgm that adds overhead timing
Code:
// diag for fmeas and fcount
// set cpu for either tlc or t4
// t4 tie pin 22 to pin 9
// tlc tie pin 13 to pin 16
// ************************************
// note that both tlc and t4 only meas
// freq same both times if a) you delay
// 40 microsec after .. begin and b)
// there are two dummy ..avail ..read
// blobs before the two real ones
// note that freq count has a similar
// issue and one dummy read should be
// done before measurements
// ************************************
// this diag code does part of what the big
// pgm does which is freqmeas, freqmeas,
// many times freqcount all on the same
// signal
// data serial print order is fmeas1,fmeas2,fcount
// fcount is the 5th of 16 count measurements to
// keep the output small
// there are many unused left over variables
// from the pgm this was extracted from
#define cpu 1 // 0 for tlc 1 for t4
#include <FreqCount.h>
#include <FreqMeasure.h>
elapsedMicros etime;
int et1,et2;
unsigned int data1,data2,data3;
unsigned short int tbit,sdelay,i,fe;
unsigned short int lim1,lim2,lim3;
unsigned short int lim4,lim5;
volatile unsigned short int cnt;
volatile unsigned int ary[45];
volatile unsigned short int lc;
volatile unsigned short int pdta;
volatile unsigned short int rp1,rp2;
volatile unsigned short int wrd0r,wrd1r,wrd2r;
volatile unsigned short int shft;
volatile unsigned short int wc;
volatile unsigned short int adr;
volatile unsigned short int fc;
volatile unsigned short int flag;
volatile unsigned short int mc;
volatile unsigned short int vc;
volatile unsigned int fm;
volatile unsigned int pe2;
volatile unsigned short int nm,fmd,fmpin,cpin;
volatile unsigned short int c8;
void setup(){
// tbit may be 20 or 40 or 80 or 140 or 200
tbit=80;
if(tbit== 80) sdelay=80;
if(tbit== 80) lim1=29;
if(tbit== 80) lim2=51;
if(tbit== 80) lim3=81;
if(tbit== 80) lim4=128;
if(tbit== 80) lim5=193;
// next two lines allow different intermeasurement
// delay on the fmeas blobs - sometimes 10 (ms)
// gives slightly less jumping betweem x 2x on the
// t4 but that is a lot of wasted time
if(cpu==0) fmd=1;
if(cpu==1) fmd=1;
if(cpu==0) fmpin=13;
if(cpu==1) fmpin=22;
if(cpu==0) cpin=16;
if(cpu==1) cpin= 9;
vc=1;
adr=1;
// in pin for fmeas
pinMode(fmpin, INPUT);
// in pin for fcount
pinMode(cpin, INPUT);
pinMode( 5, OUTPUT);
Serial.begin(9600);
digitalWrite(5, LOW);
delay(2000);
digitalWrite(5,HIGH);
}
void loop(){
mc=0;
lmc:
ary[mc]=0;
++mc;
if(mc<44)goto lmc;
Serial.println("begin");
// wait for pin 22 (fmeas) to change
wc=0;
wtchg:
rp1=digitalReadFast(fmpin);
delayMicroseconds(5);
rp2=digitalReadFast(fmpin);
delayMicroseconds(5);
if (rp1==rp2) goto wtchg;
Serial.println("change@in - start fmeas");
nm=0;
fm=0;
if (tbit== 80) delay(20);
FreqMeasure.begin();
etime=0;
delayMicroseconds(40);
// dummy meas 1
delay(fmd);
fmea:
nm = FreqMeasure.available();
if (!nm) goto fmea;
fm = FreqMeasure.read();
// dummy meas 2
delay(fmd);
fmeas:
nm = FreqMeasure.available();
if (!nm) goto fmeas;
fm = FreqMeasure.read();
if(cpu==1) goto skp3;
// dummy meas 3
delay(fmd);
fmeas7:
nm = FreqMeasure.available();
if (!nm) goto fmeas7;
fm = FreqMeasure.read();
skp3:
et1=etime;
// real meas 1
delay(fmd);
fmeasa:
nm = FreqMeasure.available();
if (!nm) goto fmeasa;
fm = FreqMeasure.read();
data1=fm;
// real meas 2
delay(fmd);
fmeas2:
nm = FreqMeasure.available();
if (!nm) goto fmeas2;
fm = FreqMeasure.read();
delay(fmd);
data2=fm;
FreqMeasure.end();
Serial.println("now start count 16 times");
delay(sdelay);
if (cpu == 1)FreqCount.begin(sdelay*1000);
if (cpu == 0)FreqCount.begin(sdelay* 1);
lc=0;
// meas cts per x ms 16 times
// and store msmnts in ary[]
lc=0;
etime=0;
loop:
if (!FreqCount.available()) goto loop;
// Serial.print("*");
cnt = FreqCount.read();
++lc;
ary[lc]=cnt;
if (lc < 16) goto loop;
et2=etime;
FreqCount.end();
Serial.println(" ");
// data3=ary[5];
// diag prints
Serial.println(data1); //f meas 1 should be aprox same each time
Serial.println(data2); //f meas 2 should be same as 1
// these 3 should be the same each time
fe=2;
data3=ary[fe];
Serial.println(data3);
data3=ary[fe+1];
Serial.println(data3);
data3=ary[fe+2];
Serial.println(data3);
Serial.println(" ");
if (cpu==1) et1=et1-2*fmd*1000;
if (cpu==0) et1=et1-3*fmd*1000;
Serial.println(et1);
et2=et2/16-80000;
Serial.println(et2);
Serial.println(" ");
delay(5000);
}