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
00001 /** 00002 * @file mcp3208.h 00003 */ 00004 00005 #include "mbed.h" 00006 00007 00008 #ifndef MCP3208_H 00009 #define MCP3208_H 00010 00011 00012 /** Polarity setting for differential inputs. 00013 * 00014 * POL_EVEN_POSITIVE sets channel [0|2|4|6] as the positive side and channel 00015 * [1|3|5|7] as the negative side. POL_EVEN_NEGATIVE sets the opposite. 00016 */ 00017 enum Polarity { 00018 POL_EVEN_POSITIVE, 00019 POL_EVEN_NEGATIVE 00020 }; 00021 00022 00023 /** Class for interfacing to the MCP3208 SPI-based ADC. 00024 * 00025 * This class will also allow interfacing to the MCP3204, but only four 00026 * inputs are provided by that chip, as opposed to the eight of the MCP3208. 00027 */ 00028 class MCP3208 00029 { 00030 public: 00031 /** Create an MCP3208 object. 00032 * 00033 * @param bus An SPI bus object. 00034 * @param cs The name of a pin to use as the chip select. 00035 */ 00036 MCP3208(SPI bus, PinName cs); 00037 ~MCP3208(); 00038 00039 /** Read from a single-ended input. 00040 * 00041 * @param channel The channel number to read from. 00042 * 00043 * @param returns The sampled value as a float between 0.0 and 1.0. 00044 */ 00045 float read_input(int channel); 00046 00047 /** Read from a pair of differential inputs. 00048 * 00049 * In differential mode, the channels are referred to as 0 to 3, with 00050 * polarity set in a separate parameter. This avoids the user having to set 00051 * the polarity as part of the channel number or having channel numbers 00052 * increase by two (i.e. the channels being 0, 2, 4, and 6). 00053 * 00054 * @param channel The channel number to read from. 00055 * @param polarity The polarity of the differential signal. 00056 * 00057 * @param returns The sampled value as a float between 0.0 and 1.0. 00058 */ 00059 float read_diff_input(int channel, Polarity polarity); 00060 00061 private: 00062 DigitalOut m_cs; 00063 SPI m_bus; 00064 00065 void select(); 00066 void deselect(); 00067 }; 00068 00069 00070 #endif
Generated on Mon Jul 18 2022 21:57:39 by 1.7.2