diff options
Diffstat (limited to 'cores/arduino/wiring_analog.c')
-rwxr-xr-x | cores/arduino/wiring_analog.c | 85 |
1 files changed, 67 insertions, 18 deletions
diff --git a/cores/arduino/wiring_analog.c b/cores/arduino/wiring_analog.c index f3767d2..529ad52 100755 --- a/cores/arduino/wiring_analog.c +++ b/cores/arduino/wiring_analog.c @@ -37,12 +37,18 @@ void analogReference(uint8_t mode) int analogRead(uint8_t pin) { - uint8_t low, high, ch = analogInPinToBit(pin); + uint8_t low, high; // 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); + ADMUX = (analog_reference << 6) | (pin & 0x07); + +#if defined(__AVR_ATmega1280__) + // 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); +#endif // without a delay, we seem to read from the wrong channel //delay(1); @@ -95,23 +101,23 @@ void analogWrite(uint8_t pin, int val) OCR2 = val; #else } else if (digitalPinToTimer(pin) == TIMER0A) { - if (val == 0) { - digitalWrite(pin, LOW); - } else { - // connect pwm to pin on timer 0, channel A - sbi(TCCR0A, COM0A1); - // set pwm duty - OCR0A = val; - } + if (val == 0) { + digitalWrite(pin, LOW); + } else { + // connect pwm to pin on timer 0, channel A + sbi(TCCR0A, COM0A1); + // set pwm duty + OCR0A = val; + } } else if (digitalPinToTimer(pin) == TIMER0B) { - if (val == 0) { - digitalWrite(pin, LOW); - } else { - // connect pwm to pin on timer 0, channel B - sbi(TCCR0A, COM0B1); - // set pwm duty - OCR0B = val; - } + if (val == 0) { + digitalWrite(pin, LOW); + } else { + // connect pwm to pin on timer 0, channel B + sbi(TCCR0A, COM0B1); + // set pwm duty + OCR0B = val; + } } else if (digitalPinToTimer(pin) == TIMER2A) { // connect pwm to pin on timer 2, channel A sbi(TCCR2A, COM2A1); @@ -123,6 +129,49 @@ void analogWrite(uint8_t pin, int val) // set pwm duty OCR2B = val; #endif +#if defined(__AVR_ATmega1280__) + // XXX: need to handle other timers here + } else if (digitalPinToTimer(pin) == TIMER3A) { + // connect pwm to pin on timer 3, channel A + sbi(TCCR3A, COM3A1); + // set pwm duty + OCR3A = val; + } else if (digitalPinToTimer(pin) == TIMER3B) { + // connect pwm to pin on timer 3, channel B + sbi(TCCR3A, COM3B1); + // set pwm duty + OCR3B = val; + } else if (digitalPinToTimer(pin) == TIMER3C) { + // connect pwm to pin on timer 3, channel C + sbi(TCCR3A, COM3C1); + // set pwm duty + OCR3C = val; + } else if (digitalPinToTimer(pin) == TIMER4A) { + // connect pwm to pin on timer 4, channel A + sbi(TCCR4A, COM4A1); + // set pwm duty + OCR4A = val; + } else if (digitalPinToTimer(pin) == TIMER4B) { + // connect pwm to pin on timer 4, channel B + sbi(TCCR4A, COM4B1); + // set pwm duty + OCR4B = val; + } else if (digitalPinToTimer(pin) == TIMER4C) { + // connect pwm to pin on timer 4, channel C + sbi(TCCR4A, COM4C1); + // set pwm duty + OCR4C = val; + } else if (digitalPinToTimer(pin) == TIMER5A) { + // connect pwm to pin on timer 5, channel A + sbi(TCCR5A, COM5A1); + // set pwm duty + OCR5A = val; + } else if (digitalPinToTimer(pin) == TIMER5B) { + // connect pwm to pin on timer 5, channel B + sbi(TCCR5A, COM5B1); + // set pwm duty + OCR5B = val; +#endif } else if (val < 128) digitalWrite(pin, LOW); else |