Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Sample code for ADC0.1 channel

Status
Not open for further replies.
Re: LPC 1768 cortex m3 ADC

hai friends,
Thanks for ur reply. Finally i got result. My coding is below. But still i have one problem,if i use while loop in main program i do not get correct answer.Can any one help me....

Code:
#ifdef __USE_CMSIS
#include "LPC17xx.h"
#endif


void vADC_Init(void)
{
uint32_t SystemFrequency=12000000;
uint32_t ADC_CLK=5000000;
uint32_t PCLKDIV, PCLK;
LPC_SC->PCONP |=(1<<15);
LPC_SC->PCONP |= (1 << 12);
LPC_PINCON->PINSEL1 |= (1<<14);
PCLKDIV = (LPC_SC->PCLKSEL0 >> 24) & 0x03;
switch ( PCLKDIV )
{
case 0x00:
default: PCLK = SystemFrequency/4; break;
case 0x01: PCLK = SystemFrequency; break;
case 0x02: PCLK = SystemFrequency/2; break;
case 0x03: PCLK = SystemFrequency/8; break;
}

LPC_ADC->ADCR = ( 1 << 1 ) |
( ( PCLK / ADC_CLK - 1 ) << 8 ) |
( 0 << 16 ) |
( 0 << 17 ) |
( 1 << 21 ) |
( 0 << 24 ) |
( 0 << 27 );
}

void main(void)
{
uint32_t i,j;
LPC_GPIO2->FIODIR=0x00000FFF;
LPC_GPIO2->FIOCLR=0x00000FFF;

vADC_Init();

LPC_ADC->ADCR |= (1<<0)
| ( 3 << 8 )
| ( 1 << 21 );
LPC_ADC->ADCR |= 1 << 24;
while(!(LPC_ADC->ADGDR & (1 << 31)));
LPC_ADC->ADCR |= 0 << 24;

int curVal=( LPC_ADC->ADDR0 >> 4 ) & 0xFFF;
LPC_GPIO2->FIOSET=curVal;

}
 
Last edited:

Re: LPC 1768 cortex m3 ADC

Only ADC initialization should be before while(1) loop. ADC read, conversion, display code should be within while(1) loop otherwise your code will execute only once and either stop or reset.
 

Re: LPC 1768 cortex m3 ADC

ya above code executes only one time. if i use while(1) loop for ADC read and Conversion i don't get any error while built, But i can't get result in hardware.if i use while loop like below,i have a problem.
Code:
void main(void)
{
	LPC_GPIO2->FIODIR=0x00000FFF;
	LPC_GPIO2->FIOCLR=0x00000FFF;

	vADC_Init();
			LPC_ADC->ADCR |= (1<<0)
		                    | ( 3 << 8 )
		                    | ( 1 << 21 );
			while(1)
			{
			LPC_ADC->ADCR |= 1 << 24;
			while(!(LPC_ADC->ADGDR & (1 << 31)));
			LPC_ADC->ADCR |= 0 <<24;

			int curVal=( LPC_ADC->ADDR0 >> 4 ) & 0xFFF;
			LPC_GPIO2->FIOSET=curVal;
			}
}
 

Re: LPC 1768 cortex m3 ADC

There was a problem in your code.

You are not configure the pin P0.24(AD0.1) as ADC Function

Configure the pin ADC function Pin Function Select Register 1.

use this line after the Port initialization

PINSEL1=0x00020000;
 
Last edited:

Re: LPC 1768 cortex m3 ADC

I configure ADC0.0 in ADC_int but that is not my problem, the above program will work without while loop. If i use while(1) loop like i use in the above program, I ll not get the ans.....
 

Re: LPC 1768 cortex m3 ADC

logically

Code C - [expand]
1
LPC_ADC->ADCR |= 0 <<24;


this line have no meaning (you are Oring with a zero value, to clear a bit you have to use AND operation)
and also youdont need to clear the SOC bit...
by the way you are starting it again...

Try this...

Code C - [expand]
1
2
3
4
5
6
7
8
9
int curVal;
while(1)
{
LPC_ADC->ADCR |= 1 << 24;
while(!((LPC_ADC->ADGDR) & (1 << 31)));
 
curVal = ( LPC_ADC->ADDR0 >> 4 ) & 0xFFF;
LPC_GPIO2->FIOSET = curVal;
}


post your ADC init() function...
 

Re: LPC 1768 cortex m3 ADC

