Gustavomurta / ESP32_frequencyMeter

ESP32 - frequencimetro
4 stars 5 forks source link

reading counter value in interrupt return always zero #1

Open mkeyno opened 2 years ago

mkeyno commented 2 years ago

Hi, I've been trying to read the frequency with PCNT lib using your code sample, I've tried to make a class for each channel, I use the signal generator on ESP32 pin, and enable Maximum counter value event and register an interrupt, however, when an interrupt happens and try to read the counter number, it always returns zero whereas when I trying to read the counter number in the loop, it returns the number any tip really appreciated

#include "driver/pcnt.h"

int inputPin = 14;
int timerMs = 300;
#define UNIT       PCNT_UNIT_0                                 // Unidade de pcnt
#define CHANNEL    PCNT_CHANNEL_0   
#define period    50  

static pcnt_isr_handle_t isr_handle = nullptr;

volatile int16_t pulseTime_0;

portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED; 

//static
void IRAM_ATTR Intr_handler(void *arg){
portENTER_CRITICAL_ISR(&timerMux);  
int16_t p; 
int unit = (int)arg;
pcnt_get_counter_value((pcnt_unit_t)unit, &p);
//pcnt_counter_clear((pcnt_unit_t)unit);   
pulseTime_0=p;
portEXIT_CRITICAL_ISR(&timerMux); 
}

void pcnt_init(void)                                                      // Rotina de inicializacao do pulse count
{
  pcnt_config_t config = { };                                        // Instancia pulse config
  config.pulse_gpio_num = inputPin;                       // Port de entrada dos pulsos
  config.ctrl_gpio_num = -1;                       // Controle da contagem
  config.unit = UNIT;                                     // Unidade de contagem
  config.channel = CHANNEL;                               // Canal de contagem
  config.counter_h_lim = 1000;                             // Limite maximo de contagem
  config.pos_mode = PCNT_COUNT_INC;                                  // Conta na subida do pulso
  config.neg_mode = PCNT_COUNT_DIS;                                  // Conta na descida do pulso
  config.lctrl_mode = PCNT_MODE_KEEP;                             // Nao usado
  config.hctrl_mode = PCNT_MODE_KEEP;                                // Se HIGH conta incrementando

  pcnt_unit_config(&config);                                         // Inicializa PCNT

  pcnt_counter_pause(UNIT);                                    // Inicializa o contador PCNT
  pcnt_counter_clear(UNIT);                                    // Zera o contador PCNT

  pcnt_event_enable(UNIT, PCNT_EVT_H_LIM); 

  pcnt_isr_service_install(0);
  pcnt_isr_handler_add(UNIT, Intr_handler, (void *)UNIT);
  //pcnt_isr_register(Intr_handler, NULL, 0, &isr_handle);                    // Rotina de Interrupt de pcnt
  pcnt_intr_enable(UNIT);                                      // Habilita interrup de pcnt

  pcnt_counter_resume(UNIT);                                   // inicia a contagem
}

unsigned long last_measurment;

static uint8_t i=0;

void  measure(void){ if(millis()-last_measurment>period){
int16_t PulseCounter=0;
pcnt_get_counter_value(UNIT,&PulseCounter);
pcnt_counter_clear(UNIT);
pcnt_counter_resume(UNIT);
Serial.println( PulseCounter ); 

last_measurment = millis();
}}

void setup(){
  Serial.begin(115200);
  Serial.println("frequency measurment ");
  pcnt_init()  ;
}

void loop()
{
///measure();

 if(millis()-last_measurment>period){

Serial.println(pulseTime_0);
last_measurment = millis();
}

}
Gustavomurta commented 2 years ago

Hi Mkeyno, Look for these links with detailed instructions.

Best Frequency Meter ever made with ESP32 - awesome! https://www.esp32.com/viewtopic.php?f=19&t=17018

ESP32 – Frequencímetro de precisão https://blog.eletrogate.com/esp32-frequencimetro-de-precisao/