AVRTools
A Library for the AVR ATmega328 and ATmega2560 Microcontrollers
Loading...
Searching...
No Matches
Analog2Digital.h
Go to the documentation of this file.
1/*
2 Analog2Digital.h - A library for analog-to-digital conversions.
3 For AVR ATMega328p (Arduino Uno) and ATMega2560 (Arduino Mega).
4 This is part of the AVRTools library.
5 Copyright (c) 2014 Igor Mikolic-Torreira. All right reserved.
6
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21
33#ifndef Analog2Digital_h
34#define Analog2Digital_h
35
36#include <stdint.h>
37
38#include "GpioPinMacros.h"
39
40
41#if defined(__AVR_ATmega2560__)
42
43
55
56#else
57
59{
60 kA2dReferenceAREF = 0x00, // 0x00 -> AREF pin, internal VREF turned off
61 kA2dReferenceAVCC = 0x01, // 0x01 -> AVCC pin, internal VREF turned off
62 kA2dReference11V = 0x03 // 0x03 -> Internal 1.1V VREF
63};
64
65#endif
66
67
68/*
69 The following macro is not intended for end-user use; it is needed to support the pin naming
70 macros in conjunction with the C/C++ preprocessor's re-scanning rules.
71*/
72
73#define _readGpioPinAnalog( ddr, port, pin, nbr, adc, ocr, com, tccr ) readA2D( adc )
74
75
76
77
95#define readGpioPinAnalog( pinName ) _readGpioPinAnalog( pinName )
96
97
98int readA2D( int8_t channel );
99
100
101
117inline uint16_t readGpioPinAnalogV( const GpioPinVariable& pinVar )
118{
119 return readA2D( pinVar.adcNbr() );
120}
121
122
135void initA2D( uint8_t ref = kA2dReferenceAVCC );
136
137
138
144void turnOffA2D();
145
146
158
159
160
169
170
171
180
181
182
191
192
193#if defined(__AVR_ATmega2560__)
194
195
206
207#endif
208
209
210
226int readA2D( int8_t channel );
227
228#endif
void turnOffA2D()
Turn off the analog-to-digital system.
Definition Analog2Digital.cpp:92
void setA2DVoltageReference256V()
Set the voltage reference for the analog-to-digital system to AREF.
Definition Analog2Digital.h:204
void initA2D(uint8_t ref=kA2dReferenceAVCC)
Initialize the analog-to-digital system.
Definition Analog2Digital.cpp:50
void setA2DVoltageReference11V()
Set the voltage reference for the analog-to-digital system to AREF.
Definition Analog2Digital.h:189
int readA2D(int8_t channel)
Read an analog voltage value.
Definition Analog2Digital.cpp:109
void setA2DVoltageReferenceAREF()
Set the voltage reference for the analog-to-digital system to AREF.
Definition Analog2Digital.h:167
void setA2DVoltageReferenceAVCC()
Set the voltage reference for the analog-to-digital system to AREF.
Definition Analog2Digital.h:178
uint16_t readGpioPinAnalogV(const GpioPinVariable &pinVar)
Read the analog value of the pin.
Definition Analog2Digital.h:117
A2DVoltageReference
Constants representing voltage references.
Definition Analog2Digital.h:49
@ kA2dReferenceAREF
Reference is AREF pin, internal VREF turned off.
Definition Analog2Digital.h:50
@ kA2dReference11V
Reference is internal 1.1V VREF.
Definition Analog2Digital.h:52
@ kA2dReference256V
Reference is internal 2.56V VREF (only available on ATmega2560)
Definition Analog2Digital.h:53
@ kA2dReferenceAVCC
Reference is AVCC pin, internal VREF turned off.
Definition Analog2Digital.h:51
void setA2DVoltageReference(A2DVoltageReference ref)
Set the voltage reference for the analog-to-digital system.
Definition Analog2Digital.cpp:100
This file contains the primary macros for naming and manipulating GPIO pin names.
This class defines a type that can encode a GPIO pin as a variable. Read the section on [GPIO Pin Var...
Definition GpioPinMacros.h:425
uint8_t adcNbr() const
Return the ADC channel number (analog-to-digital related).
Definition GpioPinMacros.h:477