Timer1.initialize(1325); -> 1325 microsecond
Timer1.pwm(PW,512); -> 50% cycle
no change during the 23800 pulses (but possible change for the next series)
maybe simpler to use timer interrupts to toggle the GPIO pin to generate the pulses
have a counter - when it reaches 23,800 pulses generated disable the interrupts
Thank you, I'll look into it (but I'm afraid the interrupt function isn't as accurate as the hardware PWM connected directly to pin 10. I'll do some tests)
adapted a pulse generator program to output 23800 pulse
uses ESP32 timer interrupts to generate the pulses and the PCNT (pulse counter module) to check the number of generated pulses
// ESP32 timer inteerrupts -
// generate 23800 pulse 1325mSec period
// use PCNT module to count pulses
// code check using external pulse counter
#include "driver/pulse_cnt.h"
#include "driver/gpio.h"
#include "esp_err.h"
#define PULSE_OUTPUT_PIN 19 // output pulse train
#define PULSE_INPUT_PIN 16 // input to PCNT to count the total pulses
// **** configure the ESP32 PCNT module to count pulses ***
#define EXAMPLE_PCNT_HIGH_LIMIT 20000
#define EXAMPLE_PCNT_LOW_LIMIT -100
pcnt_unit_config_t unit_config = {
.low_limit = EXAMPLE_PCNT_LOW_LIMIT,
.high_limit = EXAMPLE_PCNT_HIGH_LIMIT,
.flags = {
.accum_count = 1,
// .en_step_notify_up=1,
//.en_step_notify_down=0
}
};
pcnt_unit_handle_t pcnt_unit = NULL;
pcnt_chan_config_t chan_config = {
.edge_gpio_num = PULSE_INPUT_PIN,
.level_gpio_num = -1,
};
pcnt_channel_handle_t pcnt_chan = NULL;
// *** setup pulse generation ****
hw_timer_t *timer = NULL;
// timer ISR invert pin level, check if 23800 pulses complete
volatile int counter = 0, counter2 = 0, pulse_count = 0;
volatile unsigned long pulses_timer = 0; // total time of pulse train
void ARDUINO_ISR_ATTR onTimer() {
static uint32_t level = 0;
static unsigned long timer_start; // start time of pulse train
++counter;
// if start of pulse clear pulse count and get start time
if (++counter2 == 1) {
pulse_count = 0;
ESP_ERROR_CHECK(pcnt_unit_clear_count(pcnt_unit));
timer_start = millis();
}
// after 23800 pulses stop (+1 is to generate falling edge of last pulse)
if (counter2 == 23800 * 2 + 1) pulses_timer = millis() - timer_start; // time of pulse train
if (counter2 > 23800 * 2 + 1) return; // 23800 pulse complete
// invert pulse level
// digitalWrite(PULSE_OUTPUT_PIN, !digitalRead(PULSE_OUTPUT_PIN));
gpio_set_level((gpio_num_t)PULSE_OUTPUT_PIN, level); //!gpio_get_level((gpio_num_t)pin));
level = !level;
// read pulse count for display in loop()
int pulse_cnt = 0;
ESP_ERROR_CHECK(pcnt_unit_get_count(pcnt_unit, &pulse_cnt));
pulse_count = pulse_cnt;
pulses_timer = millis() - timer_start;
}
void setup() {
Serial.begin(115200);
delay(2000);
Serial.println("\n\n ESP32 timer inteerrupts - generate 23800 pulse 1325mSec period");
// setup PCNT pulse counter unit
Serial.println("install pcnt unit");
ESP_ERROR_CHECK(pcnt_new_unit(&unit_config, &pcnt_unit));
Serial.println("install pcnt channels");
ESP_ERROR_CHECK(pcnt_new_channel(pcnt_unit, &chan_config, &pcnt_chan));
Serial.println("set edge and level actions for pcnt channels");
ESP_ERROR_CHECK(pcnt_channel_set_edge_action(pcnt_chan, PCNT_CHANNEL_EDGE_ACTION_HOLD, PCNT_CHANNEL_EDGE_ACTION_INCREASE));
Serial.println("add watch point");
ESP_ERROR_CHECK(pcnt_unit_add_watch_point(pcnt_unit, EXAMPLE_PCNT_HIGH_LIMIT));
Serial.println("enable pcnt unit");
ESP_ERROR_CHECK(pcnt_unit_enable(pcnt_unit));
Serial.println("clear pcnt unit");
ESP_ERROR_CHECK(pcnt_unit_clear_count(pcnt_unit));
Serial.println("start pcnt unit");
ESP_ERROR_CHECK(pcnt_unit_start(pcnt_unit));
//
// setup timer etc or pulse output
pinMode(PULSE_OUTPUT_PIN, OUTPUT);
timer = timerBegin(2000000); // Set timer frequency Mhz
timerAttachInterrupt(timer, &onTimer); // Attach onTimer function to our timer.
// Set alarm to call onTimer function(value in microseconds).
// Repeat the alarm (third parameter) with unlimited count = 0 (fourth parameter).
timerAlarm(timer, 1325, true, 0);
}
void loop() {
// if keyboard key hit start new pulse sequence
if (Serial.available()) {
Serial.read();
counter2 = 0;
}
// display counters etc every second
static long timer1 = millis();
if (millis() - timer1 > 1000) {
timer1 = millis();
Serial.printf("pulses/sec %d ", counter);
Serial.printf("timer ticks %d ", counter2);
Serial.printf("Pulse count: %d ", pulse_count);
Serial.printf("pulses time %f\n", pulses_timer/1000.0f);
counter = 0;
}
}
serial monitor output
ESP32 timer inteerrupts - generate 23800 pulse 1325mSec period
install pcnt unit
install pcnt channels
set edge and level actions for pcnt channels
add watch point
enable pcnt unit
clear pcnt unit
start pcnt unit
pulses/sec 1510 timer ticks 1510 Pulse count: 754 pulses time 1.000000
pulses/sec 1510 timer ticks 3021 Pulse count: 1510 pulses time 2.001000
pulses/sec 1511 timer ticks 4532 Pulse count: 2265 pulses time 3.002000
pulses/sec 1511 timer ticks 6043 Pulse count: 3021 pulses time 4.003000
pulses/sec 1511 timer ticks 7554 Pulse count: 3776 pulses time 5.004000
pulses/sec 1511 timer ticks 9065 Pulse count: 4532 pulses time 6.005000
pulses/sec 1511 timer ticks 10576 Pulse count: 5287 pulses time 7.006000
pulses/sec 1511 timer ticks 12087 Pulse count: 6043 pulses time 8.007000
pulses/sec 1511 timer ticks 13598 Pulse count: 6798 pulses time 9.008000
pulses/sec 1511 timer ticks 15109 Pulse count: 7554 pulses time 10.009000
pulses/sec 1511 timer ticks 16620 Pulse count: 8309 pulses time 11.010000
pulses/sec 1511 timer ticks 18131 Pulse count: 9065 pulses time 12.012000
pulses/sec 1510 timer ticks 19642 Pulse count: 9820 pulses time 13.013000
pulses/sec 1510 timer ticks 21152 Pulse count: 10576 pulses time 14.014000
pulses/sec 1510 timer ticks 22663 Pulse count: 11331 pulses time 15.015000
pulses/sec 1510 timer ticks 24174 Pulse count: 12086 pulses time 16.014999
pulses/sec 1510 timer ticks 25685 Pulse count: 12842 pulses time 17.016001
pulses/sec 1511 timer ticks 27196 Pulse count: 13597 pulses time 18.017000
pulses/sec 1511 timer ticks 28707 Pulse count: 14353 pulses time 19.018000
pulses/sec 1511 timer ticks 30218 Pulse count: 15108 pulses time 20.018999
pulses/sec 1511 timer ticks 31729 Pulse count: 15864 pulses time 21.020000
pulses/sec 1511 timer ticks 33240 Pulse count: 16619 pulses time 22.021000
pulses/sec 1511 timer ticks 34751 Pulse count: 17375 pulses time 23.021999
pulses/sec 1511 timer ticks 36262 Pulse count: 18130 pulses time 24.023001
pulses/sec 1511 timer ticks 37773 Pulse count: 18886 pulses time 25.024000
pulses/sec 1511 timer ticks 39284 Pulse count: 19641 pulses time 26.025000
pulses/sec 1511 timer ticks 40795 Pulse count: 20397 pulses time 27.025999
pulses/sec 1511 timer ticks 42306 Pulse count: 21152 pulses time 28.027000
pulses/sec 1511 timer ticks 43817 Pulse count: 21908 pulses time 29.028000
pulses/sec 1511 timer ticks 45328 Pulse count: 22663 pulses time 30.030001
pulses/sec 1510 timer ticks 46838 Pulse count: 23419 pulses time 31.031000
pulses/sec 1510 timer ticks 48349 Pulse count: 23800 pulses time 31.535000
pulses/sec 1510 timer ticks 49860 Pulse count: 23800 pulses time 31.535000
pulses/sec 1510 timer ticks 51371 Pulse count: 23800 pulses time 31.53500
the final Pulse count: is 23800 with the pulses sequence output time 31.535seconds