RuiViana

Freq_Count_2.ino

Jul 7th, 2020
179
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // Consegui bom resultado deste aqui até 20MHz
  3. unsigned long overun = 20000;
  4. #include "driver/rmt.h"           // RMT library
  5. #include "driver/pcnt.h"          // Pulse count library
  6. #include <WiFi.h>
  7.  
  8. #define PCNT_TEST_UNIT      PCNT_UNIT_0                 // Unidade de pcnt
  9. #define PCNT_INPUT_SIG_IO   23                          // Pulse Input GPIO
  10. #define PCNT_INPUT_CTRL_IO  25                          // Control GPIO HIGH=count up, LOW=count down
  11. #define LEDC_IO  18                          // Control GPIO HIGH=count up, LOW=count down
  12.  
  13. #define PCNT_H_LIM_VAL      overun                       // Limite superior de contagem
  14.  
  15. bool flag = false;
  16. int16_t pulses = 0;
  17.  
  18. long  frequencia = 1;                 // frequencia em Hz
  19. long  canal = 1;                      // canal gerador de sinal
  20. long  resolBits = 16;                 // bits de resolucao
  21. long dutyCycle = 32770;               // valor divisor de Duty Cycle
  22.  
  23. unsigned long multPulses = 0;
  24.  
  25. //----------------------------------------------------------------------------
  26. void freqAdjust ()
  27. {
  28.   pinMode(LEDC_IO, OUTPUT);                          // GPIO_18 as Output
  29.   ledcAttachPin(LEDC_IO, canal);                     // GPIO_18 attached to PWM Channel
  30.   ledcSetup(canal, frequencia, resolBits);      // Channel  , freq  ,  bit resolution
  31.   ledcWrite(canal, dutyCycle);                  // Enable frequency with dutty cycle
  32. }
  33. //----------------------------------------------------------------------------------
  34. static void IRAM_ATTR pcnt_example_intr_handler(void *arg)
  35. {
  36.   uint32_t intr_status = PCNT.int_st.val;
  37.   int i;
  38.   for (i = 0; i < PCNT_UNIT_MAX; i++)
  39.   {
  40.     if (intr_status & (BIT(i)))
  41.     {
  42.       multPulses++;
  43.       PCNT.int_clr.val = BIT(i);
  44.     }
  45.   }
  46. }
  47. //----------------------------------------------------------------------------------
  48. void IRAM_ATTR detectsCount()
  49. {
  50.   pulses = REG_READ(PCNT_U0_CNT_REG);
  51.   pcnt_counter_clear(PCNT_TEST_UNIT);                     // Zera o contador PCNT
  52.   flag = false;
  53. }
  54. //----------------------------------------------------------------------------------
  55. void setup()
  56. {
  57.   WiFi.mode( WIFI_MODE_NULL );
  58.   btStop();
  59.   Serial.begin(115200);                                   // Inicializa a serial
  60.   pcnt_init();                                            // Inicializa o pulse count
  61.   attachInterrupt(digitalPinToInterrupt(25), detectsCount, FALLING);
  62.   freqAdjust();
  63. }
  64. //---------------------------------------------------------------------------------
  65. void loop()
  66. {
  67.   if (flag == false)
  68.   {
  69.     float calculo = 0;
  70.     float resFinal = 0;
  71.     calculo = (pulses + (multPulses * overun))  ;
  72.     resFinal = 0.00000585 * calculo +   calculo;
  73.     Serial.print("  Total: ");  Serial.println ((resFinal), 0 );     // Print
  74.     multPulses = 0;
  75.     flag = true;
  76.   }
  77. }
  78. //----------------------------------------------------------------------------------
  79. void pcnt_init(void)                                      // Rotina de inicializacao do pulse count
  80. {
  81.   pcnt_config_t pcnt_config = { };                        // Instancia pulse config
  82.   pcnt_config.pulse_gpio_num = PCNT_INPUT_SIG_IO;         // Port de entrada dos pulsos
  83.   pcnt_config.ctrl_gpio_num = PCNT_INPUT_CTRL_IO;         // Controle da contagem
  84.   pcnt_config.counter_h_lim = PCNT_H_LIM_VAL;             // Limite maximo de contagem
  85.   pcnt_config.pos_mode = PCNT_COUNT_INC;                  // Conta na subida do pulso
  86.   pcnt_config.neg_mode = PCNT_COUNT_INC;                  // Conta na descida do pulso
  87.   pcnt_config.lctrl_mode = PCNT_MODE_DISABLE;             // Nao usado
  88.   pcnt_config.hctrl_mode = PCNT_MODE_KEEP;                // Se HIGH conta incrementando
  89.   pcnt_unit_config(&pcnt_config);                         // Inicializa PCNT
  90.  
  91.   pcnt_counter_pause(PCNT_TEST_UNIT);                     // Inicializa o contador PCNT
  92.   pcnt_counter_clear(PCNT_TEST_UNIT);                     // Zera o contador PCNT
  93.   pcnt_counter_resume(PCNT_TEST_UNIT);                    // inicia a contagem
  94.  
  95.   pcnt_event_enable(PCNT_TEST_UNIT, PCNT_EVT_H_LIM);
  96.   pcnt_isr_register(pcnt_example_intr_handler, NULL, 0, NULL);
  97.   pcnt_intr_enable(PCNT_TEST_UNIT);
  98. }
RAW Paste Data