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

Revision:
1:316f86115221
Parent:
0:d37e8cb188c1
Child:
2:93009a423b45
--- a/mcp3208.cpp	Mon May 23 22:58:59 2011 +0000
+++ b/mcp3208.cpp	Tue May 24 13:58:00 2011 +0000
@@ -17,16 +17,17 @@
 void MCP3208::deselect() {m_cs = 1;}
 
 
-int MCP3208::read_input(int channel)
+float MCP3208::read_input(int channel)
 {
-    int high_byte, low_byte;
+    int command_high = START_BIT | MODE_SINGLE | ((channel & 0x04) >> 2);
+    int command_low = (channel & 0x03) << 6;
 
     select();
     
     // Odd writing requirements, see the datasheet for details
-    m_bus.write(START_BIT | MODE_SINGLE | (channel >> 2));
-    high_byte = m_bus.write(channel << 6) & 0x0F;
-    low_byte = m_bus.write(0);
+    m_bus.write(command_high);
+    int high_byte = m_bus.write(command_low) & 0x0F;
+    int low_byte = m_bus.write(0);
     
     deselect();
     
@@ -34,3 +35,23 @@
     
     return float(conv_result) / 4096;
 }
+
+
+float MCP3208::read_diff_input(int channel, Polarity polarity)
+{
+    int command_high = START_BIT | MODE_DIFF | ((channel & 0x02) >> 1);
+    int command_low = ((channel & 0x01) << 7) | (polarity << 6);
+
+    select();
+    
+    // Odd writing and reading requirements, see the datasheet for details.
+    m_bus.write(command_high);
+    int high_byte = m_bus.write(command_low) & 0x0F;
+    int low_byte = m_bus.write(0);
+    
+    deselect();
+    
+    int conv_result = (high_byte << 8) | low_byte;
+    
+    return float(conv_result) / 4096;
+}