A library to interface to the MCP3208 SPI-based ADC from Microchip. This chip provides eight analogue inputs, providing converted 12-bit values via SPI.
Dependents: Nucleo_MCP3208_Test Nucleo_MCP3208_Ticker_Test BBMv2_eps ref_BBMv2_eps ... more
mcp3208.h@1:316f86115221, 2011-05-24 (annotated)
- Committer:
- Kemp
- Date:
- Tue May 24 13:58:00 2011 +0000
- Revision:
- 1:316f86115221
- Parent:
- 0:d37e8cb188c1
- Child:
- 2:93009a423b45
Now allows reading from differential inputs. Started documenting, fixed some silly bugs.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kemp | 0:d37e8cb188c1 | 1 | #include "mbed.h" |
Kemp | 0:d37e8cb188c1 | 2 | |
Kemp | 0:d37e8cb188c1 | 3 | |
Kemp | 0:d37e8cb188c1 | 4 | #ifndef MCP3208_H |
Kemp | 0:d37e8cb188c1 | 5 | #define MCP3208_H |
Kemp | 0:d37e8cb188c1 | 6 | |
Kemp | 0:d37e8cb188c1 | 7 | |
Kemp | 1:316f86115221 | 8 | enum Polarity { |
Kemp | 1:316f86115221 | 9 | POL_EVEN_POSITIVE, |
Kemp | 1:316f86115221 | 10 | POL_EVEN_NEGATIVE |
Kemp | 1:316f86115221 | 11 | }; |
Kemp | 1:316f86115221 | 12 | |
Kemp | 1:316f86115221 | 13 | |
Kemp | 1:316f86115221 | 14 | /** Class for interfacing to the MCP3208 SPI-based ADC. |
Kemp | 1:316f86115221 | 15 | * |
Kemp | 1:316f86115221 | 16 | * This class will also allow interfacing to the MCP3204, but only four |
Kemp | 1:316f86115221 | 17 | * inputs are provided by that chip, as opposed to the eight of the MCP3208. |
Kemp | 1:316f86115221 | 18 | */ |
Kemp | 0:d37e8cb188c1 | 19 | class MCP3208 |
Kemp | 0:d37e8cb188c1 | 20 | { |
Kemp | 0:d37e8cb188c1 | 21 | public: |
Kemp | 0:d37e8cb188c1 | 22 | MCP3208(SPI bus, PinName cs); |
Kemp | 0:d37e8cb188c1 | 23 | ~MCP3208(); |
Kemp | 0:d37e8cb188c1 | 24 | |
Kemp | 1:316f86115221 | 25 | /** Read from a single-ended input. |
Kemp | 1:316f86115221 | 26 | * |
Kemp | 1:316f86115221 | 27 | * @param channel The channel number to read from. |
Kemp | 1:316f86115221 | 28 | * |
Kemp | 1:316f86115221 | 29 | * @param returns The sampled value as a float between 0.0 and 1.0. |
Kemp | 1:316f86115221 | 30 | */ |
Kemp | 1:316f86115221 | 31 | float read_input(int channel); |
Kemp | 1:316f86115221 | 32 | |
Kemp | 1:316f86115221 | 33 | /** Read from a pair of differential inputs. |
Kemp | 1:316f86115221 | 34 | * |
Kemp | 1:316f86115221 | 35 | * In differential mode, the channels are referred to as 0 to 3, with |
Kemp | 1:316f86115221 | 36 | * polarity set in a separate parameter. This avoids the user having to set |
Kemp | 1:316f86115221 | 37 | * the polarity as part of the channel number or having channel numbers |
Kemp | 1:316f86115221 | 38 | * increase by two (i.e. the channels being 0, 2, 4, and 6). |
Kemp | 1:316f86115221 | 39 | * |
Kemp | 1:316f86115221 | 40 | * @param channel The channel number to read from. |
Kemp | 1:316f86115221 | 41 | * @param polarity The polarity of the differential signal. |
Kemp | 1:316f86115221 | 42 | * |
Kemp | 1:316f86115221 | 43 | * @param returns The sampled value as a float between 0.0 and 1.0. |
Kemp | 1:316f86115221 | 44 | */ |
Kemp | 1:316f86115221 | 45 | float read_diff_input(int channel, Polarity polarity); |
Kemp | 0:d37e8cb188c1 | 46 | |
Kemp | 0:d37e8cb188c1 | 47 | private: |
Kemp | 0:d37e8cb188c1 | 48 | DigitalOut m_cs; |
Kemp | 0:d37e8cb188c1 | 49 | SPI m_bus; |
Kemp | 0:d37e8cb188c1 | 50 | |
Kemp | 0:d37e8cb188c1 | 51 | void select(); |
Kemp | 0:d37e8cb188c1 | 52 | void deselect(); |
Kemp | 0:d37e8cb188c1 | 53 | }; |
Kemp | 0:d37e8cb188c1 | 54 | |
Kemp | 0:d37e8cb188c1 | 55 | |
Kemp | 0:d37e8cb188c1 | 56 | #endif |