A Library for MCP3008

Dependents:   Nucleo_MCP3008_Test ProjetoBB KIK01 MASTER_SPI_MCP3008

Committer:
ryood
Date:
Fri Jun 30 15:58:59 2017 +0000
Revision:
3:a9e08cdf0b95
Parent:
1:49d7a43f1368
SPI object as a pointer

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 3:a9e08cdf0b95 34 MCP3008(SPI* p_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 1:49d7a43f1368 45 /** Read from a single-ended input.
ryood 1:49d7a43f1368 46 *
ryood 1:49d7a43f1368 47 * @param channel The channel number to read from.
ryood 1:49d7a43f1368 48 *
ryood 1:49d7a43f1368 49 * @param returns The sampled value as a unsigned int16_t between 0 and 1024.
ryood 1:49d7a43f1368 50 */
ryood 1:49d7a43f1368 51 uint16_t read_input_u16(int channel);
ryood 1:49d7a43f1368 52
ryood 0:77e81ce22442 53 /** Read from a pair of differential inputs.
ryood 0:77e81ce22442 54 *
ryood 0:77e81ce22442 55 * In differential mode, the channels are referred to as 0 to 3, with
ryood 0:77e81ce22442 56 * polarity set in a separate parameter. This avoids the user having to set
ryood 0:77e81ce22442 57 * the polarity as part of the channel number or having channel numbers
ryood 0:77e81ce22442 58 * increase by two (i.e. the channels being 0, 2, 4, and 6).
ryood 0:77e81ce22442 59 *
ryood 0:77e81ce22442 60 * @param channel The channel number to read from.
ryood 0:77e81ce22442 61 * @param polarity The polarity of the differential signal.
ryood 0:77e81ce22442 62 *
ryood 0:77e81ce22442 63 * @param returns The sampled value as a float between 0.0 and 1.0.
ryood 0:77e81ce22442 64 */
ryood 0:77e81ce22442 65 //float read_diff_input(int channel, Polarity polarity);
ryood 0:77e81ce22442 66
ryood 0:77e81ce22442 67 private:
ryood 0:77e81ce22442 68 DigitalOut m_cs;
ryood 3:a9e08cdf0b95 69 SPI* m_p_bus;
ryood 0:77e81ce22442 70
ryood 0:77e81ce22442 71 void select();
ryood 0:77e81ce22442 72 void deselect();
ryood 0:77e81ce22442 73 };
ryood 0:77e81ce22442 74
ryood 0:77e81ce22442 75
ryood 0:77e81ce22442 76 #endif