Antonio Hermosilla

ADC random values in KL02

Discussion created by Antonio Hermosilla on May 26, 2016
Latest reply on May 29, 2016 by Antonio Hermosilla

Hi, I'm working with a Kl02 and I'm trying to read an ADC channel but always I read random values. I've been working with different codes and ways but the mistake continues and I can't figure out the problem.

The uC is the MKL02Z32VFG4. QFN 16

I'm trying to read values from the port PTB0 -> ADC0 Channel 6  [ PIN 8 ]

To verify the correct configuration of the ADC peripheral I'm reading form other channel in this case I'm usign the port  PTB5 -> ADC0 Channel  1 [PIN 13] . This channel works perfectly and I can read right values.

 

This is my schematic. In the PIN labeled as TRIGGER_3.3 I can read perfectly the ADC value. But the PIN labeled as MA give me always random values.

 

 

schematic.JPG

Code to configure the ADC:

 

I'm working with Kinetis Desing Studio 3.1.0 and KSDK 1.3.0

 

int Interface_Adc_Init( t_Interface_Adc_Handle *adc_handle,

                                    t_Interface_Adc_Device adc )

{

  adc16_chn_config_t adcCalibrationChnConfig;

 

  if( adcList[adc].installed == Interface_Adc_Free_Device )

  {

    ADC16_DRV_StructInitUserConfigDefault( &adc_handle->adc_config_info );

    adc_handle->adc_config_info.lowPowerEnable           = false;

    adc_handle->adc_config_info.resolution                       = kAdc16ResolutionBitOfSingleEndAs12;

    adc_handle->adc_config_info.asyncClkEnable            = false;

    adc_handle->adc_config_info.clkSrc                            = kAdc16ClkSrcOfBusClk;

    adc_handle->adc_config_info.clkDividerMode              = kAdc16ClkDividerOf2;

    adc_handle->adc_config_info.longSampleTimeEnable = false; //true;

    adc_handle->adc_config_info.longSampleCycleMode  = kAdc16LongSampleCycleOf16;

 

    if ( kStatus_ADC16_Success != ADC16_DRV_Init( adc_handle->device, &adc_handle->adc_config_info ) )

    {

      while(1); // only for test use

    }

    adc_handle->adc_average_cfg.hwAverageEnable = false;//true;

    adc_handle->adc_average_cfg.hwAverageCountMode = kAdc16HwAverageCountOf16;

    if( kStatus_ADC16_Success != ADC16_DRV_ConfigHwAverage( adc_handle->device, &adc_handle->adc_average_cfg ) )

    {

      while(1); // only for test use

    }

 

    adcCalibrationChnConfig.chnIdx = kAdc16Chn31;

    adcCalibrationChnConfig.convCompletedIntEnable = false;

 

    // Configure channel0

    ADC16_DRV_ConfigConvChn(adc_handle->device, 0U, &adcCalibrationChnConfig);

 

    // Auto calibration.

    ADC16_DRV_GetAutoCalibrationParam( adc_handle->device, &adc_handle->adc_calib_param );

    ADC16_DRV_SetCalibrationParam( adc_handle->device, &adc_handle->adc_calib_param );

  }

  return( 0 );

}

/*  END  */

 

Code to start and read a conversion

 

int Interface_Adc_Start_Measure( t_Interface_Adc_Handle *adc_handle, t_Adc_Channels channel, bool intEnable )

{

  adc16_chn_config_t adc_channel_cfg;

  adc_channel_cfg.chnIdx = (adc16_chn_t)channel;

  adc_channel_cfg.convCompletedIntEnable = intEnable;

  ADC16_DRV_ConfigConvChn( adc_handle->device, 0, &adc_channel_cfg );

  return( 0 );

}

int Interface_Adc_Stop_Conversion( t_Interface_Adc_Handle *adc_handle )

{

  ADC16_DRV_PauseConv( adc_handle->device, 0 );

  return(0);

}

 

int Interface_Adc_Read_Measure( t_Interface_Adc_Handle *adc_handle )

{

  ADC16_DRV_WaitConvDone( adc_handle->device, 0 );

  return ( ADC16_DRV_GetConvValueRAW( adc_handle->device, 0 ) );

}

 

 

Main

static t_Interface_Adc_Handle adc0;

int main(void)

{

  static float adc;

  static float adc_Convertion;

  // Configure board specific pin muxing

  hardware_init();

 

  Api_System_Tick_Init( 100, Util_Clock_Base_Update ); //100uS 0.1ms.

  Api_System_Tick_Start( );

 

  Util_Periodic_Timer_Init( &systemTick1000, 10000 ); //1000ms

 

Interface_Adc_Init( &adc0, Interface_Adc0 );

  /* Synchronous tasks */

  while(1)

  {

    if( Util_Periodic_Timer_Execute( &systemTick1000 ) ) //1000ms.

    {

       Interface_Adc_Start_Measure( &adc0, Interface_Adc_Channel6, false );

       adc =Interface_Adc_Read_Measure( &adc0 );         <- This channel fail.

       Interface_Adc_Stop_Conversion( &adc0 );

 

       Interface_Adc_Start_Measure( &adc0, Interface_Adc_Channel1, false );

       adc =Interface_Adc_Read_Measure( &adc0 );      <- This channel works fine.

       Interface_Adc_Stop_Conversion( &adc0 );

    }

  }//while(1)

}

 

 

Any idea to fix the problem?

Thanks in advance and sorry for the code without colors.

Outcomes