Jump to content
Sign in to follow this  
nesslersreagent

TivaC sample ADC code problem

Recommended Posts

Hi,

 

I've recently upgraded my Energia IDE to the latest release 0101E0011. I'm using the Stellaris Launchpad with lm4f120h5qr.
My sample ADC code doesn't seem to work right. Worked well in the previous release of Energia.

The code compiles properly but does not produce the right output.

The only changes made were to change "ADC_BASE" to "ADC0_BASE" as required.
 

I've added Serial output code for debugging purposes.

The serial output produced is:

1

2

3

Code:

#include "Energia.h"
#include "driverlib/sysctl.h"
#include "driverlib/adc.h"

void setup()
{
  Serial.begin(115200);
  
  Serial.println("1");
  //enable the adc0 peripherial.
  SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
  Serial.println("2");
  //set the speed to 1msps.
  SysCtlADCSpeedSet(SYSCTL_ADCSPEED_1MSPS);
  Serial.println("3");
  //set the auto avergage to 64.
  ADCHardwareOversampleConfigure(ADC0_BASE, 64);
  Serial.println("4");
  //before setting up I must disable the sequence 3.
  ADCSequenceDisable(ADC0_BASE, 3);
  Serial.println("5");
  //set the sequence to use (adc0 sequence 3).
  ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_PROCESSOR, 0);
  Serial.println("6");
  //set up the sequence step.
  //set up the last step and start an interrupt when the conversion it's over.
  ADCSequenceStepConfigure(ADC0_BASE, 3, 0, ADC_CTL_TS | ADC_CTL_IE | ADC_CTL_END);
  Serial.println("7");
  //enable the sequence again!
  ADCSequenceEnable(ADC0_BASE, 3);
  Serial.println("8");
}

void loop()
{
}

Share this post


Link to post
Share on other sites

Hi,

Two problems with your code:

1) First, you should have a GPIO peripheral enabled and a specific pin configured as analog input pin. 

2) In this function call
    ADCSequenceStepConfigure(ADC0_BASE, 3, 0, ADC_CTL_TS | ADC_CTL_IE | ADC_CTL_END);

the pin selected for conversion is still the internal temperature sensor (ADC_CTL_TS) - replace it with your analog pin declared above. Also, you don't need to declare ADC_CTL_IE if you do not use ADC interrupts.

 

L

Share this post


Link to post
Share on other sites

Hi,

I have modified the code as per Lyon's suggestions and it works! Thanks.

Also I've realised that the ADC speed must be changed before the ADC peripheral is enabled. This was the prime cause of all my troubles.

 

Output:

1

2

3

4

5

6

7

8

 

Code:

#include "Energia.h"
#include "driverlib/sysctl.h"
#include "driverlib/adc.h"

void setup()
{
  Serial.begin(115200);
  
  Serial.println("1");
  //set the speed to 1Msps.
  SysCtlADCSpeedSet(SYSCTL_ADCSPEED_1MSPS);
  //enable the adc0 peripherial.
  Serial.println("2");
  SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
  SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
  GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);
  Serial.println("3");
  //set the auto avergage to 64.
  ADCHardwareOversampleConfigure(ADC0_BASE, 64);
  Serial.println("4");
  //before setting up I must disable the sequence 3.
  ADCSequenceDisable(ADC0_BASE, 3);
  Serial.println("5");
  //set the sequence to use (adc0 sequence 3).
  ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_PROCESSOR, 0);
  Serial.println("6");
  //set up the sequence step.
  //set up the last step and start an interrupt when the conversion it's over.
  ADCSequenceStepConfigure(ADC0_BASE, 3, 0, ADC_CTL_CH0 | ADC_CTL_END);
  Serial.println("7");
  //enable the sequence again!
  ADCSequenceEnable(ADC0_BASE, 3);
  Serial.println("8");
}

void loop()
{
}

Share this post


Link to post
Share on other sites
#include "Energia.h"

#include "driverlib/sysctl.h"

#include "driverlib/adc.h"

#define A3 PE_0

 

 

void setup()

{

 

  Serial.begin(512000);

  

  SysCtlADCSpeedSet(SYSCTL_ADCSPEED_1MSPS);

  

  SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);

  

  SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);

  

  GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_0);

  

  ADCHardwareOversampleConfigure(ADC0_BASE, 64);

  

  ADCSequenceDisable(ADC0_BASE, 3);

  

  ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_PROCESSOR, 0);

  

  ADCSequenceStepConfigure(ADC0_BASE, 3, 0, ADC_CTL_CH0 | ADC_CTL_END);

  

  ADCSequenceEnable(ADC0_BASE, 3);

  

}

 

void loop()

{

  int f;

  

  ADCProcessorTrigger(ADC0_BASE, 3);

  

  f = analogRead(A3);

 

  Serial.write(f);

  

}

 

thanks for answering my question...

 

i modified a bit of his code, and placed analogRead() in place of AnalogSequenceGet(), so is my pin for analogRead() right? Since I assumed that PE_0 is the ADC pin used...

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...