Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Frequency Counter
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2025-07-08 18:15:54
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Measures Frequency of Square Wave Input */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <FastInterruptEncoder.h> // https://github.com/levkovigor/FastInterruptEncoder
- #include <driver/pcnt.h> // ESP32 Pulse Counter driver
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
- // GPIO receiving square wave
- #define PCNT_INPUT_IO 4
- // PCNT Unit
- #define PCNT_UNIT PCNT_UNIT_0
- // Sampling interval in milliseconds
- #define SAMPLE_INTERVAL_MS 1000
- // Timing variable
- unsigned long lastSampleTime = 0;
- /* USER CODE START */
- void setup() {
- Serial.begin(115200);
- delay(1000);
- Serial.println("Non-blocking ESP32 PCNT Frequency Counter");
- // Correct ordered initialization for pcnt_config_t
- pcnt_config_t pcnt_config = {
- .pulse_gpio_num = PCNT_INPUT_IO, // GPIO receiving square wave
- .ctrl_gpio_num = PCNT_PIN_NOT_USED, // No control GPIO
- .channel = PCNT_CHANNEL_0, // Channel 0
- .unit = PCNT_UNIT, // PCNT Unit
- .pos_mode = PCNT_COUNT_INC, // Count on positive edge
- .neg_mode = PCNT_COUNT_DIS, // Do not count on negative edge
- .lctrl_mode = PCNT_MODE_KEEP, // Keep mode for low control
- .hctrl_mode = PCNT_MODE_KEEP, // Keep mode for high control
- .counter_h_lim = 32767, // Max count
- .counter_l_lim = 0 // Min count
- };
- // Initialize and configure PCNT
- pcnt_unit_config(&pcnt_config);
- // Set filter to ignore pulses shorter than ~1000 APB cycles
- pcnt_set_filter_value(PCNT_UNIT, 1000);
- pcnt_filter_enable(PCNT_UNIT);
- // Initialize counter
- pcnt_counter_pause(PCNT_UNIT);
- pcnt_counter_clear(PCNT_UNIT);
- pcnt_counter_resume(PCNT_UNIT);
- }
- void loop() {
- unsigned long now = millis();
- if (now - lastSampleTime >= SAMPLE_INTERVAL_MS) {
- lastSampleTime = now;
- int16_t count = 0;
- // Read the current count
- pcnt_get_counter_value(PCNT_UNIT, &count);
- // Clear the counter for next measurement
- pcnt_counter_clear(PCNT_UNIT);
- Serial.print("Frequency: ");
- Serial.print(count);
- Serial.println(" Hz");
- }
- // Other non-blocking tasks can be added here
- }
- /* USER CODE END */
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement