12bit 8ch ADC with SPI interface for STM32 Nucleo to support 16-bit words. SPI speed bumped up to 8MHz

Fork of MCP3208_Y by Michael Chuah

Revision:
3:1c34c302cbfb
Parent:
1:0876e83ba21b
Child:
4:d2fa630c69e1
diff -r 701c3096336e -r 1c34c302cbfb mcp3208.cpp
--- a/mcp3208.cpp	Fri Feb 20 17:53:27 2015 +0000
+++ b/mcp3208.cpp	Thu Apr 20 23:02:16 2017 +0000
@@ -9,17 +9,20 @@
     _cs(cs),
     _vref(5.0)
 {
-    _spi.frequency(2000000);
-    _spi.format(12,3);
+//    _spi.frequency(2000000);
+    _spi.frequency(8000000); // Out of MCP3208 spec
+//    _spi.format(12,3);
+    _spi.format(16,3);       // To accomodate STM32 Nucleo support for only 8-bit and 16-bit words.
     _cs = 1;
 }
 
-int
-MCP3208::binary(int ch)
+int MCP3208::binary(int ch)
 {
     _cs = 0;
-    int ret = _spi.write((0x18|ch)<<2);
-    int adb = _spi.write(0);
+//    int ret = _spi.write((0x18|ch)<<2);
+//    int adb = _spi.write(0);   // commented out due to bug with STM32 Nucleo 16-bit words sending another 16-bit word immediately after the 1st one
+    unsigned long ret = _spi.write((0x18|ch)<<2);
+    int adb = ret>>20;           // Bitshifting by 16-bits to get rid of the initial 1111 1110, another 4-bit to get the 12-bits of data
     _cs = 1;
     return adb;
 }