diff options
author | David A. Mellis <d.mellis@arduino.cc> | 2007-12-14 04:21:59 +0000 |
---|---|---|
committer | David A. Mellis <d.mellis@arduino.cc> | 2007-12-14 04:21:59 +0000 |
commit | 92ba50c7b226c951ba440badf638801f12f3a9ff (patch) | |
tree | ca555729c7eafb672c0fde62b76d852daebb93f6 /cores/arduino/wiring_analog.c | |
parent | 0292fe91fc2de0e5c5b374a6d0e0c3879fe49387 (diff) |
Adding analogReference() function - needs testing on an ATmega8 (but works on the ATmega 168).
Diffstat (limited to 'cores/arduino/wiring_analog.c')
-rwxr-xr-x | cores/arduino/wiring_analog.c | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/cores/arduino/wiring_analog.c b/cores/arduino/wiring_analog.c index 0ab32eb..30c5642 100755 --- a/cores/arduino/wiring_analog.c +++ b/cores/arduino/wiring_analog.c @@ -25,12 +25,24 @@ #include "wiring_private.h" #include "pins_arduino.h" +uint8_t analog_reference = DEFAULT; + +void analogReference(uint8_t mode) +{ + // can't actually set the register here because the default setting + // will connect AVCC and the AREF pin, which would cause a short if + // there's something connected to AREF. + analog_reference = mode; +} + int analogRead(uint8_t pin) { uint8_t low, high, ch = analogInPinToBit(pin); - // the low 4 bits of ADMUX select the ADC channel - ADMUX = (ADMUX & (unsigned int) 0xf0) | (ch & (unsigned int) 0x0f); + // set the analog reference (high two bits of ADMUX) and select the + // channel (low 4 bits). this also sets ADLAR (left-adjust result) + // to 0 (the default). + ADMUX = (analog_reference << 6) | (pin & 0x0f); // without a delay, we seem to read from the wrong channel //delay(1); |