AnsweredAssumed Answered

Free Running ADC

Question asked by Ali Asadzadeh on Feb 18, 2013
Latest reply on Feb 19, 2013 by Ali Asadzadeh

Dear All

I want to use my FRDM-KL25 board to sample 4 different analog signals on PE20-PE23, I want to write some code to scan the inputs alternatively, I want to set my sample time to 25600 samples per second, do you have any Idea how I should do that, Basically I want to set a timer interrupt and use ADC in software mode to read each channel, by this method can I get the same sample time interval on each channel the same? Can the device do continuous sampling and sending the results over uart? Do we have any speed limitation?

Here is a pseudo code, Is there a better approach? Do you have any Idea?

 

Time tick ISR is 25600HZ

{
 I++;
If(i>3)
I=0;
 analogRead(i);
}

 

And here is my ADC codes

 

 

int analogRead(int ch)
{

    // Start a conversion by writing the channel number
    ADC0->SC1[0] =    ADC_SC1_ADCH(ch);
    // Wait for it to complete.
    while( ((ADC0->SC2 & ADC_SC2_ADACT_MASK) ) != 0U)   ;
    return  (int)(ADC0->R[0]);
 }



void ADC_Init(void)
{
                //Turn on ADC 
                SIM->SCGC6 |= SIM_SCGC6_ADC0_MASK;  
                //bus clock/2 & singled-ended 16bit mode & div by 4
                ADC0->CFG1 = ADC_CFG1_ADICLK(2) | ADC_CFG1_MODE(3) | ADC_CFG1_ADIV(1);
                //select channel A & high speed mode
                //ADC0->CFG2 = !ADC_CFG2_MUXSEL_MASK;// | ADC_CFG2_ADHSC_MASK;
                ADC0->CFG2 &= ~(ADC_CFG2_ADACKEN_MASK | ADC_CFG2_ADHSC_MASK | ADC_CFG2_ADLSTS(0x03));
                //hardware trigger  & VREFH/VREFL
                //ADC0->SC2 = ADC_SC2_REFSEL(0);      //ADC_SC2_ADTRG_MASK | ADC_SC2_REFSEL(0);                           
                ADC0->SC2 =0;
                //single shot mode         
                //ADC0->SC3 &= ~ADC_SC3_ADCO_MASK;  
                ADC0->SC3 = ADC_SC3_CALF_MASK;
                //hardware average 32 sample
                //ADC0->SC3 |= ADC_SC3_AVGE_MASK | ADC_SC3_AVGS(3);
}

 

Outcomes