Dual ADC library
mcp3202.cpp@1:4e6619093761, 2013-12-15 (annotated)
- Committer:
- henryeherman
- Date:
- Sun Dec 15 02:42:30 2013 +0000
- Revision:
- 1:4e6619093761
Working Dual ADC library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
henryeherman | 1:4e6619093761 | 1 | #include "mbed.h" |
henryeherman | 1:4e6619093761 | 2 | #include "mcp3202.h" |
henryeherman | 1:4e6619093761 | 3 | |
henryeherman | 1:4e6619093761 | 4 | using namespace mbed; |
henryeherman | 1:4e6619093761 | 5 | |
henryeherman | 1:4e6619093761 | 6 | MCP3202::MCP3202(SPI &spi, void(*sel)(void), void(*usel)(void)): _spi(spi) { |
henryeherman | 1:4e6619093761 | 7 | selectfxn = sel; |
henryeherman | 1:4e6619093761 | 8 | unselectfxn = usel; |
henryeherman | 1:4e6619093761 | 9 | enable(); |
henryeherman | 1:4e6619093761 | 10 | } |
henryeherman | 1:4e6619093761 | 11 | |
henryeherman | 1:4e6619093761 | 12 | MCP3202::~MCP3202() { |
henryeherman | 1:4e6619093761 | 13 | } |
henryeherman | 1:4e6619093761 | 14 | |
henryeherman | 1:4e6619093761 | 15 | int MCP3202::readA(){ |
henryeherman | 1:4e6619093761 | 16 | //printf("Read A\r\n"); |
henryeherman | 1:4e6619093761 | 17 | select(); |
henryeherman | 1:4e6619093761 | 18 | _spi.write(STARTBIT); |
henryeherman | 1:4e6619093761 | 19 | int upperbyte = _spi.write(SGL_DIFF|MSBF); |
henryeherman | 1:4e6619093761 | 20 | int lowerbyte = _spi.write(0x00); |
henryeherman | 1:4e6619093761 | 21 | deselect(); |
henryeherman | 1:4e6619093761 | 22 | int value = ((0x0F & upperbyte) << 8) | lowerbyte; |
henryeherman | 1:4e6619093761 | 23 | return value; |
henryeherman | 1:4e6619093761 | 24 | } |
henryeherman | 1:4e6619093761 | 25 | |
henryeherman | 1:4e6619093761 | 26 | int MCP3202::readB(){ |
henryeherman | 1:4e6619093761 | 27 | //printf("Read B\r\n"); |
henryeherman | 1:4e6619093761 | 28 | select(); |
henryeherman | 1:4e6619093761 | 29 | _spi.write(STARTBIT); |
henryeherman | 1:4e6619093761 | 30 | int upperbyte = _spi.write(SGL_DIFF|ODD_SIGN|MSBF); |
henryeherman | 1:4e6619093761 | 31 | int lowerbyte = _spi.write(0x00); |
henryeherman | 1:4e6619093761 | 32 | deselect(); |
henryeherman | 1:4e6619093761 | 33 | int value = ((0x0F & upperbyte) << 8) | lowerbyte; |
henryeherman | 1:4e6619093761 | 34 | return value; |
henryeherman | 1:4e6619093761 | 35 | } |
henryeherman | 1:4e6619093761 | 36 | |
henryeherman | 1:4e6619093761 | 37 | void MCP3202::disable() { |
henryeherman | 1:4e6619093761 | 38 | bshutdown = true; |
henryeherman | 1:4e6619093761 | 39 | } |
henryeherman | 1:4e6619093761 | 40 | |
henryeherman | 1:4e6619093761 | 41 | void MCP3202::enable() { |
henryeherman | 1:4e6619093761 | 42 | bshutdown = false; |
henryeherman | 1:4e6619093761 | 43 | } |
henryeherman | 1:4e6619093761 | 44 | |
henryeherman | 1:4e6619093761 | 45 | void MCP3202::configspi() { |
henryeherman | 1:4e6619093761 | 46 | _spi.format(8, 0); |
henryeherman | 1:4e6619093761 | 47 | //_spi.frequency(); |
henryeherman | 1:4e6619093761 | 48 | } |
henryeherman | 1:4e6619093761 | 49 | |
henryeherman | 1:4e6619093761 | 50 | void MCP3202::sendValue(int value) { |
henryeherman | 1:4e6619093761 | 51 | select(); |
henryeherman | 1:4e6619093761 | 52 | _spi.write(value); |
henryeherman | 1:4e6619093761 | 53 | deselect(); |
henryeherman | 1:4e6619093761 | 54 | } |
henryeherman | 1:4e6619093761 | 55 | |
henryeherman | 1:4e6619093761 | 56 | void MCP3202::select() { |
henryeherman | 1:4e6619093761 | 57 | //Set CS low to start transmission (interrupts conversion) |
henryeherman | 1:4e6619093761 | 58 | configspi(); |
henryeherman | 1:4e6619093761 | 59 | selectfxn(); |
henryeherman | 1:4e6619093761 | 60 | } |
henryeherman | 1:4e6619093761 | 61 | |
henryeherman | 1:4e6619093761 | 62 | void MCP3202::deselect() { |
henryeherman | 1:4e6619093761 | 63 | //Set CS high to stop transmission (restarts conversion) |
henryeherman | 1:4e6619093761 | 64 | unselectfxn(); |
henryeherman | 1:4e6619093761 | 65 | } |