Advertisement
pleasedontcode

Frequency Counter rev_01

Jul 8th, 2025
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Frequency Counter
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-07-08 18:15:54
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Measures Frequency of Square Wave Input */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /* START CODE */
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <FastInterruptEncoder.h>  // https://github.com/levkovigor/FastInterruptEncoder
  27. #include <driver/pcnt.h>           // ESP32 Pulse Counter driver
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32.  
  33. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  34. // GPIO receiving square wave
  35. #define PCNT_INPUT_IO      4
  36. // PCNT Unit
  37. #define PCNT_UNIT          PCNT_UNIT_0
  38. // Sampling interval in milliseconds
  39. #define SAMPLE_INTERVAL_MS 1000
  40.  
  41. // Timing variable
  42. unsigned long lastSampleTime = 0;
  43.  
  44. /* USER CODE START */
  45. void setup() {
  46.   Serial.begin(115200);
  47.   delay(1000);
  48.   Serial.println("Non-blocking ESP32 PCNT Frequency Counter");
  49.  
  50.   // Correct ordered initialization for pcnt_config_t
  51.   pcnt_config_t pcnt_config = {
  52.     .pulse_gpio_num = PCNT_INPUT_IO,        // GPIO receiving square wave
  53.     .ctrl_gpio_num = PCNT_PIN_NOT_USED,     // No control GPIO
  54.     .channel = PCNT_CHANNEL_0,                // Channel 0
  55.     .unit = PCNT_UNIT,                        // PCNT Unit
  56.     .pos_mode = PCNT_COUNT_INC,               // Count on positive edge
  57.     .neg_mode = PCNT_COUNT_DIS,               // Do not count on negative edge
  58.     .lctrl_mode = PCNT_MODE_KEEP,             // Keep mode for low control
  59.     .hctrl_mode = PCNT_MODE_KEEP,             // Keep mode for high control
  60.     .counter_h_lim = 32767,                   // Max count
  61.     .counter_l_lim = 0                        // Min count
  62.   };
  63.  
  64.   // Initialize and configure PCNT
  65.   pcnt_unit_config(&pcnt_config);
  66.   // Set filter to ignore pulses shorter than ~1000 APB cycles
  67.   pcnt_set_filter_value(PCNT_UNIT, 1000);
  68.   pcnt_filter_enable(PCNT_UNIT);
  69.  
  70.   // Initialize counter
  71.   pcnt_counter_pause(PCNT_UNIT);
  72.   pcnt_counter_clear(PCNT_UNIT);
  73.   pcnt_counter_resume(PCNT_UNIT);
  74. }
  75.  
  76. void loop() {
  77.   unsigned long now = millis();
  78.  
  79.   if (now - lastSampleTime >= SAMPLE_INTERVAL_MS) {
  80.     lastSampleTime = now;
  81.  
  82.     int16_t count = 0;
  83.     // Read the current count
  84.     pcnt_get_counter_value(PCNT_UNIT, &count);
  85.     // Clear the counter for next measurement
  86.     pcnt_counter_clear(PCNT_UNIT);
  87.  
  88.     Serial.print("Frequency: ");
  89.     Serial.print(count);
  90.     Serial.println(" Hz");
  91.   }
  92.  
  93.   // Other non-blocking tasks can be added here
  94. }
  95. /* USER CODE END */
  96.  
  97. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement