Jump to content
L.R.A

ADC peripheral has alot of fuctions

Recommended Posts

Hi all,

 

Thanks for all the info, I had allready had a look at the datasheet of the Tm4C1294 and I have seen others trying to use the comparators. But it seems nobody thusfar has an example of using the comparator in Energia. If I am not wrong - please correct me if I am - the Tivaware collection of examples is ment to be used in CCS. I have had a brief afair using CCS with the older TIVA LM4F120 but that wasnt really inspiring. For my MSP430 devices I use CCS solely but they are also simpler in many ways. 

 

Ill keep looking, thanks again for the info.

 

cheers

Cor

Share this post


Link to post
Share on other sites

The example can be easily adapted to Energia, most of the times by just adding void setup(){} and void loop(){} 

 

I have the example here if you want to check it out. Just tought this was a good way for you to see into tivaware with energia since this is a simpler task than most peripherals. 

 

i'll attach it here if you want to check it out later

Share this post


Link to post
Share on other sites

Hello,

 

Ive just started testing the code on an LM4F120. To start exploring/ changing I added a few println statements to follow the proces. Oddly enough the 1st and 2nd println statement in the setup section do not show up in the serialmonitor. The beep signal from the loop comes every second as planned.

 

Next up will be to change the adcinterrupt to follow the voltage on PE3.

 

cheers

Cor

void setup()
{
  Serial.begin(9600);
  // put your setup code here, to run once:
  
  SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
  Serial.println("starting" );                                <<<<< NEVER SHOWS UP

  ADCIntEnable(ADC0_BASE,0);                       

  ADCComparatorRegionSet(ADC0_BASE,0 , 500, 3500);
  ADCComparatorConfigure(ADC0_BASE,0,ADC_COMP_INT_LOW_HALWAYS);
  ADCComparatorIntEnable(ADC0_BASE,0);
  ADCIntRegister(ADC0_BASE,0,ADCinterrupt);
  Serial.println("started");                                <<<< NEVER SHOWS UP

}

void loop()
{
  // put your main code here, to run repeatedly:
   Serial.println("beep" );                       


  // wait 10 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(1000);                     

}

Share this post


Link to post
Share on other sites

Hi,

 

Ive got the code working after changing it rather radically based on information from this webpage

http://e2e.ti.com/support/microcontrollers/stellaris_arm/f/471/p/45647/164197.aspx#164197

#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_memmap.h"
#include "driverlib/adc.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"

//
// Global variable to count the number of digital comparator interrupts.  This
// variable is not needed for the proper operation of the ADC or digital
// comparators.
//
//*****************************************************************************
unsigned long g_ulNumOfInt;

//*****************************************************************************
//
// Global variable to count the number of digital Comparator interrupts that
// have been triggered.  This variable is not needed for the proper operation
// of the ADC or digital comparators.
//
//*****************************************************************************
unsigned long g_ulDCIntStatus;

void ADC0IntHandler()
{
    //
    // Read the digital comparator interrupt status register.  This global
    // variable will keep track of which digital comparator triggered the
    // interrupt.
    //
    g_ulDCIntStatus = ADCComparatorIntStatus(ADC0_BASE);

    //
    // Clear all of the digital comparator interrupt bits.  We do this because
    // more than one digital comparator interrupt may have been triggered this
    // interrupt.
    //
    ADCComparatorIntClear(ADC0_BASE, 0x0F);

    //
    // Increment the variable that keeps track of how many interrupts have
    // been triggered.
    //
    g_ulNumOfInt++;   
}



