From 5d15f091a8ccde5bcd3f3205e6d34b72b14ddcde Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig Date: Sat, 27 Aug 2016 11:48:04 +0200 Subject: [PATCH 1/2] Add all analog references supported by the ATtinyX5 series --- hardware/arduino/avr/cores/arduino/Arduino.h | 16 ++++++++++++---- .../arduino/avr/cores/arduino/wiring_analog.c | 2 +- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/hardware/arduino/avr/cores/arduino/Arduino.h b/hardware/arduino/avr/cores/arduino/Arduino.h index f1da68da7..e71c9c3e1 100644 --- a/hardware/arduino/avr/cores/arduino/Arduino.h +++ b/hardware/arduino/avr/cores/arduino/Arduino.h @@ -61,10 +61,18 @@ void yield(void); #define FALLING 2 #define RISING 3 -#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) -#define DEFAULT 0 -#define EXTERNAL 1 -#define INTERNAL 2 +#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) + #define DEFAULT 0 + #define EXTERNAL 1 + #define INTERNAL1V1 2 + #define INTERNAL INTERNAL1V1 +#elif defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) + #define DEFAULT 0 + #define EXTERNAL 1 + #define INTERNAL1V1 2 + #define INTERNAL INTERNAL1V1 + #define INTERNAL2V56 6 + #define INTERNAL2V56_EXTCAP 7 #else #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__) #define INTERNAL1V1 2 diff --git a/hardware/arduino/avr/cores/arduino/wiring_analog.c b/hardware/arduino/avr/cores/arduino/wiring_analog.c index 1a4701ab6..c545e6d52 100644 --- a/hardware/arduino/avr/cores/arduino/wiring_analog.c +++ b/hardware/arduino/avr/cores/arduino/wiring_analog.c @@ -64,7 +64,7 @@ int analogRead(uint8_t pin) // channel (low 4 bits). this also sets ADLAR (left-adjust result) // to 0 (the default). #if defined(ADMUX) - ADMUX = (analog_reference << 6) | (pin & 0x07); + ADMUX = ((analog_reference & 0x3) << 6) | ((analog_reference & 0x4) ? 0x10 : 0) | (pin & 0x07); #endif // without a delay, we seem to read from the wrong channel From a2a17a0c83afdd6812664f32161db0ccdb535ad6 Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig Date: Wed, 7 Sep 2016 19:02:48 +0200 Subject: [PATCH 2/2] Requested changes to not change code for non ATtinyX5s --- hardware/arduino/avr/cores/arduino/Arduino.h | 8 ++++---- hardware/arduino/avr/cores/arduino/wiring_analog.c | 6 +++++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/hardware/arduino/avr/cores/arduino/Arduino.h b/hardware/arduino/avr/cores/arduino/Arduino.h index e71c9c3e1..09c144895 100644 --- a/hardware/arduino/avr/cores/arduino/Arduino.h +++ b/hardware/arduino/avr/cores/arduino/Arduino.h @@ -68,11 +68,11 @@ void yield(void); #define INTERNAL INTERNAL1V1 #elif defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) #define DEFAULT 0 - #define EXTERNAL 1 - #define INTERNAL1V1 2 + #define EXTERNAL 4 + #define INTERNAL1V1 8 #define INTERNAL INTERNAL1V1 - #define INTERNAL2V56 6 - #define INTERNAL2V56_EXTCAP 7 + #define INTERNAL2V56 9 + #define INTERNAL2V56_EXTCAP 13 #else #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__) #define INTERNAL1V1 2 diff --git a/hardware/arduino/avr/cores/arduino/wiring_analog.c b/hardware/arduino/avr/cores/arduino/wiring_analog.c index c545e6d52..967c2b976 100644 --- a/hardware/arduino/avr/cores/arduino/wiring_analog.c +++ b/hardware/arduino/avr/cores/arduino/wiring_analog.c @@ -64,7 +64,11 @@ int analogRead(uint8_t pin) // channel (low 4 bits). this also sets ADLAR (left-adjust result) // to 0 (the default). #if defined(ADMUX) - ADMUX = ((analog_reference & 0x3) << 6) | ((analog_reference & 0x4) ? 0x10 : 0) | (pin & 0x07); +#if defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) + ADMUX = (analog_reference << 4) | (pin & 0x07); +#else + ADMUX = (analog_reference << 6) | (pin & 0x07); +#endif #endif // without a delay, we seem to read from the wrong channel