|
| 1 | +#include "Arduino.h" |
| 2 | +#include "driver/mcpwm.h" |
| 3 | + |
| 4 | + |
| 5 | +#include "Arduino.h" |
| 6 | +#include "driver/mcpwm.h" |
| 7 | +#include "driver/gpio.h" |
| 8 | + |
| 9 | +#define GPIO_CAP0_IN GPIO_NUM_26 |
| 10 | +#define GPIO_CAP1_IN GPIO_NUM_27 |
| 11 | +#define GPIO_TRIGGER GPIO_NUM_14 // New GPIO pin for triggering the counter |
| 12 | + |
| 13 | + |
| 14 | +struct CaptureData { |
| 15 | + uint32_t time_rising_edge; |
| 16 | + uint32_t time_falling_edge; |
| 17 | + uint32_t period; |
| 18 | + bool last_was_rising; |
| 19 | +}; |
| 20 | + |
| 21 | +CaptureData cap_data[2]; |
| 22 | +volatile float latest_duty_cycle[2] = {0.0f, 0.0f}; // Updated by the MCPWM capture ISR |
| 23 | +volatile int counter = 0; |
| 24 | + |
| 25 | +void IRAM_ATTR capture_handler(void *arg) { |
| 26 | + uint32_t cap_unit = (uint32_t)arg; |
| 27 | + uint32_t curr_time = mcpwm_capture_signal_get_value(MCPWM_UNIT_0, (mcpwm_capture_signal_t)cap_unit); |
| 28 | + |
| 29 | + if (mcpwm_capture_signal_get_edge(MCPWM_UNIT_0, (mcpwm_capture_signal_t)cap_unit) == MCPWM_POS_EDGE) { |
| 30 | + if (cap_data[cap_unit].last_was_rising) { |
| 31 | + cap_data[cap_unit].period = curr_time - cap_data[cap_unit].time_rising_edge; |
| 32 | + } |
| 33 | + cap_data[cap_unit].time_rising_edge = curr_time; |
| 34 | + cap_data[cap_unit].last_was_rising = true; |
| 35 | + } else { |
| 36 | + cap_data[cap_unit].time_falling_edge = curr_time; |
| 37 | + cap_data[cap_unit].last_was_rising = false; |
| 38 | + } |
| 39 | + |
| 40 | + if (cap_data[cap_unit].period != 0) { |
| 41 | + uint32_t high_time = cap_data[cap_unit].time_falling_edge - cap_data[cap_unit].time_rising_edge; |
| 42 | + latest_duty_cycle[cap_unit] = ((float)high_time / (float)cap_data[cap_unit].period) * 100.0f; |
| 43 | + Serial.printf("CAP%d Duty cycle: %0.2f%%\n", cap_unit, latest_duty_cycle[cap_unit]); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +void IRAM_ATTR trigger_handler(void* arg) { |
| 48 | + for (int i = 0; i < 2; i++) { |
| 49 | + if (latest_duty_cycle[i] > 50.0f) { |
| 50 | + counter++; |
| 51 | + } else { |
| 52 | + counter--; |
| 53 | + } |
| 54 | + } |
| 55 | + Serial.printf("Counter (triggered): %d\n", counter); |
| 56 | +} |
| 57 | + |
| 58 | +void setup() { |
| 59 | + Serial.begin(115200); |
| 60 | + Serial.println("Testing MCPWM capture on two pins..."); |
| 61 | + |
| 62 | + // Initialize MCPWM capture |
| 63 | + mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM_CAP_0, GPIO_CAP0_IN); |
| 64 | + mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM_CAP_1, GPIO_CAP1_IN); |
| 65 | + |
| 66 | + mcpwm_capture_enable(MCPWM_UNIT_0, MCPWM_SELECT_CAP0, MCPWM_BOTH_EDGE, 0); |
| 67 | + mcpwm_capture_enable(MCPWM_UNIT_0, MCPWM_SELECT_CAP1, MCPWM_BOTH_EDGE, 0); |
| 68 | + |
| 69 | + intr_handle_t cap0_intr_handle = NULL; |
| 70 | + intr_handle_t cap1_intr_handle = NULL; |
| 71 | + |
| 72 | + mcpwm_isr_register(MCPWM_UNIT_0, capture_handler, (void*)MCPWM_SELECT_CAP0, ESP_INTR_FLAG_IRAM, &cap0_intr_handle); |
| 73 | + mcpwm_isr_register(MCPWM_UNIT_0, capture_handler, (void*)MCPWM_SELECT_CAP1, ESP_INTR_FLAG_IRAM, &cap1_intr_handle); |
| 74 | + |
| 75 | + // Initialize the GPIO_TRIGGER pin for external interrupt |
| 76 | + gpio_config_t io_conf; |
| 77 | + io_conf.intr_type = GPIO_INTR_POSEDGE; // Trigger on rising edge |
| 78 | + io_conf.mode = GPIO_MODE_INPUT; |
| 79 | + io_conf.pin_bit_mask = (1ULL << GPIO_TRIGGER); |
| 80 | + io_conf.pull_down_en = GPIO_PULLDOWN_ENABLE; |
| 81 | + io_conf.pull_up_en = GPIO_PULLUP_DISABLE; |
| 82 | + gpio_config(&io_conf); |
| 83 | + |
| 84 | + // Register ISR for the trigger pin |
| 85 | + gpio_install_isr_service(ESP_INTR_FLAG_IRAM); |
| 86 | + gpio_isr_handler_add(GPIO_TRIGGER, trigger_handler, NULL); |
| 87 | + |
| 88 | + // Clear capture data |
| 89 | + memset(cap_data, 0, sizeof(cap_data)); |
| 90 | +} |
| 91 | + |
| 92 | +void loop() { |
| 93 | + delay(1000); |
| 94 | +} |
0 commit comments