Code: Select all
//#include <driver/dma.h>
#include <driver/gpio.h>
#include <driver/adc.h>
#include <esp_adc_cal.h>
#include <driver/mcpwm.h>
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
// PWM
#define PWM1_Ch 0
#define PWM1_Res 10 // 10 bits = 1024 levels = 0,05859375A per step
#define PWM1_Freq 1000 // 980 - 1020 range, 1000 nominal
int PWM1_DutyCycle = 1000; // 0= -12V 1000= 12V
// ADC-Kanal für die Verwendung definieren
#define ADC1_GPIO35_CHANNEL ADC1_CHANNEL_7
#define ADC_CHANNEL ADC1_CHANNEL_7
#define ADC_WIDTH ADC_WIDTH_BIT_12
#define ADC_ATTEN ADC_ATTEN_DB_11
#define ADC_VREF 1100
esp_adc_cal_characteristics_t adc_chars;
void IRAM_ATTR pwmHigh(void*)
{
portENTER_CRITICAL_ISR(&timerMux);
// ADC-Messwert abfragen
raw_value = adc1_get_raw((adc1_channel_t)ADC_CHANNEL);
// Berechnen des Spannungswerts aus dem ADC-Messwert
voltage = esp_adc_cal_raw_to_voltage(raw_value, &adc_chars);
Serial.printf("\r\nInterrupt:", voltage);
portEXIT_CRITICAL_ISR(&timerMux);
}
void TaskCP(void*) {
VOID SETUP() { ////////////////////////////////////////// SETUP //////////////////////////////////////////
adc1_config_width(ADC_WIDTH_BIT_12);
adc1_config_channel_atten(ADC1_GPIO35_CHANNEL, ADC_ATTEN_DB_11);
esp_adc_cal_value_t val_type = esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN, ADC_WIDTH, ADC_VREF, &adc_chars);
// Hier kann man die Pin-Belegung für PWM und ADC festlegen
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0A, CTRL_PILOT);
// Hier wird der PWM-Signalgenerator konfiguriert
mcpwm_config_t pwm_config;
pwm_config.frequency = 1000; // 1kHz
pwm_config.cmpr_a = 50; // Duty-Cycle von 50%
pwm_config.counter_mode = MCPWM_UP_COUNTER;
pwm_config.duty_mode = MCPWM_DUTY_MODE_0;
mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_0, &pwm_config);
mcpwm_start(MCPWM_UNIT_0, MCPWM_TIMER_0);
// Hier wird der Interrupt-Handler für das PWM-Signal registriert
mcpwm_isr_register(MCPWM_UNIT_0, pwmHigh, NULL, ESP_INTR_FLAG_IRAM, NULL);
}
VOID LOOP() { ////////////////////////////////////////// LOOP //////////////////////////////////////////
adcHigh = voltage;
DELAY(1); //time for another task run.
}
}
Please support!
THX