12bit 8ch ADC with SPI interface

Files at this revision

API Documentation at this revision

Comitter:
ykuroda
Date:
Wed Oct 24 05:52:21 2012 +0000
Parent:
0:2300f3b42cdb
Commit message:
refine code

Changed in this revision

mcp3208.cpp Show annotated file Show diff for this revision Revisions of this file
mcp3208.h Show annotated file Show diff for this revision Revisions of this file
diff -r 2300f3b42cdb -r 0876e83ba21b mcp3208.cpp
--- a/mcp3208.cpp	Wed Oct 24 03:41:03 2012 +0000
+++ b/mcp3208.cpp	Wed Oct 24 05:52:21 2012 +0000
@@ -7,20 +7,19 @@
 MCP3208::MCP3208(PinName mosi, PinName miso, PinName clk, PinName cs)
 :   _spi(mosi,miso,clk),
     _cs(cs),
-    vref(5.0)
+    _vref(5.0)
 {
     _spi.frequency(2000000);
-    _spi.format(14,3);
-    _cs = 1;    
+    _spi.format(12,3);
+    _cs = 1;
 }
 
 int
 MCP3208::binary(int ch)
 {
     _cs = 0;
-    int ret = _spi.write(0x18|ch);
+    int ret = _spi.write((0x18|ch)<<2);
     int adb = _spi.write(0);
-    int dum = _spi.write(0);
     _cs = 1;
     return adb;
 }
@@ -28,6 +27,6 @@
 float
 MCP3208::volt(int ch)
 {
-    return vref * binary(ch) / 4095;
+    return _vref * binary(ch) / 4095;
 }
 
diff -r 2300f3b42cdb -r 0876e83ba21b mcp3208.h
--- a/mcp3208.h	Wed Oct 24 03:41:03 2012 +0000
+++ b/mcp3208.h	Wed Oct 24 05:52:21 2012 +0000
@@ -10,14 +10,13 @@
   protected:
     SPI _spi;
     DigitalOut _cs;
+    float _vref;
   
   public:
     MCP3208(PinName mosi=p11, PinName miso=p12, PinName clk=p13, PinName cs=p14);
     int binary(int ch);
     float volt(int ch);
-    float vref;
-    void start(void){_cs = 0;};
-    void stop(void){_cs = 1;};
+    float vref(float v){return _vref=v;}
 };
 
-#endif  //_MCP3208?H
+#endif  //_MCP3208_H