Jump to content
L.R.A

ADC peripheral has alot of fuctions

Recommended Posts

The ADC peripheral has alot of useful features so i tough i show some.

 

For example the ADC does an oversample by hardware so you don't have to do it by software, you can chose a potency of 2 until 64 (2,4,8,16,32,64):


#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"


uint32_t valor[1]={
  0}; 
float temperatura;

void setup()
{
  Serial.begin(9600);
  // put your setup code here, to run once:
  SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
}

void loop()
{

  ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_PROCESSOR, 0);
  ADCSequenceStepConfigure(ADC0_BASE, 3, 0, ADC_CTL_TS | ADC_CTL_IE |ADC_CTL_END);
  ADCHardwareOversampleConfigure(ADC0_BASE,64); //here you chose how many oversamples
  ADCSequenceEnable(ADC0_BASE, 3);  
  ADCIntClear(ADC0_BASE, 3);
  ADCProcessorTrigger(ADC0_BASE, 3);
  while(!ADCIntStatus(ADC0_BASE, 3, false))
  {
  }  
  ADCIntClear(ADC0_BASE, 3);
  ADCSequenceDataGet(ADC0_BASE, 3, valor);
  ADCSequenceDisable(ADC0_BASE, 3);
  temperatura =  (1475.0 - ((2475.0 * valor[0])) / 4096.0)/10.0;
  Serial.println(temperatura);
  
  delay(100);
  // put your main code here, to run repeatedly:

}

Share this post


Link to post
Share on other sites

Second code,

 

There's probably alot of times you had a analog sensor and you wanted to check when it's values were below or higher than a certain value. In robotics i had this problem so i didn't need to check the ADC and find useless values.

 

So Tiva has a comparator in the ADC wich is configurable to generate a interrupt. 

 

I have this code made but i still would like to run some more tests 

#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"



uint32_t valor[1]={
  0}; 
float temperatura;


void ADCinterrupt(){
  ADCComparatorIntClear(ADC0_BASE,0);
  
  ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_PROCESSOR, 0);
  ADCSequenceStepConfigure(ADC0_BASE, 3, 0, ADC_CTL_TS | ADC_CTL_IE |ADC_CTL_END);
  ADCHardwareOversampleConfigure(ADC0_BASE,64);
  ADCSequenceEnable(ADC0_BASE, 3);  
  ADCIntClear(ADC0_BASE, 3);
  ADCProcessorTrigger(ADC0_BASE, 3);
  while(!ADCIntStatus(ADC0_BASE, 3, false))
  {
  }  
  ADCIntClear(ADC0_BASE, 3);
  ADCSequenceDataGet(ADC0_BASE, 3, valor);
  ADCSequenceDisable(ADC0_BASE, 3);
  temperatura =  (1475.0 - ((2475.0 * valor[0])) / 4096.0)/10.0;
  Serial.println(temperatura);  
  
}



void setup()
{
  Serial.begin(9600);
  // put your setup code here, to run once:
  SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
  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);
}

void loop()
{
  // put your main code here, to run repeatedly:

}

Share this post


Link to post
Share on other sites

Nice! IIRC the MSP430 FRAM Wolverine has this now... the Window Comparator in its ADC12_B peripheral.

 

Just remember that the comparator code (the second) is still untested.

 

The ADC has more features but some involve working with more complicated peripherals, like the uDMA in the TM4C1294

Share this post


Link to post
Share on other sites

Hi 

 

Have you allready been able to test the comparator code ? I am interested in this.

 

kind regards

Cor

Share this post


Link to post
Share on other sites

Hi 

 

Have you allready been able to test the comparator code ? I am interested in this.

 

kind regards

Cor

 

I still haven't. For my current projects i still didn't had the need for it

Share this post


Link to post
Share on other sites

I will see if the code works on a simple IR reflectance sensor. Not sure when I can test the code but this seems the closest to the type of code I am looking for. My sensors are used to readout electricity/gas/water meters where blinking sections of the meters lead to a higher/lower reflected portion of IR light. The difference between the passage of the blinking-section and the rest of the readout is very small so a normal digital "on/off" is out of the question. 

 

I am currently using an Arduino for this purpose but that uses constant polling to read the meters.

Share this post


Link to post
Share on other sites

I will see if the code works on a simple IR reflectance sensor. Not sure when I can test the code but this seems the closest to the type of code I am looking for. My sensors are used to readout electricity/gas/water meters where blinking sections of the meters lead to a higher/lower reflected portion of IR light. The difference between the passage of the blinking-section and the rest of the readout is very small so a normal digital "on/off" is out of the question.

