ashkanrezaee

ADC problem

Discussion created by ashkanrezaee on Jul 1, 2020
Latest reply on Jul 8, 2020 by Estephania Martinez

I want to take samples from 3 ADC channels from QN9080DK board, but it seems that somehow their result are related to each other. I use the following codes and each time I recall these functions: ADC_CH_4, ADC_CH_5, ADC_CH_9

 

*************

while(true)
{
delay (70000);
ADC_CH_4();
ADC_CH_5();
ADC_CH_9();
PRINTF("%f;%f;%f\r\n", fresult4, fresult5,fresult9);
}

 

*************

 

static void ADC_CH_4(void)
{
//Activate ADC4
POWER_EnableADC(true);
ADC_CONF4();
ADC_Enable(ADC, true);
ADC_DoSoftwareTrigger(ADC);
while (!(ADC_GetStatusFlags(ADC) & kADC_DataReadyFlag))
{
}
adcConvResult4 = ADC_GetConversionResult(ADC);
fresult4 = ADC_ConversionResult2Mv(ADC, ADC_CHANNEL_4, ADC_CFG_IDX, g_AdcBandgap, g_AdcVinn4,
adcConvResult4);
}

static void ADC_CH_5(void)
{
//Activate ADC5
POWER_EnableADC(true);
ADC_CONF5();
ADC_Enable(ADC, true);
ADC_DoSoftwareTrigger(ADC);
while (!(ADC_GetStatusFlags(ADC) & kADC_DataReadyFlag))
{
}
adcConvResult5 = ADC_GetConversionResult(ADC);
fresult5 = ADC_ConversionResult2Mv(ADC, ADC_CHANNEL_5, ADC_CFG_IDX, g_AdcBandgap, g_AdcVinn5,
adcConvResult5);
}

static void ADC_CH_9(void)
{
//Activate ADC9
POWER_EnableADC(true);
ADC_CONF9();
ADC_Enable(ADC, true);
ADC_DoSoftwareTrigger(ADC);
while (!(ADC_GetStatusFlags(ADC) & kADC_DataReadyFlag))
{
}
adcConvResult9 = ADC_GetConversionResult(ADC);
fresult9 = ADC_ConversionResult2Mv(ADC, ADC_CHANNEL_9, ADC_CFG_IDX, g_AdcBandgap, g_AdcVinn9,
adcConvResult9);
}

static void ADC_CONF4(void)
{
adc_config_t adcConfigStruct4;
adc_sd_config_t adcSdConfigStruct4;

ADC_GetDefaultConfig(&adcConfigStruct4);
adcConfigStruct4.channelEnable = (1U << ADC_CHANNEL_4);
adcConfigStruct4.channelConfig = (ADC_CFG_IDX << ADC_CHANNEL_4);
adcConfigStruct4.triggerSource = ADC_TRIGGER;
adcConfigStruct4.clock = kADC_Clock2M;
adcConfigStruct4.convMode = kADC_ConvModeSingle;
ADC_Init(ADC, &adcConfigStruct4);

ADC_GetSdDefaultConfig(&adcSdConfigStruct4);
adcSdConfigStruct4.downSample = kADC_DownSample32;
ADC_SetSdConfig(ADC, ADC_CFG_IDX, &adcSdConfigStruct4);
/* Bandgap voltage */
g_AdcBandgap = ADC_GetBandgapCalibrationResult(ADC, ADC_CFG_IDX);

/* Calibration VINN value */
g_AdcVinn4 = ADC_GetVinnCalibrationResult(ADC, &adcConfigStruct4);
ADC_Enable(ADC, true);
}
static void ADC_CONF5(void)
{
adc_config_t adcConfigStruct5;
adc_sd_config_t adcSdConfigStruct5;

ADC_GetDefaultConfig(&adcConfigStruct5);
adcConfigStruct5.channelEnable = (1U << ADC_CHANNEL_5);
adcConfigStruct5.channelConfig = (ADC_CFG_IDX << ADC_CHANNEL_5);
adcConfigStruct5.triggerSource = ADC_TRIGGER;
adcConfigStruct5.clock = kADC_Clock2M;
adcConfigStruct5.convMode = kADC_ConvModeSingle;
ADC_Init(ADC, &adcConfigStruct5);

ADC_GetSdDefaultConfig(&adcSdConfigStruct5);
adcSdConfigStruct5.downSample = kADC_DownSample32;
ADC_SetSdConfig(ADC, ADC_CFG_IDX, &adcSdConfigStruct5);
/* Bandgap voltage */
//g_AdcBandgap = ADC_GetBandgapCalibrationResult(ADC, ADC_CFG_IDX);

/* Calibration VINN value */
g_AdcVinn5 = ADC_GetVinnCalibrationResult(ADC, &adcConfigStruct5);
ADC_Enable(ADC, true);
}


static void ADC_CONF9(void)
{
adc_config_t adcConfigStruct9;
adc_sd_config_t adcSdConfigStruct9;

ADC_GetDefaultConfig(&adcConfigStruct9);
adcConfigStruct9.channelEnable = (1U << ADC_CHANNEL_9);
adcConfigStruct9.channelConfig = (ADC_CFG_IDX << ADC_CHANNEL_9);
adcConfigStruct9.triggerSource = ADC_TRIGGER;
adcConfigStruct9.clock = kADC_Clock2M;
adcConfigStruct9.convMode = kADC_ConvModeSingle;
ADC_Init(ADC, &adcConfigStruct9);

ADC_GetSdDefaultConfig(&adcSdConfigStruct9);
adcSdConfigStruct9.downSample = kADC_DownSample32;
ADC_SetSdConfig(ADC, ADC_CFG_IDX, &adcSdConfigStruct9);
/* Bandgap voltage */
//g_AdcBandgap = ADC_GetBandgapCalibrationResult(ADC, ADC_CFG_IDX);

/* Calibration VINN value */
g_AdcVinn9 = ADC_GetVinnCalibrationResult(ADC, &adcConfigStruct9);
ADC_Enable(ADC, true);
}

 

 

What is the problem of the code?

Is there any way to improve it?

I'm sampling 12 samples per second. How can I add a timer instead of using the "delay" function to get the accurate time?

Outcomes