Dual ADC library
MCP482X.cpp@0:b58572ce83b6, 2013-08-14 (annotated)
- Committer:
- henryeherman
- Date:
- Wed Aug 14 00:21:42 2013 +0000
- Revision:
- 0:b58572ce83b6
Initial commit of a library to communicate with the MCP482X SPI based DAC from microchip.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
henryeherman | 0:b58572ce83b6 | 1 | #include "mbed.h" |
henryeherman | 0:b58572ce83b6 | 2 | #include "MCP482X.h" |
henryeherman | 0:b58572ce83b6 | 3 | |
henryeherman | 0:b58572ce83b6 | 4 | using namespace mbed; |
henryeherman | 0:b58572ce83b6 | 5 | |
henryeherman | 0:b58572ce83b6 | 6 | MCP482X::MCP482X(SPI &spi, DigitalOut &cspin): _spi(spi), _cspin(cspin) { |
henryeherman | 0:b58572ce83b6 | 7 | gainA = 1; |
henryeherman | 0:b58572ce83b6 | 8 | gainB = 1; |
henryeherman | 0:b58572ce83b6 | 9 | valA = 0; |
henryeherman | 0:b58572ce83b6 | 10 | valB = 0; |
henryeherman | 0:b58572ce83b6 | 11 | enable(); |
henryeherman | 0:b58572ce83b6 | 12 | _cspin.write(1); |
henryeherman | 0:b58572ce83b6 | 13 | } |
henryeherman | 0:b58572ce83b6 | 14 | |
henryeherman | 0:b58572ce83b6 | 15 | MCP482X::~MCP482X() { |
henryeherman | 0:b58572ce83b6 | 16 | } |
henryeherman | 0:b58572ce83b6 | 17 | |
henryeherman | 0:b58572ce83b6 | 18 | int MCP482X::writeA(int value){ |
henryeherman | 0:b58572ce83b6 | 19 | int tempval = 0; |
henryeherman | 0:b58572ce83b6 | 20 | valA = (value & 0x0FFF); |
henryeherman | 0:b58572ce83b6 | 21 | tempval = SELECTDACA(valA); |
henryeherman | 0:b58572ce83b6 | 22 | |
henryeherman | 0:b58572ce83b6 | 23 | if(gainA == 1) { |
henryeherman | 0:b58572ce83b6 | 24 | tempval = SELECT1XGAIN(tempval); |
henryeherman | 0:b58572ce83b6 | 25 | } else { |
henryeherman | 0:b58572ce83b6 | 26 | tempval = SELECT2XGAIN(tempval); |
henryeherman | 0:b58572ce83b6 | 27 | } |
henryeherman | 0:b58572ce83b6 | 28 | |
henryeherman | 0:b58572ce83b6 | 29 | if(bshutdown==true) { |
henryeherman | 0:b58572ce83b6 | 30 | tempval = SELECTPWROFF(tempval); |
henryeherman | 0:b58572ce83b6 | 31 | } else { |
henryeherman | 0:b58572ce83b6 | 32 | tempval = SELECTPWRON(tempval); |
henryeherman | 0:b58572ce83b6 | 33 | } |
henryeherman | 0:b58572ce83b6 | 34 | |
henryeherman | 0:b58572ce83b6 | 35 | sendValue(tempval); |
henryeherman | 0:b58572ce83b6 | 36 | return tempval; |
henryeherman | 0:b58572ce83b6 | 37 | } |
henryeherman | 0:b58572ce83b6 | 38 | |
henryeherman | 0:b58572ce83b6 | 39 | int MCP482X::writeB(int value){ |
henryeherman | 0:b58572ce83b6 | 40 | |
henryeherman | 0:b58572ce83b6 | 41 | int tempval = 0; |
henryeherman | 0:b58572ce83b6 | 42 | valB = (value & 0x0FFF); |
henryeherman | 0:b58572ce83b6 | 43 | tempval = SELECTDACB(valB); |
henryeherman | 0:b58572ce83b6 | 44 | |
henryeherman | 0:b58572ce83b6 | 45 | if(gainB == 1) { |
henryeherman | 0:b58572ce83b6 | 46 | tempval = SELECT1XGAIN(tempval); |
henryeherman | 0:b58572ce83b6 | 47 | } else { |
henryeherman | 0:b58572ce83b6 | 48 | tempval = SELECT2XGAIN(tempval); |
henryeherman | 0:b58572ce83b6 | 49 | } |
henryeherman | 0:b58572ce83b6 | 50 | |
henryeherman | 0:b58572ce83b6 | 51 | if(bshutdown==true) { |
henryeherman | 0:b58572ce83b6 | 52 | tempval = SELECTPWROFF(tempval); |
henryeherman | 0:b58572ce83b6 | 53 | } else { |
henryeherman | 0:b58572ce83b6 | 54 | tempval = SELECTPWRON(tempval); |
henryeherman | 0:b58572ce83b6 | 55 | } |
henryeherman | 0:b58572ce83b6 | 56 | |
henryeherman | 0:b58572ce83b6 | 57 | sendValue(tempval); |
henryeherman | 0:b58572ce83b6 | 58 | return tempval; |
henryeherman | 0:b58572ce83b6 | 59 | } |
henryeherman | 0:b58572ce83b6 | 60 | |
henryeherman | 0:b58572ce83b6 | 61 | void MCP482X::setGainA(int value){ |
henryeherman | 0:b58572ce83b6 | 62 | if(value==1 || value == 2) |
henryeherman | 0:b58572ce83b6 | 63 | gainA = value; |
henryeherman | 0:b58572ce83b6 | 64 | else |
henryeherman | 0:b58572ce83b6 | 65 | gainA = 1; |
henryeherman | 0:b58572ce83b6 | 66 | } |
henryeherman | 0:b58572ce83b6 | 67 | |
henryeherman | 0:b58572ce83b6 | 68 | void MCP482X::setGainB(int value){ |
henryeherman | 0:b58572ce83b6 | 69 | if(value==1 || value == 2) |
henryeherman | 0:b58572ce83b6 | 70 | gainB = value; |
henryeherman | 0:b58572ce83b6 | 71 | else |
henryeherman | 0:b58572ce83b6 | 72 | gainB = 1; |
henryeherman | 0:b58572ce83b6 | 73 | } |
henryeherman | 0:b58572ce83b6 | 74 | |
henryeherman | 0:b58572ce83b6 | 75 | void MCP482X::disable() { |
henryeherman | 0:b58572ce83b6 | 76 | bshutdown = true; |
henryeherman | 0:b58572ce83b6 | 77 | writeA(valA); |
henryeherman | 0:b58572ce83b6 | 78 | writeB(valB); |
henryeherman | 0:b58572ce83b6 | 79 | } |
henryeherman | 0:b58572ce83b6 | 80 | |
henryeherman | 0:b58572ce83b6 | 81 | void MCP482X::enable() { |
henryeherman | 0:b58572ce83b6 | 82 | bshutdown = false; |
henryeherman | 0:b58572ce83b6 | 83 | writeA(valA); |
henryeherman | 0:b58572ce83b6 | 84 | writeB(valB); |
henryeherman | 0:b58572ce83b6 | 85 | } |
henryeherman | 0:b58572ce83b6 | 86 | |
henryeherman | 0:b58572ce83b6 | 87 | void MCP482X::configspi() { |
henryeherman | 0:b58572ce83b6 | 88 | _spi.format(16, 0); |
henryeherman | 0:b58572ce83b6 | 89 | _spi.frequency(50000); |
henryeherman | 0:b58572ce83b6 | 90 | } |
henryeherman | 0:b58572ce83b6 | 91 | |
henryeherman | 0:b58572ce83b6 | 92 | void MCP482X::sendValue(int value) { |
henryeherman | 0:b58572ce83b6 | 93 | configspi(); |
henryeherman | 0:b58572ce83b6 | 94 | _cspin.write(0); |
henryeherman | 0:b58572ce83b6 | 95 | _spi.write(value); |
henryeherman | 0:b58572ce83b6 | 96 | _cspin.write(1); |
henryeherman | 0:b58572ce83b6 | 97 | } |