Hello,
I need to generate a specified number of pulses with a Teensy3.2
First time I configured timer1 with analogwritefrequency and analogwrite to generate the pulses on pin 3 and I attached an interrupt on the pin 3. The interrupt routine only counts the pulses and stops when the limit is reached. I counted the real pulses with another board : ESP32. I was surprised to find that ESP32 counted more pulses than the interrupt routine. For lower frequencies (50kHz) and a total number of about 1,800,000 pulses there is only one extra pulse counted by ESP32. For 500kHz : 12 pulses. For many pulses (18millions) and 700kHz, there are more than 10,000 extra pulses counted by ESP32. This is the code used for generating the pulses.
The second approach was to manually configure the Timer1 and to count the pulses when a compare match occurs. This is the code.
The result is very wrong: for a 200kHz signal and a limit of 1,920,000 pulses, ESP32 counts only 131,646 pulses. Obviously in the interrupt routine occur other events, but I don’t know how to filter them to count only for one compare match event.
Could you please help me to properly configure the interrupt routine?
I need to generate a specified number of pulses with a Teensy3.2
First time I configured timer1 with analogwritefrequency and analogwrite to generate the pulses on pin 3 and I attached an interrupt on the pin 3. The interrupt routine only counts the pulses and stops when the limit is reached. I counted the real pulses with another board : ESP32. I was surprised to find that ESP32 counted more pulses than the interrupt routine. For lower frequencies (50kHz) and a total number of about 1,800,000 pulses there is only one extra pulse counted by ESP32. For 500kHz : 12 pulses. For many pulses (18millions) and 700kHz, there are more than 10,000 extra pulses counted by ESP32. This is the code used for generating the pulses.
Code:
uint8_t pulse_pin = 3;
unsigned long NoOfPulses=0;
unsigned long pulseCount=0;
void setup() {
pinMode(pulse_pin, OUTPUT);
pulseCount=0;
delay(15000);
NoOfPulses=18502051;
analogWriteResolution(16);
analogWriteFrequency(pulse_pin, 700000.0L);
analogWrite(pulse_pin, 32000);
attachInterrupt (pulse_pin, pulseCounting, RISING);
}
void pulseCounting()
{
pulseCount++;
if (pulseCount >= NoOfPulses)
{
detachInterrupt (pulse_pin);
analogWriteFrequency(pulse_pin, 0.0); //in the same case (ramping still working and the end freq not restored), it will be restored now
analogWrite(pulse_pin, 32000);
Serial.println("Count ended!");
Serial.print("Counts: ");Serial.println(pulseCount);
}
}
void loop() {}
The second approach was to manually configure the Timer1 and to count the pulses when a compare match occurs. This is the code.
Code:
uint8_t pulsePin = 3;
volatile unsigned long pulseCount = 0;
unsigned long Npulses = 0;
#define FTM1_CH0_PIN 3
#define FTM_PINCFG(pin) FTM_PINCFG2(pin)
#define FTM_PINCFG2(pin) CORE_PIN ## pin ## _CONFIG
void setup() {
pinMode(pulsePin, OUTPUT);
FTM_PINCFG(FTM1_CH0_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
Npulses = 1920000;
double freq=200000.0L;
pulseCount=0;
delay(17000);
FTM1_COMBINE=0;
FTM1_SC=B1001000;
FTM1_C0SC=B1101000;
double fff=0.0L;
fff=48000000.0L/freq;
FTM1_MOD = (int) fff;
FTM1_C0V = (int) fff/2.0L;
NVIC_ENABLE_IRQ(IRQ_FTM1);
}
extern "C" void ftm1_isr(void) {
pulseCount++;
if(pulseCount>=Npulses)
{
FTM1_C0V = 0;
FTM1_MOD = 0;
Serial.println("end");
Serial.print("Pulses:");Serial.println(pulseCount);
}
}
void loop(void)
{}
The result is very wrong: for a 200kHz signal and a limit of 1,920,000 pulses, ESP32 counts only 131,646 pulses. Obviously in the interrupt routine occur other events, but I don’t know how to filter them to count only for one compare match event.
Could you please help me to properly configure the interrupt routine?