How can I select the ADC input channel on LPC1769?

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

How can I select the ADC input channel on LPC1769?

Jump to solution
2,780 Views
csabakatona
Contributor II

Hi,

 

I'm trying to find out without any luck.

 

I started from periph_adc project, and I was able to read the AD0.0 (P0.23) without problem (the code is in the attachment).

 

Then I replaced the ADC_CHANNLE with

 ADC_CHANNLE_T myADC_CHANNLE

so I can change channel from AD_CH0 to AD_CH7.

Compiling this code it was running well only when myADC_CHANNLE = AD_CH0, else stucked in a while cycle.

Then modified set _LPC_ADC_ID (I saw similar method in a lot of example

_LPC_ADC_ID->CR |= 0x01;

hoping I could get AD_CH1 data, but the program get into this cycle:

void HardFault_Handler(void)
{ while(1) {}
}

 

I was searching a lot for the solution, but can not find more information.

 

Is there anybody could help me?

 

Best wishes!

Original Attachment has been moved to: lpcxpresso.txt.zip

Labels (3)
0 Kudos
1 Solution
1,827 Views
DavidS
NXP Employee
NXP Employee

Hi Csaba,

Kerry has good example and if helps that is great.

I reproduced your issue with the periph_adc example.

What I found was the board_sysinit.c Board_SetupMuxing() function is setting up a bunch of pins to default functions including setting up the ADC_CH0 to be used.

The adc.c code does not show that this has magically happened and therefore when you switch the _ADC_CHANNLE #define to something other than ADC_CH0 the desired pin is not configured correctly.

My example changes the to use ADC_CH1 in the adc.c main().  Look for "//DES" in comments.

The condensed code change is here:

#define _ADC_CHANNLE ADC_CH1 //DES was ADC_CH0

/* Chip_IOCON_PinMux(0, 25, IOCON_ADMODE_EN, IOCON_FUNC1); */ //DES old API
/*ADC Init */
#if (_ADC_CHANNLE == ADC_CH1) //DES ADC_CH0 is setup to ADC analog function in board_sysinit.c via the pinmuxing structure. When wanting different ADC channel we need to enable it.
Chip_IOCON_PinMux(LPC_IOCON, 0, 24, IOCON_MODE_INACT, IOCON_FUNC1); //DES new API....enabling ADC_CH1 on port 0 pin 24
#endif
Chip_ADC_Init(_LPC_ADC_ID, &ADCSetup);
Chip_ADC_EnableChannel(_LPC_ADC_ID, _ADC_CHANNLE, ENABLE);

If you want to setup the desired ADC_CH1 by default you could add the following line into the pinmuxing structure in board_sysinit.c like following:

/* Pin muxing configuration */
STATIC const PINMUX_GRP_T pinmuxing[] = {
{0, 0, IOCON_MODE_INACT | IOCON_FUNC2}, /* TXD3 */
{0, 1, IOCON_MODE_INACT | IOCON_FUNC2}, /* RXD3 */
{0, 4, IOCON_MODE_INACT | IOCON_FUNC2}, /* CAN-RD2 */
{0, 5, IOCON_MODE_INACT | IOCON_FUNC2}, /* CAN-TD2 */
{0, 22, IOCON_MODE_INACT | IOCON_FUNC0}, /* Led 0 */
{0, 23, IOCON_MODE_INACT | IOCON_FUNC1}, /* ADC 0 */
// {0, 24, IOCON_MODE_INACT | IOCON_FUNC1}, /* ADC 1 */ //DES uncomment to added ADC_CH1 as default pin function
{0, 26, IOCON_MODE_INACT | IOCON_FUNC2}, /* DAC */

Regards,

David 

View solution in original post

7 Replies
1,827 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Csaba,

     Thank you for your interest in NXP LPC product, I would like to provide service for you!

    

    From your description, the result should be the same as the code which you attached, this code is just from the official lpcopen code.

   

_LPC_ADC_ID->CR |= 0x01;
  This code is used to enable ADC channel 0, not channel 1.
  Did you try to get the result of ADC channel 1?
  Please check you code.
If you still have problem, please attached your modified code.
I will help you to check it.


Have a great day,
Kerry

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
1,827 Views
csabakatona
Contributor II

Dear Kerry,

Thank you for your reply.

I tried your suggestion, but the result is the same.

This is the code:

#include "board.h"
#include "chip.h"

#define _LPC_ADC_ID LPC_ADC

static ADC_CLOCK_SETUP_T ADCSetup;
static volatile uint8_t Burst_Mode_Flag = 0;

ADC_CHANNEL_T ADC_Ch = ADC_CH2;

int main(void)
{

    uint16_t dataADC;
    SystemCoreClockUpdate();
    Board_Init();

    Chip_ADC_Init(LPC_ADC, &ADCSetup);
    Chip_ADC_EnableChannel(LPC_ADC, ADC_Ch, ENABLE);
    //LPC_ADC->CR |= 2;//ADC_CR_CH_SEL(channel);

    if (Burst_Mode_Flag) {
        Chip_ADC_SetBurstCmd(LPC_ADC, ENABLE);
    }
    else {
        Chip_ADC_SetBurstCmd(LPC_ADC, DISABLE);
    }

    while (1) {
        if (!Burst_Mode_Flag) {
            Chip_ADC_SetStartMode(LPC_ADC, ADC_START_NOW, ADC_TRIGGERMODE_RISING);
        }
        while (Chip_ADC_ReadStatus(LPC_ADC, ADC_Ch, ADC_DR_DONE_STAT) != SET) {}
        Chip_ADC_ReadValue(LPC_ADC, ADC_Ch, &dataADC);

        if (Burst_Mode_Flag) {
            Chip_ADC_SetBurstCmd(_LPC_ADC_ID , DISABLE);
        }
    }

    return 0;
}

0 Kudos
1,827 Views
csabakatona
Contributor II

When I change ADC_Ch to ADC_CH0 I can read a valid value from dataADC.

But with any other value I get only 0.

This code enables the ADC channel with the CHIP_ADC_Enable procedure. If I use the LPC_ADC->CR |=2 then the program stucks in the loop containing the Chip_ADC_ReadStatus function.

Have you got an idea what I am doing wrong ?

Have a nice day!

0 Kudos
1,828 Views
DavidS
NXP Employee
NXP Employee

Hi Csaba,

Kerry has good example and if helps that is great.

I reproduced your issue with the periph_adc example.

What I found was the board_sysinit.c Board_SetupMuxing() function is setting up a bunch of pins to default functions including setting up the ADC_CH0 to be used.

The adc.c code does not show that this has magically happened and therefore when you switch the _ADC_CHANNLE #define to something other than ADC_CH0 the desired pin is not configured correctly.

My example changes the to use ADC_CH1 in the adc.c main().  Look for "//DES" in comments.

The condensed code change is here:

#define _ADC_CHANNLE ADC_CH1 //DES was ADC_CH0

/* Chip_IOCON_PinMux(0, 25, IOCON_ADMODE_EN, IOCON_FUNC1); */ //DES old API
/*ADC Init */
#if (_ADC_CHANNLE == ADC_CH1) //DES ADC_CH0 is setup to ADC analog function in board_sysinit.c via the pinmuxing structure. When wanting different ADC channel we need to enable it.
Chip_IOCON_PinMux(LPC_IOCON, 0, 24, IOCON_MODE_INACT, IOCON_FUNC1); //DES new API....enabling ADC_CH1 on port 0 pin 24
#endif
Chip_ADC_Init(_LPC_ADC_ID, &ADCSetup);
Chip_ADC_EnableChannel(_LPC_ADC_ID, _ADC_CHANNLE, ENABLE);

If you want to setup the desired ADC_CH1 by default you could add the following line into the pinmuxing structure in board_sysinit.c like following:

/* Pin muxing configuration */
STATIC const PINMUX_GRP_T pinmuxing[] = {
{0, 0, IOCON_MODE_INACT | IOCON_FUNC2}, /* TXD3 */
{0, 1, IOCON_MODE_INACT | IOCON_FUNC2}, /* RXD3 */
{0, 4, IOCON_MODE_INACT | IOCON_FUNC2}, /* CAN-RD2 */
{0, 5, IOCON_MODE_INACT | IOCON_FUNC2}, /* CAN-TD2 */
{0, 22, IOCON_MODE_INACT | IOCON_FUNC0}, /* Led 0 */
{0, 23, IOCON_MODE_INACT | IOCON_FUNC1}, /* ADC 0 */
// {0, 24, IOCON_MODE_INACT | IOCON_FUNC1}, /* ADC 1 */ //DES uncomment to added ADC_CH1 as default pin function
{0, 26, IOCON_MODE_INACT | IOCON_FUNC2}, /* DAC */

Regards,

David 

1,827 Views
csabakatona
Contributor II

Hi David,

Works like a charm! You are my savior!

Thank you very much!

0 Kudos
1,827 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Csaba Katona,

      I have attached a project which have the ADC code, it convert AD0_2, P0[25], this code works ok on my side, you can refer to it.

     You can refer to the ADC.c and ADC.h file.

   Please use this code on your side.

Wish it helps you!

If you still have question, please let me know!


Have a great day,
Kerry

 

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
1,827 Views
csabakatona
Contributor II

Dear Zhou,

Thank you very much for the project!

I tried to start it in Keil 5, it asked me to install legacy package. After that I restart Keil, the project was built but then it couldn't find some dll, so I could not try the code. I will try to find the reason later.

But thanks again for your code! 

0 Kudos