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

Try this program from keil, change it to LPC1768 and configure your FIOs..

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
#include <lpc21xx.h>
 
unsigned val = 0;
 
int main(void)
{
 
VPBDIV = 0x02;          //Set the Pclk to 30 Mhz
IODIR1 = 0x00FF0000;    // P1.16..23 defined as Outputs  
ADCR   = 0x00270601;    // Setup A/D: 10-bit AIN0 @ 3MHz 
ADCR  |= 0x01000000;    // Start A/D Conversion 
 
while(1)
{
          
do
{
    val = ADDR;                   // Read A/D Data Register 
}        
while ((val & 0x80000000) == 0);  //Wait for the conversion to complete
val = ((val >> 6) & 0x03FF)<<16;  //Extract the A/D result 
 
IOCLR1 = ~val;                    //write the result onto the LED pins
IOSET1 =  val;   
}
 
}

 

Re: LPC 1768 cortex m3 ADC

keil generate only .HEX file.I could't fuse hex file in LPC 1768,I need .AXF file.LPC Xpresso only generate that type of file. Can you know any other compiler generating .AXF file.
 

Re: LPC 1768 cortex m3 ADC

what is the programmer you are using?? will that support .elf file which is created by cross Studio for ARM.. surely most of the programmers will have a option to load hex file...
and also The ADC in Arm is very easy doing SOC with ADCR and waiting for done flag and reading the result works fine for me...
 

Re: LPC 1768 cortex m3 ADC

ya you are right.... i finished ADC,But i can't do repeated conversion. For repeated conversion i used while(1) loop,But i dint get the correct answer.

With out using while(1) loop i got the answer without any problem,the conversion ll done only one time.when i reset my device next conversion will done.
Now,Repeating ADC conversion continuously is the problem.Normally we use while(1) loop for repeating the same process unconditionally.if i use while(1) loop conversion will not done.

And i have LCP 1768(cortex M3) with LPC link and LPC Xpresso Software.By using these,i can able to fuse only .AXE file.It will not support any other format.
 

Re: LPC 1768 cortex m3 ADC

No,I think that is different.The same concept is done for (LPC 2378 & 2148 ) with keil without any issues. But not in LPC 1768 with Xpresso.I need to do for LPC 1768.
 

Re: LPC 1768 cortex m3 ADC

how did u find one conversion worked for you?? do you having debugging interface??
try writing direct code for reading two times and put while 1(copy and paste the code two times and put while 1).. ater that try monitoring A/D Status register (STAT - address 0x4003 4030) instead of done flag in global register..
LPC 1768 looks same to me..
 

Re: LPC 1768 cortex m3 ADC

with out using while loop in main program,the conversion ll done single time(main loop ll execute one time.) Also i had a result from hardware.
 

Re: LPC 1768 cortex m3 ADC

what about the second time??
Code:
main()
{
init();
SOC;
wait;
put the result in output;
init();
SOC;
wait;
put the result in output;
while(1);
}
 

Re: LPC 1768 cortex m3 ADC

Output shows 1st conversion data.How i found that is first conversion data means,if the data is second conversion data means when i reset my device the data will not change.BUT I HAVE DIFFERENT DATA WHAT I GET BEFORE RESET. In case i get same data after reset, that'll be second conversion data.... but that is differ So it is FIRST CONVERTION DATA.
 

Re: LPC 1768 cortex m3 ADC

you mean not changing according to analog input..? that may also happen with program went on a abort situation.... so we cant tell with a hardware, to learn ARM you need debugging interface...
 

Re: LPC 1768 cortex m3 ADC

Below code is ADC for LPC 1768. I had result for first conversion.But output is not changing according to the analog input.So any one help me.
 
Last edited:

Re: LPC 1768 cortex m3 ADC

you are using which adc channel??
first time you configured ADC channel 1 (1<<1) but next time channel 0 (1 << 0)..
 

Re: LPC 1768 cortex m3 ADC

Sorry,But i tried below code.Output will not change.If i use while(1) also.
Code:
void vADC_Init(void)
{
	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 << 0 ) |
                   ( ( PCLK / ADC_CLK - 1 ) << 8 ) |
                   ( 0 << 16 ) |
                   ( 0 << 17 ) |
                   ( 1 << 21 ) |
                   ( 0 << 24 ) |
                   ( 0 << 27 );
}

int main(void)
{
	LPC_GPIO2->FIODIR=0x00000FFF;
	LPC_GPIO2->FIOCLR=0x00000FFF;

	vADC_Init();
	LPC_ADC->ADCR |= 1 << 24;
	while(!(LPC_ADC->ADGDR & (1 << 31)));
	int curVal=( LPC_ADC->ADDR0 >> 4 ) & 0xFFF;
	LPC_GPIO2->FIOSET=curVal;

}
 

Re: LPC 1768 cortex m3 ADC

ya,Above program is for single conversion.
For multiple conversion i used while(1) loop after the vADC_Init() in main program.But output not changing for corresponding analog input.

- - - Updated - - -

I got answer by adding one more function(LED BLINKING) in main program.But without that function it won't.
 

Re: LPC 1768 cortex m3 ADC

this is the code with led blinking function.
Code:
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<<0)
					|((PCLK / ADC_CLK -1) << 8)
				    |(0<<16)
					|(0<<17)
					|(1<<21)
					|(0<<24)
					|(0<<27);
}

ADC_Read()
{
	uint32_t cur_val;

	LPC_ADC->ADCR |= (1<<24);



			while(!(LPC_ADC->ADGDR & (1<<31)));
	LPC_ADC->ADCR |=(0<<24);
	cur_val = (LPC_ADC->ADDR0 >>4) & 0XFFF;
	LPC_GPIO2->FIOSET = cur_val;

}

LED_BLINK()
{
	LPC_GPIO2->FIOSET = 0X000000FF;
	delay(1000);
	LPC_GPIO2->FIOCLR = 0X000000FF;
	delay(1000);
}
void delay(uint32_t val)
{
	uint32_t i,j;
	for(i=0;i<val;i++);
	for(j=0;j<10000000;j++);
}
void main()
{

	LPC_GPIO2->FIODIR = 0X00000FFF;
	LPC_GPIO2->FIOCLR = 0X00000FFF;
	ADC_INT();
	while(1)
	{
	ADC_Read();
	delay(100000);
	LED_BLINK();
	delay(100000);
	}
}
 

Re: LPC 1768 cortex m3 ADC

Hi i think i found the problem

- - - Updated - - -


Code C - [expand]
1
2
LPC_GPIO2->FIOCLR = 0xFFF; //Add this line in your pro...
    LPC_GPIO2->FIOSET = cur_val;



- - - Updated - - -

you did missed the clearing port.....
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top