[Read ADC] recommend way to get conversion result for a single channel after start conversion chain

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

[Read ADC] recommend way to get conversion result for a single channel after start conversion chain

519 Views
mym
Contributor I

Hi,

My software is working in K312, RTD version is 3.0.1 P01, chain type is normal, conversion mode is one-short.

Image of my reading ADC function is like: 

unsigned short ReadAdc(inst, chn){
unsigned long timeout;
unsigned short result = 0;
 Adc_Sar_Ip_ChanResultType result_tmp;
 
  Adc_Sar_Ip_StartConversion(inst, ADC_SAR_IP_CONV_CHAIN_NORMAL);
  Adc_Sar_Ip_GetConvResult( inst, chn, ADC_SAR_IP_CONV_CHAIN_NORMAL, &result_tmp);
  while( result_tmp.ValidFlag == false && timeout < 300 )
  {
    Adc_Sar_Ip_GetConvResult( inst, chn, ADC_SAR_IP_CONV_CHAIN_NORMAL, &result_tmp);
    timeout++;
  }
  if ( result_tmp.ValidFlag )
  {
    result = result_tmp.ConvData;
  }

  return result ;
}
 
There is several channels in one instance.
I read adc of ch1 first.
After several seconds, I try to read adc of ch2 in same instance.
I have got a wrong value which seems a old value.
if I read adc of ch2 twice, result of the second times is correct.
 
Are results of unread channels will not be cleared by start a new conversion?
A new conversion only overwrite the result of unread channels?
 
For the solution, I read result of target channel once, before start a new conversion to clear the valid flag, and then start new conversion and read result of channel until valid flag is set to true. It's working correctly.
 
Is it a recommended usage to read ADC? If not, what's the recommended of APIs about conversion to read ADC?
Tags (1)
0 Kudos
Reply
3 Replies

457 Views
Julián_AragónM
NXP TechSupport
NXP TechSupport

Hi @mym,

The input channels are muxed into the ADC, so you need to set the ADCx_Rn register to the channel number you want to convert, run the conversion, read the results and then set the other ADCx_Rn in that ADC to the next channel to be converted. The next conversion can then be triggered. You have to tell each conversion the input channel it is to convert.

You can simply start a SW triggered normal conversion, and wait for the notification to be triggered and read the data:

 

void AdcEndOfChainNotif(void)
{
    notif_triggered = TRUE;
    data_bandgap = Adc_Sar_Ip_GetConvData(ADCHWUNIT_0_INSTANCE, ADC_SAR_USED_CH_BANDGAP);
    data_POT_0 = Adc_Sar_Ip_GetConvData(ADCHWUNIT_0_INSTANCE, ADC_SAR_USED_CH_POT_0);

	/* Process the result to get the value in volts */
    data_bandgap_volt = ((float) data_bandgap / adcMax) * (ADC_VREFH - ADC_VREFL);
    data_POT_0_volt = ((float) data_POT_0 / adcMax) * (ADC_VREFH - ADC_VREFL);

    __asm volatile ("nop");

}}

int main (void)
{
...
    /* Start a SW triggered normal conversion on ADC_SAR */
    Adc_Sar_Ip_StartConversion(ADCHWUNIT_0_INSTANCE, ADC_SAR_IP_CONV_CHAIN_NORMAL);
    /* Wait for the notification to be triggered and read the data */
    while (notif_triggered != TRUE)
    {
    	__asm volatile ("nop");
    }
    notif_triggered = FALSE;
...
}

 

Take a look into these community examples:

The first example uses the PIT trigger to trigger BCTU conversion list to perform conversions on different ADC1 channels.

Best regards,
Julián

0 Kudos
Reply

422 Views
mym
Contributor I

Hi Julián

Thank you for your answer.

I still have some doubts.

In K3, REG NCMR seems used to select channels I want to convert?

1.Select channel by sdk function Adc_Sar_SetNormalChain 

2.Start convert instance.

3.Read convert result and Confirm the valid flag in result.

4.Keep Reading until valid flag became 1.

5.Use data after valid flag became 1.

Is this a feasible usage?

 

Thanks

reguards

YM

0 Kudos
Reply

415 Views
Julián_AragónM
NXP TechSupport
NXP TechSupport

Hi @mym,

Yes, that is correct.

Best regards,
Julián

0 Kudos
Reply