Last active
January 12, 2023 01:36
-
-
Save econeale/e4b6a65947b669ac1c4751b573459068 to your computer and use it in GitHub Desktop.
Use PCNT component in Arduino Sketch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "driver/pcnt.h" | |
uint8_t pcnt_pin = 16; | |
unsigned long check_time = 0; | |
int16_t count = 0; | |
pcnt_config_t tac_config; | |
void setup() { | |
Serial.begin ( 115200 ); | |
pinMode(pcnt_pin, INPUT_PULLUP); | |
tac_config.pulse_gpio_num = (int) pcnt_pin; // Attach to the pin | |
tac_config.ctrl_gpio_num = PCNT_PIN_NOT_USED; // Disable control pin | |
tac_config.channel = PCNT_CHANNEL_0; // Attach to Channel 0 | |
tac_config.unit = PCNT_UNIT_0; // Attach to Unit 0 | |
tac_config.pos_mode = PCNT_COUNT_INC; // Increase on rising edge | |
tac_config.neg_mode = PCNT_COUNT_DIS; // Disable negative counting | |
tac_config.hctrl_mode = PCNT_MODE_KEEP; // Keep the current settings when the control pin is high | |
tac_config.lctrl_mode = PCNT_MODE_KEEP; // Keep the current settings when the control pin is low | |
tac_config.counter_h_lim = 20; // Maximum number for counter (cannot exceed 20) | |
tac_config.counter_l_lim = -20; // Minimum number for counter (cannot be lower than -20) | |
if ( pcnt_unit_config(&tac_config) == ESP_OK ) { | |
pcnt_filter_disable(PCNT_UNIT_0); // Disable the filter to make sure we catch small pulses | |
pcnt_counter_pause(PCNT_UNIT_0); // Make doubly sure the counter is set to zero | |
pcnt_counter_clear(PCNT_UNIT_0); | |
pcnt_counter_resume(PCNT_UNIT_0); | |
} | |
} | |
void loop() { | |
if(millis() - check_time > 30000) { | |
pcnt_counter_pause(PCNT_UNIT_0); // Pause the counter | |
pcnt_get_counter_value(PCNT_UNIT_0, &count); // Get the counter value | |
pcnt_counter_clear(PCNT_UNIT_0); // Clear the counter | |
pcnt_counter_resume(PCNT_UNIT_0); // Restart the counter | |
Serial.print("Counter: "); | |
Serial.println(count); | |
} | |
} |
Good catch!
Actually U did. You placed it in the variables area. I put mine along with the other setup. That's probably why I missed it. BTW. Is there a way to use this setup and use encoders at the same time. I have a project that needs a frequency (pulse) counter such as this and an encoder to adjust other parameters. When I use say this setup along with any other encoder library, which seems to be using an instance of PCNT, they interfere with each other. I even call myself putting this pulse counter on say unit #1, which i am assuming this starts using another counter module and load the encoder library which i am assuming is on unit 0. channel 0 and the encoder works but the counter stops working. Any ideas? Thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You forgot to add the PCNT instance of pcnt_config_t tac_config; at the beginning of your pulse counter variables. It will complain that tac_config was not declared.
Later