Conveting code from the old legacy MCPWM driver to the new one.

joadske
Posts: 1
Joined: Sun Feb 18, 2024 10:21 am

Conveting code from the old legacy MCPWM driver to the new one.

Postby joadske » Sun Feb 18, 2024 11:26 am

Hi, I need help conveting this code from the old legacy MCPWM driver to the new one.
I've read the documentation and looked at the examples that exist, but I haven't been able to get it to work.

What I want is to read the pulse length equal, mcpwm_capture_signal_get_value

Code: Select all

// Frequency Meter
#include <Arduino.h>
#include "driver/mcpwm.h"

#define GPIO_CAP0_IN   34          //Set GPIO 34 as CAP0

float calF = 1.00002047;            // Cal value.
float lastHZ = 50.0000;
float oldHZ = 50.0000;
uint32_t cap_count= 0;              // cycles between cap interrupts
uint32_t last_count= 0;             // last cap_count value
float_t cap_calc= 0;                // calculated frequency
int MainPeriod = 90; 
const size_t numReadings = 25;      // - 4  Adjust this based on your requirements/speed
uint32_t total = 0;                 
uint32_t readings[numReadings];
uint8_t currentIndex = 0;
unsigned long previousMillis = 0;
uint8_t cap_edge = 0;

bool IRAM_ATTR cap_cb(mcpwm_unit_t mcpwm, mcpwm_capture_channel_id_t cap_channel, const cap_event_data_t *edata, void *user_data) {
  
  cap_count = mcpwm_capture_signal_get_value(MCPWM_UNIT_0, MCPWM_SELECT_CAP0);
  cap_edge = mcpwm_capture_signal_get_edge(MCPWM_UNIT_0, MCPWM_SELECT_CAP0); //Capture signal edge: 1 - positive edge, 2 - negtive edge

  if (cap_edge = MCPWM_POS_EDGE){ 
    total = total - readings[currentIndex] + (cap_count - last_count);
    readings[currentIndex] = (cap_count - last_count);
    currentIndex = (currentIndex + 1) % numReadings;
  }
  last_count = cap_count;
  return 0;
}


void setup(){
  delay(100);
  Serial.begin(115200);                                     //Start Serial
  Serial.println("starting...");
  
  //set the cap input pin
  mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM_CAP_0, GPIO_CAP0_IN);

  // Setup Capture channel configuration
  mcpwm_capture_config_t MCPWM_cap_config = {     // Capture channel configuration
  .cap_edge = MCPWM_POS_EDGE,                     // MCPWM_BOTH_EDGES  MCPWM_NEG_EDGE  MCPWM_POS_EDGE
  .cap_prescale = 1,                              // Prescale or 2
  .capture_cb = cap_cb,                           // callback
  .user_data = nullptr,                           // User defined ISR callback function args
  };
  
  //Enable pull down on CAP0 signal input
  gpio_pulldown_t((gpio_num_t)GPIO_CAP0_IN);                
  //enable and config capture channel. Inside an ESP_ERROR_CHECK() to avoid trouble
  ESP_ERROR_CHECK(mcpwm_capture_enable_channel(MCPWM_UNIT_0, MCPWM_SELECT_CAP0, &MCPWM_cap_config)); 

  Serial.print("Setup done, starting...");
  delay(500);
}

void loop(){
  if (millis() - previousMillis >= MainPeriod) {
    lastHZ = calF * (1/((float(total / numReadings))*(1 / 80.0e6))/2); //80Mhz  /2 when .cap_prescale = 1 else 2
    Serial.print(lastHZ,4);
    Serial.println(" Hz");
    previousMillis = millis();
  }
  yield();   // Do i need this? 
  delay(10); // Do i need this? 
} 

Who is online

Users browsing this forum: No registered users and 43 guests