Page 1 of 1

Pulse counter / totalizer usage - PCNT supporting functions

Posted: Tue Aug 29, 2017 7:51 am
by onpa_esp32
Friends,
I would like to use pulse counting on ESP32 in Arduino IDE. Unfortunately, IDF offers a lot of functions for working with counter, I don't know, how to use it in Arduino IDE. Honestly, I'm using Platform IO, but nevermind...

Arduino IDE doesn't know "pcnt" functions, like
pcnt_unit_config(pcnt_config_t *pcnt_config).

Maybe I'm doing something wrong, maybe I don't see any hints...

Could you help me and describe, how to use pulse counter ESP32 hardware in Arduino IDE ?

Thanks,
Ondrej

Re: Pulse counter / totalizer usage - PCNT supporting functions

Posted: Thu May 28, 2020 7:55 pm
by technerdchris
The below code compiles for me in Arduino IDE (1.8.12 as of May 2020). I did use Sketch -> Include Library to add ESP32 library.

In Windows 10, Arduino installs its libraries in the user folder documents. pcnt.h is in the folder:

Code: Select all

Documents\ArduinoData\packages\esp32\hardware\esp32\1.0.2\tools\sdk\include\driver\driver
Although this compiles, I'm now on the quest to get the PCNT to work. :roll: :| :mrgreen:

Code: Untitled.cpp Select all


#include "driver/pcnt.h"
pcnt_config_t pcnt_config = {
.pulse_gpio_num = 2, // set gpio for pulse input gpio
.ctrl_gpio_num = -1, // no gpio for control
.lctrl_mode = PCNT_MODE_KEEP, // when control signal is low, keep the primary counter mode
.hctrl_mode = PCNT_MODE_KEEP, // when control signal is high, keep the primary counter mode
.pos_mode = PCNT_COUNT_INC, // increment the counter on positive edge
.neg_mode = PCNT_COUNT_DIS, // do nothing on falling edge
.counter_h_lim = 2500,
.counter_l_lim = 0,
.unit = PCNT_UNIT_0, /*!< PCNT unit number */
.channel = PCNT_CHANNEL_0
};
pcnt_isr_handle_t user_isr_handle = NULL; //user's ISR service handle

void setup() {
pcnt_unit_config(&pcnt_config); //init unit
pcnt_set_filter_value(PCNT_UNIT_0, test_count);
pcnt_filter_enable(PCNT_UNIT_0);
/* Register ISR handler and enable interrupts for PCNT unit */
pcnt_isr_register(pcnt_intr_handler, NULL, 0, &user_isr_handle);
pcnt_intr_enable(PCNT_UNIT_0);

/* Everything is set up, now go to counting */
pcnt_counter_resume(PCNT_UNIT_0);
}