Hello everyone, I am using lpc2378, I had initialized ADC0 for channel 1 at pin P0.24 in polling. But it is not working. I have given 1V to the pin but it reads only 0. Please check this initialization code
void init_ADC0(void) { PCONP |= (1<<12); PINSEL1 |= (1<<16);
AD0CR = ((1<<1) | (1<<21) | (5 << 8)); }
uint16_t read_ADC() { uint32_t ADC_data;
AD0CR |= (1<<24); while(!(AD0DR1&0x80000000)); AD0CR &= ~(1<<24); ADC_data = AD0DR1; ADC_data = (ADC_data>>6)&0x3FF; return (uint16_t)ADC_data; } Is there anything wrong in my code ?
Iqbal
Which type of symbolic meanings you are talking about ?
PCONP |= (1<<12);
could be rewritten to:
enum { PCONP_PCTIM0 = 1, ///< Controls power to timer0 PCONP_PCTIM1 = 2, ///< Controls power to timer1 PCONP_PCUART0 = 3, ///< Controls power to UART0 PCONP_PCUART1 = 4, ///< Controls power to UART1 ... }; ... PCONP |= 1u << PCONP_PCADC0;
By doing like this, will my code work ?
This is basic textbook stuff:
Instead of writing
Where "(1<<12)" is a pretty meaningless "magic number", you should define a meaningful name; eg,
#define PCONP_TIMER0_ON (1<<12)
so that your code becomes:
PCONP |= PCONP_TIMER0_ON
and it becomes much clearer what your code is intended to do!
I still find that really ugly, and would prefer
#define PCONP_TIM0_ON (1u << PCONP_PCADC0)
So that the code becomes:
It could, of course, also be in an enum, if you prefer.
It won't make any difference to the operation; but it will make the source code far easier to read & understand - and, therefore, to spot errors!
Again this is basic textbook programming practice - nothing specifically to do with the LPC2378, or keil, or...
Ok, I am giving you the sample of the above code again.
void init_ADC(void) { uint32_t clk_div; PCONP |= (1<<12); PINSEL1 |= (1<<16); clk_div = SET_ADC0_CLK_AT_kHz(3000); AD0CR = (ADC0_CH_1 | PDN | (clk_div << 8)); } uint16_t read_ADC_data() { uint32_t ADC_data; AD0CR |= START_ADC0_CONV; while(!(AD0DR1 & DONE)); AD0CR &= STOP_ADC0_CONV; ADC_data = (AD0DR1 >> 6)&0x3FF; return (uint16_t)ADC_data; }
Please give me any solution.