Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This code is modified from the original analogRead function provided in Arduino's source code (wiring_analog.c)
- // This function can be appended directly to the stock file wiring_analog.c
- uint8_t analogRead8bit(uint8_t pin)
- {
- uint8_t result;
- #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
- if (pin >= 54) pin -= 54; // allow for channel or pin numbers
- #elif defined(__AVR_ATmega32U4__)
- if (pin >= 18) pin -= 18; // allow for channel or pin numbers
- #elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__)
- if (pin >= 24) pin -= 24; // allow for channel or pin numbers
- #elif defined(analogPinToChannel) && (defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__))
- pin = analogPinToChannel(pin);
- #else
- if (pin >= 14) pin -= 14; // allow for channel or pin numbers
- #endif
- #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);
- #endif
- #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)
- // start the conversion
- sbi(ADCSRA, ADSC);
- // ADSC is cleared when the conversion finishes
- while (bit_is_set(ADCSRA, ADSC));
- //read only ADCH, 8 bits
- result = ADCH;
- #else
- // we don't have an ADC, return 0
- result = 0;
- #endif
- // combine the two bytes
- return result;
- }
Add Comment
Please, Sign In to add comment