void setup()
{
  Serial.begin(9600);
  // put your setup code here, to run once:
  
  SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
  
  //
    // For this example ADC0 is used with AIN10 on Port B pin 4.  The actual
    // port and pins used may be different on your part, consult the data sheet
    // for more information.  GPIO port B needs to be enabled so these pins can
    // be used.
    // TODO: change this to whichever GPIO port you are using.
    //
  SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
                                     
  //
    // Select the analog ADC function for these pins.  Consult the data sheet
    // to see which functions are allocated per pin.
    // TODO: change this to select the port/pin you are using.
    //                                   
  GPIOPinTypeADC(GPIO_PORTB_BASE, GPIO_PIN_4); //Configure GPIO B4 as ADC
  
    //
    // Enable processor interrupts.
    //
//  IntMasterEnable();

    //
    // Enable the ADC sample sequence 0 interrupt on the processor (NVIC).
    //
//  IntEnable(INT_ADC0);

  ADCReferenceSet(ADC0_BASE, ADC_REF_INT); //Set reference to the internal 3v reference

  //
    // Enable sample sequence 0 with a continuous signal trigger and a priority
    // of zero.  Sequence 0 will sample continuouly until the program is
    // halted.
    //

  ADCSequenceConfigure(ADC0_BASE, 0, ADC_TRIGGER_ALWAYS, 0);
  
  // Configure step 0 on sequence 0.  Sample channel 10 (ADC_CTL_CH10) in
    // single-ended mode (default) and send the ADC sample to digital
    // comparator 0 (ADC_CTL_CMP0).  NOTE: When you send the ADC sample to the
    // digital comparator, you will no longer be able to read the raw ADC data
    // on that specific step.  The option is send the data to the FIFOs OR to
    // the digital comparator.  Sequence 0 has 8 programmable steps, sequence 1
    // and 2 have 4 programmable steps, and sequence 3 has 1 programmable step.
    //   
    
  ADCSequenceStepConfigure(ADC0_BASE, 0, 0, ADC_CTL_CH10 | ADC_CTL_CMP0);

    //
    // connect the ADC to an interrupt
    //
   
    ADCIntRegister(ADC0_BASE, 0, ADC0IntHandler); 
    //
    // Since sample sequence 0 is now configured, it must be enabled.
    //
   
    ADCSequenceEnable(ADC0_BASE, 0);
   
  // Configure the ADC digital comparator. 
    //
    ADCComparatorConfigure(ADC0_BASE, 0, ADC_COMP_INT_LOW_HONCE);
  //
    // Define the low-band and high-band for the digital comparators 0
    // The low-band is defined as anything less or equal to ADC_VALUE_0_95V,
    // the mid-band is defined as anything greater than ADC_VALUE_0_95V and
    // less or equal to ADC_VALUE_1_05V, and the high-band is defined as
    // anything greater than ADC_VALUE_1_05V.  Note that the maximum value that
    // the low-band reference or high-band reference can be for the 10-bit ADCs
    // is 1023 and that the low-band reference has to be less than or equal to
    // the high-band reference.
    //
    ADCComparatorRegionSet(ADC0_BASE, 0, 300, 400);
   g_ulNumOfInt = 0; 
   
   //
    // Clear all the interrupts and and triggers for digital comparators 0 to
    // 3.  The "true" parameters clear the the trigger and interrupt flags.
    //
    ADCComparatorReset(ADC0_BASE, 0, true, true);
    
  Serial.println("starting sampling" );
//
    // Enable the digital comparator interrupts on sample sequence 0.
    //
    ADCComparatorIntEnable(ADC0_BASE, 0);    

   ADCIntEnable(ADC0_BASE, 0); 

    
  Serial.println("leaving setup" );                       

}

void loop()
{
  unsigned long ulADC0_Value[8];

  // put your main code here, to run repeatedly:
   Serial.print(g_ulNumOfInt );                       

  Serial.print(" ");
  ADCSequenceDataGet(ADC0_BASE, 0, ulADC0_Value);
  Serial.println(ulADC0_Value[0]);
 
 delay(1000);   
}

Share this post


Link to post
Share on other sites

The second code, with the adc comparator, i sent it to a friend since i realy didn't have anytigh analog to test with besides the internal temp sensor. he said it work. I say i'm going to throw someone out of a window.

 

Thank you @@CorB

Share this post


Link to post
Share on other sites

Thanks for this! The hardware oversample is a very cool feature, I'll be using it in the future I think as it simplifies my code significantly. No more sampling loops, for starters. Or a lot fewer at least.

Share this post


Link to post
Share on other sites

Hi 

 

The code I just shared (which is just an adaptation of code I found on the internet) doesnt oversample, Maybe someone can tell where the oversample commands should be inserted in the code ?

 

cheers

Cor

Share this post


Link to post
Share on other sites

So after the line 

ADCSequenceStepConfigure(ADC0_BASE, 0, 0, ADC_CTL_CH10 | ADC_CTL_CMP0);

I should insert 

ADCHardwareOversampleConfigure(ADC0_BASE,64);

 

Cor 

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.


×
×
  • Create New...