Kinetis ADC usage issue

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

Kinetis ADC usage issue

896 Views
alvenwu
Contributor I

I try to use the ADC conversion function on the Kinetis,  basically there are two steps ,   1. ADC initialization 2 Start a ADC conversion.   I use the interrupt mode , which means after the conversion completion , ADC should raise a interrupt , then the program tries to get the conversion data from the ADC data register. Is that process right ? Here is two questions:

1.  should the cpu pins involved ADC device be configured at first during the initialization phase.

2.  there are two modes , query  and interrupt mode to indicate the conversion completion , but when I set the interrupt enable , and start the conversion , however , seems the interrupt is lost, what kind of problem would cause it ?

Here the source code of ADC module:

1. initialization:

static int adc_setup(FAR struct adc_lowerhalf_s *dev)
{
    uint32_t regval = 0;
        FAR struct kinetis_adc_s *pt_Adcdev = (FAR struct kinetis_adc_s *)dev;
        
    DEBUGASSERT(NULL != pt_Adcdev);

        /* config the sim clock rate*/   
        adc_simclock();                    
        
        /* CFG config the adc clock to get the ADCK, and select sample time and low-power   */
        regval =  ADC_CFG1_ADLPC | ADC_CFG1_ADICLK_BUSCLK | ADC_CFG1_ADIV_DIV5
                     | ADC_CFG1_ADLSMP | ADC_CFG1_MODE_1011BIT;                    
        adc_putreg(pt_Adcdev, KINETIS_ADC_CFG1_OFFSET, regval);

        /* SC2 select the trigger :software */      
        regval = ADC_SC2_REFSEL_ALT;
        adc_putreg(pt_Adcdev, KINETIS_ADC_SC2_OFFSET, regval);     

        /* SC3 select hardware averaging*/
        regval = ADC_SC3_AVGS_32SMPLS | ADC_SC3_AVGE;
        adc_putreg(pt_Adcdev, KINETIS_ADC_SC3_OFFSET, regval);        
        
         /* calibrate ADC */
        if (FALSE == adc_alibration(pt_Adcdev))
        {
        return -EINVAL;
    }

        return OK;
}

2. start the conversion

static void adc_start(FAR struct adc_lowerhalf_s *dev, FAR char cChannelid)
{
    uint32_t regval = 0;
    FAR struct kinetis_adc_s *pt_Adcdev = (FAR struct kinetis_adc_s *)dev;
        DEBUGASSERT(NULL != pt_Adcdev);
        
        /*choose a channel*/
    regval = ((cChannelid << ADC_SC1_ADCH_SHIFT) & ADC_SC1_ADCH_MASK) | ADC_SC1_AIEN;
        
        printf("adc start!\n");

        /*start a conversion*/
        adc_putreg(pt_Adcdev, KINETIS_ADC_SC1A_OFFSET, regval);    
}

0 Kudos
2 Replies

562 Views
Robin_Shen
NXP TechSupport
NXP TechSupport

Q1.  should the cpu pins involved ADC device be configured at first during the initialization phase.
A1   The pins with analog functions assigned to them default to their analog function after reset. So you don't need to configure PORTx_PCRn[MUX] select the pin as ADC input function.

analog default.png

Q2.  there are two modes , query  and interrupt mode to indicate the conversion completion , but when I set the interrupt enable , and start the conversion , however , seems the interrupt is lost, what kind of problem would cause it ?
A2   I agree with Mark. Seems that you forget enable ADC interrupt in NVIC.

I recommend you refer the ADC examples in MCUXpresso SDK.

SDK_2.3.1_FRDM-K22F ADC examples.png

adc16_interrupt in SDK_2.3.1_FRDM-K22F.png

First Search MCU and Build MCUXpresso SDK.

Search MCU and Build MCUXpresso SDK.png
Then Download SDK

Download SDK.png

Download SDK Archive.png

Best Regards,

Robin

 

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos

563 Views
mjbcswitzerland
Specialist V

Hi Alven

>> should the cpu pins involved ADC device be configured at first during the initialization phase.
This depends on the device and the ADC being used. Dedicated ADC inputs need no initialisation but if a pin with ADC multiplexed with other functions is involved it may require it.

>> seems the interrupt is lost
Do you enable the ADC interrupt in the NVIC?

See also http://www.utasker.com/docs/uTasker/uTaskerADC.pdf


Regards

Mark

P.S. Don't forget to calibrate the ADC before first use!


uTasker developer and supporter (+5'000 hours experience on +60 Kinetis derivatives in +80 product developments)
Kinetis: http://www.utasker.com/kinetis.html

0 Kudos