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);
}
Hello Shaocheng,
- First please confirm the hardware of X, Y, Z axis output of ADXL335 have signal.
- Check the data of the register ADC0_RA .
-If the ADXL335 work well , there is ADC demo you can refer the code compare with your code :
FRDM-KL46Z|Freedom Development Platform|Kinetis MCU|NXP
After download and install it, you can find the ADC demo :
kinetis_kl46_sc\kinetis_kl46_sc_rev2\klxx-sc-baremetal\src\projects\adc_demo
- Also on the KDS, if you create one Processor Expert project, there is also ADC demo :
Hope it helps!
Have a great day,
Alice
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Thank you so much Alice! The demo in "kinetis_kl46_sc\kinetis_kl46_sc_rev2\klxx-sc-baremetal\src\projects\adc_demo" really helps!!! I will go back and check what happened in my code.
BTW, I observed the printf() function in demo code, however I removed it and used uart to print on my PC instead. How can I use the printf() in the demo code please?
I have another two questions which have to do with learning KDS. 1. I realized that I cannot make things happen with only datasheet but making reference to demo codes. Is that possible only digesting the datasheet if we build project? 2. I have no knowledge about Processor Expert project. Where can I start please?
Thank you and have a nice day!
Hello Shaocheng,
-
Do you meaning the demo under kinetis_kl46_sc ?
If yes, the demos use the UART to printf . Yon can find the printf function in the file of "printf.c",
then you can step into check using which UART:
- About your fist question , sorry i do not know your meaning clearly , could you please also
take some screenshot show what about the datasheet .
About the Processor Expert , there is a User Guide in the attachment , i recommend you first
read the Chapter4 , it show how to create PE project . Then you can refer to the demo code i mentioned
last time .
Hope it helps
Have a great day,
Alice
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------