ESP32 hardware timer with input pin for interrupt?

cyberman54
Posts: 27
Joined: Sun Jan 14, 2018 7:47 pm

ESP32 hardware timer with input pin for interrupt?

Postby cyberman54 » Wed Mar 20, 2019 8:30 am

I need a free running hardware timer, which counts until an edge signal on a GPIO occurs. I want to capture the counter in this moment, to take a timestamp.

After studying the ESP32 hardware manual i am not sure what would be the most straight forward hardware based way to realize this: using pulse counter? watchdog timer? ... ?

Any help/tips on this would be appreciated, thanks.

idahowalker
Posts: 166
Joined: Wed Aug 01, 2018 12:06 pm

Re: ESP32 hardware timer with input pin for interrupt?

Postby idahowalker » Wed Mar 20, 2019 11:20 am

What are the ways you have come up with and the issues you gave with those ways?

Bhanuka92
Posts: 17
Joined: Wed Jan 02, 2019 6:36 pm

Re: ESP32 hardware timer with input pin for interrupt?

Postby Bhanuka92 » Fri Mar 22, 2019 12:17 pm

In case you need to measure the speed of the Digital Input,
You can set up a hardware counter to count the input and a timer interrupt to read the hardware counter and calculate the speed.

This is the code for hardware counter on ESP32 , I used this code with ESP32 hardware is a industrial controller with ESP32, NORVI IIOT.

Code: Select all

extern "C" {
#include "soc/pcnt_struct.h"
}
#include "driver/pcnt.h"

byte pulsePin = 13;

int flowcountint=0;
int16_t flowCounter = 0;
int16_t Pulses = 0;
int16_t x;
volatile uint32_t us_time, us_time_diff;


volatile byte state = LOW;
volatile byte state2 = LOW;
volatile byte state_tmr = 0;
volatile byte value_ready = 0;

#define PCNT_TEST_UNIT PCNT_UNIT_0
#define PCNT_H_LIM_VAL 32767
#define PCNT_L_LIM_VAL -1

void setup() {

pinMode(pulsePin,INPUT);
Serial.begin(115200);
Serial.println("");
init_counter();

}

void loop() {
readPcntCounter_0();

// pcnt_counter_clear(PCNT_TEST_UNIT);
// pcnt_counter_resume(PCNT_TEST_UNIT);

delay(500);
}


//initialize counter for flow0
void init_counter() {

pcnt_config_t pcnt_config = {
pulsePin, // Pulse input gpio_num, if you want to use gpio16, pulse_gpio_num = 16, a negative value will be ignored
PCNT_PIN_NOT_USED, // Control signal input gpio_num, a negative value will be ignored
PCNT_MODE_KEEP, // PCNT low control mode
PCNT_MODE_KEEP, // PCNT high control mode
PCNT_COUNT_INC, // PCNT positive edge count mode
PCNT_COUNT_DIS, // PCNT negative edge count mode
PCNT_H_LIM_VAL, // Maximum counter value
PCNT_L_LIM_VAL, // Minimum counter value
PCNT_TEST_UNIT, // PCNT unit number
PCNT_CHANNEL_0, // the PCNT channel
};

if(pcnt_unit_config(&pcnt_config) == ESP_OK) //init unit
Serial.println("Config Unit_0 = ESP_OK");

pcnt_filter_enable(PCNT_TEST_UNIT);
pcnt_set_filter_value(PCNT_TEST_UNIT,500);

pcnt_intr_disable(PCNT_TEST_UNIT);
pcnt_event_disable(PCNT_TEST_UNIT, PCNT_EVT_L_LIM);
pcnt_event_disable(PCNT_TEST_UNIT, PCNT_EVT_H_LIM);
pcnt_event_disable(PCNT_TEST_UNIT, PCNT_EVT_THRES_0);
pcnt_event_disable(PCNT_TEST_UNIT, PCNT_EVT_THRES_1);
pcnt_event_disable(PCNT_TEST_UNIT, PCNT_EVT_ZERO);

pcnt_counter_pause(PCNT_TEST_UNIT);

pcnt_counter_clear(PCNT_TEST_UNIT);

pcnt_intr_enable(PCNT_TEST_UNIT);

pcnt_counter_resume(PCNT_TEST_UNIT);

}

void readPcntCounter_0() {
if(pcnt_get_counter_value(PCNT_TEST_UNIT, &flowCounter) == ESP_OK)
flowcountint = (int) flowCounter;
Serial.println("Count value");
delay(10);
Serial.print("flowCounter = ");
Serial.println(flowCounter);
}

Who is online

Users browsing this forum: Bing [Bot] and 83 guests