A Library for MCP3008

Dependents:   Nucleo_MCP3008_Test ProjetoBB KIK01 MASTER_SPI_MCP3008

Committer:
ryood
Date:
Fri Jun 09 03:59:25 2017 +0000
Revision:
0:77e81ce22442
Child:
1:49d7a43f1368
first commit (diff mode not tested)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ryood 0:77e81ce22442 1 /**
ryood 0:77e81ce22442 2 * @file mcp32008.h
ryood 0:77e81ce22442 3 */
ryood 0:77e81ce22442 4
ryood 0:77e81ce22442 5 #include "mbed.h"
ryood 0:77e81ce22442 6
ryood 0:77e81ce22442 7
ryood 0:77e81ce22442 8 #ifndef MCP3008_H
ryood 0:77e81ce22442 9 #define MCP3008_H
ryood 0:77e81ce22442 10
ryood 0:77e81ce22442 11
ryood 0:77e81ce22442 12 /** Polarity setting for differential inputs.
ryood 0:77e81ce22442 13 *
ryood 0:77e81ce22442 14 * POL_EVEN_POSITIVE sets channel [0|2|4|6] as the positive side and channel
ryood 0:77e81ce22442 15 * [1|3|5|7] as the negative side. POL_EVEN_NEGATIVE sets the opposite.
ryood 0:77e81ce22442 16 */
ryood 0:77e81ce22442 17 enum Polarity {
ryood 0:77e81ce22442 18 POL_EVEN_POSITIVE,
ryood 0:77e81ce22442 19 POL_EVEN_NEGATIVE
ryood 0:77e81ce22442 20 };
ryood 0:77e81ce22442 21
ryood 0:77e81ce22442 22
ryood 0:77e81ce22442 23 /** Class for interfacing to the MCP3008 SPI-based ADC.
ryood 0:77e81ce22442 24 *
ryood 0:77e81ce22442 25 */
ryood 0:77e81ce22442 26 class MCP3008
ryood 0:77e81ce22442 27 {
ryood 0:77e81ce22442 28 public:
ryood 0:77e81ce22442 29 /** Create an MCP3008 object.
ryood 0:77e81ce22442 30 *
ryood 0:77e81ce22442 31 * @param bus An SPI bus object.
ryood 0:77e81ce22442 32 * @param cs The name of a pin to use as the chip select.
ryood 0:77e81ce22442 33 */
ryood 0:77e81ce22442 34 MCP3008(SPI bus, PinName cs);
ryood 0:77e81ce22442 35 ~MCP3008();
ryood 0:77e81ce22442 36
ryood 0:77e81ce22442 37 /** Read from a single-ended input.
ryood 0:77e81ce22442 38 *
ryood 0:77e81ce22442 39 * @param channel The channel number to read from.
ryood 0:77e81ce22442 40 *
ryood 0:77e81ce22442 41 * @param returns The sampled value as a float between 0.0 and 1.0.
ryood 0:77e81ce22442 42 */
ryood 0:77e81ce22442 43 float read_input(int channel);
ryood 0:77e81ce22442 44
ryood 0:77e81ce22442 45 /** Read from a pair of differential inputs.
ryood 0:77e81ce22442 46 *
ryood 0:77e81ce22442 47 * In differential mode, the channels are referred to as 0 to 3, with
ryood 0:77e81ce22442 48 * polarity set in a separate parameter. This avoids the user having to set
ryood 0:77e81ce22442 49 * the polarity as part of the channel number or having channel numbers
ryood 0:77e81ce22442 50 * increase by two (i.e. the channels being 0, 2, 4, and 6).
ryood 0:77e81ce22442 51 *
ryood 0:77e81ce22442 52 * @param channel The channel number to read from.
ryood 0:77e81ce22442 53 * @param polarity The polarity of the differential signal.
ryood 0:77e81ce22442 54 *
ryood 0:77e81ce22442 55 * @param returns The sampled value as a float between 0.0 and 1.0.
ryood 0:77e81ce22442 56 */
ryood 0:77e81ce22442 57 //float read_diff_input(int channel, Polarity polarity);
ryood 0:77e81ce22442 58
ryood 0:77e81ce22442 59 private:
ryood 0:77e81ce22442 60 DigitalOut m_cs;
ryood 0:77e81ce22442 61 SPI m_bus;
ryood 0:77e81ce22442 62
ryood 0:77e81ce22442 63 void select();
ryood 0:77e81ce22442 64 void deselect();
ryood 0:77e81ce22442 65 };
ryood 0:77e81ce22442 66
ryood 0:77e81ce22442 67
ryood 0:77e81ce22442 68 #endif