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.

TM4C129ENCPDT: Read Accelerometer ADC.

Part Number: TM4C129ENCPDT

Hi, when I'm trying to read the sensors on my board the following code works well to read the joystick values and the microphone. But it doesn't work for reading the accelerometers. Even though accelerometers are on x = PE0, y = PE1, z = PE2. And PE0 maps to CH3, PE1 = CH2 and PE2 = CH1.

See code below, thanks in advance.

#define JOYSTICKX 1
#define JOYSTICKY 2
#define MICROPHONE 3
#define ACCELX 4
#define ACCELY 5
#define ACCELZ 6

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOE)) {}

SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_ADC0)) {}

GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_4); //Joystick Y
GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_3); //Joystick X
GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_0); //Accelerometer X

ADCSequenceConfigure(ADC0_BASE, JOYSTICKX, ADC_TRIGGER_PROCESSOR, 0);
ADCSequenceStepConfigure(ADC0_BASE, JOYSTICKX, 0, ADC_CTL_IE | ADC_CTL_END | ADC_CTL_CH9); //Joystick X
ADCSequenceEnable(ADC0_BASE, JOYSTICKX);

ADCSequenceConfigure(ADC0_BASE, JOYSTICKY, ADC_TRIGGER_PROCESSOR, 0);
ADCSequenceStepConfigure(ADC0_BASE, JOYSTICKY, 0, ADC_CTL_IE | ADC_CTL_END | ADC_CTL_CH0); //Joystick Y
ADCSequenceEnable(ADC0_BASE, JOYSTICKY);

ADCSequenceConfigure(ADC0_BASE, ACCELX, ADC_TRIGGER_PROCESSOR, 0);
ADCSequenceStepConfigure(ADC0_BASE, ACCELX, 0, ADC_CTL_IE | ADC_CTL_END | ADC_CTL_CH3); //AccelX
ADCSequenceEnable(ADC0_BASE, ACCELX);


ADCProcessorTrigger(ADC0_BASE, JOYSTICKX);
while(!ADCIntStatus(ADC0_BASE, JOYSTICKX, false)){}
ADCSequenceDataGet(ADC0_BASE, JOYSTICKX, &x);

ADCProcessorTrigger(ADC0_BASE, JOYSTICKY);
while(!ADCIntStatus(ADC0_BASE, JOYSTICKY, false)){}
ADCSequenceDataGet(ADC0_BASE, JOYSTICKY, &y);

