#include "stdio.h" // Library STDIO
#include "driver/ledc.h" // Library ESP32 LEDC
#include "driver/pcnt.h" // Library ESP32 PCNT
#include "soc/pcnt_struct.h"
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32-C3!");
uint32_t f = getCpuFrequencyMhz();
Serial.println(f);
init_osc_freq();
}
void loop() {
}
void init_osc_freq () // Initialize Oscillator to test Freq Meter
{
ledc_timer_config_t ledc_timer = {}; // LEDC timer config instance
ledc_timer.duty_resolution = ledc_timer_bit_t(1); // Set resolution
ledc_timer.freq_hz = 40000000; // Set Oscillator frequency
ledc_timer.speed_mode = LEDC_LOW_SPEED_MODE; // Set low speed mode ESP32C3
ledc_timer.timer_num = LEDC_TIMER_0; // Set LEDC timer index - 0
ledc_timer_config(&ledc_timer); // Set LEDC Timer config
ledc_channel_config_t ledc_channel = {}; // LEDC Channel config instance
ledc_channel.channel = LEDC_CHANNEL_0; // Set HS Channel - 0
ledc_channel.duty = 1; // Set Duty Cycle 50%
ledc_channel.gpio_num = 2; // LEDC Oscillator output GPIO 33
ledc_channel.intr_type = LEDC_INTR_DISABLE; // LEDC Fade interrupt disable
ledc_channel.speed_mode = LEDC_LOW_SPEED_MODE; // Set LEDC high speed mode
ledc_channel.timer_sel = LEDC_TIMER_0; // Set timer source of channel - 0
ledc_channel_config(&ledc_channel); // Config LEDC channel
}
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
uint32_t multPulses = 0;
//----------------------------------------------------------------------------------
static void IRAM_ATTR pcnt_intrrupt_handler(void *arg) // Counting overflow pulses
{
portENTER_CRITICAL_ISR(&timerMux); // disabling the interrupts
multPulses++; // increment Overflow counter
PCNT.int_clr.val = BIT(PCNT_UNIT_0); // Clear Pulse Counter interrupt bit
portEXIT_CRITICAL_ISR(&timerMux); // enabling the interrupts
}
//----------------------------------------------------------------------------------
void init_PCNT(void) // Initialize and run PCNT unit
{
pcnt_config_t pcnt_config = { }; // PCNT unit instance
pcnt_config.pulse_gpio_num = 3; // Pulse input GPIO 3 - Freq Meter Input
pcnt_config.ctrl_gpio_num = 0; // Control signal input GPIO 3
pcnt_config.unit = PCNT_UNIT_0; // Unidade de contagem PCNT - 0
pcnt_config.channel = PCNT_CHANNEL_0; // PCNT unit number - 0
pcnt_config.counter_h_lim = 20000; // Maximum counter value - 20000
pcnt_config.pos_mode = PCNT_COUNT_INC; // PCNT positive edge count mode - inc
pcnt_config.neg_mode = PCNT_COUNT_INC; // PCNT negative edge count mode - inc
pcnt_config.lctrl_mode = PCNT_MODE_DISABLE; // PCNT low control mode - disable
pcnt_config.hctrl_mode = PCNT_MODE_KEEP; // PCNT high control mode - won't change counter mode
pcnt_unit_config(&pcnt_config); // Initialize PCNT unit
pcnt_counter_pause(PCNT_UNIT_0); // Pause PCNT unit
pcnt_counter_clear(PCNT_UNIT_0); // Clear PCNT unit
pcnt_event_enable(PCNT_UNIT_0, PCNT_EVT_H_LIM); // Enable event to watch - max count
pcnt_intrrupt_handler(pcnt_intr_handler, NULL, 0, NULL); // Setup Register ISR handler
pcnt_intr_enable(PCNT_UNIT_0); // Enable interrupts for PCNT unit
pcnt_counter_resume(PCNT_UNIT_0); // Resume PCNT unit - starts count
}