K60 ADC problem - Results are random

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

K60 ADC problem - Results are random

Jump to solution
5,833 Views
michalhrouda
Contributor I

Hi,

I have chosen MK60DN256ZVLL10 for my design and I have a problem with ADC (with both ADCs)

When I set input channel between 0 and 0x1C (including internal temp sensor) the result of the ADC is zero or random

(On channels 0 ~ 23 i get 0 or random values depending on MUXSEL setup and temperature sensor channel result is always random :smileyshocked: )

I get non zero (and correct) results only when i choose REFLH or Bandgap channels.

I tried Processor expert ADC component, Init_ADC component and code written by myself only (same behavior)

I tried all possible configurations of conversion modes, sampling timing, sampling time and calibrating feature.

Voltage on VREFH pin is correct (3.5 V) and voltage on pins that I would like to measure is correct and without any noise too.

List of pins that I use for measurement:

HUMIDITY      

=> PTC2/SPI0_PCS2/UART1_CTS_b/FTM0_CH1/FB_AD12/ADC0_SE4b/CMP1_IN0/TSI0_CH15 [72]

BAT_MEAS     

=> PTE0/SPI1_PCS1/UART1_TX/SDHC0_D1/I2C1_SDA/ADC1_SE4a [1]

PROX_MEAS     

=> PTB1/I2C0_SDA/FTM1_CH1/RMII0_MDC/MII0_MDC/FTM1_QD_PHB/ADC0_SE9/ADC1_SE9/TSI0_CH6 [54]

PWRSRC_MEAS   

=> PTC1/SPI0_PCS3/UART1_RTS_b/FTM0_CH0/FB_AD13/ADC0_SE15/TSI0_CH14 [71]

//enable bandgap

    PMC_REGSC |= PMC_REGSC_BGBE_MASK;

 

    //Processor Expert Init

    AD0_Init();

    AD1_Init();

    //Calibration

    ADC0_SC3 |= 0x80;

    while ((ADC0_SC1A && 0x80) == 0);

    ADC1_SC3 |= 0x80;

    while ((ADC1_SC1A && 0x80) == 0);

    for(;;)

    {

 

        ADC1_SC1A = 4;

        while ((ADC1_SC1A && 0x80) == 0);

        bat = ADC1_RA;

 

 

        ADC0_SC1A = 15;

        while ((ADC0_SC1A && 0x80) == 0);

        pwr = ADC0_RA;

 

        ADC0_SC1A = 9;

        while ((ADC0_SC1A && 0x80) == 0);

        prox = ADC0_RA;

 

        ADC0_SC1A = 4;

        while ((ADC0_SC1A && 0x80) == 0);

        hum = ADC0_RA;

 

        FRTOS1_vTaskDelay(500/portTICK_RATE_MS);

    }


Labels (1)
0 Kudos
1 Solution
2,988 Views
JimDon
Senior Contributor III

First, I hope you tried the code I posted.

This code works fine for me when things are properly inited, using a device init project:

  
unsigned long result;
....
for(;;) {  
       ADC0_SC1A=11;
       while ((ADC0_SC2 & 0x80));
       while (!(ADC0_SC1A & 0x80));
       result=ADC0_RA;
  }

If it is hanging, then something is not set up right.

I have attached the mcuinit file generated by device init I used.

Enjoy!!

View solution in original post

0 Kudos
31 Replies
2,312 Views
michalhrouda
Contributor I

My problem has been coased by broken JTAG interface...

0 Kudos
2,312 Views
michalhrouda
Contributor I

I've tried ADC measurements on my other K60 board and it behaves in the same way... :smileysad: Internal bandgap and internal temperature sensor measurements are correct but measurements on channels 1 ~ 24 are wrong (not zero, but some random value)

Here is part pcb layout and scheme of my other design

pointrange.png

pointrange-scheme.png

0 Kudos
2,312 Views
JimDon
Senior Contributor III

Ok this is a PE project generated for a FREEDOM board, which is an M0+

I absolutely  know beyond any shadow of a doubt that it 100% works.

