Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Consegui bom resultado deste aqui até 20MHz
- unsigned long overun = 20000;
- #include "driver/rmt.h" // RMT library
- #include "driver/pcnt.h" // Pulse count library
- #include <WiFi.h>
- #define PCNT_TEST_UNIT PCNT_UNIT_0 // Unidade de pcnt
- #define PCNT_INPUT_SIG_IO 23 // Pulse Input GPIO
- #define PCNT_INPUT_CTRL_IO 25 // Control GPIO HIGH=count up, LOW=count down
- #define LEDC_IO 18 // Control GPIO HIGH=count up, LOW=count down
- #define PCNT_H_LIM_VAL overun // Limite superior de contagem
- bool flag = false;
- int16_t pulses = 0;
- long frequencia = 1; // frequencia em Hz
- long canal = 1; // canal gerador de sinal
- long resolBits = 16; // bits de resolucao
- long dutyCycle = 32770; // valor divisor de Duty Cycle
- unsigned long multPulses = 0;
- //----------------------------------------------------------------------------
- void freqAdjust ()
- {
- pinMode(LEDC_IO, OUTPUT); // GPIO_18 as Output
- ledcAttachPin(LEDC_IO, canal); // GPIO_18 attached to PWM Channel
- ledcSetup(canal, frequencia, resolBits); // Channel , freq , bit resolution
- ledcWrite(canal, dutyCycle); // Enable frequency with dutty cycle
- }
- //----------------------------------------------------------------------------------
- static void IRAM_ATTR pcnt_example_intr_handler(void *arg)
- {
- uint32_t intr_status = PCNT.int_st.val;
- int i;
- for (i = 0; i < PCNT_UNIT_MAX; i++)
- {
- if (intr_status & (BIT(i)))
- {
- multPulses++;
- PCNT.int_clr.val = BIT(i);
- }
- }
- }
- //----------------------------------------------------------------------------------
- void IRAM_ATTR detectsCount()
- {
- pulses = REG_READ(PCNT_U0_CNT_REG);
- pcnt_counter_clear(PCNT_TEST_UNIT); // Zera o contador PCNT
- flag = false;
- }
- //----------------------------------------------------------------------------------
- void setup()
- {
- WiFi.mode( WIFI_MODE_NULL );
- btStop();
- Serial.begin(115200); // Inicializa a serial
- pcnt_init(); // Inicializa o pulse count
- attachInterrupt(digitalPinToInterrupt(25), detectsCount, FALLING);
- freqAdjust();
- }
- //---------------------------------------------------------------------------------
- void loop()
- {
- if (flag == false)
- {
- float calculo = 0;
- float resFinal = 0;
- calculo = (pulses + (multPulses * overun)) ;
- resFinal = 0.00000585 * calculo + calculo;
- Serial.print(" Total: "); Serial.println ((resFinal), 0 ); // Print
- multPulses = 0;
- flag = true;
- }
- }
- //----------------------------------------------------------------------------------
- void pcnt_init(void) // Rotina de inicializacao do pulse count
- {
- pcnt_config_t pcnt_config = { }; // Instancia pulse config
- pcnt_config.pulse_gpio_num = PCNT_INPUT_SIG_IO; // Port de entrada dos pulsos
- pcnt_config.ctrl_gpio_num = PCNT_INPUT_CTRL_IO; // Controle da contagem
- pcnt_config.counter_h_lim = PCNT_H_LIM_VAL; // Limite maximo de contagem
- pcnt_config.pos_mode = PCNT_COUNT_INC; // Conta na subida do pulso
- pcnt_config.neg_mode = PCNT_COUNT_INC; // Conta na descida do pulso
- pcnt_config.lctrl_mode = PCNT_MODE_DISABLE; // Nao usado
- pcnt_config.hctrl_mode = PCNT_MODE_KEEP; // Se HIGH conta incrementando
- pcnt_unit_config(&pcnt_config); // Inicializa PCNT
- pcnt_counter_pause(PCNT_TEST_UNIT); // Inicializa o contador PCNT
- pcnt_counter_clear(PCNT_TEST_UNIT); // Zera o contador PCNT
- pcnt_counter_resume(PCNT_TEST_UNIT); // inicia a contagem
- pcnt_event_enable(PCNT_TEST_UNIT, PCNT_EVT_H_LIM);
- pcnt_isr_register(pcnt_example_intr_handler, NULL, 0, NULL);
- pcnt_intr_enable(PCNT_TEST_UNIT);
- }
Add Comment
Please, Sign In to add comment