ADC LPC1769 - please help

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

ADC LPC1769 - please help

4,163 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Jantje7600 on Sun Dec 28 10:25:17 MST 2014
Can someone please provide me with a working ADC code for LPC1769 so that I can learn it and test it ?

I have included CMSIS and linked it and I have made a new project. I just want to copy it and build it to learn from it since I can't find any good tutorials on the internet.
I really hope someone can give me that instead of referring me somewhere else... Thanks
0 Kudos
Reply
17 Replies

3,414 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Superfred on Sun Aug 23 04:22:47 MST 2015
One more:
describe a little bit what you want to do and what is connected to your 1769.
And if vTaskDelay always hangs or where exactly the problem happens.
0 Kudos
Reply

3,414 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Superfred on Sun Aug 23 04:00:49 MST 2015
Hello Daniel,
so you made a new workspace and imported the CMSIS, the FreeRtos and the SimpleDemo object? Can you check if the Embeddedartists board also using a 12 MHz crystal?
If all this is yes, I do not understand why it is not working.

Idea: put some other code in main.c before the Rtos stuff and check if its running.

Other idea: start two tasks without using vTaskDelay inside, are both running? As example, blink two LEDs but use not vTaskDelay but a FOR loop to delay the LEDs.

Fred
0 Kudos
Reply

3,414 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by dandumit on Sat Aug 22 13:17:29 MST 2015
Hi,
Clocksource it's a 12Mhz crystal.
I didn't succeed to make any RTOS project to run on this until now.
I didn't changed a thing on your project. That's I am a bit confused.: you mention about Systick but I cannot find any reference for it.
I will dig further . If you have any ideeas are wellcome!
0 Kudos
Reply

3,414 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Superfred on Sat Aug 22 10:12:14 MST 2015
Hi Daniel,
I am on holiday, having no access to my PC, so all I tell you is what I have in mind, so  maybe wrong....
Rtos needs the Systick timer interrupt to switch between Tasks, what I remember this is and a heap memory routine is the only thing really needed, so if vTaskdelay is not working it is maybe this..
What is your clocksource? 5mhz crystal?
Have you got a other FreeRtos project running?
Have you changed anything in my project? Because I am sure it was working.
Try to start only one Task, maybe the blink task...
Fred
0 Kudos
Reply

3,414 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by dandumit on Sat Aug 22 07:46:53 MST 2015
Hello,

I have 1769 as well.
Board it's made by me , but it's more like an lpcxpresso.

What do you mean by SystemtickTimer ? should I declare it by myself ? Until now all the codes that I have found as examples have successfully run on this board.
Sould I declare an interrupt ?

Kind regards,
Daniel
0 Kudos
Reply

3,414 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Superfred on Sat Aug 22 01:29:22 MST 2015
Hello Daniel,
I never had problems with vTaskDelay functions, they are the basic functions oft FreeRtos.
My code is tested on 1769 Lpcxpresso board, at least the Led should blink.

What Mikrocontroller do you have? Rtos needs the SystemtickTimer, maybe here is the problem? Or clock setup?
Fred
0 Kudos
Reply

3,414 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by dandumit on Fri Aug 21 22:12:53 MST 2015
Hello Superfred,
Thank you for example ! It's instructive. However I have a problem :
When program reaches vTaskDelay of vTaskDelayUntil , then freezes.
Any ideeas ?
Thank you,
Daniel
0 Kudos
Reply

3,414 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Superfred on Thu Jan 08 03:20:51 MST 2015
Hi Jantje,

was the example useful?
Any problems?
A feedback would be nice!

Ps: I found out that the use of RTOS Queues is better than the use of Semaphores.

Fred
0 Kudos
Reply

3,414 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Superfred on Fri Jan 02 06:11:49 MST 2015
Hi Jantje,

as promised I made an example for you.
It is not very well commented and the code style is maybe not very good (I learned programming myself), but it is maybe still helpful for you.

To use it:
Download the zipfile in the attachment.
Create a new folder and let LPCXpresso point on it (switch workspace).
Then klick on "Import Project(s) on the Quickstart panel.
Use the upper "Browse" Button to select the downloaded zipfile.
Press Next & Finish.

