RuiViana

Freq_pnct_V17.ino

Jul 7th, 2020
1,561
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 flag2 = false;
  16. bool flag = false;
  17. int16_t pulses = 0;
  18.  
  19. long  frequencia = 1;                 // frequencia em Hz
  20. long  canal = 1;                      // canal gerador de sinal
  21. long  resolBits = 16;                 // bits de resolucao
  22. long dutyCycle = 32767;               // valor divisor de Duty Cycle
  23. unsigned long multPulses = 0;
  24. unsigned long teste = 0;
  25. unsigned long teste2 = 0;
  26. //----------------------------------------------------------------------------
  27. void freqAdjust ()
  28. {
  29.   pinMode(LEDC_IO, OUTPUT);                          // GPIO_18 as Output
  30.   ledcAttachPin(LEDC_IO, canal);                     // GPIO_18 attached to PWM Channel
  31.   ledcSetup(canal, frequencia, resolBits);      // Channel  , freq  ,  bit resolution
  32.   ledcWrite(canal, dutyCycle);                  // Enable frequency with dutty cycle
  33. }
  34. //----------------------------------------------------------------------------------
  35. static void IRAM_ATTR pcnt_example_intr_handler(void *arg)
  36. {
  37.   //  uint32_t intr_status = PCNT.int_st.val;
  38.   //  int i;
  39.   //  for (i = 0; i < PCNT_UNIT_MAX; i++)
  40.   //  {
  41.   //    if (intr_status & (BIT(i)))
  42.   //    {
  43.   multPulses++;
  44.   REG_WRITE(0x3FF5708C, 0X000000FF);
  45.   //PCNT.int_clr.val = BIT(0);
  46.   //teste2 = REG_READ(0x3FF57080);
  47.   ///teste2 = PCNT.int_st.val;
  48.   //    }
  49.   //  }
  50. }
  51. //----------------------------------------------------------------------------------
  52. void IRAM_ATTR detectsCount()
  53. {
  54.   pulses = REG_READ(PCNT_U0_CNT_REG);
  55.   pcnt_counter_clear(PCNT_TEST_UNIT);                     // Zera o contador PCNT
  56.   flag = false;
  57. }
  58. //----------------------------------------------------------------------------------
  59. void pcnt_init(void)                                      // Rotina de inicializacao do pulse count
  60. {
  61.   pcnt_config_t pcnt_config = { };                        // Instancia pulse config
  62.   pcnt_config.pulse_gpio_num = PCNT_INPUT_SIG_IO;         // Port de entrada dos pulsos
  63.   pcnt_config.ctrl_gpio_num = PCNT_INPUT_CTRL_IO;         // Controle da contagem
  64.   pcnt_config.counter_h_lim = PCNT_H_LIM_VAL;             // Limite maximo de contagem
  65.   pcnt_config.pos_mode = PCNT_COUNT_INC;                  // Conta na subida do pulso
  66.   pcnt_config.neg_mode = PCNT_COUNT_INC;                  // Conta na descida do pulso
  67.   pcnt_config.lctrl_mode = PCNT_MODE_DISABLE;             // Nao usado
  68.   pcnt_config.hctrl_mode = PCNT_MODE_KEEP;                // Se HIGH conta incrementando
  69.   pcnt_unit_config(&pcnt_config);                         // Inicializa PCNT
  70.  
  71.   pcnt_counter_pause(PCNT_TEST_UNIT);                     // Inicializa o contador PCNT
  72.   pcnt_counter_clear(PCNT_TEST_UNIT);                     // Zera o contador PCNT
  73.   pcnt_counter_resume(PCNT_TEST_UNIT);                    // inicia a contagem
  74.  
  75.   pcnt_event_enable(PCNT_TEST_UNIT, PCNT_EVT_H_LIM);
  76.   pcnt_isr_register(pcnt_example_intr_handler, NULL, 0, NULL);
  77.   pcnt_intr_enable(PCNT_TEST_UNIT);
  78. }
  79. //----------------------------------------------------------------------------------
  80. void setup()
  81. {
  82.   WiFi.mode( WIFI_MODE_NULL );
  83.   btStop();
  84.   Serial.begin(115200);                                   // Inicializa a serial
  85.   pcnt_init();                                            // Inicializa o pulse count
  86.   attachInterrupt(digitalPinToInterrupt(25), detectsCount, FALLING);
  87.   freqAdjust();
  88. }
  89. //---------------------------------------------------------------------------------
  90. void loop()
  91. {
  92.   if (flag == false)
  93.   {
  94.     float calculo = 0;
  95.     float resFinal = 0;
  96.     Serial.print("  pulses: ");  Serial.print (pulses);      // Print
  97.     Serial.print("  multPulses: ");  Serial.print (multPulses );      // Print
  98.     calculo = (pulses + (multPulses * overun))  ;
  99.     resFinal = 0.000006 * calculo +   calculo;
  100.     Serial.print("  Total: ");  Serial.println ((resFinal), 0 );     // Print
  101.     multPulses = 0;
  102.     flag = true;
  103.   }
  104. }
RAW Paste Data