A Library for MCP3008

Dependents:   Nucleo_MCP3008_Test ProjetoBB KIK01 MASTER_SPI_MCP3008

Committer:
ryood
Date:
Sun Jun 18 19:37:35 2017 +0000
Revision:
1:49d7a43f1368
Parent:
0:77e81ce22442
Child:
2:96130c28149e
Add: MCP3008::read_input_u16()

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 0:77e81ce22442 24 /*
ryood 0:77e81ce22442 25 int command_high = START_BIT | MODE_SINGLE | ((channel & 0x04) >> 2);
ryood 0:77e81ce22442 26 int command_low = (channel & 0x03) << 6;
ryood 0:77e81ce22442 27 */
ryood 0:77e81ce22442 28 int command_high = START_BIT;
ryood 0:77e81ce22442 29 int command_low = MODE_SINGLE | ((channel & 0x07) << 4);
ryood 0:77e81ce22442 30
ryood 0:77e81ce22442 31 select();
ryood 0:77e81ce22442 32
ryood 0:77e81ce22442 33 // Odd writing requirements, see the datasheet for details
ryood 0:77e81ce22442 34 m_bus.write(command_high);
ryood 0:77e81ce22442 35 int high_byte = m_bus.write(command_low) & 0x03;
ryood 0:77e81ce22442 36 int low_byte = m_bus.write(0);
ryood 0:77e81ce22442 37
ryood 0:77e81ce22442 38 deselect();
ryood 0:77e81ce22442 39
ryood 0:77e81ce22442 40 int conv_result = (high_byte << 8) | low_byte;
ryood 0:77e81ce22442 41
ryood 0:77e81ce22442 42 return ((float)conv_result) / 1024;
ryood 0:77e81ce22442 43 }
ryood 0:77e81ce22442 44
ryood 1:49d7a43f1368 45 uint16_t MCP3008::read_input_u16(int channel)
ryood 1:49d7a43f1368 46 {
ryood 1:49d7a43f1368 47 /*
ryood 1:49d7a43f1368 48 int command_high = START_BIT | MODE_SINGLE | ((channel & 0x04) >> 2);
ryood 1:49d7a43f1368 49 int command_low = (channel & 0x03) << 6;
ryood 1:49d7a43f1368 50 */
ryood 1:49d7a43f1368 51 int command_high = START_BIT;
ryood 1:49d7a43f1368 52 int command_low = MODE_SINGLE | ((channel & 0x07) << 4);
ryood 1:49d7a43f1368 53
ryood 1:49d7a43f1368 54 select();
ryood 1:49d7a43f1368 55
ryood 1:49d7a43f1368 56 // Odd writing requirements, see the datasheet for details
ryood 1:49d7a43f1368 57 m_bus.write(command_high);
ryood 1:49d7a43f1368 58 int high_byte = m_bus.write(command_low) & 0x03;
ryood 1:49d7a43f1368 59 int low_byte = m_bus.write(0);
ryood 1:49d7a43f1368 60
ryood 1:49d7a43f1368 61 deselect();
ryood 1:49d7a43f1368 62
ryood 1:49d7a43f1368 63 int conv_result = (high_byte << 8) | low_byte;
ryood 1:49d7a43f1368 64
ryood 1:49d7a43f1368 65 return conv_result;
ryood 1:49d7a43f1368 66 }
ryood 1:49d7a43f1368 67
ryood 0:77e81ce22442 68 #if 0 // not tested.
ryood 0:77e81ce22442 69 float MCP3008::read_diff_input(int channel, Polarity polarity)
ryood 0:77e81ce22442 70 {
ryood 0:77e81ce22442 71 /*
ryood 0:77e81ce22442 72 int command_high = START_BIT | MODE_DIFF | ((channel & 0x02) >> 1);
ryood 0:77e81ce22442 73 int command_low = ((channel & 0x01) << 7) | (polarity << 6);
ryood 0:77e81ce22442 74 */
ryood 0:77e81ce22442 75 int command_high = START_BIT;
ryood 0:77e81ce22442 76 int command_low = MODE_DIFF | ((channel & 0x03) << 5) | (polarity << 4);
ryood 0:77e81ce22442 77
ryood 0:77e81ce22442 78 select();
ryood 0:77e81ce22442 79
ryood 0:77e81ce22442 80 // Odd writing and reading requirements, see the datasheet for details.
ryood 0:77e81ce22442 81 m_bus.write(command_high);
ryood 0:77e81ce22442 82 int high_byte = m_bus.write(command_low) & 0x03;
ryood 0:77e81ce22442 83 int low_byte = m_bus.write(0);
ryood 0:77e81ce22442 84
ryood 0:77e81ce22442 85 deselect();
ryood 0:77e81ce22442 86
ryood 0:77e81ce22442 87 int conv_result = (high_byte << 8) | low_byte;
ryood 0:77e81ce22442 88
ryood 0:77e81ce22442 89 return float(conv_result) / 1024;
ryood 0:77e81ce22442 90 }
ryood 0:77e81ce22442 91 #endif