Another option to consider is the comparator on the Tiva LP (if you don't need the ADC readings).

Share this post


Link to post
Share on other sites

Hi Igor,

 

The idea is to use the comparator, the code from LRA is for the comparator..

 

regards

Cor

Igor is likely referring to the actual comparator onboard the chip. Most microcontrollers do have a comparator independent of the rest of the ADC hardware.

Share this post


Link to post
Share on other sites

If anybody can explain this in more detail with an example that could be helpfull. 

 

There's a simple comparator in the Tiva, just to compare 2 values and output either 1 or 0 depending on the values.

Maybe i can wrap up some coding, let me see

 

if you're interested in programing more with TivaWare you should check it:

http://www.ti.com/tool/sw-tm4c

 

Coding what you want is actualy realy simple and maybe you should try it. Inside the folder, TivaWare_C_Series-2.1.0.12573\docs there is a filed named SW-TM4C-DRL-UG-2.1.0.12573. That is the updated version of the peripheral library guide wich is a very nice help. The peripheral guide in the TI website is not updated

Share this post


Link to post
Share on other sites

 

 

AnalogComparators
An analog comparator is a peripheral that compares two analog voltages and provides a logical output that signals the comparison result. Note: Not all comparators have the option to drive an output pin. See “Signal Description” on page 1663 for more information. The comparator can provide its output to a device pin, acting as a replacement for an analog comparator on the board. In addition, the comparator can signal the application via interrupts or triggerthestartofasamplesequenceintheADC.TheinterruptgenerationandADCtriggeringlogic isseparateandindependent.Thisflexibilitymeans,forexample,thataninterruptcanbegenerated on a rising edge and the ADC triggered on a falling edge. TheTM4C1294NCPDTmicrocontrollerprovidesthreeindependentintegratedanalogcomparators with the following functions: ■ Compare external pin input to external pin input or to internal programmable voltage reference ■ Compare a test voltage against any one of the following voltages: – An individual external reference voltage – A shared single external reference voltage – A shared internal reference voltage
That is from SPMS433A which is the TM4C1294NCPDT datasheet. The TM4C123 also has a few comparators. Excuse the formatting, copy paste from a PDF buggered it.
 
Essentially it is a component which will compare 2 voltages to see whether one is higher than the other or not. You can either poll the comparator in a similar way to polling a digital input to retrieve a nice simple true or false value, or it can be set to generate interrupts and on a few comparators even output the comparator result directly to a GPIO pin corresponding to it. A comparator will never give you the actual voltage, it will only tell you whether one voltage is higher than the other, in a way its similar to taking too metal blocks without any markings on them, I could perhaps tell you that one is heavier than the other but I couldnt tell you the blocks actual mass, I would need a set of scales to do that and it takes a little longer for me to put the blocks on the scales and record a mass. The comparator can very quickly and flexibly tell you that one voltage is higher than the other but you would have to use the ADC to get the actual voltages and the ADC is slow in comparison.
 
You can actually buy comparators as a discrete component you can add to your board. Typically they will take 2 input values, have 1 output and also require a VCC and ground connection of course. Wire up 2 potentiometers to one with an LED on the output and the LED will only turn on if potentiometer 1 is set higher than potentiometer 2. Wicked things.
The Tiva C ADC modules have 8 comparators per module (2 modules on the connected launchpad).
The TM4C129 itself has 3 standalone comparators.
 
As another example of on chip comparators being useful actually, I needed 3 external interrupts on an arduino nano using the AtMega328P. it only has 2 external interrupt pins (cosavrsuxtroll) but does have an on chip comparator capable of triggering rising and falling edge interrupts too so I cheated and used that as a 3rd interrupt.
 
 
 
In your particular case. Input 1 to the comparator would need to be the sensor value. Input 2 I would use a potentiometer at first. Adjust the potentiometer to produce a voltage just a hair below the voltage you require from the sensor to detect the marking, when the sensor exceeds that voltage it can trigger the interrupt directly via the comparator. No need to keep monitoring the ADC.
 
Sorry if anything is wrong or too much info but its like 1am and I had a long day yet once I start typing something I often can't stop so you have the writings of a guy ready to just drop on the floor and sleep

Share this post


Link to post
Share on other sites
@@SixSixSevenSeven, i belive this is the text that got buggy copying. This is more readeable. Great info  ;)

 

An analog comparator is a peripheral that compares two analog voltages and provides a logical output that signals the comparison result.

 

Note: Not all comparators have the option to drive an output pin. See “Signal Description” on page 1663 for more information.

 

The comparator can provide its output to a device pin, acting as a replacement for an analog comparator on the board. In addition, the comparator can signal the application via interrupts or  trigger the start of a sample sequence in the ADC. The interrupt generation and ADC triggering logic is separate and independent. This flexibility means, for example, that an interrupt can be generated on a rising edge and the ADC triggered on a falling edge.

 

 The TM4C1294NCPDT microcontroller provides three independent integrated analog comparators with the following functions:

  • ■ Compare external pin input to external pin input or to internal programmable voltage reference
  • ■ Compare a test voltage against any one of the following voltages:
  • – An individual external reference voltage
  • – A shared single external reference voltage
  • – A shared internal reference voltage

Share this post


Link to post
Share on other sites

 

Igor is likely referring to the actual comparator onboard the chip. Most microcontrollers do have a comparator independent of the rest of the ADC hardware.

 

Yup, that is what I meant. 

If you look in the Tiva documentation it talks about analog comparators and digital comparators.

The ADC comparators are the digital ones, but it has analog comparator(s) also - as explained in more detail by SixSixSevenSeven.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×