I used the default cpu setup, added the ADC component, set the clock for it, added 5 inputs and set the pins for the 5 inputs.

I also noticed that if your inputs are floating, guess what - you get random data.

So, import this project and study it.

Hope this helps.

0 Kudos
2,312 Views
Florijan
Contributor III

Hi,

thanks for reply. First I have to say that measured ADC (11th)channel pin is not floating and is defined using 4k7 potentiometer at about a half Vcc (1,6V) so it is well defined and steady. My code is:

int main(void)

{

  MCU_init(); /* call device initialization */

   for(;;) {  

       ADC0_SC1A=11;

       while ((ADC0_SC2 & 0x80));

       while (!(ADC0_SC1A & 0x80));

       result=ADC0_RA;

  }

  return 0;

}

Init code for ADC generated by Device Initialization GUI:

  /* ### Init_ADC init code */

  /* SIM_SCGC6: ADC0=1 */

  SIM_SCGC6 |= (uint32_t)0x08000000UL;                      

  /* ADC0_CFG1: ADLPC=0,ADIV=3,ADLSMP=1,MODE=1,ADICLK=0 */

  ADC0_CFG1 = (uint32_t)0x74UL;                 

  /* ADC0_CFG2: ADACKEN=0,ADHSC=0,ADLSTS=0 */

  ADC0_CFG2 &= (uint32_t)~0xFFFFFFEFUL;                     

  /* ADC0_CV1: CV=0 */

  ADC0_CV1 = (uint32_t)0x00UL;                 

  /* ADC0_CV2: CV=0 */

  ADC0_CV2 = (uint32_t)0x00UL;                 

  /* ADC0_OFS: OFS=4 */

  ADC0_OFS = (uint32_t)0x04UL;                 

  /* ADC0_SC2: ADACT=0,ADTRG=0,ACFE=0,ACFGT=0,ACREN=0,DMAEN=0,REFSEL=0 */

  ADC0_SC2 = (uint32_t)0x00UL;                 

  /* ADC0_SC3: CAL=0,CALF=0,ADCO=0,AVGE=0,AVGS=0 */

  ADC0_SC3 = (uint32_t)0x00UL;                 

  /* ADC0_SC1A: COCO=0,AIEN=0,DIFF=0,ADCH=0x1F */

  ADC0_SC1A = (uint32_t)0x1FUL;                 

  /* ADC0_SC1B: COCO=0,AIEN=0,DIFF=0,ADCH=0x1F */

  ADC0_SC1B = (uint32_t)0x1FUL;                 

1. If I am stepping cpu the first pass of infinite loop the result is 0 at next pass the result is about 1,4V at next pass the result is 1,9V,... The result is wrong by as much as 30%.

2. But if I run the cpu without stepping it, it never reaches the line"result=ADC0_RA;" if I pause the execution it stops at line "while (!(ADC0_SC1A & 0x80));" like the conversion is never finished.

It is true that I am not using ADC calibration yet but this shouldn't be the cause of described behavior.

Best regards

0 Kudos
2,312 Views
karstenklein
Contributor I

Hi Florijan,

I have exactly the same behaviour on an KL25 (Freedom board), as you described.

The first time in single step mode it works, afterwards the software hangs on:

      while (!(ADC0_SC1A & 0x80));

If I let the software run free (resetting the Freedom board, no debugger) it seams to work.

Have you figured out what your problem with the hangup was ?

Best regards

Karsten

0 Kudos
2,312 Views
Florijan
Contributor III

Hi,

maybe you have some issues with ADC initialization code.

Have you tried init code that Jim Donelson posted in one of the above posts?

Best regards

Florijan

0 Kudos
2,312 Views
karstenklein
Contributor I

Hi Florijan,

basically the init code is the same (Mux_Select, AVGS = 0).

As mentioned before in free running mode, everything looks fine so far.

The problem seams to be related to the breakpoint- and single step stuff.

Placing a breakpoint here:

ADC0_SC1A=11;

Executing a two single-steps,

causes normally the software to hang up here:

