Temperature measurement with ADC

cancel
Showing results for 
Search instead for 
Did you mean: 

Temperature measurement with ADC

545 Views
prashantdev
Contributor III

I am using MK61FN1M0VMJ12 microcontroller. I am trying to read it's temperature with built in sensor with ADC CH 26.  But I get constant reading of 64875(ADC0_RA) in differential and value 64 when in single ended mode. Same behavior is seen on CH 30. Same piece of code I tested on my K60 Tower Kit and it is working there properly. Can u please tell me what I'm doing wrong. Here is the initialization code:

void init_adc(){

tADC_Config Master_Adc_Config;
disable_irq(ADC0_irq_no) ;   // not ready for this interrupt yet. Plug vector first.
disable_irq(72) ;    // not ready for this interrupt yet. Plug vector first.
  SIM_SCGC6 |= (SIM_SCGC6_ADC0_MASK );
  SIM_SCGC6 |= SIM_SCGC6_PDB_MASK ;
// Configure System Integration Module for defaults as far as ADC
  SIM_SOPT7 &= ~(SIM_SOPT7_ADC0ALTTRGEN_MASK  | // selects PDB not ALT trigger
                SIM_SOPT7_ADC0PRETRGSEL_MASK );
  SIM_SOPT7 = SIM_SOPT7_ADC0TRGSEL(0);       // applies only in case of ALT trigger, in which case
  PDB0_SC =  PDB_SC_CONT_MASK       // Contintuous, rather than one-shot, mode
         | PDB_SC_PDBEN_MASK      // PDB enabled
//          | PDB_SC_PDBIE_MASK      // PDB Interrupt Enable
         | PDB_SC_PRESCALER(0x5)  // Slow down the period of the PDB for testing
         | PDB_SC_TRGSEL(0xf)     // Trigger source is Software Trigger to be invoked in this file
         | PDB_SC_MULT(2);        // Multiplication factor 20 for the prescale divider for the counter clock
                                  // the software trigger, PDB_SC_SWTRIG_MASK is not triggered at this time.
  PDB0_IDLY = 0x0000;   // need to trigger interrupt every counter reset which happens when modulus reached
  PDB0_MOD = 0xffff;    // largest period possible with the slections above, so slow you can see each conversion.
// channel 0 pretrigger 0 and 1 enabled and delayed
  PDB0_CH0C1 = PDB_C1_EN(0x01)
           | PDB_C1_TOS(0x01)
           | PDB_C1_EN(0x02)
           | PDB_C1_TOS(0x02) ;
  PDB0_CH0DLY0 = 0x2000 ;
  PDB0_CH0DLY1 = 0x4000 ;
  PDB0_SC =  PDB_SC_CONT_MASK        // Contintuous, rather than one-shot, mode
         | PDB_SC_PDBEN_MASK       // PDB enabled
//          | PDB_SC_PDBIE_MASK       // PDB Interrupt Enable
         | PDB_SC_PRESCALER(0x5)   // Slow down the period of the PDB for testing
         | PDB_SC_TRGSEL(0xf)      // Trigger source is Software Trigger to be invoked in this file
         | PDB_SC_MULT(2)          // Multiplication factor 20 for the prescale divider for the counter clock
         | PDB_SC_LDOK_MASK;       // Need to ok the loading or it will not load certain regsiters!
                                   // the software trigger, PDB_SC_SWTRIG_MASK is not triggered at this time.
 
  Master_Adc_Config.CONFIG1  = ADLPC_NORMAL
                            | ADC_CFG1_ADIV(ADIV_4)
                            | ADLSMP_LONG
                            | ADC_CFG1_MODE(MODE_16)
                            | ADC_CFG1_ADICLK(ADICLK_BUS);
  Master_Adc_Config.CONFIG2  = MUXSEL_ADCA
                            | ADACKEN_DISABLED
                            | ADHSC_HISPEED
                            | ADC_CFG2_ADLSTS(ADLSTS_20) ;
  Master_Adc_Config.STATUS2  = ADTRG_HW
                            | ACFE_DISABLED
                            | ACFGT_GREATER
                            | ACREN_ENABLED
                            | DMAEN_DISABLED
                            | ADC_SC2_REFSEL(REFSEL_EXT);
  Master_Adc_Config.STATUS3  = CAL_OFF
                            | ADCO_SINGLE
                            | AVGE_ENABLED
                            | ADC_SC3_AVGS(AVGS_32);
  Master_Adc_Config.PGA      = PGAEN_DISABLED
                            | PGACHP_NOCHOP
                            | PGALP_NORMAL
                            | ADC_PGA_PGAG(PGAG_64);
  Master_Adc_Config.STATUS1A = AIEN_OFF | DIFF_SINGLE | ADC_SC1_ADCH(31);
  Master_Adc_Config.STATUS1B = AIEN_OFF | DIFF_SINGLE | ADC_SC1_ADCH(31);
  ADC_Config_Alt(ADC0_BASE_PTR, &Master_Adc_Config);  // config ADC
  ADC_Cal(ADC0_BASE_PTR);                    // do the calibration
  Master_Adc_Config.STATUS1A = AIEN_ON | DIFF_SINGLE | ADC_SC1_ADCH(26);
  Master_Adc_Config.STATUS1B = AIEN_ON | DIFF_SINGLE | ADC_SC1_ADCH(30);
  ADC_Config_Alt(ADC0_BASE_PTR, &Master_Adc_Config);  // config ADC0
  enable_irq(ADC0_irq_no) ;  
  PDB0_SC |= PDB_SC_SWTRIG_MASK ;    // kick off the PDB  - just once

}

