A library to interface to the MCP3208 SPI-based ADC from Microchip. This chip provides eight analogue inputs, providing converted 12-bit values via SPI.

Dependents:   Nucleo_MCP3208_Test Nucleo_MCP3208_Ticker_Test BBMv2_eps ref_BBMv2_eps ... more

Committer:
Kemp
Date:
Tue May 24 13:58:00 2011 +0000
Revision:
1:316f86115221
Parent:
0:d37e8cb188c1
Child:
2:93009a423b45
Now allows reading from differential inputs. Started documenting, fixed some silly bugs.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kemp 0:d37e8cb188c1 1 #include "mcp3208.h"
Kemp 0:d37e8cb188c1 2
Kemp 0:d37e8cb188c1 3
Kemp 0:d37e8cb188c1 4 #define START_BIT 0x04
Kemp 0:d37e8cb188c1 5 #define MODE_SINGLE 0x02 // Single-ended mode
Kemp 0:d37e8cb188c1 6 #define MODE_DIFF 0x00 // Differential mode
Kemp 0:d37e8cb188c1 7
Kemp 0:d37e8cb188c1 8
Kemp 0:d37e8cb188c1 9 MCP3208::MCP3208(SPI bus, PinName cs)
Kemp 0:d37e8cb188c1 10 : m_cs(cs), m_bus(bus)
Kemp 0:d37e8cb188c1 11 {}
Kemp 0:d37e8cb188c1 12
Kemp 0:d37e8cb188c1 13 MCP3208::~MCP3208() {}
Kemp 0:d37e8cb188c1 14
Kemp 0:d37e8cb188c1 15
Kemp 0:d37e8cb188c1 16 void MCP3208::select() {m_cs = 0;}
Kemp 0:d37e8cb188c1 17 void MCP3208::deselect() {m_cs = 1;}
Kemp 0:d37e8cb188c1 18
Kemp 0:d37e8cb188c1 19
Kemp 1:316f86115221 20 float MCP3208::read_input(int channel)
Kemp 0:d37e8cb188c1 21 {
Kemp 1:316f86115221 22 int command_high = START_BIT | MODE_SINGLE | ((channel & 0x04) >> 2);
Kemp 1:316f86115221 23 int command_low = (channel & 0x03) << 6;
Kemp 0:d37e8cb188c1 24
Kemp 0:d37e8cb188c1 25 select();
Kemp 0:d37e8cb188c1 26
Kemp 0:d37e8cb188c1 27 // Odd writing requirements, see the datasheet for details
Kemp 1:316f86115221 28 m_bus.write(command_high);
Kemp 1:316f86115221 29 int high_byte = m_bus.write(command_low) & 0x0F;
Kemp 1:316f86115221 30 int low_byte = m_bus.write(0);
Kemp 0:d37e8cb188c1 31
Kemp 0:d37e8cb188c1 32 deselect();
Kemp 0:d37e8cb188c1 33
Kemp 0:d37e8cb188c1 34 int conv_result = (high_byte << 8) | low_byte;
Kemp 0:d37e8cb188c1 35
Kemp 0:d37e8cb188c1 36 return float(conv_result) / 4096;
Kemp 0:d37e8cb188c1 37 }
Kemp 1:316f86115221 38
Kemp 1:316f86115221 39
Kemp 1:316f86115221 40 float MCP3208::read_diff_input(int channel, Polarity polarity)
Kemp 1:316f86115221 41 {
Kemp 1:316f86115221 42 int command_high = START_BIT | MODE_DIFF | ((channel & 0x02) >> 1);
Kemp 1:316f86115221 43 int command_low = ((channel & 0x01) << 7) | (polarity << 6);
Kemp 1:316f86115221 44
Kemp 1:316f86115221 45 select();
Kemp 1:316f86115221 46
Kemp 1:316f86115221 47 // Odd writing and reading requirements, see the datasheet for details.
Kemp 1:316f86115221 48 m_bus.write(command_high);
Kemp 1:316f86115221 49 int high_byte = m_bus.write(command_low) & 0x0F;
Kemp 1:316f86115221 50 int low_byte = m_bus.write(0);
Kemp 1:316f86115221 51
Kemp 1:316f86115221 52 deselect();
Kemp 1:316f86115221 53
Kemp 1:316f86115221 54 int conv_result = (high_byte << 8) | low_byte;
Kemp 1:316f86115221 55
Kemp 1:316f86115221 56 return float(conv_result) / 4096;
Kemp 1:316f86115221 57 }