fixed analog pin mapping for 32u4

This commit is contained in:
Zach Eveland 2011-09-13 23:52:26 -04:00
parent 18d76057fe
commit d4e9db463b
2 changed files with 38 additions and 2 deletions

View File

@ -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);

View File

@ -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