Advertisement
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 flag2 = false;
- 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 = 32767; // valor divisor de Duty Cycle
- unsigned long multPulses = 0;
- unsigned long teste = 0;
- unsigned long teste2 = 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++;
- REG_WRITE(0x3FF5708C, 0X000000FF);
- //PCNT.int_clr.val = BIT(0);
- //teste2 = REG_READ(0x3FF57080);
- ///teste2 = PCNT.int_st.val;
- // }
- // }
- }
- //----------------------------------------------------------------------------------
- void IRAM_ATTR detectsCount()
- {
- pulses = REG_READ(PCNT_U0_CNT_REG);
- pcnt_counter_clear(PCNT_TEST_UNIT); // Zera o contador PCNT
- flag = false;
- }
- //----------------------------------------------------------------------------------
- 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);
- }
- //----------------------------------------------------------------------------------
- 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;
- Serial.print(" pulses: "); Serial.print (pulses); // Print
- Serial.print(" multPulses: "); Serial.print (multPulses ); // Print
- calculo = (pulses + (multPulses * overun)) ;
- resFinal = 0.000006 * calculo + calculo;
- Serial.print(" Total: "); Serial.println ((resFinal), 0 ); // Print
- multPulses = 0;
- flag = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement