AVRTools
A Library for the AVR ATmega328 and ATmega2560 Microcontrollers
Macros | Enumerations | Functions
Analog2Digital.h File Reference

This file provides functions that access the analog-to-digital conversion capability of the ATmega328 and ATmega2560 microcontrollers. More...

#include <stdint.h>
#include "GpioPinMacros.h"
Include dependency graph for Analog2Digital.h:

Go to the source code of this file.

Macros

#define readGpioPinAnalog(pinName)
 Read the analog value of the pin. More...
 

Enumerations

enum  A2DVoltageReference { kA2dReferenceAREF, kA2dReferenceAVCC, kA2dReference11V, kA2dReference256V }
 Constants representing voltage references. More...
 

Functions

int readA2D (int8_t channel)
 Read an analog voltage value. More...
 
uint16_t readGpioPinAnalogV (const GpioPinVariable &pinVar)
 Read the analog value of the pin. More...
 
void initA2D (uint8_t ref=kA2dReferenceAVCC)
 Initialize the analog-to-digital system. More...
 
void turnOffA2D ()
 Turn off the analog-to-digital system.
 
void setA2DVoltageReference (A2DVoltageReference ref)
 Set the voltage reference for the analog-to-digital system. More...
 
void setA2DVoltageReferenceAREF ()
 Set the voltage reference for the analog-to-digital system to AREF. More...
 
void setA2DVoltageReferenceAVCC ()
 Set the voltage reference for the analog-to-digital system to AREF. More...
 
void setA2DVoltageReference11V ()
 Set the voltage reference for the analog-to-digital system to AREF. More...
 
void setA2DVoltageReference256V ()
 Set the voltage reference for the analog-to-digital system to AREF. More...
 

Detailed Description

This file provides functions that access the analog-to-digital conversion capability of the ATmega328 and ATmega2560 microcontrollers.

To use these functions, include Analog2Digital.h in your source code and link against Analog2Digital.cpp.

Macro Definition Documentation

§ readGpioPinAnalog

#define readGpioPinAnalog (   pinName)

Read the analog value of the pin.

This function returns a number between 0 and 1023 that corresponds to voltage between 0 and a maximum reference value. The reference value is set using one of the setA2DVoltageReferenceXXX() functions.

Returns
an value between 0 and 1023.
Note
Before calling this function must fist initialize the analog-to-digital sub-system by calling initA2D().

Enumeration Type Documentation

§ A2DVoltageReference

Constants representing voltage references.

Enumerator
kA2dReferenceAREF 

Reference is AREF pin, internal VREF turned off.

kA2dReferenceAVCC 

Reference is AVCC pin, internal VREF turned off.

kA2dReference11V 

Reference is internal 1.1V VREF.

kA2dReference256V 

Reference is internal 2.56V VREF (only available on ATmega2560)

Function Documentation

§ initA2D()

void initA2D ( uint8_t  ref = kA2dReferenceAVCC)

Initialize the analog-to-digital system.

You must call this function before using any of the analog-to-digital functions.

  • ref provides the voltage reference to be used for analog-to-digital conversions. Pass one of the constants from enum A2DVoltageReference. If no value is provided, the default is kA2dReferenceAVCC.
Note
This function only works for CPU clocks running at either 8 MHz, 12 MHz, or 16 MHz.

§ readA2D()

int readA2D ( int8_t  channel)

Read an analog voltage value.

Voltage is read relative to the currently set reference value.

  • channel is an ADC channel number (between 0 and 7 on ATmega328; between 0 and 15 on ATMega2560).
Returns
a number between 0 and 1023.
Note
Generally users will not call this function but instead call readPinAnalog() passing it a pin name macro generated by Analog().

§ readGpioPinAnalogV()

uint16_t readGpioPinAnalogV ( const GpioPinVariable pinVar)
inline

Read the analog value of the pin.

This function returns a number between 0 and 1023 that corresponds to voltage between 0 and a maximum reference value. The reference value is set using one of the setA2DVoltageReferenceXXX() functions.

Returns
an value between 0 and 1023.
Note
Before calling this function must fist initialize the analog-to-digital sub-system by calling initA2D().

§ setA2DVoltageReference()

void setA2DVoltageReference ( A2DVoltageReference  ref)

Set the voltage reference for the analog-to-digital system.

After your have initialized the analog-to-digital system with initA2D(), you can use this function to change the voltage reference.

  • ref provides the voltage reference to be used for analog-to-digital conversions. Pass one of the constants from enum A2DVoltageReference.

§ setA2DVoltageReference11V()

void setA2DVoltageReference11V ( )
inline

Set the voltage reference for the analog-to-digital system to AREF.

This is an inline synonym for setA2DVoltageReference( kA2dReference11V )

§ setA2DVoltageReference256V()

void setA2DVoltageReference256V ( )
inline

Set the voltage reference for the analog-to-digital system to AREF.

This is an inline synonym for setA2DVoltageReference( kA2dReference256V )

Note
this function is only available on ATmega2560 (not on the ATmega328).

§ setA2DVoltageReferenceAREF()

void setA2DVoltageReferenceAREF ( )
inline

Set the voltage reference for the analog-to-digital system to AREF.

This is an inline synonym for setA2DVoltageReference( kA2dReferenceAREF )

§ setA2DVoltageReferenceAVCC()

void setA2DVoltageReferenceAVCC ( )
inline

Set the voltage reference for the analog-to-digital system to AREF.

This is an inline synonym for setA2DVoltageReference( kA2dReferenceAVCC )