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 #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 0:77e81ce22442 45 #if 0 // not tested.
ryood 0:77e81ce22442 46 float MCP3008::read_diff_input(int channel, Polarity polarity)
ryood 0:77e81ce22442 47 {
ryood 0:77e81ce22442 48 /*
ryood 0:77e81ce22442 49 int command_high = START_BIT | MODE_DIFF | ((channel & 0x02) >> 1);
ryood 0:77e81ce22442 50 int command_low = ((channel & 0x01) << 7) | (polarity << 6);
ryood 0:77e81ce22442 51 */
ryood 0:77e81ce22442 52 int command_high = START_BIT;
ryood 0:77e81ce22442 53 int command_low = MODE_DIFF | ((channel & 0x03) << 5) | (polarity << 4);
ryood 0:77e81ce22442 54
ryood 0:77e81ce22442 55 select();
ryood 0:77e81ce22442 56
ryood 0:77e81ce22442 57 // Odd writing and reading requirements, see the datasheet for details.
ryood 0:77e81ce22442 58 m_bus.write(command_high);
ryood 0:77e81ce22442 59 int high_byte = m_bus.write(command_low) & 0x03;
ryood 0:77e81ce22442 60 int low_byte = m_bus.write(0);
ryood 0:77e81ce22442 61
ryood 0:77e81ce22442 62 deselect();
ryood 0:77e81ce22442 63
ryood 0:77e81ce22442 64 int conv_result = (high_byte << 8) | low_byte;
ryood 0:77e81ce22442 65
ryood 0:77e81ce22442 66 return float(conv_result) / 1024;
ryood 0:77e81ce22442 67 }
ryood 0:77e81ce22442 68 #endif