Thanks.... But i already tried without clearing 24th bit. But NO response......
Below i post full code.Can u check and find the problem
Code:
/*

#ifdef __USE_CMSIS
#include "LPC17xx.h"
#endif
ADC_INT()
{
	uint32_t systemfrequency = 12000000;
	uint32_t ADC_CLK = 1000000;
	uint32_t PCLKDIV,PCLK;

	LPC_SC->PCONP |= (1<<12);
	LPC_PINCON->PINSEL1 |= (1<<14);
	PCLKDIV = (LPC_SC->PCLKSEL0 >>24) & 0X03;
	switch(PCLKDIV)
	{
	case 0x00:default: PCLK =systemfrequency/4;break;
	case 0x01: PCLK = systemfrequency;break;
	case 0x02: PCLK = systemfrequency/2;break;
	case 0x03: PCLK = systemfrequency/8;break;
	}
	LPC_ADC->ADCR |= (1<<1)
					|((PCLK / ADC_CLK -1) << 8)
					|(0<<16)
					|(0<<17)
					|(1<<21)
					|(0<<24)
					|(0<<27);
}
int main(void)
{
	uint32_t i,cur_val;
	LPC_GPIO2->FIODIR = 0X00000FFF;
	LPC_GPIO2->FIOCLR = 0X00000FFF;
	ADC_INT();

	LPC_ADC->ADCR |= (1<<0)
					|(3<<8)
					|(1<<21);
while(1)
{
	LPC_ADC->ADCR |=(1<<24);
	while(!(LPC_ADC->ADGDR & (1<<31)));

	cur_val = (LPC_ADC->ADDR0 >>4) & 0XFFF;
	LPC_GPIO2->FIOSET = cur_val;
	for(i=0;i<12000;i++);
}
}
 

Re: LPC 1768 cortex m3 ADC

Ok then you are already starting convertion in init...

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
ADC_INT()
{
    uint32_t systemfrequency = 12000000;
    uint32_t ADC_CLK = 1000000;
    uint32_t PCLKDIV,PCLK;
 
    LPC_SC->PCONP |= (1<<12);
    LPC_PINCON->PINSEL1 |= (1<<14);
    PCLKDIV = (LPC_SC->PCLKSEL0 >>24) & 0X03;
    switch(PCLKDIV)
    {
    case 0x00:default: PCLK =systemfrequency/4;break;
    case 0x01: PCLK = systemfrequency;break;
    case 0x02: PCLK = systemfrequency/2;break;
    case 0x03: PCLK = systemfrequency/8;break;
    }
    LPC_ADC->ADCR = (1<<1)
                    |((PCLK / ADC_CLK -1) << 8)
                    |(0<<16)
                    |(0<<17)
                    |(1<<21)
                    |(0<<24)
                    |(0<<27); // Not oring...
}
int main(void)
{
    uint32_t i,cur_val;
    LPC_GPIO2->FIODIR = 0X00000FFF;
    LPC_GPIO2->FIOCLR = 0X00000FFF;
    ADC_INT();
 
// removed starting
while(1)
{
    while(!(LPC_ADC->ADGDR & (1<<31))); // wait for end of first conv
 
    cur_val = (LPC_ADC->ADDR0 >>4) & 0XFFF;
    LPC_GPIO2->FIOSET = cur_val;
 
    LPC_ADC->ADCR = (1<<1) 
                    |((PCLK / ADC_CLK -1) << 8)
                    |(0<<16)
                    |(0<<17)
                    |(1<<21)
                    |(0<<24)
                    |(0<<27);  // now start second
}
}

 

Re: LPC 1768 cortex m3 ADC

then make it start with it,,,,,,,,,

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
ADC_INT()
{
    uint32_t systemfrequency = 12000000;
    uint32_t ADC_CLK = 1000000;
    uint32_t PCLKDIV,PCLK;
 
    LPC_SC->PCONP |= (1<<12);
    LPC_PINCON->PINSEL1 |= (1<<14);
    PCLKDIV = (LPC_SC->PCLKSEL0 >>24) & 0X03;
    switch(PCLKDIV)
    {
    case 0x00:default: PCLK =systemfrequency/4;break;
    case 0x01: PCLK = systemfrequency;break;
    case 0x02: PCLK = systemfrequency/2;break;
    case 0x03: PCLK = systemfrequency/8;break;
    }
    LPC_ADC->ADCR = (1<<1)
                    |((PCLK / ADC_CLK -1) << 8)
                    |(0<<16)
                    |(0<<17)
                    |(1<<21)
                    |(1<<24)
                    |(0<<27); // (1<<24)
}
int main(void)
{
    uint32_t i,cur_val;
    LPC_GPIO2->FIODIR = 0X00000FFF;
    LPC_GPIO2->FIOCLR = 0X00000FFF;
    ADC_INT();
 
// removed starting
while(1)
{
    while(!(LPC_ADC->ADGDR & (1<<31))); // wait for end of first conv
 
    cur_val = (LPC_ADC->ADDR0 >>4) & 0XFFF;
    LPC_GPIO2->FIOSET = cur_val;
 
    LPC_ADC->ADCR = (1<<1) 
                    |((PCLK / ADC_CLK -1) << 8)
                    |(0<<16)
                    |(0<<17)
                    |(1<<21)
                    |(1<<24)
                    |(0<<27);  // (1<<24)
}
}

 

Re: LPC 1768 cortex m3 ADC

may be you got worked without selecting ADC pins but you have to select them before used.... what compiler you are using did you worked with proteus??
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top