ADCProcessorTrigger(ADC0_BASE, ACCELZ);
while(!ADCIntStatus(ADC0_BASE, ACCELZ, false)){}
ADCSequenceDataGet(ADC0_BASE, ACCELZ, &accZ);

  • I don't know how to edit my post, but I'm triggering the processor on ACCELX on line 39-41, i pasted the wrong code.

  • Hi,

      You have the below line. 

    GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_0); //Accelerometer X

    If you are going to take sample on PE2, PE1 and PE0 then you need to change to:

    GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_2 | GPIO_PIN_1 | GPIO_PIN_0);

    Another thing I see in you code is as follows:

    ADCSequenceConfigure(ADC0_BASE, JOYSTICKX, ADC_TRIGGER_PROCESSOR, 0);
    ADCSequenceStepConfigure(ADC0_BASE, JOYSTICKX, 0, ADC_CTL_IE | ADC_CTL_END | ADC_CTL_CH9); //Joystick X
    ADCSequenceEnable(ADC0_BASE, JOYSTICKX);

    ADCSequenceConfigure(ADC0_BASE, JOYSTICKY, ADC_TRIGGER_PROCESSOR, 0);
    ADCSequenceStepConfigure(ADC0_BASE, JOYSTICKY, 0, ADC_CTL_IE | ADC_CTL_END | ADC_CTL_CH0); //Joystick Y
    ADCSequenceEnable(ADC0_BASE, JOYSTICKY);

    ADCSequenceConfigure(ADC0_BASE, ACCELX, ADC_TRIGGER_PROCESSOR, 0);
    ADCSequenceStepConfigure(ADC0_BASE, ACCELX, 0, ADC_CTL_IE | ADC_CTL_END | ADC_CTL_CH3); //AccelX
    ADCSequenceEnable(ADC0_BASE, ACCELX);

    First you make CH9 as the only channel to sample in the sequencer 0 and also make it the last channel in the sequencer. Later you make CH0 which overrides the previous setting. Later you make CH3 the last channel again for sequencer 0 and use CH3 conversion complete to trigger interrupt. They just overwrite the previous setting. 

    If you want to take sample for 3 channels using sequencer 0 then you will do something as follows. Below is only an example. 

    GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_3 | GPIO_PIN_2 | GPIO_PIN_1);

    ADCSequenceConfigure(ADC0_BASE, 0, ADC_TRIGGER_PROCESSOR, 0);

    ADCSequenceStepConfigure(ADC0_BASE, 0, 0, ADC_CTL_CH0 );  // CH0 is the step 0 of sequencer 0

    ADCSequenceStepConfigure(ADC0_BASE, 0, 1, ADC_CTL_CH1 ); // CH1 is the step 1 of sequencer 0

    ADCSequenceStepConfigure(ADC0_BASE, 0, 2, ADC_CTL_CH2 | ADC_CTL_IE | ADC_CTL_END); //CH2 is the step 2 of sequencer 0. CH2 is the last channel in sequencer 0                                                                                                                                                                   // and use the CH2 conversion complete to trigger an interrupt.

    ADCSequenceEnable(ADC0_BASE, 0);

  • Thank you so much. I'm only getting it to partially work for now. But doing progress.


    This code works. But when I instead try to change the ADCSequenceStepConfigure end to the microphone (CH8) it stops working.

    ADCSequenceConfigure(ADC0_BASE, 0, ADC_TRIGGER_PROCESSOR, 0);
    ADCSequenceStepConfigure(ADC0_BASE, 0, 0, ADC_CTL_CH0); //Joystick Y
    ADCSequenceStepConfigure(ADC0_BASE, 0, 1, ADC_CTL_CH1); //AccelZ
    ADCSequenceStepConfigure(ADC0_BASE, 0, 2, ADC_CTL_CH2); //AccelY
    ADCSequenceStepConfigure(ADC0_BASE, 0, 3, ADC_CTL_IE | ADC_CTL_END | ADC_CTL_CH3); //AccelX
    //ADCSequenceStepConfigure(ADC0_BASE, 0, 4, ADC_CTL_IE | ADC_CTL_END | ADC_CTL_CH8); //Microphone
    ADCSequenceEnable(ADC0_BASE, 0);
    
    
    ADCProcessorTrigger(ADC0_BASE, 0);
    while(!ADCIntStatus(ADC0_BASE, 0, false)){}
    ADCSequenceDataGet(ADC0_BASE, 0, &y);
    ADCSequenceDataGet(ADC0_BASE, 1, &accZ);
    ADCSequenceDataGet(ADC0_BASE, 2, &accY);
    ADCSequenceDataGet(ADC0_BASE, 3, &accX);
    //ADCSequenceDataGet(ADC0_BASE, 4, &mic);


  • Hi,

     You want to use CH8 for microphone as the last channel in the sequencer. Be aware that CH8 is on PE5 pin. Did you make sure you add PE5?

    GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_5 | GPIO_PIN_3 | GPIO_PIN_2 | GPIO_PIN_1 | GPIO_PIN_0);

  • Hi,

    Yes I've added GPIO_PIN_5. Below is my setup, and these are all the peripherals I want to use.

    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
    while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOE)) {}
    
    SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
    while(!SysCtlPeripheralReady(SYSCTL_PERIPH_ADC0)) {}
    
    GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_4); //Joystick Y
    GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_3); //Joystick X
    GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_0); //AccelX
    GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_1); //AccelY
    GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_2); //AccelZ
    GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_5); //Microphone
    
    
    ADCSequenceConfigure(ADC0_BASE, 0, ADC_TRIGGER_PROCESSOR, 0);
    ADCSequenceStepConfigure(ADC0_BASE, 0, 0, ADC_CTL_CH0); //Joystick Y
    ADCSequenceStepConfigure(ADC0_BASE, 0, 1, ADC_CTL_CH1); //AccelZ
    ADCSequenceStepConfigure(ADC0_BASE, 0, 2, ADC_CTL_CH2); //AccelY
    ADCSequenceStepConfigure(ADC0_BASE, 0, 3, ADC_CTL_CH3); //AccelX
    ADCSequenceStepConfigure(ADC0_BASE, 0, 4, ADC_CTL_CH8); //Microphone
    ADCSequenceStepConfigure(ADC0_BASE, 0, 5, ADC_CTL_IE | ADC_CTL_END | ADC_CTL_CH9); //Joystick X
    ADCSequenceEnable(ADC0_BASE, 0);


    When I run this:

     
    ADCProcessorTrigger(ADC0_BASE, 0);
    while(!ADCIntStatus(ADC0_BASE, 0, false)){}
    ADCSequenceDataGet(ADC0_BASE, 0, &y);
    ADCSequenceDataGet(ADC0_BASE, 1, &accZ);
    ADCSequenceDataGet(ADC0_BASE, 2, &accY);
    ADCSequenceDataGet(ADC0_BASE, 3, &accX);
    ADCSequenceDataGet(ADC0_BASE, 4, &mic);
    ADCSequenceDataGet(ADC0_BASE, 5, &x);


    The output is:

    Joystick X: 12000000
    Joystick Y: 2101
    MIC : 12000000
    Accel Z: 2087
    Accel X: 2891
    Accel Y: 2064

    And when I run this (uncomment row 7-8):

    ADCProcessorTrigger(ADC0_BASE, 0);
    while(!ADCIntStatus(ADC0_BASE, 0, false)){}
    ADCSequenceDataGet(ADC0_BASE, 0, &y);
    ADCSequenceDataGet(ADC0_BASE, 1, &accZ);
    ADCSequenceDataGet(ADC0_BASE, 2, &accY);
    ADCSequenceDataGet(ADC0_BASE, 3, &accX);
    ADCSequenceDataGet(ADC0_BASE, 4, &mic);
    ADCSequenceDataGet(ADC0_BASE, 5, &x);
    


    Joystick X: 0
    Joystick Y: 0
    MIC : 0
    Accel Z: 0
    Accel X: 0
    Accel Y: 0


    I heard something about FIFO queues? Is my sequence too long?

  • ADC module only support 4 sequencers. 

    See below usage of ADCSequenceDataGet. The second argument is the sequencer number. All of your channels are in the same sequencer 0. There is no sequencer 4 or 5. 

  • Thank you, I will take a look at it tomorrow and try to get it to work. You have been extremely helpful and I appreciate it very much.