Everything is under "SimpleDemo" Object in the Object Explorer, select it and Build & Debug.

This example uses RTOS to read the AD-Channel 0 and Sends the data out on UART3/P0.2 (115200/N/8/1).
It beeps (Timer0/P1.28).
It receives Data from UART3, sends it out again and beeps.
It blinks the onboard LED.
Please notice that I am using Semaphores or RTOS Delays whenever the Program waits, so the system is very fast although we are using 4 tasks.

I don't want to describe much here, check it and ask if something is unclear.
Fred
0 Kudos
Reply

3,414 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Jantje7600 on Thu Jan 01 12:57:49 MST 2015
Hello Fred,

I plan to use it for my circuit and compare the read analog value with a reference value to control the circuit. Thanks, you have helped me a lot.

One last question, in Arduino I used the serial monitor to view the ADC value that was read. Can you please tell me the steps to simply read the value of x? (I have never used UART btw)

As for the other examples, it would be very great if you could share them because that would mean I have more practice material.
0 Kudos
Reply

3,414 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Superfred on Wed Dec 31 03:34:11 MST 2014
Hi Jantje,

sorry, I forgot to include some parts of the code (the #define... section).
That's why feedback is so important.
Here is the (hopefully) correct version :

#define ADC_OFFSET          0x10
#define ADC_INDEX           4
#define ADC_DONE            0x80000000
#define ADC_OVERRUN         0x40000000
#define ADC_ADINT           0x00010000

/*****************************************************************************
** Function name:        ADCInit
**
** Descriptions:        initialize ADC channel
**
** parameters:            ADC clock rate
** Returned value:        None
**
*****************************************************************************/
void ADCInit( uint32_t ADC_Clk )
{
  /* Enable CLOCK into ADC controller */
  LPC_SC->PCONP |= (1 << 12);


  /* all the related pins are set to ADC inputs, AD0.0~7 */
  LPC_PINCON->PINSEL0 &= ~0x000000F0;    /* P0.2~3, A0.6~7, function 10 */
  LPC_PINCON->PINSEL0 |= 0x000000A0;
  LPC_PINCON->PINSEL1 &= ~0x003FC000;    /* P0.23~26, A0.0~3, function 01 */
  LPC_PINCON->PINSEL1 |= 0x00154000;
  LPC_PINCON->PINSEL3 |= 0xF0000000;    /* P1.30~31, A0.4~5, function 11 */
  /* No pull-up no pull-down (function 10) on these ADC pins. */
  LPC_PINCON->PINMODE0 &= ~0x000000F0;
  LPC_PINCON->PINMODE0 |= 0x000000A0;
  LPC_PINCON->PINMODE1 &= ~0x003FC000;
  LPC_PINCON->PINMODE1 |= 0x002A8000;
  LPC_PINCON->PINMODE3 &= ~0xF0000000;
  LPC_PINCON->PINMODE3 |= 0xA0000000;

  /* By default, the PCLKSELx value is zero, thus, the PCLK for
  all the peripherals is 1/4 of the SystemFrequency. */
  /* Bit 24~25 is for ADC */



  LPC_ADC->ADCR = ( 0x01 << 0 ) |  /* SEL=1,select channel 0~7 on ADC0 */
        ( ( 25000000 / ADC_Clk - 1 ) << 8 ) |  /* CLKDIV = Fpclk / ADC_Clk - 1 */
        ( 0 << 16 ) |         /* BURST = 0, no BURST, software controlled */
        ( 0 << 17 ) |          /* CLKS = 0, 11 clocks/10 bits */
        ( 1 << 21 ) |          /* PDN = 1, normal operation */
        ( 0 << 24 ) |          /* START = 0 A/D conversion stops */
        ( 0 << 27 );        /* EDGE = 0 (CAP/MAT singal falling,trigger A/D conversion) */

  return;
}

/*****************************************************************************
** Function name:        ADCRead
**
** Descriptions:        Read ADC channel
**
** parameters:            Channel number
** Returned value:        Value read 
**
*****************************************************************************/
uint32_t ADCRead( uint8_t channelNum )
{
  uint32_t regVal, ADC_Data;

  /* channel number is 0 through 7 */

  LPC_ADC->ADCR &= 0xFFFFFF00;
  LPC_ADC->ADCR |= (1 << 24) | (1 << channelNum);
                /* switch channel,start A/D convert */

  while ( 1 )            /* wait until end of A/D convert */
  {
    regVal = LPC_ADC->ADGDR;
    /* read result of A/D conversion */
    if ( regVal & ADC_DONE )
    {
      break;
    }
  }

  LPC_ADC->ADCR &= 0xF8FFFFFF;    /* stop ADC now */
  if ( regVal & ADC_OVERRUN )    /* save data when it's not overrun, otherwise, return zero */
  {
    return ( 0 );
  }
  ADC_Data = ( regVal >> 4 ) & 0xFFF;
  return ( ADC_Data );    /* return A/D conversion value */
}


int main(void)
{
   ADCInit(13000000); //13 MHZ clock rate
    while(1)
    {
         int x;
         x = ADCRead(0);
    }
}


Note that this code sets AD0.0 to AD0.7 as analog input. You have to modify the PINCON-Commands if you don't need all of them set into this mode.

And for the compiler warning:
It says simply that you don't do anything with variable x. If for example send it out on a serial (UART) interface the warning wil disappear.

For the other examples: do you plan to use RTOS? This depends on what you are planning to do with the microcontroller. But  in common I strongly recommend to use it.
Fred.




0 Kudos
Reply

3,414 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Jantje7600 on Tue Dec 30 12:32:15 MST 2014
The ADC Done bit is bit 31 of ADGDR (AD0GDR) so I have to use #define ADC_DONE (1 << 31) and OVERRUN (1<<30) right?
0 Kudos
Reply

3,414 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Jantje7600 on Tue Dec 30 11:09:39 MST 2014
That would be great if you could do that because bit by bit I'm learning it (and this kind of code will help me tremendously).

So about the ADC:

I made a new folder, I imported a project and I selected CMSISv2p00_LPC17xx, I made a new C project and when it asked "CMSIS-Core Library to link project to:" I selected CMSISv2p00_LPC17xx and then clicked next and then finish.

In the src folder I have the ADCZ.c (I named it like that), crstartup_lpc175x_6x.c and crp.c

In ADCZ.c I have copied your code and when I build it I get three errors:

1. 'ADC_DONE' undeclared (first use in this function)
2.  'ADC_OVERRUN' undeclared (first use in this function)
3. *** [src/ADCZZZ.o] Error 1

and a warning

4.  warning: variable 'x' set but not used [-Wunused-but-set-variable]

Do you know how I can solve this?

And also, the value of the analog read conversion is stored in x so how can I read the value x in LPCXpresso? In Arduino I used the serial monitor but I don't know how to do it here.

Thanks in advance !
0 Kudos
Reply

3,414 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Superfred on Tue Dec 30 08:02:23 MST 2014
Hello,

you need  to import CMSISv2p00_LPC17xx  for my AD-driver.
And as Zero noted correctly, the init call should be:
ADCInit(13000000);


If you need more help please tell me, I can send you also 1769 + CMSISv2 examples for:
RTOS
ADC + RTOS
SPI
I2C + EEPROM on EA-Module
RFM70/73 radio module
Serial

Fred
0 Kudos
Reply

3,414 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Jantje7600 on Mon Dec 29 09:39:41 MST 2014
Thank you both of you. Especially Fred because that was exactly what I was looking for. I will test it for sure. The examples in LPCOpen are a little bit too complicated for me as a newbie.
0 Kudos
Reply

3,414 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Superfred on Mon Dec 29 04:05:11 MST 2014
Hello,

have a look on this thread for an example without lpcopen:
http://www.lpcware.com/content/forum/how-to-convert-analog-to-digital-using-lpc1769-processor

Fred
0 Kudos
Reply

3,414 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by 1234567890 on Sun Dec 28 13:09:26 MST 2014
Assuming you use a LPCXpresso board it's that easy: Download (or simply use the import wizzard...) http://www.lpcware.com/system/files/lpcopen_2_10_lpcxpresso_nxp_lpcxpresso_1769.zip and import it to your workspace.
There are lots of examples, e.g. periph_adc for this special question.
0 Kudos
Reply