while (!(ADC0_SC1A & 0x80));

Best regards

Karsten

Am 17.04.2013 11:57 schrieb florijan japelj:

>

Freescale Community <https://community.freescale.com/index.jspa>

<https://community.freescale.com/index.jspa>

>

K60 ADC problem - Results are zero or random

created by florijan japelj

<https://community.freescale.com/people/Florijan> in /Kinetis

Microcontrollers/ - View the full discussion

<https://community.freescale.com/message/324780#324780>

0 Kudos
2,312 Views
Florijan
Contributor III

Hi Karsten,

I don't remember what caused this behavior in my case. Check again if you made a project for the right mcu, on freedom board I think is MKL25Z128.

Best regards,

Florijan

0 Kudos
2,989 Views
JimDon
Senior Contributor III

First, I hope you tried the code I posted.

This code works fine for me when things are properly inited, using a device init project:

  
unsigned long result;
....
for(;;) {  
       ADC0_SC1A=11;
       while ((ADC0_SC2 & 0x80));
       while (!(ADC0_SC1A & 0x80));
       result=ADC0_RA;
  }

If it is hanging, then something is not set up right.

I have attached the mcuinit file generated by device init I used.

Enjoy!!

0 Kudos
2,312 Views
Florijan
Contributor III

Hi Jim,

do you have enabled any interrupts in this init code because in line "CMP0_DACCR = (uint8_t)0x00U;" the interrupt is activated:

PE_ISR(isr_default)

{

  /* Write your interrupt code here ... */

}

Best regards,

Florijan

0 Kudos
2,312 Views
JimDon
Senior Contributor III

Where in the code that I posted did you find "CMP0_DACCR = (uint8_t)0x00U;"?

File name and line number please, because I don't see it in code I've posted.

0 Kudos
2,312 Views
Florijan
Contributor III

Yes accidentally I have copy pasted code from another source. I will try your init and I will report tomorrow.

best regards

0 Kudos
2,312 Views
JimDon
Senior Contributor III

BTW you get that because the clock gate for CMP in SCM_SG4 was never set.

0 Kudos
2,312 Views
Florijan
Contributor III

Finally success. The ADC is working as it should. I made a silly mistake. I was watching pinout for 64-bit mcu but in freedom board there is a 80-pin mcu, so I connected the potentiometer to the wrong ADC input.

I would like to thank you Jim for your time and Michal I am not sure if this is of any help for your case.

Best regards

2,312 Views
michalhrouda
Contributor I

Have you tried measuring on bandgap and tempsensor channels?

0 Kudos
2,312 Views
Florijan
Contributor III

Today I have put my hands on Kinetis M0+ MCU the first time and guess what? I have the same problem as you do. This mcu is part of demo board purchased directly from Freescale, so I think it is most probably something wrong with the initialization of ADC rather than pcb layout issue. I have made initialization using Device Initialization GUI and maybe there is some bug in it. I will definitely open a new service request and I suggest you do the same.

best regards

2,312 Views
JimDon
Senior Contributor III

florijan,

It is hard to help you if you do not post your code. If you used PE to generate the code, it does work, it may just not be clear how to use it.

0 Kudos
2,312 Views
Florijan
Contributor III

Dear michalhrouda,

the first thing I have noticed is that in sentence

while ((ADC0_SC1A && 0x80) == 0); 

you are using double '&&' instead of single '&', so you should write

while ((ADC0_SC1A & 0x80) == 0); 

try this and report.

Best regards,

Florijan

0 Kudos
2,312 Views
michalhrouda
Contributor I

Thank you for your remark. This mistake was in my code all the time. But my application still behaves in the same way even after fixing that error. The problem is in hardware

Best regards,

Michal

0 Kudos
2,312 Views
Florijan
Contributor III

Hi,

I have a lot of experience with 8-bit mcus and their ADC modules and if you overclock the ADC it will broke down. In such case it will behave exactly as you described. I think you should replace MCU and be very careful with new one not to overclock or underclock the ADC.

Please report.

Best regards,

Florijan

0 Kudos