diff options
author | David A. Mellis <d.mellis@arduino.cc> | 2010-10-17 17:55:53 -0400 |
---|---|---|
committer | David A. Mellis <d.mellis@arduino.cc> | 2010-10-17 17:55:53 -0400 |
commit | a4afb42b08555310142d01bbf346285e4fc9bff5 (patch) | |
tree | 9fe489e064ce72475a0f1dcc348f5cd66a7f54ed /cores/arduino/wiring_analog.c | |
parent | b861fe903f5a91990667e0eb47d2f83586bd0408 (diff) |
Modifying basic functions (digital and analog, read and write) to use register-based ifdefs, not cpu-based.
http://code.google.com/p/arduino/issues/detail?id=307
http://code.google.com/p/arduino/issues/detail?id=316
http://code.google.com/p/arduino/issues/detail?id=323
http://code.google.com/p/arduino/issues/detail?id=324
Diffstat (limited to 'cores/arduino/wiring_analog.c')
-rw-r--r--[-rwxr-xr-x] | cores/arduino/wiring_analog.c | 266 |
1 files changed, 171 insertions, 95 deletions
diff --git a/cores/arduino/wiring_analog.c b/cores/arduino/wiring_analog.c index da0cf07..d248f4c 100755..100644 --- a/cores/arduino/wiring_analog.c +++ b/cores/arduino/wiring_analog.c @@ -19,6 +19,8 @@ Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + Modified 28 September 2010 by Mark Sproul + $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $ */ @@ -41,22 +43,27 @@ int analogRead(uint8_t pin) #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) if (pin >= 54) pin -= 54; // allow for channel or pin numbers +#else + if (pin >= 14) pin -= 14; // allow for channel or pin numbers +#endif +#if 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); -#else - if (pin >= 14) pin -= 14; // allow for channel or pin numbers #endif // 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). +#if defined(ADMUX) ADMUX = (analog_reference << 6) | (pin & 0x07); +#endif // without a delay, we seem to read from the wrong channel //delay(1); +#if defined(ADCSRA) && defined(ADCL) // start the conversion sbi(ADCSRA, ADSC); @@ -67,8 +74,13 @@ int analogRead(uint8_t pin) // and ADCH until ADCH is read. reading ADCL second would // cause the results of each conversion to be discarded, // as ADCL and ADCH would be locked when it completed. - low = ADCL; + low = ADCL; high = ADCH; +#else + // we dont have an ADC, return 0 + low = 0; + high = 0; +#endif // combine the two bytes return (high << 8) | low; @@ -86,98 +98,162 @@ void analogWrite(uint8_t pin, int val) // for consistenty with Wiring, which doesn't require a pinMode // call for the analog output pins. pinMode(pin, OUTPUT); - - if (digitalPinToTimer(pin) == TIMER1A) { - // connect pwm to pin on timer 1, channel A - sbi(TCCR1A, COM1A1); - // set pwm duty - OCR1A = val; - } else if (digitalPinToTimer(pin) == TIMER1B) { - // connect pwm to pin on timer 1, channel B - sbi(TCCR1A, COM1B1); - // set pwm duty - OCR1B = val; -#if defined(__AVR_ATmega8__) - } else if (digitalPinToTimer(pin) == TIMER2) { - // connect pwm to pin on timer 2, channel B - sbi(TCCR2, COM21); - // set pwm duty - 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; - } - } 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; - } - } else if (digitalPinToTimer(pin) == TIMER2A) { - // connect pwm to pin on timer 2, channel A - sbi(TCCR2A, COM2A1); - // set pwm duty - OCR2A = val; - } else if (digitalPinToTimer(pin) == TIMER2B) { - // connect pwm to pin on timer 2, channel B - sbi(TCCR2A, COM2B1); - // set pwm duty - OCR2B = val; -#endif -#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) - // 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) + if (val == 0) + { digitalWrite(pin, LOW); - else + } + else if (val == 255) + { digitalWrite(pin, HIGH); + } + else + { + switch(digitalPinToTimer(pin)) + { + // XXX fix needed for atmega8 + #if defined(TCCR0) && defined(COM00) && !defined(__AVR_ATmega8__) + case TIMER0A: + // connect pwm to pin on timer 0 + sbi(TCCR0, COM00); + OCR0 = val; // set pwm duty + break; + #endif + + #if defined(TCCR0A) && defined(COM0A1) + case TIMER0A: + // connect pwm to pin on timer 0, channel A + sbi(TCCR0A, COM0A1); + OCR0A = val; // set pwm duty + break; + #endif + + #if defined(TCCR0A) && defined(COM0B1) + case TIMER0B: + // connect pwm to pin on timer 0, channel B + sbi(TCCR0A, COM0B1); + OCR0B = val; // set pwm duty + break; + #endif + + #if defined(TCCR1A) && defined(COM1A1) + case TIMER1A: + // connect pwm to pin on timer 1, channel A + sbi(TCCR1A, COM1A1); + OCR1A = val; // set pwm duty + break; + #endif + + #if defined(TCCR1A) && defined(COM1B1) + case TIMER1B: + // connect pwm to pin on timer 1, channel B + sbi(TCCR1A, COM1B1); + OCR1B = val; // set pwm duty + break; + #endif + + #if defined(TCCR2) && defined(COM21) + case TIMER2: + // connect pwm to pin on timer 2 + sbi(TCCR2, COM21); + OCR2 = val; // set pwm duty + break; + #endif + + #if defined(TCCR2A) && defined(COM2A1) + case TIMER2A: + // connect pwm to pin on timer 2, channel A + sbi(TCCR2A, COM2A1); + OCR2A = val; // set pwm duty + break; + #endif + + #if defined(TCCR2A) && defined(COM2B1) + case TIMER2B: + // connect pwm to pin on timer 2, channel B + sbi(TCCR2A, COM2B1); + OCR2B = val; // set pwm duty + break; + #endif + + #if defined(TCCR3A) && defined(COM3A1) + case TIMER3A: + // connect pwm to pin on timer 3, channel A + sbi(TCCR3A, COM3A1); + OCR3A = val; // set pwm duty + break; + #endif + + #if defined(TCCR3A) && defined(COM3B1) + case TIMER3B: + // connect pwm to pin on timer 3, channel B + sbi(TCCR3A, COM3B1); + OCR3B = val; // set pwm duty + break; + #endif + + #if defined(TCCR3A) && defined(COM3C1) + case TIMER3C: + // connect pwm to pin on timer 3, channel C + sbi(TCCR3A, COM3C1); + OCR3C = val; // set pwm duty + break; + #endif + + #if defined(TCCR4A) && defined(COM4A1) + case TIMER4A: + // connect pwm to pin on timer 4, channel A + sbi(TCCR4A, COM4A1); + OCR4A = val; // set pwm duty + break; + #endif + + #if defined(TCCR4A) && defined(COM4B1) + case TIMER4B: + // connect pwm to pin on timer 4, channel B + sbi(TCCR4A, COM4B1); + OCR4B = val; // set pwm duty + break; + #endif + + #if defined(TCCR4A) && defined(COM4C1) + case TIMER4C: + // connect pwm to pin on timer 4, channel C + sbi(TCCR4A, COM4C1); + OCR4C = val; // set pwm duty + break; + #endif + + #if defined(TCCR5A) && defined(COM5A1) + case TIMER5A: + // connect pwm to pin on timer 5, channel A + sbi(TCCR5A, COM5A1); + OCR5A = val; // set pwm duty + break; + #endif + + #if defined(TCCR5A) && defined(COM5B1) + case TIMER5B: + // connect pwm to pin on timer 5, channel B + sbi(TCCR5A, COM5B1); + OCR5B = val; // set pwm duty + break; + #endif + + #if defined(TCCR5A) && defined(COM5C1) + case TIMER5C: + // connect pwm to pin on timer 5, channel C + sbi(TCCR5A, COM5C1); + OCR5C = val; // set pwm duty + break; + #endif + + case NOT_ON_TIMER: + default: + if (val < 128) { + digitalWrite(pin, LOW); + } else { + digitalWrite(pin, HIGH); + } + } + } } |