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:
Mon May 23 22:58:59 2011 +0000
Revision:
0:d37e8cb188c1
Child:
1:316f86115221
Initial release - no checking, single-ended mode only, $OTHER_LIMITATIONS.

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 0:d37e8cb188c1 20 int MCP3208::read_input(int channel)
Kemp 0:d37e8cb188c1 21 {
Kemp 0:d37e8cb188c1 22 int high_byte, low_byte;
Kemp 0:d37e8cb188c1 23
Kemp 0:d37e8cb188c1 24 select();
Kemp 0:d37e8cb188c1 25
Kemp 0:d37e8cb188c1 26 // Odd writing requirements, see the datasheet for details
Kemp 0:d37e8cb188c1 27 m_bus.write(START_BIT | MODE_SINGLE | (channel >> 2));
Kemp 0:d37e8cb188c1 28 high_byte = m_bus.write(channel << 6) & 0x0F;
Kemp 0:d37e8cb188c1 29 low_byte = m_bus.write(0);
Kemp 0:d37e8cb188c1 30
Kemp 0:d37e8cb188c1 31 deselect();
Kemp 0:d37e8cb188c1 32
Kemp 0:d37e8cb188c1 33 int conv_result = (high_byte << 8) | low_byte;
Kemp 0:d37e8cb188c1 34
Kemp 0:d37e8cb188c1 35 return float(conv_result) / 4096;
Kemp 0:d37e8cb188c1 36 }