forked from kcozens/avrlib-2014
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha2d.h
151 lines (132 loc) · 4.72 KB
/
a2d.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*! \file a2d.h \brief Analog-to-Digital converter function library. */
//*****************************************************************************
//
// File Name : 'a2d.h'
// Title : Analog-to-digital converter functions
// Author : Pascal Stang - Copyright (C) 2002
// Created : 4/08/2002
// Revised : 4/30/2002
// Version : 1.1
// Target MCU : Atmel AVR series
// Editor Tabs : 4
//
// This code is distributed under the GNU Public License
// which can be found at http://www.gnu.org/licenses/gpl.txt
//
/// \ingroup driver_avr
/// \defgroup a2d A/D Converter Function Library (a2d.c)
/// \code #include "a2d.h" \endcode
/// \par Overview
/// This library provides an easy interface to the analog-to-digital
/// converter available on many AVR processors. Updated to support
/// the ATmega128.
//
//****************************************************************************
//@{
#ifndef A2D_H
#define A2D_H
// defines
// A2D clock prescaler select
// *selects how much the CPU clock frequency is divided
// to create the A2D clock frequency
// *lower division ratios make conversion go faster
// *higher division ratios make conversions more accurate
#define ADC_PRESCALE_DIV2 0x00 ///< 0x01,0x00 -> CPU clk/2
#define ADC_PRESCALE_DIV4 0x02 ///< 0x02 -> CPU clk/4
#define ADC_PRESCALE_DIV8 0x03 ///< 0x03 -> CPU clk/8
#define ADC_PRESCALE_DIV16 0x04 ///< 0x04 -> CPU clk/16
#define ADC_PRESCALE_DIV32 0x05 ///< 0x05 -> CPU clk/32
#define ADC_PRESCALE_DIV64 0x06 ///< 0x06 -> CPU clk/64
#define ADC_PRESCALE_DIV128 0x07 ///< 0x07 -> CPU clk/128
// default value
#define ADC_PRESCALE ADC_PRESCALE_DIV64
// do not change the mask value
#define ADC_PRESCALE_MASK 0x07
// A2D voltage reference select
// *this determines what is used as the
// full-scale voltage point for A2D conversions
#define ADC_REFERENCE_AREF 0x00 ///< 0x00 -> AREF pin, internal VREF turned off
#define ADC_REFERENCE_AVCC 0x01 ///< 0x01 -> AVCC pin, internal VREF turned off
#define ADC_REFERENCE_RSVD 0x02 ///< 0x02 -> Reserved
#define ADC_REFERENCE_256V 0x03 ///< 0x03 -> Internal 2.56V VREF
// default value
#define ADC_REFERENCE ADC_REFERENCE_AVCC
// do not change the mask value
#define ADC_REFERENCE_MASK 0xC0
// bit mask for A2D channel multiplexer
#define ADC_MUX_MASK 0x1F
// channel defines (for reference and use in code)
// these channels supported by all AVRs with A2D
#define ADC_CH_ADC0 0x00
#define ADC_CH_ADC1 0x01
#define ADC_CH_ADC2 0x02
#define ADC_CH_ADC3 0x03
#define ADC_CH_ADC4 0x04
#define ADC_CH_ADC5 0x05
#define ADC_CH_ADC6 0x06
#define ADC_CH_ADC7 0x07
#define ADC_CH_122V 0x1E ///< 1.22V voltage reference
#define ADC_CH_AGND 0x1F ///< AGND
// these channels supported only in ATmega128
// differential with gain
#define ADC_CH_0_0_DIFF10X 0x08
#define ADC_CH_1_0_DIFF10X 0x09
#define ADC_CH_0_0_DIFF200X 0x0A
#define ADC_CH_1_0_DIFF200X 0x0B
#define ADC_CH_2_2_DIFF10X 0x0C
#define ADC_CH_3_2_DIFF10X 0x0D
#define ADC_CH_2_2_DIFF200X 0x0E
#define ADC_CH_3_2_DIFF200X 0x0F
// differential
#define ADC_CH_0_1_DIFF1X 0x10
#define ADC_CH_1_1_DIFF1X 0x11
#define ADC_CH_2_1_DIFF1X 0x12
#define ADC_CH_3_1_DIFF1X 0x13
#define ADC_CH_4_1_DIFF1X 0x14
#define ADC_CH_5_1_DIFF1X 0x15
#define ADC_CH_6_1_DIFF1X 0x16
#define ADC_CH_7_1_DIFF1X 0x17
#define ADC_CH_0_2_DIFF1X 0x18
#define ADC_CH_1_2_DIFF1X 0x19
#define ADC_CH_2_2_DIFF1X 0x1A
#define ADC_CH_3_2_DIFF1X 0x1B
#define ADC_CH_4_2_DIFF1X 0x1C
#define ADC_CH_5_2_DIFF1X 0x1D
// compatibility for new Mega processors
// ADCSR hack apparently no longer necessary in new AVR-GCC
#ifdef ADCSRA
#ifndef ADCSR
#define ADCSR ADCSRA
#endif
#endif
#ifdef ADATE
#define ADFR ADATE
#endif
// function prototypes
//! Initializes the A/D converter.
/// Turns ADC on and prepares it for use.
void a2dInit(void);
//! Turn off A/D converter
void a2dOff(void);
//! Sets the division ratio of the A/D converter clock.
/// This function is automatically called from a2dInit()
/// with a default value.
void a2dSetPrescaler(unsigned char prescale);
//! Configures which voltage reference the A/D converter uses.
/// This function is automatically called from a2dInit()
/// with a default value.
void a2dSetReference(unsigned char ref);
//! sets the a2d input channel
void a2dSetChannel(unsigned char ch);
//! start a conversion on the current a2d input channel
void a2dStartConvert(void);
//! return TRUE if conversion is complete
u08 a2dIsComplete(void);
//! Starts a conversion on A/D channel# ch,
/// returns the 10-bit value of the conversion when it is finished.
unsigned short a2dConvert10bit(unsigned char ch);
//! Starts a conversion on A/D channel# ch,
/// returns the 8-bit value of the conversion when it is finished.
unsigned char a2dConvert8bit(unsigned char ch);
#endif
//@}