Driver for National Semiconductor ADC128Sxxx family of analog to digital converters
ADC128S.cpp@2:f6c4a79f2ee0, 2011-04-27 (annotated)
- Committer:
- shimniok
- Date:
- Wed Apr 27 19:07:26 2011 +0000
- Revision:
- 2:f6c4a79f2ee0
- Parent:
- 1:0edd6142cd67
added methods to set channel, get current channel, and changed behavior of channel read method to permit sequential reads on each channel.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shimniok | 0:28addf1f4c26 | 1 | // ADC128S a library for the National Semiconductor ADC128S family of ADCs |
shimniok | 0:28addf1f4c26 | 2 | // |
shimniok | 0:28addf1f4c26 | 3 | // by Michael Shimniok - http://www.bot-thoughts.com/ |
shimniok | 0:28addf1f4c26 | 4 | // |
shimniok | 0:28addf1f4c26 | 5 | #include "mbed.h" |
shimniok | 0:28addf1f4c26 | 6 | #include "ADC128S.h" |
shimniok | 0:28addf1f4c26 | 7 | |
shimniok | 2:f6c4a79f2ee0 | 8 | ADC128S::ADC128S(PinName mosi, PinName miso, PinName sck, PinName cs) : _adc(mosi, miso, sck), _cs(cs), _channel(0) { |
shimniok | 0:28addf1f4c26 | 9 | _adc.format(16,3); |
shimniok | 0:28addf1f4c26 | 10 | _adc.frequency(8000000); |
shimniok | 0:28addf1f4c26 | 11 | } |
shimniok | 0:28addf1f4c26 | 12 | |
shimniok | 2:f6c4a79f2ee0 | 13 | int ADC128S::getChannel() { |
shimniok | 2:f6c4a79f2ee0 | 14 | return _channel; |
shimniok | 2:f6c4a79f2ee0 | 15 | } |
shimniok | 2:f6c4a79f2ee0 | 16 | |
shimniok | 2:f6c4a79f2ee0 | 17 | void ADC128S::setChannel(int channel) { |
shimniok | 2:f6c4a79f2ee0 | 18 | _cs = 0; |
shimniok | 2:f6c4a79f2ee0 | 19 | _adc.write(channel<<11); // send channel for next acquisition; XXXAAAXX XXXXXXXX |
shimniok | 2:f6c4a79f2ee0 | 20 | _adc.write(channel<<11); // send channel for next acquisition; XXXAAAXX XXXXXXXX |
shimniok | 2:f6c4a79f2ee0 | 21 | _cs = 1; |
shimniok | 2:f6c4a79f2ee0 | 22 | _channel = channel; |
shimniok | 2:f6c4a79f2ee0 | 23 | } |
shimniok | 2:f6c4a79f2ee0 | 24 | |
shimniok | 2:f6c4a79f2ee0 | 25 | unsigned int ADC128S::read() { |
shimniok | 2:f6c4a79f2ee0 | 26 | unsigned int result = 0; |
shimniok | 2:f6c4a79f2ee0 | 27 | _cs = 0; |
shimniok | 2:f6c4a79f2ee0 | 28 | // get next acquisition, send next channel: XXXAAAXX XXXXXXXX |
shimniok | 2:f6c4a79f2ee0 | 29 | _channel++; |
shimniok | 2:f6c4a79f2ee0 | 30 | _channel %= 8; |
shimniok | 2:f6c4a79f2ee0 | 31 | result = _adc.write(_channel<<11); |
shimniok | 2:f6c4a79f2ee0 | 32 | _cs = 1; |
shimniok | 2:f6c4a79f2ee0 | 33 | |
shimniok | 2:f6c4a79f2ee0 | 34 | return result; |
shimniok | 2:f6c4a79f2ee0 | 35 | } |
shimniok | 2:f6c4a79f2ee0 | 36 | |
shimniok | 0:28addf1f4c26 | 37 | unsigned int ADC128S::read(int channel) { |
shimniok | 0:28addf1f4c26 | 38 | unsigned int result = 0; |
shimniok | 0:28addf1f4c26 | 39 | _cs = 0; |
shimniok | 0:28addf1f4c26 | 40 | _adc.write(channel<<11); // send channel for next acquisition; XXXAAAXX XXXXXXXX |
shimniok | 2:f6c4a79f2ee0 | 41 | _cs = 1; |
shimniok | 2:f6c4a79f2ee0 | 42 | wait_us(50); |
shimniok | 2:f6c4a79f2ee0 | 43 | _cs = 0; |
shimniok | 0:28addf1f4c26 | 44 | result = _adc.write(channel<<11); // get next acquisition |
shimniok | 0:28addf1f4c26 | 45 | _cs = 1; |
shimniok | 0:28addf1f4c26 | 46 | |
shimniok | 0:28addf1f4c26 | 47 | return result; |
shimniok | 2:f6c4a79f2ee0 | 48 | } |