// https://wokwi.com/projects/380733829667456001
// el modulo DAC no funciona en el simulador
#define PinADC 34
#define DACPin 25
int i = 0;
int valores_adc[19] = {0,109,230,454,804,954,1231,1471,1666,1933,2167,2397,2751,2973,3333,3685,3943,4030,4095};
float voleres_osci[19] = {0,0.134,0.233,0.407,0.682,0.809,1.02,1.21,1.36,1.57,1.75,1.95,2.22,2.4,2.66,2.88,3.01,3.05,3.09};
float pendiente = 0;
float voltage = 0;
int j = 0;
int valores_dac[12] = {0,25,50,75,100,125,150,175,20,225,250,255};
float tensiones_dac[12] ={0,0,0,0,0,0,0,0,0,0,0,0};
int dacVal;
int adcVAl;
float volt_dac;
void setup() {
//set the resolution to 12 bits (0-4096)
analogReadResolution(12);
// voltage full-range 150mV - 3100 mV
analogSetAttenuation(ADC_11db); // ADC_0db, ADC_2_5db, ADC_6db, ADC_11db
// DAC channel 1 is attached to GPIO25, DAC channel 2 is attached to GPIO26
// acepta valores de 0 a 255 -> Vout = 0 a 3.3v
Serial.begin(115200);
Serial.printf("Ejemplo de configuracion y uso ADC-DAC \n");
}
void loop()
{
for (j = 0; j<12;j++)
{
dacWrite(DACPin, valores_dac[j]);
int adcVal = analogRead(PinADC); // leemos el valor del ADC
//float voltage = (adcVal+97.539)/1316.9; // voltage corregido
i = 0;
//función calibración ADC en todos los puntos
if (adcVal == 0)
{
voltage = 0;
}
else if (adcVal == 4095)
{
voltage = 3.09;
}
else
{
while (adcVal>valores_adc[i])
{
i++;
}
pendiente = (valores_adc[i] - valores_adc[i-1])/(valores_osci[i] - valores_osci[i-1]);
voltage = pendiente*(adcVal-valores_adc[i-1]) + valores_osci[i-1];
}
tensiones_dac[j] = voltage;
}
for (i=0;i<12;i++)
{
Serial.printf("Tensiones:%1.3f\n",tensiones_dac[i]);
}
//dacWrite(DACPin, dacVal); // ilumina más o menos el LED
//Serial.printf("ADC Val: %d, DAC Val: %d, Voltage: %1.3f V \n", adcVal,dacVal, voltage);
delay(1000);
}