Hello:
I'm trying to learn the use of ADC and DAC in my ESP-WROOM-32 board. First I tried generating signals using a DAC and a timer, and it worked fine.
However, I'm stuck with my second task, which is to read a signal from the ADC and then outputting it to a DAC. This does not work, and the board keeps resetting itself. I can't tell what I'm doing wrong, SO here's my current code, maybe you can guide me:
#define INT_LED 2
#define DAC1 25
#define DAC2 26
#define ADC1_0 36
#define ADC1_3 39
#define PWM 32
#define PULSE 13
// Configurar propiedades del PWM
const int freq = 500;
const int ledChannel = 0;
const int resolution = 8;
int led_ctr = 0;
int i = 0, j = 0;
long d = 50;
int bit_ctr = 0;
int analog;
boolean flag_timer0 = false;
boolean flag_timer1 = false;
// Configurar interrupción Timer0
hw_timer_t *Timer0_Cfg = NULL;
hw_timer_t *Timer1_Cfg = NULL;
void IRAM_ATTR Timer0_ISR()
{
// Prender y apagar LED interno
led_ctr++;
if(led_ctr==24000)
{
digitalWrite(INT_LED, !digitalRead(INT_LED));
led_ctr = 0;
}
flag_timer0 = true;
digitalWrite(PULSE, !digitalRead(PULSE));
analog = analogRead(ADC1_3);
dacWrite(DAC1, int(analog/16));
}
void setup()
{
// Configurar la dirección de los GPIO
pinMode(INT_LED,OUTPUT);
pinMode(DAC1,OUTPUT);
pinMode(PWM,OUTPUT);
pinMode(PULSE,OUTPUT);
pinMode(ADC1_0,INPUT);
pinMode(ADC1_3,INPUT);
adcAttachPin(ADC1_3);
// Configuración interrupción Timer0 para 48 kHz
Timer0_Cfg = timerBegin(0, 80, true);
timerAttachInterrupt(Timer0_Cfg, &Timer0_ISR, true);
timerAlarmWrite(Timer0_Cfg, 21, true);
timerAlarmEnable(Timer0_Cfg);
}
void loop()
{
}
i expected the internal led to blink, and the DAC to output whatever is coming in from the ADC, but it lasts for about 2 seconds, then the board resets itself.
I have several doubts, especially regarding how long does the ADC take for a conversion. My timer is set at 48 kHz, so maybe the interrupt is to fast and it doesn't last enough to complete the reading?
This is what i have so far. If anyone can give me a hand, I'll be very grateful.
Best regards,
Fenhasan