int analogRead(uint8_t pin) { #if defined( NUM_DIGITAL_PINS ) if ( pin >= NUM_DIGITAL_PINS ) pin -= NUM_DIGITAL_PINS; // allow for channel or pin numbers #endif // fix? Validate pin? if(pin >= NUM_ANALOG_INPUTS) return 0; //Not a valid pin. #ifndef ADCSRA return digitalRead(analogInputToDigitalPin(pin)) ? 1023 : 0; //No ADC, so read as a digital pin instead. #endif #if defined(ADMUX) ADMUX = ((analog_reference & ADMUX_REFS_MASK) << REFS0) | ((pin & ADMUX_MUX_MASK) << MUX0); //select the channel and reference #if defined(REFS2) ADMUX |= (((analog_reference & 0x04) >> 2) << REFS2); //some have an extra reference bit in a weird position. #endif #endif #if defined(HAVE_ADC) && HAVE_ADC sbi(ADCSRA, ADSC); //Start conversion while(ADCSRA & (1<<ADSC)); //Wait for conversion to complete. uint8_t low = ADCL; uint8_t high = ADCH; return (high << 8) | low; #else return LOW; #endif }
int16_t Babuino::readAnalog(uint8_t i) { if (i < NUM_ANALOG_INPUTS) // defined in pins_arduino.h return analogRead(analogInputToDigitalPin(i)); return -1; }