12bit 8ch ADC with SPI interface for STM32 Nucleo to support 16-bit words. SPI speed bumped up to 8MHz
Fork of MCP3208_Y by
mcp3208.cpp@3:1c34c302cbfb, 2017-04-20 (annotated)
- Committer:
- mcx
- Date:
- Thu Apr 20 23:02:16 2017 +0000
- Revision:
- 3:1c34c302cbfb
- Parent:
- 1:0876e83ba21b
- Child:
- 4:d2fa630c69e1
Streaming of 4 footpad with 4 sensors each (16 inputs). Loop time of only 1.2ms due to Serial printf speed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ykuroda | 0:2300f3b42cdb | 1 | // |
ykuroda | 0:2300f3b42cdb | 2 | // |
ykuroda | 0:2300f3b42cdb | 3 | // |
ykuroda | 0:2300f3b42cdb | 4 | #include "mcp3208.h" |
ykuroda | 0:2300f3b42cdb | 5 | |
ykuroda | 0:2300f3b42cdb | 6 | |
ykuroda | 0:2300f3b42cdb | 7 | MCP3208::MCP3208(PinName mosi, PinName miso, PinName clk, PinName cs) |
ykuroda | 0:2300f3b42cdb | 8 | : _spi(mosi,miso,clk), |
ykuroda | 0:2300f3b42cdb | 9 | _cs(cs), |
ykuroda | 1:0876e83ba21b | 10 | _vref(5.0) |
ykuroda | 0:2300f3b42cdb | 11 | { |
mcx | 3:1c34c302cbfb | 12 | // _spi.frequency(2000000); |
mcx | 3:1c34c302cbfb | 13 | _spi.frequency(8000000); // Out of MCP3208 spec |
mcx | 3:1c34c302cbfb | 14 | // _spi.format(12,3); |
mcx | 3:1c34c302cbfb | 15 | _spi.format(16,3); // To accomodate STM32 Nucleo support for only 8-bit and 16-bit words. |
ykuroda | 1:0876e83ba21b | 16 | _cs = 1; |
ykuroda | 0:2300f3b42cdb | 17 | } |
ykuroda | 0:2300f3b42cdb | 18 | |
mcx | 3:1c34c302cbfb | 19 | int MCP3208::binary(int ch) |
ykuroda | 0:2300f3b42cdb | 20 | { |
ykuroda | 0:2300f3b42cdb | 21 | _cs = 0; |
mcx | 3:1c34c302cbfb | 22 | // int ret = _spi.write((0x18|ch)<<2); |
mcx | 3:1c34c302cbfb | 23 | // int adb = _spi.write(0); // commented out due to bug with STM32 Nucleo 16-bit words sending another 16-bit word immediately after the 1st one |
mcx | 3:1c34c302cbfb | 24 | unsigned long ret = _spi.write((0x18|ch)<<2); |
mcx | 3:1c34c302cbfb | 25 | int adb = ret>>20; // Bitshifting by 16-bits to get rid of the initial 1111 1110, another 4-bit to get the 12-bits of data |
ykuroda | 0:2300f3b42cdb | 26 | _cs = 1; |
ykuroda | 0:2300f3b42cdb | 27 | return adb; |
ykuroda | 0:2300f3b42cdb | 28 | } |
ykuroda | 0:2300f3b42cdb | 29 | |
ykuroda | 0:2300f3b42cdb | 30 | float |
ykuroda | 0:2300f3b42cdb | 31 | MCP3208::volt(int ch) |
ykuroda | 0:2300f3b42cdb | 32 | { |
ykuroda | 1:0876e83ba21b | 33 | return _vref * binary(ch) / 4095; |
ykuroda | 0:2300f3b42cdb | 34 | } |
ykuroda | 0:2300f3b42cdb | 35 |