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:
Sat Jun 11 21:19:56 2011 +0000
Revision:
3:e7de500b1f2d
Parent:
2:93009a423b45
Minor tweak.

Who changed what in which revision?

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