A Library for MCP3008

Dependents:   Nucleo_MCP3008_Test ProjetoBB KIK01 MASTER_SPI_MCP3008

Committer:
ryood
Date:
Sun Jun 18 19:58:42 2017 +0000
Revision:
2:96130c28149e
Parent:
1:49d7a43f1368
Child:
3:a9e08cdf0b95
clean up;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ryood 0:77e81ce22442 1 #include "mcp3008.h"
ryood 0:77e81ce22442 2
ryood 0:77e81ce22442 3
ryood 0:77e81ce22442 4 #define START_BIT 0x01
ryood 0:77e81ce22442 5 #define MODE_SINGLE 0x80 // Single-ended mode
ryood 0:77e81ce22442 6 #define MODE_DIFF 0x00 // Differential mode
ryood 0:77e81ce22442 7
ryood 0:77e81ce22442 8
ryood 0:77e81ce22442 9 MCP3008::MCP3008(SPI bus, PinName cs)
ryood 0:77e81ce22442 10 : m_cs(cs), m_bus(bus)
ryood 0:77e81ce22442 11 {
ryood 0:77e81ce22442 12 deselect();
ryood 0:77e81ce22442 13 }
ryood 0:77e81ce22442 14
ryood 0:77e81ce22442 15 MCP3008::~MCP3008() {}
ryood 0:77e81ce22442 16
ryood 0:77e81ce22442 17
ryood 0:77e81ce22442 18 void MCP3008::select() {m_cs = 0;}
ryood 0:77e81ce22442 19 void MCP3008::deselect() {m_cs = 1; wait_us(1);}
ryood 0:77e81ce22442 20
ryood 0:77e81ce22442 21
ryood 0:77e81ce22442 22 float MCP3008::read_input(int channel)
ryood 0:77e81ce22442 23 {
ryood 2:96130c28149e 24 int conv_result = read_input_u16(channel);
ryood 0:77e81ce22442 25 return ((float)conv_result) / 1024;
ryood 0:77e81ce22442 26 }
ryood 0:77e81ce22442 27
ryood 1:49d7a43f1368 28 uint16_t MCP3008::read_input_u16(int channel)
ryood 1:49d7a43f1368 29 {
ryood 1:49d7a43f1368 30 /*
ryood 1:49d7a43f1368 31 int command_high = START_BIT | MODE_SINGLE | ((channel & 0x04) >> 2);
ryood 1:49d7a43f1368 32 int command_low = (channel & 0x03) << 6;
ryood 1:49d7a43f1368 33 */
ryood 1:49d7a43f1368 34 int command_high = START_BIT;
ryood 1:49d7a43f1368 35 int command_low = MODE_SINGLE | ((channel & 0x07) << 4);
ryood 1:49d7a43f1368 36
ryood 1:49d7a43f1368 37 select();
ryood 1:49d7a43f1368 38
ryood 1:49d7a43f1368 39 // Odd writing requirements, see the datasheet for details
ryood 1:49d7a43f1368 40 m_bus.write(command_high);
ryood 1:49d7a43f1368 41 int high_byte = m_bus.write(command_low) & 0x03;
ryood 1:49d7a43f1368 42 int low_byte = m_bus.write(0);
ryood 1:49d7a43f1368 43
ryood 1:49d7a43f1368 44 deselect();
ryood 1:49d7a43f1368 45
ryood 1:49d7a43f1368 46 int conv_result = (high_byte << 8) | low_byte;
ryood 1:49d7a43f1368 47
ryood 1:49d7a43f1368 48 return conv_result;
ryood 1:49d7a43f1368 49 }
ryood 1:49d7a43f1368 50
ryood 0:77e81ce22442 51 #if 0 // not tested.
ryood 0:77e81ce22442 52 float MCP3008::read_diff_input(int channel, Polarity polarity)
ryood 0:77e81ce22442 53 {
ryood 0:77e81ce22442 54 /*
ryood 0:77e81ce22442 55 int command_high = START_BIT | MODE_DIFF | ((channel & 0x02) >> 1);
ryood 0:77e81ce22442 56 int command_low = ((channel & 0x01) << 7) | (polarity << 6);
ryood 0:77e81ce22442 57 */
ryood 0:77e81ce22442 58 int command_high = START_BIT;
ryood 0:77e81ce22442 59 int command_low = MODE_DIFF | ((channel & 0x03) << 5) | (polarity << 4);
ryood 0:77e81ce22442 60
ryood 0:77e81ce22442 61 select();
ryood 0:77e81ce22442 62
ryood 0:77e81ce22442 63 // Odd writing and reading requirements, see the datasheet for details.
ryood 0:77e81ce22442 64 m_bus.write(command_high);
ryood 0:77e81ce22442 65 int high_byte = m_bus.write(command_low) & 0x03;
ryood 0:77e81ce22442 66 int low_byte = m_bus.write(0);
ryood 0:77e81ce22442 67
ryood 0:77e81ce22442 68 deselect();
ryood 0:77e81ce22442 69
ryood 0:77e81ce22442 70 int conv_result = (high_byte << 8) | low_byte;
ryood 0:77e81ce22442 71
ryood 0:77e81ce22442 72 return float(conv_result) / 1024;
ryood 0:77e81ce22442 73 }
ryood 0:77e81ce22442 74 #endif