KL17 ADC always returns 0xff

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

KL17 ADC always returns 0xff

1,178 Views
johndemayor
Contributor I

Hi there,

I've been trying to get a really barebone example running of the ADC functionality on the KL17. I can't seem to get the microcontroller to return anything else than 0xff. At first, I thought I did something wrong with wires on the board, but then I changed the code to sample the internal temperature sensor of the KL17 - which also returned 0xff. I must be doing wrong something really basic; please take a look at my code:

#include "board.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "fsl_device_registers.h"

int main(void) {
     uint16_t data = 0;

     /* Init board hardware. */
    BOARD_InitPins();
    BOARD_BootClockRUN();
    BOARD_InitDebugConsole();

     // Enable clock to ADC peripheral
     SIM->SCGC6           |= (1<<27);                    // ADC0 clock enable

     // Registers
     ADC0->CFG1          = 0b00000001;                // ClockDiv=1, 8 bit mode, Bus clock / 2
     ADC0->CFG2          = 0b00001000;               // Fastest sample time (for low impedance sources)
     ADC0->SC3            = 0b00000000;               // No continuous conversion, no hardware average
     ADC0->SC2            = 0b00000000;               // Default register setup
     ADC0->SC1[0]      = 0b00011111;               // Channel all '1' means disable
    
    // ADC test code
    while(1){
         ADC0->SC1[0]     = 0b00011010;               // Temperature sensor, auto starts conversion

          while(ADC0->SC2 & (1<<7));                    // Conversion active or hardware averaging in progress
          while(!(ADC0->SC1[0] & (1<<7)));          // Wait for conversion to complete
          data = ADC0->R[0];                              // Return result
    }
}

Please help me on this - thanks a lot in advance!

Labels (1)
0 Kudos
6 Replies

878 Views
Robin_Shen
NXP TechSupport
NXP TechSupport

Hi John,
I am not sure if you have solved the problem.
For different packages, the ADC reference may different. Which package are you using?

And if you have set the VREFEN bit in VREF_SC?

ADC analog supply and reference connections.png

VREFEN.png

Best Regards,

Robin

0 Kudos

878 Views
gn2303
Contributor I

I have found the problem. In my case it is because I used a lower pin count model of the KL17z. Models with 36 pins or less, does not have VREFL and VREFH as voltage reference. Hence, the voltage reference must be changed by changing the REFSEL in the SC2 register. These are actually stated in the reference manual.

Hope this solves the problem.

0 Kudos

878 Views
Robin_Shen
NXP TechSupport
NXP TechSupport

Hi John,

If you slow down the ADC clock(ADC0->CFG1= 0x61) and then set a breakpoint at line (data = ADC0->R[0];), will the ADC0_RA result always equal to 0xff?

I only have FRDM-KL27Z , and the result changes each time Go to this line.

ADC0_RA.png
I have also ask our internal team who can test the code on KL17, will give you the feedback when I get it.

Best Regards,

Robin

0 Kudos

878 Views
JHinkle
Senior Contributor I

I have a custom KL17 board.

I just tried the following code to read pin E30 which is ADC0_SE23.

Here is my code and it works as expected.

Set up pin MUX

PORTE->PCR[30] = PORT_PCR_MUX(0); // adc - 5v sense

Sorry - Can't figure out how to post code in comment field


word ADC_read16b(void)
{
      ADC0_SC1A = ADC_SC1_ADCH(0x17); // start channel ADC23

      while(ADC0_SC2 & ADC_SC2_ADACT_MASK){}; //Conversion in progress

      while(!(ADC0_SC1A & ADC_SC1_COCO_MASK)){}; //Wait until conversion complete

      return ADC0_RA;
}

void test_ADC(void)
{
      word R;

      SIM_SCGC6 |= SIM_SCGC6_ADC0_MASK;

      ADC0_CFG1 |= ADC_CFG1_MODE(3) | ADC_CFG1_ADIV(3); // single ended 16 bit divide bus clock by 8
      ADC0_SC1A |= ADC_SC1_ADCH(0x1f); // all channels disabled

   R = ADC_read16b(); .... NOTE -- This read value as expected

}

Hope that helps.

Joe

0 Kudos

878 Views
johndemayor
Contributor I

Hi Gabriel,

thanks for your efforts on this.

I can confirm your findings:

  1. The code runs fine on the FRDM-KL27z dev kit.
  2. The KL17 returns 0 when reading VREFL. Makes me wonder if this is hard-strapped to zero or an actual ADC conversion is performed.

Can someone (maybe from NXP?) give any insights in why this code would run on a KL27 but not on a KL17?

Best regards,

John

0 Kudos

878 Views
gn2303
Contributor I

I have the same issue. The ADC seemed to work flawlessly on the FRDM-KL27z dev kit, both using the code from the SDK and manually setting the registers. However it doesn't work on another board and always returns 0xFFFF, except when reading from channel 30 (VREFL) which returns 0.

0 Kudos