From 92ba50c7b226c951ba440badf638801f12f3a9ff Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Fri, 14 Dec 2007 04:21:59 +0000 Subject: Adding analogReference() function - needs testing on an ATmega8 (but works on the ATmega 168). --- cores/arduino/wiring.c | 4 ---- cores/arduino/wiring.h | 5 +++++ cores/arduino/wiring_analog.c | 16 ++++++++++++++-- 3 files changed, 19 insertions(+), 6 deletions(-) (limited to 'cores/arduino') diff --git a/cores/arduino/wiring.c b/cores/arduino/wiring.c index 5dda822..ae8fcdd 100755 --- a/cores/arduino/wiring.c +++ b/cores/arduino/wiring.c @@ -170,10 +170,6 @@ void init() sbi(TCCR2, WGM20); #endif - // set a2d reference to AVCC (5 volts) - cbi(ADMUX, REFS1); - sbi(ADMUX, REFS0); - // set a2d prescale factor to 128 // 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range. // XXX: this will not work properly for other clock speeds, and diff --git a/cores/arduino/wiring.h b/cores/arduino/wiring.h index e0c5315..df43e6c 100755 --- a/cores/arduino/wiring.h +++ b/cores/arduino/wiring.h @@ -57,6 +57,10 @@ extern "C"{ #define FALLING 2 #define RISING 3 +#define INTERNAL 3 +#define DEFAULT 1 +#define EXTERNAL 0 + // undefine stdlib's abs if encountered #ifdef abs #undef abs @@ -93,6 +97,7 @@ void pinMode(uint8_t, uint8_t); void digitalWrite(uint8_t, uint8_t); int digitalRead(uint8_t); int analogRead(uint8_t); +void analogReference(uint8_t mode); void analogWrite(uint8_t, int); void beginSerial(long); 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); -- cgit v1.2.3-18-g5258