AnsweredAssumed Answered

Problem with reading analog acceleration sensor with ADC

Question asked by Shaocheng Luo on Sep 25, 2016
Latest reply on Sep 26, 2016 by Alice_Yang

I have trouble in using ADC functionality in FRDM-KL46Z under Kinetis IDE. I used PTE20 (ADC0_SE0),PTE16 (ADC0_SE1),PTE18 (ADC0_SE2),to read X, Y, Z axis output of ADXL335. However, the ADC output were always 0, as the initial values were not zero. I did set the registered according to the datasheet but have no idea what happened. Please find my code below.

/*ADC.c*/
void adc_init(void)
{
   SIM_SCGC5 |= SIM_SCGC5_PORTE_MASK;
   PORTE->PCR[20] = PORT_PCR_MUX(0);
   SIM_SCGC6 |= SIM_SCGC6_ADC0_MASK;

// Initialize ADC0

  Master_Adc_Config.CONFIG1  = ADLPC_NORMAL | ADC_CFG1_ADIV(ADIV_1) | ADLSMP_LONG | ADC_CFG1_MODE(MODE_10)
                              | ADC_CFG1_ADICLK(ADICLK_BUS);
  Master_Adc_Config.CONFIG2  = MUXSEL_ADCA | ADACKEN_DISABLED | ADHSC_NORMAL | ADC_CFG2_ADLSTS(ADLSTS_20) ;
  //Master_Adc_Config.COMPARE1 = 0x1234u ;
  //Master_Adc_Config.COMPARE2 = 0x5678u ;
  Master_Adc_Config.STATUS2  = ADTRG_SW | ACFE_DISABLED | ACFGT_LESS | ACREN_DISABLED | DMAEN_DISABLED | ADC_SC2_REFSEL(REFSEL_EXT);
  Master_Adc_Config.STATUS3  = CAL_OFF | ADCO_SINGLE | AVGE_DISABLED | ADC_SC3_AVGS(AVGS_4);
// 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);

  ADC_Config_Alt(ADC0_BASE_PTR, &Master_Adc_Config);  // config ADC
}

uint16_t adc_read(uint8_t channel)
{
   ADC0_SC1A = AIEN_OFF | DIFF_SINGLE | ADC_SC1_ADCH(channel) ;     // start conversion
   while((ADC0_SC1A & ADC_SC1_COCO_MASK)!= 0){};
   return ADC0_RA;
}


/*main.c*/
int main(void)
{
        //unsigned char uartStr[10];
        unsigned int  strNumX, strNumY, strNumZ = 0;

        unsigned char uLCDMsg[5] = "";
        /*initialized those values to be 1*/
        uint16_t X_value = 1;
        uint16_t Y_value = 1;
        uint16_t Z_value = 1;

        //__disable_irq();

        //uart_init (UART1, SYSCLK, BAUDRATE);
        SLCD_Init();
        adc_init();

        //__enable_irq();

        X_value = adc_read(0);
        Y_value = adc_read(1);
        Z_value = adc_read(2);

        snprintf(uLCDMsg, 5, "%4d", X_value);
        SLCD_WriteMsg(uLCDMsg);
}

Outcomes