From d0b0f3e45d5c4cf3a7fbcf5832c5e6cc764016e0 Mon Sep 17 00:00:00 2001 From: Zach Eveland Date: Tue, 13 Sep 2011 23:52:26 -0400 Subject: fixed analog pin mapping for 32u4 --- cores/arduino/wiring_analog.c | 7 +++++-- variants/leonardo/pins_arduino.h | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/cores/arduino/wiring_analog.c b/cores/arduino/wiring_analog.c index d248f4c..62b91cf 100644 --- a/cores/arduino/wiring_analog.c +++ b/cores/arduino/wiring_analog.c @@ -46,8 +46,11 @@ int analogRead(uint8_t pin) #else if (pin >= 14) pin -= 14; // allow for channel or pin numbers #endif - -#if defined(ADCSRB) && defined(MUX5) + +#if defined(__AVR_ATmega32U4__) + pin = analogPinToChannel(pin); + ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((pin >> 3) & 0x01) << MUX5); +#elif defined(ADCSRB) && defined(MUX5) // the MUX5 bit of ADCSRB selects whether we're reading from channels // 0 to 7 (MUX5 low) or 8 to 15 (MUX5 high). ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((pin >> 3) & 0x01) << MUX5); diff --git a/variants/leonardo/pins_arduino.h b/variants/leonardo/pins_arduino.h index 6fc3ea9..65ab5e2 100644 --- a/variants/leonardo/pins_arduino.h +++ b/variants/leonardo/pins_arduino.h @@ -48,6 +48,10 @@ const static uint8_t A5 = 19; const static uint8_t A6 = 20; const static uint8_t A7 = 21; +// __AVR_ATmega32U4__ has an unusual mapping of pins to channels +extern const uint8_t PROGMEM analog_pin_to_channel_PGM[]; +#define analogPinToChannel(P) ( pgm_read_byte( analog_pin_to_channel_PGM + (P) ) ) + #ifdef ARDUINO_MAIN // On the Arduino board, digital pins are also used @@ -228,6 +232,35 @@ const uint8_t PROGMEM digital_pin_to_timer_PGM[18] = { NOT_ON_TIMER, }; +const uint8_t PROGMEM analog_pin_to_channel_PGM[11] = { + /* + A0 PF7 ADC7 + A1 PF6 ADC6 + A2 PF5 ADC5 + A3 PF4 ADC4 + A4 PF1 ADC1 + A5 PF0 ADC0 + D4 A6 PD4 ADC8 + D5 A7 PD6 FastPWM OC4D/ADC9 + D6 A8 PD7 FastPWM #OC4D/ADC10 + D9 A9 PB5 PWM16 OC1A/#OC4B/ADC13/PCINT5 + D10 A10 PB6 PWM16 OC1B/0c4B/ADC12/PCINT6 + */ + + 7, + 6, + 5, + 4, + 1, + 0, + 8, + 9, + + 10, + 13, + 12 +}; + #endif #endif \ No newline at end of file -- cgit v1.2.3-18-g5258