void adc0_isr(void){ if (( ADC0_SC1A & ADC_SC1_COCO_MASK ) == ADC_SC1_COCO_MASK) { 

  printf("ADC0A: %d\t",ADC0_RA);

 } else if (( ADC0_SC1B & ADC_SC1_COCO_MASK ) == ADC_SC1_COCO_MASK) {

  printf("ADC0B: %d\r",ADC0_RB);

 }}

Labels (1)
Tags (4)
0 Kudos
9 Replies

336 Views
Alexis_A
NXP TechSupport
NXP TechSupport

Hi prashantdev‌,

The temperature sensor should be only in the AD26 only and the AD31 mention that disable the module, if you want to do it single ended disable the SC1n[DIFF].

pastedImage_1.png

Also, for more information, check the following document that explains how to convert the measurement to temperature.

Let me know if this helps you.

Best Regards,

Alexis Andalon

0 Kudos

336 Views
prashantdev
Contributor III

Thnx Alexis for ur reply.

I'm disabling the channel before calibration. After calibration I've enabled it again.

0 Kudos

336 Views
Alexis_A
NXP TechSupport
NXP TechSupport

Hi Prashant D,

I reproduce this using Processor Expert and could succesfully obtain the measurement, here's an example project of how to do this.

Let me know if this helps you.

Best Regards,

Alexis Andalon

0 Kudos

336 Views
prashantdev
Contributor III

Thanks for ur effort. Can u tell me if I'm doing something wrong in above posted code ? Because I've tested same code on different K61 microcontroller and behavior is same. Though when I run this code on my K60 tower kit it works. I don't know what I'm doing wrong.

0 Kudos

336 Views
Alexis_A
NXP TechSupport
NXP TechSupport

Hello Prashant,

Could you try other ADC modules (ADC1 / ADC2 / ADC3) or other channels to check if this behavior is the same?

Best Regards,

Alexis Andalon

0 Kudos

336 Views
prashantdev
Contributor III

Yes it is same with all channels

0 Kudos

336 Views
Alexis_A
NXP TechSupport
NXP TechSupport

Hello Prashant, 

Which voltage does it have VREFH  and VREFL? Also do you have other MCU where you could test this?

Best Regards,

Alexis Andalon

0 Kudos

336 Views
prashantdev
Contributor III

It shows same value (64875) while reading VREFH and VREFL channel. My code works properly on K60N512VMD100 tower kit.

0 Kudos

336 Views
Alexis_A
NXP TechSupport
NXP TechSupport

Hello Prashant,

Since this is in your development board 

Best Regards,

Alexis Andalon

0 Kudos