mirror of
https://github.com/LuiCat/ArduinoTaikoController.git
synced 2024-12-21 19:05:55 +01:00
70 lines
2.1 KiB
C++
70 lines
2.1 KiB
C++
|
#include "AnalogReadNow.h"
|
||
|
|
||
|
#include "wiring_private.h"
|
||
|
#include "pins_arduino.h"
|
||
|
|
||
|
void analogSwitchPin(uint8_t pin)
|
||
|
{
|
||
|
#if defined(analogPinToChannel)
|
||
|
#if defined(__AVR_ATmega32U4__)
|
||
|
if (pin >= 18) pin -= 18; // allow for channel or pin numbers
|
||
|
#endif
|
||
|
pin = analogPinToChannel(pin);
|
||
|
#elif 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
|
||
|
#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);
|
||
|
#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)
|
||
|
#if defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
|
||
|
ADMUX = (DEFAULT << 4) | (pin & 0x07);
|
||
|
#else
|
||
|
ADMUX = (DEFAULT << 6) | (pin & 0x07);
|
||
|
#endif
|
||
|
#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);
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
int analogReadNow()
|
||
|
{
|
||
|
#if defined(ADCSRA) && defined(ADCL)
|
||
|
// ADSC is cleared when the conversion finishes
|
||
|
while (bit_is_set(ADCSRA, ADSC));
|
||
|
|
||
|
// we have to read ADCL first; doing so locks both ADCL
|
||
|
// 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.
|
||
|
uint8_t low, high;
|
||
|
low = ADCL;
|
||
|
high = ADCH;
|
||
|
|
||
|
// combine the two bytes
|
||
|
return (high << 8) | low;
|
||
|
#else
|
||
|
// we dont have an ADC, return 0
|
||
|
return 0;
|
||
|
#endif
|
||
|
}
|