This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Stellaris LM4F120XL assigning ADC to GPIO

Hi,

I am trying to configure the GPIO to provide inputs to the ADC. My GPIO initialisation code is below. My initialisation works for some pins but, for example, GPIO port D PIN 0 ends up with a voltage on it after initialisation.

I have no idea why this should be happening. The code looks fine to me.

Anyone any ideas?

I'm using CCS 5.

Thanks,

Crawford

#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/rom.h"
#include "driverlib/pin_map.h"

void GPIOinit2(void)
{

ROM_SysCtlPeripheralEnable (SYSCTL_PERIPH_GPIOD);//enable GPIO-D peripheral

ROM_GPIOPinTypeADC (GPIO_PORTD_BASE, GPIO_PIN_0 |GPIO_PIN_1 |GPIO_PIN_2 | GPIO_PIN_3);


ROM_SysCtlPeripheralEnable (SYSCTL_PERIPH_GPIOE);//enable GPIO-E peripheral
ROM_GPIOPinTypeADC (GPIO_PORTE_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5);


ROM_SysCtlPeripheralEnable (SYSCTL_PERIPH_GPIOF);//enable GPIO-F peripheral


ROM_GPIOPinTypeGPIOOutput (GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4);


// --------------------Configure PB6 as T0CCP0 for PWM---------------------
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
ROM_GPIOPinConfigure(GPIO_PB6_T0CCP0);
ROM_GPIOPinTypeTimer(GPIO_PORTB_BASE, GPIO_PIN_6);

}

  • Quickly - several things:

     a) appears you've correctly enabled the ports bearing potential ADC channels - but never enable the ADC Peripheral itself!  (i.e. SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);  // one suspects ADC0 is reasonable choice

    b) #include "driverlib/adc.h" should make some appearance - its absence notable...

    c) ADC seeks 16MHz System Clock - minimum.  Are you in compliance?

    d) your early code does not yet, "ADCSequenceConfigure(), nor ADCSequenceStepConfigure(), nor ADCSequenceEnable()" - all likely required to properly transact w/the ADC.

    Such "mixed signal devices" (your MCU) have a fairly complex input circuit arrangement in which a "holding capacitor" charges w/ sampling voltage - which may result from any/all channels which see a voltage introduction.  Thus - your report of some "voltage presence" - upon a "potential" ADC input pin - may not be cause for alarm.

    Perhaps a better test would be to complete the ADC initialization process.   (single-ended.c) w/in Examples\Peripherals\ADC\ (my suggestion)  Insure that any/all external voltages you apply are w/in ADC input min/max range - and ideally arise/fall with application of proper power to your MCU...

  • Thanks, but I initialise the ADC in another part of code before I initialise the GPIO.

    int main(void) {
    State = Waiting;

    // Enable lazy stacking for interrupt handlers. This allows floating-point
    // instructions to be used within interrupt handlers, but at the expense of
    // extra stack usage.

    ROM_FPULazyStackingEnable ();
    ROM_FPUEnable ();

    //Set up system clock to run at 50MHz
    SysClockInit();

    //--Initialise screen and display test image
    ScreenInit();

    //initialise UART0
    UARTinit();

    //initialise ADC0
    ADCinit();

    //initialise GPIO peripheral
    GPIOinit2();

    With GPIO PD0 it's as if there is already a voltage on it before initialisation.

    I have just noticed that 3.3V appears on the PD0 pin after I initialise the UART. The code for the UART initialisation is below. I don't see how this should affect pin PD0.

    It's all very confusing.

    #include "inc/hw_types.h"
    #include "inc/hw_ints.h"
    #include "inc/hw_memmap.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/gpio.h"
    #include "driverlib/rom.h"
    #include "utils/uartstdio.h"
    #include "inc/hw_uart.h"
    #include "driverlib/interrupt.h"
    #include "driverlib/uart.h"
    extern char OutBuf[80];
    #define byte unsigned char
    extern byte OutPtr;

    //#define GPIO_PA0_U0RX 0x00000001
    //#define GPIO_PA1_U0TX 0x00000401

    void UARTinit(void)
    {

    //initialise UART0
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

    GPIOPinConfigure(GPIO_PA0_U0RX);
    GPIOPinConfigure(GPIO_PA1_U0TX);
    GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    SysCtlPeripheralEnable (SYSCTL_PERIPH_GPIOF);
    UARTConfigSetExpClk (UART0_BASE, ROM_SysCtlClockGet (), 115200,
    (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE));

    UARTFIFOLevelSet(UART0_BASE, UART_FIFO_TX4_8, UART_FIFO_RX4_8); // FIFO 8 chars
    UARTFIFOEnable(UART0_BASE); //enable FIFOs

    IntEnable(INT_UART0); //enable UART interrupt

    UARTIntEnable(UART0_BASE, UART_INT_TX);

    UARTStdioInit(0); //tells uartstdio functions to use UART0
    }

  • You are aware (or now will be) that certain launchpads tied 2 pairs of MCU pins together!  (this via 2, 0-ohm resistors)  Review of your board schematic will detail.  (we don't like/use those boards - especially w/that "default" board behavior)

    Your quick/detailed observation strongly suggests that cross-connect - indeed - is your issue.  Good finding - your part.

    Your preinit of ADC was not mentioned in initial post - caused needless "wheelspin..."

  • No I wasn't aware of this.

    I recant my last note about the UART. It's coming from the step before this, the Initialisation of the Display boosterpack. I'll need to see if the display is already using this pin for something.

    Thanks for your help.

    Crawford

  • Your board schematic - identifying the "cross pin" connections (2 of them) should not avoid your attention.

    Real thanks results from tick of Verify Answer...  (atop my earlier post - beneficial to future posters who "re-visit" your issue)

    Update: 10:56 CST - Verify duly received - acknowledged - and appreciated.  Merci mon ami...

  • Just to confirm the issue here. I was seeing a voltage on Pin PD0, because PD0 is connected to PB6 by a 0 ohm resistor and I am trying to use PB6 as a PWM output.

    PD1 is connected to PB7 in the same way, which is also an issue, but just wasn't showing itself.

    Furthermore, I am also using the Kentec display booster pack, that uses PB6, so double whammy there.

    I've removed the 0 ohm resistors and commented out the Display code. All seems fine now.

    Now for some re-jigging of pins and software...