This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwiring_analog.c
210 lines (187 loc) · 5.65 KB
/
wiring_analog.c
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* Copyright (c) 2005-2006 David A. Mellis
* Modified for Waspmote by Libelium, 2016
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 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 Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Version: 3.0
*
*
*/
#include "wiring_private.h"
#include "pins_waspmote.h"
uint8_t analog_reference = DEFAULT;
/*!
* @brief This function sets the analog_reference for ADC conversion
* @param uint8_t mode: Select the voltage reference selections for ADC
* @arg EXTERNAL: AREF, Internal VREF turned off
* @arg DEFAULT: AVCC with external capacitor at AREF pin
* @arg INTERNAL1V1: Internal 1V1 reference with capacitor at AREF pin
* @arg INTERNAL2V56: Internal 2V56 reference with capacitor at AREF pin
* @return void
*
*/
void analogReference(uint8_t mode)
{
analog_reference = mode;
// set Reference Selection bits
if (analog_reference == INTERNAL2V56)
{
sbi(ADMUX, REFS1);
sbi(ADMUX, REFS0);
}
else if (analog_reference == INTERNAL1V1)
{
sbi(ADMUX, REFS1);
cbi(ADMUX, REFS0);
}
else if (analog_reference == EXTERNAL)
{
cbi(ADMUX, REFS1);
cbi(ADMUX, REFS0);
}
else
{
// DEFAULT
cbi(ADMUX, REFS1);
sbi(ADMUX, REFS0);
}
// stabilization time
delay(10);
}
/*!
* @brief This function reads the analog pin
* @param uint8_t pin: input analog pin to read from
* @arg ANALOG1
* @arg ANALOG2
* @arg ANALOG3
* @arg ANALOG4
* @arg ANALOG5
* @arg ANALOG6
* @arg ANALOG7
* @return analog value in bits (from 0 to 1023) as it is using a 10-bit ADC
*
*/
int analogRead(uint8_t pin)
{
uint8_t low, high, ch = analogInPinToBit(pin);
// store channel previously set
uint8_t channel = (ADMUX & (unsigned int) 0x0f);
// enables the ADC
sbi(ADCSRA, ADEN);
// check if there is need to change the channel and wait for stabilization
if( channel != ch )
{
// the low 4 bits of ADMUX select the ADC channel
ADMUX = (ADMUX & (unsigned int) 0xf0) | (ch & (unsigned int) 0x0f);
// without a delay, we seem to read from the wrong channel
delay(1);
}
// start the conversion.
sbi(ADCSRA, ADSC);
// When the conversion is complete, ADSC bit
// (inside ADCSRA register) returns to zero.
while (bit_is_set(ADCSRA, ADSC));
// we have to read ADCL first; doing so locks both ADCL
// and ADCH until ADCH is read. reading ADCL second would
// cause the results of each conversion to be discarded,
// as ADCL and ADCH would be locked when it completed.
low = ADCL;
high = ADCH;
// combine the two bytes
return (high << 8) | low;
}
// Right now, PWM output only works on the pins with
// hardware support. These are defined in the appropriate
// pins_*.c file. For the rest of the pins, we default
// to digital output.
void analogWrite(uint8_t pin, int val)
{
// We need to make sure the PWM output is enabled for those pins
// that support it, as we turn it off when digitally reading or
// writing with them. Also, make sure the pin is in output mode
// for consistenty with Wiring, which doesn't require a pinMode
// call for the analog output pins.
pinMode(pin, OUTPUT);
if (digitalPinToTimer(pin) == TIMER1A) {
// connect pwm to pin on timer 1, channel A
sbi(TCCR1A, COM1A1);
// set pwm duty
OCR1A = val;
} else if (digitalPinToTimer(pin) == TIMER1B) {
// connect pwm to pin on timer 1, channel B
sbi(TCCR1A, COM1B1);
// set pwm duty
OCR1B = val;
#if defined(__AVR_ATmega168__)
} else if (digitalPinToTimer(pin) == TIMER0A) {
// connect pwm to pin on timer 0, channel A
sbi(TCCR0A, COM0A1);
// set pwm duty
OCR0A = val;
} else if (digitalPinToTimer(pin) == TIMER0B) {
// connect pwm to pin on timer 0, channel B
sbi(TCCR0A, COM0B1);
// set pwm duty
OCR0B = val;
} else if (digitalPinToTimer(pin) == TIMER2A) {
// connect pwm to pin on timer 2, channel A
sbi(TCCR2A, COM2A1);
// set pwm duty
OCR2A = val;
} else if (digitalPinToTimer(pin) == TIMER2B) {
// connect pwm to pin on timer 2, channel B
sbi(TCCR2A, COM2B1);
// set pwm duty
OCR2B = val;
#elif defined(__AVR_ATmega1281__)
} else if (digitalPinToTimer(pin) == TIMER0A) {
// connect pwm to pin on timer 0, channel A
sbi(TCCR0A, COM0A1);
// set pwm duty
OCR0A = val;
} else if (digitalPinToTimer(pin) == TIMER0B) {
// connect pwm to pin on timer 0, channel B
sbi(TCCR0A, COM0B1);
// set pwm duty
OCR0B = val;
} else if (digitalPinToTimer(pin) == TIMER2A) {
// connect pwm to pin on timer 2, channel A
sbi(TCCR2A, COM2A1);
// set pwm duty
OCR2A = val;
} else if (digitalPinToTimer(pin) == TIMER2B) {
// connect pwm to pin on timer 2, channel B
sbi(TCCR2A, COM2B1);
// set pwm duty
OCR2B = val;
} else if (digitalPinToTimer(pin) == TIMER3A) {
// connect pwm to pin on timer 3, channel A
sbi(TCCR3A, COM3A1);
// set pwm duty
OCR3A = val;
} else if (digitalPinToTimer(pin) == TIMER3B) {
// connect pwm to pin on timer 3, channel B
sbi(TCCR3A, COM3B1);
// set pwm duty
OCR3B = val;
#else
} else if (digitalPinToTimer(pin) == TIMER2) {
// connect pwm to pin on timer 2, channel B
sbi(TCCR2, COM21);
// set pwm duty
OCR2 = val;
#endif
} else if (val < 128)
digitalWrite(pin, LOW);
else
digitalWrite(pin, HIGH);
}