Dual ADC library
Revision 1:4e6619093761, committed 2013-12-15
- Comitter:
- henryeherman
- Date:
- Sun Dec 15 02:42:30 2013 +0000
- Parent:
- 0:b58572ce83b6
- Commit message:
- Working Dual ADC library
Changed in this revision
--- 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
--- 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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mcp3202.cpp Sun Dec 15 02:42:30 2013 +0000 @@ -0,0 +1,65 @@ +#include "mbed.h" +#include "mcp3202.h" + +using namespace mbed; + +MCP3202::MCP3202(SPI &spi, void(*sel)(void), void(*usel)(void)): _spi(spi) { + selectfxn = sel; + unselectfxn = usel; + enable(); +} + +MCP3202::~MCP3202() { +} + +int MCP3202::readA(){ + //printf("Read A\r\n"); + select(); + _spi.write(STARTBIT); + int upperbyte = _spi.write(SGL_DIFF|MSBF); + int lowerbyte = _spi.write(0x00); + deselect(); + int value = ((0x0F & upperbyte) << 8) | lowerbyte; + return value; +} + +int MCP3202::readB(){ + //printf("Read B\r\n"); + select(); + _spi.write(STARTBIT); + int upperbyte = _spi.write(SGL_DIFF|ODD_SIGN|MSBF); + int lowerbyte = _spi.write(0x00); + deselect(); + int value = ((0x0F & upperbyte) << 8) | lowerbyte; + return value; +} + +void MCP3202::disable() { + bshutdown = true; +} + +void MCP3202::enable() { + bshutdown = false; +} + +void MCP3202::configspi() { + _spi.format(8, 0); + //_spi.frequency(); +} + +void MCP3202::sendValue(int value) { + select(); + _spi.write(value); + deselect(); +} + +void MCP3202::select() { + //Set CS low to start transmission (interrupts conversion) + configspi(); + selectfxn(); +} + +void MCP3202::deselect() { + //Set CS high to stop transmission (restarts conversion) + unselectfxn(); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mcp3202.h Sun Dec 15 02:42:30 2013 +0000 @@ -0,0 +1,71 @@ +#include "mbed.h" +#include "bit.h" +#ifndef MCP3202_H +#define MCP3202_H + +#define MCP482X_VREF 2048 + +#define STARTBIT BIT(0) +#define SGL_DIFF BIT(7) +#define ODD_SIGN BIT(6) +#define MSBF BIT(5) + +#define SELECTSTART(VALUE) SETBITS(VALUE,STARTBIT) +#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 MCP3202 { +public: + +/* +* Constructor +*/ +MCP3202(SPI &spi, void(*sel)(void), void(*usel)(void)); + +/* +* Destructor +*/ +~MCP3202(); + +/* +* Write to DAC A +*/ +int readA(); + + +/* +* Write to DAC B +*/ +int readB(); + +/* +* Shutdown DAC +*/ +void disable(); + +/* +* Power On DAC +*/ +void enable(); + +void select(); + +void deselect(); + +private: + +void(*selectfxn)(void); +void(*unselectfxn)(void); +bool bshutdown; +SPI &_spi; + +void configspi(); +void sendValue(int value); +}; // end class MCP3202 + +#endif //MCP3202_H \ No newline at end of file