ADC problem

cancel
Showing results for 
Search instead for 
Did you mean: 

ADC problem

850 Views
ashkanrezaee
Contributor III

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?

Labels (1)
0 Kudos
5 Replies

430 Views
estephania_mart
NXP TechSupport
NXP TechSupport

Hello,

As the code is not complete I'm not sure which can be the issue here, you will need to make sure to enable the clock gating of all the pins you are using as well as making sure that all the pins are configured correctly in the code. make sure that when you are using the ADC in the init function it is being used correctly,

About the improvements, If you are planning to use Bluetooth LE the code you are using will not work as you need it to be in a task based approach. You can also create the structures directly and just call them in the initialization. 

For the timers I would recommend you to check the timers example available, once you understand the timers usage merge both of the examples.

You can also look in the community for some reference posts

How can I set/change ADC data rate in QN9080? 

https://community.nxp.com/thread/531153 

regards,
Estephania

0 Kudos

430 Views
ashkanrezaee
Contributor III

I checked pin config but I couldn't find anything.

you can find the whole code attached.

0 Kudos

430 Views
estephania_mart
NXP TechSupport
NXP TechSupport

Hello,

I would recommend you to remove the several calls you have to the initialization of the ADC as you are doing it every time you are entering to the configuration.

I believe there is a confusion in the management and usage of the ADC, could you please check the QN908x ADC Configuration Guide ?

I believe that the Single scan mode might work for your needs.

Regards ,
Estephania

0 Kudos

430 Views
ashkanrezaee
Contributor III

Hi Estephania,

Can I ask you to be more specific? For single scan mode, I will change it but for your first two points, I really have no clue. I don´t know which functions I should remove and which parts of configuration I need to change.

I will appreciate it if you write more specifically.

Thank you

0 Kudos

430 Views
estephania_mart
NXP TechSupport
NXP TechSupport

Hello,

Every time that the delay passes, you call your 3 functions.

ADC_CH_4();
 ADC_CH_5();
 ADC_CH_9();

In each of them you configure the ADC, you call the ADC_Init , ADC power,  etc. This is not a good practice , and can also be causing some issues .

Also, I would recommend you to start with the interrupt example for the ADC and before adding the DAC in your code , make sure that you are reading correctly the 3 ADC channels before adding more logic to your code

Regards,

Estephania

0 Kudos