Used as part of the OU_Davis_Old_Robot library
Revision 0:2cb766caa4c7, committed 2017-11-01
- Comitter:
- DrewSchaef
- Date:
- Wed Nov 01 15:56:02 2017 +0000
- Commit message:
- Committed to allow program to be published
Changed in this revision
| MCP3008.cpp | Show annotated file Show diff for this revision Revisions of this file |
| MCP3008.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP3008.cpp Wed Nov 01 15:56:02 2017 +0000
@@ -0,0 +1,30 @@
+/**by Austin Saunders
+
+*/
+
+#include <mbed.h>
+#include "MCP3008.h"
+
+MCP3008::MCP3008(PinName mosi, PinName miso, PinName clk, PinName cs)
+: _spi(mosi,miso,clk),
+ _cs(cs),
+ _vref(3.3)
+{
+ _spi.frequency(1000000);
+ _spi.format(8,0);
+ _cs = 1;
+}
+
+int
+MCP3008::read(int ch)
+{
+ _cs = 0;
+ _spi.write(0x01);
+ _data1 = _spi.write(0x80|(ch<<4));
+ _data2 = _spi.write(0x00);
+ int adb = (_data1<<8) | _data2;
+ adb = (adb & 0x03FF);
+ _cs = 1;
+ float ain = (adb*_vref)/1023;
+ return adb;
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP3008.h Wed Nov 01 15:56:02 2017 +0000
@@ -0,0 +1,23 @@
+//
+//
+//
+#ifndef _MCP3008_H
+#define _MCP3008_H
+
+#include <mbed.h>
+
+class MCP3008 {
+ protected:
+ SPI _spi;
+ DigitalOut _cs;
+ float _vref;
+ int _data1;
+ int _data2;
+
+ public:
+ MCP3008(PinName mosi=p11, PinName miso=p12, PinName clk=p13, PinName cs=p14);
+ int read(int ch);
+ float vref(float v){return _vref=v;}
+};
+
+#endif //_MCP3008_H
\ No newline at end of file