Driver for National Semiconductor ADC128Sxxx family of analog to digital converters

Committer:
shimniok
Date:
Wed Apr 27 19:07:26 2011 +0000
Revision:
2:f6c4a79f2ee0
Parent:
1:0edd6142cd67
Child:
3:e304ec6ed416
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?

UserRevisionLine numberNew contents of line
shimniok 1:0edd6142cd67 1 /* mbed library for the National Semiconductor ADC128S family of analog to digital converters
shimniok 1:0edd6142cd67 2 *
shimniok 1:0edd6142cd67 3 * by Michael Shimniok - http://www.bot-thoughts.com/
shimniok 1:0edd6142cd67 4 */
shimniok 1:0edd6142cd67 5
shimniok 1:0edd6142cd67 6 /** An interface for driving a National Semiconductor ADC128Sxxx analog to digital converter using SPI
shimniok 1:0edd6142cd67 7 *
shimniok 1:0edd6142cd67 8 * @code
shimniok 1:0edd6142cd67 9 * #include "mbed.h"
shimniok 1:0edd6142cd67 10 * #include "ADC128S.h"
shimniok 1:0edd6142cd67 11 *
shimniok 1:0edd6142cd67 12 * Serial pc(USBTX, USBRX); // tx, rx
shimniok 1:0edd6142cd67 13 * ADC128S adc(p5, p6, p7, p15); // mosi, miso, sck, cs
shimniok 1:0edd6142cd67 14 *
shimniok 1:0edd6142cd67 15 * int main() {
shimniok 1:0edd6142cd67 16 * unsigned int result;
shimniok 1:0edd6142cd67 17 * pc.baud(115200);
shimniok 1:0edd6142cd67 18 *
shimniok 1:0edd6142cd67 19 * while(1) {
shimniok 1:0edd6142cd67 20 * for (int i = 0; i < 8; i++) {
shimniok 1:0edd6142cd67 21 * result = adc.read(i);
shimniok 1:0edd6142cd67 22 * pc.printf("ADC(%d)=%d\n", i, result);
shimniok 1:0edd6142cd67 23 * }
shimniok 1:0edd6142cd67 24 * pc.printf("\n");
shimniok 1:0edd6142cd67 25 * }
shimniok 1:0edd6142cd67 26 * }
shimniok 1:0edd6142cd67 27 * @endcode
shimniok 1:0edd6142cd67 28 */
shimniok 1:0edd6142cd67 29 class ADC128S {
shimniok 1:0edd6142cd67 30 public:
shimniok 1:0edd6142cd67 31 /** Create an ADC128S interface
shimniok 1:0edd6142cd67 32 *
shimniok 1:0edd6142cd67 33 * @param mosi MOSI line
shimniok 1:0edd6142cd67 34 * @param miso MISO line
shimniok 1:0edd6142cd67 35 * @param sck SCK line
shimniok 1:0edd6142cd67 36 * @param cs !CS/!SS line
shimniok 1:0edd6142cd67 37 */
shimniok 1:0edd6142cd67 38 ADC128S(PinName mosi, PinName miso, PinName sck, PinName cs);
shimniok 1:0edd6142cd67 39
shimniok 2:f6c4a79f2ee0 40
shimniok 2:f6c4a79f2ee0 41 /** Get the next channel to be converted with next call to read()
shimniok 2:f6c4a79f2ee0 42 *
shimniok 2:f6c4a79f2ee0 43 * @returns an integer representing the adc channel about to be converted
shimniok 2:f6c4a79f2ee0 44 */
shimniok 2:f6c4a79f2ee0 45 int getChannel(void);
shimniok 2:f6c4a79f2ee0 46
shimniok 2:f6c4a79f2ee0 47 /** Set channel to be used for the next conversion.
shimniok 2:f6c4a79f2ee0 48 *
shimniok 2:f6c4a79f2ee0 49 * @param channel is the adc channel you want to read with next call to read()
shimniok 2:f6c4a79f2ee0 50 */
shimniok 2:f6c4a79f2ee0 51 void setChannel(int channel);
shimniok 2:f6c4a79f2ee0 52
shimniok 2:f6c4a79f2ee0 53 /** Set channel to be used for the next conversion.
shimniok 2:f6c4a79f2ee0 54 *
shimniok 2:f6c4a79f2ee0 55 * @param channel is the adc channel you want to read with next call to read()
shimniok 2:f6c4a79f2ee0 56 */
shimniok 2:f6c4a79f2ee0 57 void setChannel(int channel);
shimniok 2:f6c4a79f2ee0 58
shimniok 2:f6c4a79f2ee0 59
shimniok 1:0edd6142cd67 60 /** Read in analog value from the specified ADC channel
shimniok 1:0edd6142cd67 61 *
shimniok 1:0edd6142cd67 62 * @param channel The ADC channel to read
shimniok 2:f6c4a79f2ee0 63 * @returns the analog to digital conversion result
shimniok 1:0edd6142cd67 64 */
shimniok 1:0edd6142cd67 65 unsigned int read(int channel);
shimniok 1:0edd6142cd67 66
shimniok 1:0edd6142cd67 67 private:
shimniok 1:0edd6142cd67 68 SPI _adc;
shimniok 1:0edd6142cd67 69 DigitalOut _cs;
shimniok 0:28addf1f4c26 70 };