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:
  1. #include "driver/pcnt.h"
  2. pcnt_config_t pcnt_config = {
  3.         .pulse_gpio_num = 2,            // set gpio for pulse input gpio
  4.         .ctrl_gpio_num = -1,            // no gpio for control
  5.         .lctrl_mode = PCNT_MODE_KEEP,   // when control signal is low, keep the primary counter mode
  6.         .hctrl_mode = PCNT_MODE_KEEP,   // when control signal is high, keep the primary counter mode
  7.         .pos_mode = PCNT_COUNT_INC,     // increment the counter on positive edge
  8.         .neg_mode = PCNT_COUNT_DIS,     // do nothing on falling edge
  9.         .counter_h_lim = 2500,
  10.         .counter_l_lim = 0,
  11.         .unit = PCNT_UNIT_0,               /*!< PCNT unit number */
  12.         .channel = PCNT_CHANNEL_0
  13.     };
  14. pcnt_isr_handle_t user_isr_handle = NULL; //user's ISR service handle
  15.  
  16. void setup() {
  17.     pcnt_unit_config(&pcnt_config);        //init unit
  18.     pcnt_set_filter_value(PCNT_UNIT_0, test_count);
  19.     pcnt_filter_enable(PCNT_UNIT_0);
  20.     /* Register ISR handler and enable interrupts for PCNT unit */
  21.     pcnt_isr_register(pcnt_intr_handler, NULL, 0, &user_isr_handle);
  22.     pcnt_intr_enable(PCNT_UNIT_0);
  23.  
  24.     /* Everything is set up, now go to counting */
  25.     pcnt_counter_resume(PCNT_UNIT_0);
  26. }