vinod Karuvat

ADC

Discussion created by vinod Karuvat on Jul 18, 2014
Latest reply on Apr 16, 2015 by Rui Faria

Hello ,

 

I am using the KL04 (24 pin package). The issue I am having is with the ADC of this chip. I am driving a motor and trying to measure the BEMF. The issue is with the ADC read of the BEMF. The voltage supplied is 8.5V. The VEFHH and VREFL are connected to the Vcc and GND in the 24 pin package. Also I have a LPF of 3K and 10K. So, effectively,  the 8.5V is stepped down to 1.96V.

 

So, VREFH = 3.3V and hence-forth for 10bit ADC will show 3FF for 3.3V. Which implies for 1.96V I shld get 260(hex). but end up getting 118. I don't think I am having too much impedance loaded up in my pi. The schematic is shown below

free.png

 

 

 

The code for ADC initialisation and read is as shown below -

 

static void adcInit(void){

                    uint8_t roughReg;

                    uint16_t varRough;

                    uint8_t calibStatus;

 

                    SIM_SCGC6 |= SIM_SCGC6_ADC0_MASK;

                    ADC0_SC1A = 0;

                    ADC0_SC1A |= 0x80;

                    adcConfig();

                    calibStatus = adcCalib();

                    if(calibStatus == 1){  // calibration successful //

                            varRough =(ADC0_CLP0+ADC0_CLP1+ADC0_CLP2+ADC0_CLP3+ADC0_CLP4+ADC0_CLPS)/2;

                            varRough |=0x8000;

                            ADC0_PG = varRough;

                    }

                    else while(1);

                    adcConfig();

                              

                }

               

 

                static void adcConfig(void){                         

                    ADC0_CFG1 |= 8;     // 10 bit ADC mode //

                    //ADC0_CFG1 |= ADC_CFG1_MODE(2);

                }

 

 

                static uint8_t adcCalib(void){ 

                    uint16_t varRough;

                    uint8_t status;

                    ADC0_SC2 &=~ADC_SC2_ADTRG_MASK;

                    ADC0_SC3 &= ~ADC_SC3_CAL_MASK;

                    ADC0_SC3 |=  ADC_SC3_CAL_MASK;

                    while((ADC0_SC1A & 0x00000080) != 0x00000080);

                    // COCO flag is set //

                    ADC0_SC1A &= ~ADC_SC1_COCO_MASK;

                    status = 1;

                    return(status);

                }

 

 

and the adc read -

 

static uint16_t adcRead(uint8_t adcChannel){

                    switch(adcChannelRead){                                       

                        case 'W':

                        PORTB_PCR1  &= ~PORT_PCR_MUX_MASK;   // --- W ad

                        ADC0_SC1A = 0x05;

                        break;                   

                        case 'V':

                        PORTB_PCR0 &= ~PORT_PCR_MUX_MASK;    // --- V adc

                        ADC0_SC1A = 0x06;

                        break;  

                        case 'U':

                        PORTA_PCR7  &= ~PORT_PCR_MUX_MASK;   // --- U adc

                        ADC0_SC1A = 0x07;

                        break;

                        default:

                        asm("nop");

                        break;

                    }      

                    //ADC0_SC1A |= 0x07;

                    while((ADC0_SC1A & 0x00000080) != 0x00000080);

                    adcDataObt = ADC0_RA;

                    return(adcDataObt);

                }

 

 

why is this happening. Am I doing something wrong here.

Outcomes