bikashh
Full Member level 5
Dear Friends, i am trying to configure my LPC2368 ADC but getting zero output for all value. For testing purpose i am feeding voltage(0 to 3.3v) to ADC from a voltage divider. Please let me know for any correction in the below code.The mode i am using is software controlled.thanks in advance.
void ADCInit(void)
{
/* Enable CLOCK into ADC controller */
PCONP |= (1 << 12); /* all the related pins are set to ADC inputs, AD0.0~7 */
PINSEL0 = PINSEL0 & 0xFFFF7FFF; //ADC0 p0.23,bit 15(0),14(1)-> 1111 1111 1111 1111 0111 1111 1111 1111
AD0CR = ( 0x01 << 0 ) | /* SEL=1,select channel 0~7 on ADC0 */
( ((Fpclk /4000000UL) - 1) << 8 ) | /* CLKDIV = Fpclk / 1000000 - 1 */
( 0 << 16 ) | /* BURST = 0, no BURST, software controlled */
( 0 << 17 ) | /* CLKS = 0, 11 clocks/10 bits */
( 1 << 21 ) | /* PDN = 1, normal operation */
( 0 << 22 ) | /* TEST1:0 = 00 */
( 0 << 24 ) | /* START = 0; A/D conversion stops */
( 0 << 27 ); /* EDGE = 0 (CAP/MAT singal falling,trigger A/D conversion) */
AD0INTEN = 0x01; /* Enable AD0 interrupt*/
}
int ADC0Read(void)
{
DWORD ADC_Data=0;
DWORD ADCReg=0;
AD0CR &= 0xFFFFFF00; /* switch channel,start A/D convert */
AD0CR |= (1 << 24) | (1 << 0);
do
{
ADCReg = AD0DR0; // Read A/D Data Register
} while ((ADCReg & 0x80000000) == 0); // Wait for end of A/D Conversion
AD0CR = 0x00000000;
ADC_Data = (( ADCReg & 0x0000FFC0 ) >> 6);
return (ADC_Data); /* return A/D conversion value */
}
/*MAIN PROGRAM*/
main( )
{
int result=0;
Init_UART0(9600);
InitTimer();
uart0Puts("Initialized OK \n\r");
ADCInit();
while(1)
{
result = ADC0Read();
uart0Puts("ADC OUTPUT->");
uart0Puts((char*)itoa(result));
uart0Puts("\n\r");
delay_ms(2000);
}
}
void ADCInit(void)
{
/* Enable CLOCK into ADC controller */
PCONP |= (1 << 12); /* all the related pins are set to ADC inputs, AD0.0~7 */
PINSEL0 = PINSEL0 & 0xFFFF7FFF; //ADC0 p0.23,bit 15(0),14(1)-> 1111 1111 1111 1111 0111 1111 1111 1111
AD0CR = ( 0x01 << 0 ) | /* SEL=1,select channel 0~7 on ADC0 */
( ((Fpclk /4000000UL) - 1) << 8 ) | /* CLKDIV = Fpclk / 1000000 - 1 */
( 0 << 16 ) | /* BURST = 0, no BURST, software controlled */
( 0 << 17 ) | /* CLKS = 0, 11 clocks/10 bits */
( 1 << 21 ) | /* PDN = 1, normal operation */
( 0 << 22 ) | /* TEST1:0 = 00 */
( 0 << 24 ) | /* START = 0; A/D conversion stops */
( 0 << 27 ); /* EDGE = 0 (CAP/MAT singal falling,trigger A/D conversion) */
AD0INTEN = 0x01; /* Enable AD0 interrupt*/
}
int ADC0Read(void)
{
DWORD ADC_Data=0;
DWORD ADCReg=0;
AD0CR &= 0xFFFFFF00; /* switch channel,start A/D convert */
AD0CR |= (1 << 24) | (1 << 0);
do
{
ADCReg = AD0DR0; // Read A/D Data Register
} while ((ADCReg & 0x80000000) == 0); // Wait for end of A/D Conversion
AD0CR = 0x00000000;
ADC_Data = (( ADCReg & 0x0000FFC0 ) >> 6);
return (ADC_Data); /* return A/D conversion value */
}
/*MAIN PROGRAM*/
main( )
{
int result=0;
Init_UART0(9600);
InitTimer();
uart0Puts("Initialized OK \n\r");
ADCInit();
while(1)
{
result = ADC0Read();
uart0Puts("ADC OUTPUT->");
uart0Puts((char*)itoa(result));
uart0Puts("\n\r");
delay_ms(2000);
}
}