GY-63_MS5611/libraries/AsyncAnalog/AsyncAnalog.cpp

69 lines
1.5 KiB
C++
Raw Normal View History

2021-01-29 06:31:58 -05:00
//
// FILE: AsyncAnalog.cpp
// AUTHOR: Rob Tillaart
2023-10-17 13:46:28 -04:00
// VERSION: 0.1.6
2021-01-29 06:31:58 -05:00
// DATE: 2018-09-05
2023-10-17 13:46:28 -04:00
// PURPOSE: Async version of analogRead, prevent blocking wait
// URL: https://github.com/RobTillaart/AsyncAnalog
2021-01-29 06:31:58 -05:00
#include "AsyncAnalog.h"
2023-10-17 13:46:28 -04:00
2021-01-29 06:31:58 -05:00
#if defined(ARDUINO_ARCH_AVR)
AsyncAnalog::AsyncAnalog(const uint8_t pin)
{
_pin = pin;
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
if (_pin >= 54) _pin -= 54;
#else
if (_pin >= 14) _pin -= 14;
#endif
}
2021-10-19 05:14:50 -04:00
2021-01-29 06:31:58 -05:00
void AsyncAnalog::start()
{
#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
#if defined(ADMUX)
2021-10-19 05:14:50 -04:00
// set the analogue reference (high two bits of ADMUX) and select the
2021-01-29 06:31:58 -05:00
// channel (low 4 bits). this also sets ADLAR (left-adjust result)
// to 0 (the default).
ADMUX = (DEFAULT << 6) | (_pin & 0x07);
#endif
sbi(ADCSRA, ADSC);
}
bool AsyncAnalog::ready()
{
// ADSC is cleared when the conversion finishes
return bit_is_set(ADCSRA, ADSC) == 0;
}
int AsyncAnalog::value()
{
2023-10-17 13:46:28 -04:00
// ADCL has to be read 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.
uint16_t lo = ADCL;
uint16_t hi = ADCH;
// Combine two parts.
// _lastValue = (hi * 256) + lo;
return (hi * 256) + lo;
2021-01-29 06:31:58 -05:00
}
2023-10-17 13:46:28 -04:00
#endif // ARDUINO_ARCH_AVR
2021-01-29 06:31:58 -05:00
2021-10-19 05:14:50 -04:00
2023-10-17 13:46:28 -04:00
// -- END OF FILE --