Driver for National Semiconductor ADC128Sxxx family of analog to digital converters
Revision 2:f6c4a79f2ee0, committed 2011-04-27
- Comitter:
- shimniok
- Date:
- Wed Apr 27 19:07:26 2011 +0000
- Parent:
- 1:0edd6142cd67
- Child:
- 3:e304ec6ed416
- Commit message:
- added methods to set channel, get current channel, and changed behavior of channel read method to permit sequential reads on each channel.
Changed in this revision
ADC128S.cpp | Show annotated file Show diff for this revision Revisions of this file |
ADC128S.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/ADC128S.cpp Wed Mar 02 05:50:24 2011 +0000 +++ b/ADC128S.cpp Wed Apr 27 19:07:26 2011 +0000 @@ -5,17 +5,44 @@ #include "mbed.h" #include "ADC128S.h" -ADC128S::ADC128S(PinName mosi, PinName miso, PinName sck, PinName cs) : _adc(mosi, miso, sck), _cs(cs) { +ADC128S::ADC128S(PinName mosi, PinName miso, PinName sck, PinName cs) : _adc(mosi, miso, sck), _cs(cs), _channel(0) { _adc.format(16,3); _adc.frequency(8000000); } +int ADC128S::getChannel() { + return _channel; +} + +void ADC128S::setChannel(int channel) { + _cs = 0; + _adc.write(channel<<11); // send channel for next acquisition; XXXAAAXX XXXXXXXX + _adc.write(channel<<11); // send channel for next acquisition; XXXAAAXX XXXXXXXX + _cs = 1; + _channel = channel; +} + +unsigned int ADC128S::read() { + unsigned int result = 0; + _cs = 0; + // get next acquisition, send next channel: XXXAAAXX XXXXXXXX + _channel++; + _channel %= 8; + result = _adc.write(_channel<<11); + _cs = 1; + + return result; +} + unsigned int ADC128S::read(int channel) { unsigned int result = 0; _cs = 0; _adc.write(channel<<11); // send channel for next acquisition; XXXAAAXX XXXXXXXX + _cs = 1; + wait_us(50); + _cs = 0; result = _adc.write(channel<<11); // get next acquisition _cs = 1; return result; -} \ No newline at end of file +}
--- a/ADC128S.h Wed Mar 02 05:50:24 2011 +0000 +++ b/ADC128S.h Wed Apr 27 19:07:26 2011 +0000 @@ -37,10 +37,30 @@ */ ADC128S(PinName mosi, PinName miso, PinName sck, PinName cs); + + /** Get the next channel to be converted with next call to read() + * + * @returns an integer representing the adc channel about to be converted + */ + int getChannel(void); + + /** Set channel to be used for the next conversion. + * + * @param channel is the adc channel you want to read with next call to read() + */ + void setChannel(int channel); + + /** Set channel to be used for the next conversion. + * + * @param channel is the adc channel you want to read with next call to read() + */ + void setChannel(int channel); + + /** Read in analog value from the specified ADC channel * * @param channel The ADC channel to read - * @param returns the analog to digital conversion result + * @returns the analog to digital conversion result */ unsigned int read(int channel);