8 channel ADC library

Files at this revision

API Documentation at this revision

Comitter:
henryeherman
Date:
Sun Dec 15 02:42:47 2013 +0000
Parent:
0:b58572ce83b6
Commit message:
Working 8 channel ADC library

Changed in this revision

MCP482X.cpp Show diff for this revision Revisions of this file
MCP482X.h Show diff for this revision Revisions of this file
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 b58572ce83b6 -r 7dde73ad2b78 MCP482X.cpp
--- a/MCP482X.cpp	Wed Aug 14 00:21:42 2013 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,97 +0,0 @@
-#include "mbed.h"
-#include "MCP482X.h"
-
-using namespace mbed;
-
-MCP482X::MCP482X(SPI &spi, DigitalOut &cspin): _spi(spi), _cspin(cspin) {
-    gainA = 1;
-    gainB = 1;
-    valA = 0;
-    valB = 0;     
-    enable();
-    _cspin.write(1);
-}
-
-MCP482X::~MCP482X() {
-}
-
-int MCP482X::writeA(int value){
-    int tempval = 0;
-    valA = (value & 0x0FFF);
-    tempval = SELECTDACA(valA);
-    
-    if(gainA == 1) {
-        tempval = SELECT1XGAIN(tempval);           
-    } else {
-        tempval = SELECT2XGAIN(tempval);        
-    }
-    
-    if(bshutdown==true) {
-        tempval = SELECTPWROFF(tempval);
-    } else {
-        tempval = SELECTPWRON(tempval);
-    }
-    
-    sendValue(tempval);
-    return tempval;
-}
-
-int MCP482X::writeB(int value){
-
-    int tempval = 0;
-    valB = (value & 0x0FFF);
-    tempval = SELECTDACB(valB);
-    
-    if(gainB == 1) {
-        tempval = SELECT1XGAIN(tempval);           
-    } else {
-        tempval = SELECT2XGAIN(tempval);        
-    }
-    
-    if(bshutdown==true) {
-        tempval = SELECTPWROFF(tempval);
-    } else {
-        tempval = SELECTPWRON(tempval);
-    }
-    
-    sendValue(tempval);
-    return tempval;
-}
-
-void MCP482X::setGainA(int value){
-    if(value==1 || value == 2)
-        gainA = value;
-    else
-        gainA = 1;
-}
-
-void MCP482X::setGainB(int value){
-    if(value==1 || value == 2)
-        gainB = value;
-    else
-        gainB = 1;
-}
-
-void MCP482X::disable() {
-    bshutdown = true;
-    writeA(valA);
-    writeB(valB);        
-}
-
-void MCP482X::enable() {
-    bshutdown = false;
-    writeA(valA);
-    writeB(valB);
-}
-
-void MCP482X::configspi() {
-    _spi.format(16, 0);
-    _spi.frequency(50000);
-}
-
-void MCP482X::sendValue(int value) {
-    configspi();
-    _cspin.write(0);
-    _spi.write(value);    
-    _cspin.write(1);
-}
\ No newline at end of file
diff -r b58572ce83b6 -r 7dde73ad2b78 MCP482X.h
--- a/MCP482X.h	Wed Aug 14 00:21:42 2013 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,82 +0,0 @@
-#include "mbed.h"
-#include "bit.h"
-#ifndef MCP482X_H
-#define MCP482X_H
-
-#define MCP482X_VREF 2048
-
-#define DACSELBIT       BIT(15)
-#define DACUNDEF        BIT(14)
-#define GAINBIT         BIT(13)
-#define SHDNBIT         BIT(12)
-
-#define SELECTDACA(VALUE)        CLEARBITS(VALUE,DACSELBIT)
-#define SELECTDACB(VALUE)        SETBITS(VALUE,DACSELBIT)
-#define SELECT1XGAIN(VALUE)      SETBITS(VALUE,GAINBIT)
-#define SELECT2XGAIN(VALUE)      CLEARBITS(VALUE,GAINBIT)
-#define SELECTPWRON(VALUE)       SETBITS(VALUE, SHDNBIT)
-#define SELECTPWROFF(VALUE)      CLEARBITS(VALUE, SHDNBIT)
-
-// BITS 0-11 set the output voltage!!!
-
-class MCP482X {
-public:
-
-/*
-* Constructor
-*/
-MCP482X(SPI &spi, DigitalOut &cspin);
-
-/*
-* Destructor
-*/
-~MCP482X();
-
-/*
-* Write to DAC A
-*/
-int writeA(int value);
-
-
-/*
-* Write to DAC B
-*/
-int writeB(int value);
-
-
-/*
-* Set Gain for DAC A 1 or 2
-*/
-void setGainA(int value);
-
-
-/*
-* Set Gain for DAC B 1 or 2
-*/
-void setGainB(int value);
-
-/*
-* Shutdown DAC
-*/
-void disable();
-
-/*
-* Power On DAC
-*/
-void enable();
-
-private:
-
-int gainA;
-int gainB;
-int valA;
-int valB;    
-bool bshutdown;
-SPI &_spi;
-DigitalOut &_cspin;
-
-void configspi();
-void sendValue(int value);
-}; // end class MCP482X
-
-#endif //MCP482X_H
\ No newline at end of file
diff -r b58572ce83b6 -r 7dde73ad2b78 mcp3208.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mcp3208.cpp	Sun Dec 15 02:42:47 2013 +0000
@@ -0,0 +1,116 @@
+#include "mbed.h"
+#include "mcp3208.h"
+
+using namespace mbed;
+
+mcp3208::mcp3208(SPI &spi, void(*sel)(void), void(*usel)(void)): _spi(spi) {    
+    selectfxn = sel;
+    unselectfxn = usel;         
+}
+
+mcp3208::~mcp3208() {
+}
+
+int mcp3208::read0(){
+    //printf("Read 0\r\n");
+    select();
+    _spi.write(USELECTADC0);
+    int upperbyte = _spi.write(LSELECTADC0);
+    int lowerbyte = _spi.write(0x00);
+    deselect();
+    int value = ((0x0F & upperbyte) << 8) | lowerbyte;
+    return value;
+}
+
+int mcp3208::read1(){
+    //printf("Read 1\r\n");
+    select();
+    _spi.write(USELECTADC1);
+    int upperbyte = _spi.write(LSELECTADC1);
+    int lowerbyte = _spi.write(0x00);
+    deselect();
+    int value = ((0x0F & upperbyte) << 8) | lowerbyte;
+    return value;
+}
+
+int mcp3208::read2(){
+    //printf("Read 2\r\n");
+    select();
+    _spi.write(USELECTADC2);
+    int upperbyte = _spi.write(LSELECTADC2);
+    int lowerbyte = _spi.write(0x00);
+    deselect();
+    int value = ((0x0F & upperbyte) << 8) | lowerbyte;
+    return value;
+}
+
+int mcp3208::read3(){
+    //printf("Read 3\r\n");
+    select();
+    _spi.write(USELECTADC3);
+    int upperbyte = _spi.write(LSELECTADC3);
+    int lowerbyte = _spi.write(0x00);
+    deselect();
+    int value = ((0x0F & upperbyte) << 8) | lowerbyte;
+    return value;
+}
+
+int mcp3208::read4(){
+    //printf("Read 4\r\n");
+    select();
+    _spi.write(USELECTADC4);
+    int upperbyte = _spi.write(LSELECTADC4);
+    int lowerbyte = _spi.write(0x00);
+    deselect();
+    int value = ((0x0F & upperbyte) << 8) | lowerbyte;
+    return value;
+}
+
+int mcp3208::read5(){
+    //printf("Read 5\r\n");
+    select();
+    _spi.write(USELECTADC5);
+    int upperbyte = _spi.write(LSELECTADC5);
+    int lowerbyte = _spi.write(0x00);
+    deselect();
+    int value = ((0x0F & upperbyte) << 8) | lowerbyte;
+    return value;
+}
+
+int mcp3208::read6(){
+    //printf("Read 6\r\n");
+    select();
+    _spi.write(USELECTADC6);
+    int upperbyte = _spi.write(LSELECTADC6);
+    int lowerbyte = _spi.write(0x00);
+    deselect();
+    int value = ((0x0F & upperbyte) << 8) | lowerbyte;
+    return value;
+}
+
+int mcp3208::read7(){
+    //printf("Read 7\r\n");
+    select();
+    _spi.write(USELECTADC7);
+    int upperbyte = _spi.write(LSELECTADC7);
+    int lowerbyte = _spi.write(0x00);
+    deselect();
+    int value = ((0x0F & upperbyte) << 8) | lowerbyte;
+    return value;
+}
+
+void mcp3208::configspi() {
+    _spi.format(8, 0);
+    //_spi.frequency();
+}
+
+void mcp3208::select() {
+    //Set CS low to start transmission (interrupts conversion)   
+    configspi(); 
+    selectfxn();
+}
+
+void mcp3208::deselect() {
+    //Set CS high to stop transmission (restarts conversion)    
+    unselectfxn();    
+}
\ No newline at end of file
diff -r b58572ce83b6 -r 7dde73ad2b78 mcp3208.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mcp3208.h	Sun Dec 15 02:42:47 2013 +0000
@@ -0,0 +1,117 @@
+#include "mbed.h"
+#include "bit.h"
+#ifndef MCP3208_H
+#define MCP3208_H
+
+#define MCP3208_STARTBIT        BIT(2)
+#define MCP3208_SGL_DIFF        BIT(1)
+#define MCP3208_ADCSEL2         BIT(0)
+#define MCP3208_ADCSEL1         BIT(7)
+#define MCP3208_ADCSEL0         BIT(6)
+
+#define USELECTADC0      (MCP3208_STARTBIT|MCP3208_SGL_DIFF)
+#define LSELECTADC0      (0)
+
+#define USELECTADC1      (MCP3208_STARTBIT|MCP3208_SGL_DIFF)
+#define LSELECTADC1      (MCP3208_ADCSEL0)
+
+#define USELECTADC2      (MCP3208_STARTBIT|MCP3208_SGL_DIFF)
+#define LSELECTADC2      (MCP3208_ADCSEL1)
+
+#define USELECTADC3      (MCP3208_STARTBIT|MCP3208_SGL_DIFF)
+#define LSELECTADC3      (MCP3208_ADCSEL1|MCP3208_ADCSEL0)
+
+#define USELECTADC4      (MCP3208_STARTBIT|MCP3208_SGL_DIFF|MCP3208_ADCSEL2)
+#define LSELECTADC4      (0)
+
+#define USELECTADC5      (MCP3208_STARTBIT|MCP3208_SGL_DIFF|MCP3208_ADCSEL2)
+#define LSELECTADC5      (MCP3208_ADCSEL0)
+
+#define USELECTADC6      (MCP3208_STARTBIT|MCP3208_SGL_DIFF|MCP3208_ADCSEL2)
+#define LSELECTADC6      (MCP3208_ADCSEL1)
+
+#define USELECTADC7      (MCP3208_STARTBIT|MCP3208_SGL_DIFF|MCP3208_ADCSEL2)
+#define LSELECTADC7      (MCP3208_ADCSEL1|MCP3208_ADCSEL0)
+
+
+// BITS 0-11 set the output voltage!!!
+
+class mcp3208 {
+public:
+
+/*
+* Constructor
+*/
+mcp3208(SPI &spi, void(*sel)(void), void(*usel)(void));
+
+/*
+* Destructor
+*/
+~mcp3208();
+
+/*
+* Write to DAC 0
+*/
+int read0();
+
+
+/*
+* Write to DAC 1
+*/
+int read1();
+
+/*
+* Write to DAC 2
+*/
+int read2();
+
+/*
+* Write to DAC 3
+*/
+int read3();
+
+/*
+* Write to DAC 4
+*/
+int read4();
+
+/*
+* Write to DAC 5
+*/
+int read5();
+
+/*
+* Write to DAC 6
+*/
+int read6();
+
+/*
+* Write to DAC 7
+*/
+int read7();
+
+/*
+* Select the ADC for data retrievel
+*/
+void select();
+
+/*
+* Deselect the ADC
+*/
+void deselect();
+
+private:
+
+// These function pointer allows you to
+// use a routine to control the chip select line
+// important for me since all of my chip select lines are
+// controlled by a array of shift registers!
+void(*selectfxn)(void);
+void(*unselectfxn)(void);
+SPI &_spi;
+
+// Configure the SPI periphreal to drive our ADCs
+void configspi();
+}; // end class MCP3208
+
+#endif //MCP3208_H
\ No newline at end of file