-
Notifications
You must be signed in to change notification settings - Fork 1
/
ADC.cpp
executable file
·154 lines (123 loc) · 5.07 KB
/
ADC.cpp
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
152
153
154
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA *
* *
***************************************************************************
* *
* (c) Copyright, 1997-2012, ANSR *
* *
***************************************************************************
* *
* Filename: ADC.cpp *
* *
***************************************************************************/
#include "armlib.h"
/// Reserve memory for singleton object.
static ADC adcSingletonObject;
/**
* Get a pointer to the ADC singleton object.
*/
ADC *ADC::GetInstance()
{
return &adcSingletonObject;
}
/**
* Enable the A/D converter hardware. Multiple channels may be selected by ORing together the
* channel selection. The ADC channel 0 and/or 1 is automatically enabled based on the desired
* channels.
*
* @param adcChannel one or more desired ADC channels to enable
*/
void ADC::Enable(ADCChannel adcChannel)
{
uint32_t clockDiv;
// Calculate the divider to get the desired 4.5MHz ADC clock.
clockDiv = ((SystemControl::GetInstance()->GetPClock() / 450000) + 5) / 10;
if (clockDiv != 0)
--clockDiv;
// Set the pin select values required for each ADC pin.
if (adcChannel & AD0_1)
PINSEL1 |= (0x01 << 24);
if (adcChannel & AD0_2)
PINSEL1 |= (0x01 << 26);
if (adcChannel & AD0_3)
PINSEL1 |= (0x01 << 28);
if (adcChannel & AD0_4)
PINSEL1 |= (0x01 << 18);
if (adcChannel & AD0_6)
PINSEL0 |= (0x03 << 8);
if (adcChannel & AD0_7)
PINSEL0 |= (0x03 << 10);
if (adcChannel & AD1_0)
PINSEL0 |= (0x03 << 12);
if (adcChannel & AD1_1)
PINSEL0 |= (0x03 << 16);
if (adcChannel & AD1_2)
PINSEL0 |= (0x03 << 20);
if (adcChannel & AD1_3)
PINSEL0 |= (0x03 << 24);
if (adcChannel & AD1_4)
PINSEL0 |= (0x03 << 26);
if (adcChannel & AD1_5)
PINSEL0 |= (0x03 << 30);
if (adcChannel & AD1_6)
PINSEL1 |= (0x02 << 10);
if (adcChannel & AD1_7)
PINSEL1 |= (0x01 << 12);
// Enable the ADC channel 0 if we use any ADC channels from it.
if (adcChannel & 0x00ff)
AD0CR = (clockDiv << 8) | ADxCR_PDN;
// ENable the ADC channel 1 if we use any ADC channels from it.
if (adcChannel & 0xff00)
AD1CR = (clockDiv << 8) | ADxCR_PDN;
}
/**
* Start an A/D conversion and return the current channel value.
*
* @param adcChannel desired ADC channel to read
*
* @return ADC reading in the range 0 to 1023.
*/
uint32_t ADC::Read (ADCChannel adcChannel)
{
uint32_t value;
// Determine if we are using ADC converter 0 or 1 based on the desired channel.
if (adcChannel < 0x0100)
{
// Set the desired channel and start the conversion.
AD0CR = (AD0CR & 0xffffff00) | adcChannel;
AD0CR |= ADxCR_START_NOW;
// Wait for the conversion to complete.
do
{
value = AD0GDR;
} while ((value & ADxGDR_DONE) == 0x00);
// Stop the converter.
AD0CR &= 0xF8FFFFFF;
} else {
// Set the desired channel and start the conversion.
AD1CR = (AD1CR & 0xffffff00) | (adcChannel >> 8);
AD1CR |= ADxCR_START_NOW;
// Wait for the conversion to complete.
do
{
value = AD1GDR;
} while ((value & ADxGDR_DONE) == 0x00);
// Stop the converter.
AD1CR &= 0xF8FFFFFF;
}
// Return the ADC reading.
return (value >> 6) & 0x3ff;
}