| 1 |
/* |
| 2 |
wiring_analog.c - analog input and output |
| 3 |
Part of Arduino - http://www.arduino.cc/ |
| 4 |
|
| 5 |
Copyright (c) 2005-2006 David A. Mellis |
| 6 |
|
| 7 |
This library is free software; you can redistribute it and/or |
| 8 |
modify it under the terms of the GNU Lesser General Public |
| 9 |
License as published by the Free Software Foundation; either |
| 10 |
version 2.1 of the License, or (at your option) any later version. |
| 11 |
|
| 12 |
This library 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 GNU |
| 15 |
Lesser General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU Lesser General |
| 18 |
Public License along with this library; if not, write to the |
| 19 |
Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
| 20 |
Boston, MA 02111-1307 USA |
| 21 |
|
| 22 |
Modified 28 September 2010 by Mark Sproul |
| 23 |
*/ |
| 24 |
|
| 25 |
#include "wiring_private.h" |
| 26 |
#include "pins_arduino.h" |
| 27 |
|
| 28 |
uint8_t analog_reference = DEFAULT; |
| 29 |
|
| 30 |
void analogReference(uint8_t mode) |
| 31 |
{ |
| 32 |
// can't actually set the register here because the default setting |
| 33 |
// will connect AVCC and the AREF pin, which would cause a short if |
| 34 |
// there's something connected to AREF. |
| 35 |
analog_reference = mode; |
| 36 |
} |
| 37 |
|
| 38 |
int analogRead(uint8_t pin) |
| 39 |
{ |
| 40 |
|
| 41 |
#if defined(analogPinToChannel) |
| 42 |
#if defined(__AVR_ATmega32U4__) |
| 43 |
if (pin >= 18) pin -= 18; // allow for channel or pin numbers |
| 44 |
#endif |
| 45 |
pin = analogPinToChannel(pin); |
| 46 |
#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) |
| 47 |
if (pin >= 54) pin -= 54; // allow for channel or pin numbers |
| 48 |
#elif defined(__AVR_ATmega32U4__) |
| 49 |
if (pin >= 18) pin -= 18; // allow for channel or pin numbers |
| 50 |
#elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__) |
| 51 |
if (pin >= 24) pin -= 24; // allow for channel or pin numbers |
| 52 |
#else |
| 53 |
if (pin >= 14) pin -= 14; // allow for channel or pin numbers |
| 54 |
#endif |
| 55 |
|
| 56 |
#if defined(ADCSRB) && defined(MUX5) |
| 57 |
// the MUX5 bit of ADCSRB selects whether we're reading from channels |
| 58 |
// 0 to 7 (MUX5 low) or 8 to 15 (MUX5 high). |
| 59 |
ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((pin >> 3) & 0x01) << MUX5); |
| 60 |
#endif |
| 61 |
|
| 62 |
// set the analog reference (high two bits of ADMUX) and select the |
| 63 |
// channel (low 4 bits). this also sets ADLAR (left-adjust result) |
| 64 |
// to 0 (the default). |
| 65 |
#if defined(ADMUX) |
| 66 |
#if defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) |
| 67 |
ADMUX = (analog_reference << 4) | (pin & 0x07); |
| 68 |
#else |
| 69 |
ADMUX = (analog_reference << 6) | (pin & 0x07); |
| 70 |
#endif |
| 71 |
#endif |
| 72 |
|
| 73 |
// without a delay, we seem to read from the wrong channel |
| 74 |
//delay(1); |
| 75 |
|
| 76 |
#if defined(ADCSRA) && defined(ADC) |
| 77 |
// start the conversion |
| 78 |
sbi(ADCSRA, ADSC); |
| 79 |
|
| 80 |
// ADSC is cleared when the conversion finishes |
| 81 |
while (bit_is_set(ADCSRA, ADSC)); |
| 82 |
|
| 83 |
// ADC macro takes care of reading ADC register. |
| 84 |
// avr-gcc implements the proper reading order: ADCL is read first. |
| 85 |
return ADC; |
| 86 |
#else |
| 87 |
// we dont have an ADC, return 0 |
| 88 |
return 0; |
| 89 |
#endif |
| 90 |
} |
| 91 |
|
| 92 |
// Right now, PWM output only works on the pins with |
| 93 |
// hardware support. These are defined in the appropriate |
| 94 |
// pins_*.c file. For the rest of the pins, we default |
| 95 |
// to digital output. |
| 96 |
void analogWrite(uint8_t pin, int val) |
| 97 |
{ |
| 98 |
// We need to make sure the PWM output is enabled for those pins |
| 99 |
// that support it, as we turn it off when digitally reading or |
| 100 |
// writing with them. Also, make sure the pin is in output mode |
| 101 |
// for consistenty with Wiring, which doesn't require a pinMode |
| 102 |
// call for the analog output pins. |
| 103 |
pinMode(pin, OUTPUT); |
| 104 |
if (val == 0) |
| 105 |
{ |
| 106 |
digitalWrite(pin, LOW); |
| 107 |
} |
| 108 |
else if (val == 255) |
| 109 |
{ |
| 110 |
digitalWrite(pin, HIGH); |
| 111 |
} |
| 112 |
else |
| 113 |
{ |
| 114 |
switch(digitalPinToTimer(pin)) |
| 115 |
{ |
| 116 |
// XXX fix needed for atmega8 |
| 117 |
#if defined(TCCR0) && defined(COM00) && !defined(__AVR_ATmega8__) |
| 118 |
case TIMER0A: |
| 119 |
// connect pwm to pin on timer 0 |
| 120 |
sbi(TCCR0, COM00); |
| 121 |
OCR0 = val; // set pwm duty |
| 122 |
break; |
| 123 |
#endif |
| 124 |
|
| 125 |
#if defined(TCCR0A) && defined(COM0A1) |
| 126 |
case TIMER0A: |
| 127 |
// connect pwm to pin on timer 0, channel A |
| 128 |
sbi(TCCR0A, COM0A1); |
| 129 |
OCR0A = val; // set pwm duty |
| 130 |
break; |
| 131 |
#endif |
| 132 |
|
| 133 |
#if defined(TCCR0A) && defined(COM0B1) |
| 134 |
case TIMER0B: |
| 135 |
// connect pwm to pin on timer 0, channel B |
| 136 |
sbi(TCCR0A, COM0B1); |
| 137 |
OCR0B = val; // set pwm duty |
| 138 |
break; |
| 139 |
#endif |
| 140 |
|
| 141 |
#if defined(TCCR1A) && defined(COM1A1) |
| 142 |
case TIMER1A: |
| 143 |
// connect pwm to pin on timer 1, channel A |
| 144 |
sbi(TCCR1A, COM1A1); |
| 145 |
OCR1A = val; // set pwm duty |
| 146 |
break; |
| 147 |
#endif |
| 148 |
|
| 149 |
#if defined(TCCR1A) && defined(COM1B1) |
| 150 |
case TIMER1B: |
| 151 |
// connect pwm to pin on timer 1, channel B |
| 152 |
sbi(TCCR1A, COM1B1); |
| 153 |
OCR1B = val; // set pwm duty |
| 154 |
break; |
| 155 |
#endif |
| 156 |
|
| 157 |
#if defined(TCCR1A) && defined(COM1C1) |
| 158 |
case TIMER1C: |
| 159 |
// connect pwm to pin on timer 1, channel C |
| 160 |
sbi(TCCR1A, COM1C1); |
| 161 |
OCR1C = val; // set pwm duty |
| 162 |
break; |
| 163 |
#endif |
| 164 |
|
| 165 |
#if defined(TCCR2) && defined(COM21) |
| 166 |
case TIMER2: |
| 167 |
// connect pwm to pin on timer 2 |
| 168 |
sbi(TCCR2, COM21); |
| 169 |
OCR2 = val; // set pwm duty |
| 170 |
break; |
| 171 |
#endif |
| 172 |
|
| 173 |
#if defined(TCCR2A) && defined(COM2A1) |
| 174 |
case TIMER2A: |
| 175 |
// connect pwm to pin on timer 2, channel A |
| 176 |
sbi(TCCR2A, COM2A1); |
| 177 |
OCR2A = val; // set pwm duty |
| 178 |
break; |
| 179 |
#endif |
| 180 |
|
| 181 |
#if defined(TCCR2A) && defined(COM2B1) |
| 182 |
case TIMER2B: |
| 183 |
// connect pwm to pin on timer 2, channel B |
| 184 |
sbi(TCCR2A, COM2B1); |
| 185 |
OCR2B = val; // set pwm duty |
| 186 |
break; |
| 187 |
#endif |
| 188 |
|
| 189 |
#if defined(TCCR3A) && defined(COM3A1) |
| 190 |
case TIMER3A: |
| 191 |
// connect pwm to pin on timer 3, channel A |
| 192 |
sbi(TCCR3A, COM3A1); |
| 193 |
OCR3A = val; // set pwm duty |
| 194 |
break; |
| 195 |
#endif |
| 196 |
|
| 197 |
#if defined(TCCR3A) && defined(COM3B1) |
| 198 |
case TIMER3B: |
| 199 |
// connect pwm to pin on timer 3, channel B |
| 200 |
sbi(TCCR3A, COM3B1); |
| 201 |
OCR3B = val; // set pwm duty |
| 202 |
break; |
| 203 |
#endif |
| 204 |
|
| 205 |
#if defined(TCCR3A) && defined(COM3C1) |
| 206 |
case TIMER3C: |
| 207 |
// connect pwm to pin on timer 3, channel C |
| 208 |
sbi(TCCR3A, COM3C1); |
| 209 |
OCR3C = val; // set pwm duty |
| 210 |
break; |
| 211 |
#endif |
| 212 |
|
| 213 |
#if defined(TCCR4A) |
| 214 |
case TIMER4A: |
| 215 |
//connect pwm to pin on timer 4, channel A |
| 216 |
sbi(TCCR4A, COM4A1); |
| 217 |
#if defined(COM4A0) // only used on 32U4 |
| 218 |
cbi(TCCR4A, COM4A0); |
| 219 |
#endif |
| 220 |
OCR4A = val; // set pwm duty |
| 221 |
break; |
| 222 |
#endif |
| 223 |
|
| 224 |
#if defined(TCCR4A) && defined(COM4B1) |
| 225 |
case TIMER4B: |
| 226 |
// connect pwm to pin on timer 4, channel B |
| 227 |
sbi(TCCR4A, COM4B1); |
| 228 |
OCR4B = val; // set pwm duty |
| 229 |
break; |
| 230 |
#endif |
| 231 |
|
| 232 |
#if defined(TCCR4A) && defined(COM4C1) |
| 233 |
case TIMER4C: |
| 234 |
// connect pwm to pin on timer 4, channel C |
| 235 |
sbi(TCCR4A, COM4C1); |
| 236 |
OCR4C = val; // set pwm duty |
| 237 |
break; |
| 238 |
#endif |
| 239 |
|
| 240 |
#if defined(TCCR4C) && defined(COM4D1) |
| 241 |
case TIMER4D: |
| 242 |
// connect pwm to pin on timer 4, channel D |
| 243 |
sbi(TCCR4C, COM4D1); |
| 244 |
#if defined(COM4D0) // only used on 32U4 |
| 245 |
cbi(TCCR4C, COM4D0); |
| 246 |
#endif |
| 247 |
OCR4D = val; // set pwm duty |
| 248 |
break; |
| 249 |
#endif |
| 250 |
|
| 251 |
|
| 252 |
#if defined(TCCR5A) && defined(COM5A1) |
| 253 |
case TIMER5A: |
| 254 |
// connect pwm to pin on timer 5, channel A |
| 255 |
sbi(TCCR5A, COM5A1); |
| 256 |
OCR5A = val; // set pwm duty |
| 257 |
break; |
| 258 |
#endif |
| 259 |
|
| 260 |
#if defined(TCCR5A) && defined(COM5B1) |
| 261 |
case TIMER5B: |
| 262 |
// connect pwm to pin on timer 5, channel B |
| 263 |
sbi(TCCR5A, COM5B1); |
| 264 |
OCR5B = val; // set pwm duty |
| 265 |
break; |
| 266 |
#endif |
| 267 |
|
| 268 |
#if defined(TCCR5A) && defined(COM5C1) |
| 269 |
case TIMER5C: |
| 270 |
// connect pwm to pin on timer 5, channel C |
| 271 |
sbi(TCCR5A, COM5C1); |
| 272 |
OCR5C = val; // set pwm duty |
| 273 |
break; |
| 274 |
#endif |
| 275 |
|
| 276 |
case NOT_ON_TIMER: |
| 277 |
default: |
| 278 |
if (val < 128) { |
| 279 |
digitalWrite(pin, LOW); |
| 280 |
} else { |
| 281 |
digitalWrite(pin, HIGH); |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
|