Class similar to AnalogIn that uses burst mode to run continious background conversions so when the input is read, the last value can immediatly be returned. This slightly modified version allows NC pins.
Dependents: Pinscape_Controller
Fork of FastAnalogIn by
Diff: FastAnalogIn.cpp
- Revision:
- 1:575f4d2d6e9c
- Parent:
- 0:c2a7b899e6c7
diff -r c2a7b899e6c7 -r 575f4d2d6e9c FastAnalogIn.cpp --- a/FastAnalogIn.cpp Thu May 09 17:46:32 2013 +0000 +++ b/FastAnalogIn.cpp Sat May 11 08:56:22 2013 +0000 @@ -91,17 +91,15 @@ unsigned short FastAnalogIn::read_u16( void ) { + volatile unsigned int retval; //If object is enabled return current value of datareg - if (running ) { - unsigned int retval = *datareg; - retval = retval >> 4; - retval = retval & 4095; - return retval; - } + if (running ) + retval = *datareg; + //If it isn't running, enable it and wait until new value is written to datareg else { //Force a read to clear done bit, enable the ADC channel - volatile unsigned int retval = *datareg; + retval = *datareg; enable(); //Wait until it is converted while(1) { @@ -121,16 +119,17 @@ //Disable again disable(); - retval = retval >> 4; - retval = retval & 4095; - return retval; } - + + //Do same thing as standard mbed lib, unused bit 0-3, replicate 4-7 in it + retval &= ~0xFFFF000F; + retval |= (retval >> 8) & 0x000F; + return retval; }; float FastAnalogIn::read( void ) { unsigned short value = read_u16(); - return (float)value/4095; + return (float)value/65535; }