12bit 8ch ADC with SPI interface
Fork of MCP3208 by
mcp3208.cpp@0:2300f3b42cdb, 2012-10-24 (annotated)
- Committer:
- ykuroda
- Date:
- Wed Oct 24 03:41:03 2012 +0000
- Revision:
- 0:2300f3b42cdb
- Child:
- 1:0876e83ba21b
MCP3208 ADC Library
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 | 0:2300f3b42cdb | 10 | vref(5.0) |
ykuroda | 0:2300f3b42cdb | 11 | { |
ykuroda | 0:2300f3b42cdb | 12 | _spi.frequency(2000000); |
ykuroda | 0:2300f3b42cdb | 13 | _spi.format(14,3); |
ykuroda | 0:2300f3b42cdb | 14 | _cs = 1; |
ykuroda | 0:2300f3b42cdb | 15 | } |
ykuroda | 0:2300f3b42cdb | 16 | |
ykuroda | 0:2300f3b42cdb | 17 | int |
ykuroda | 0:2300f3b42cdb | 18 | MCP3208::binary(int ch) |
ykuroda | 0:2300f3b42cdb | 19 | { |
ykuroda | 0:2300f3b42cdb | 20 | _cs = 0; |
ykuroda | 0:2300f3b42cdb | 21 | int ret = _spi.write(0x18|ch); |
ykuroda | 0:2300f3b42cdb | 22 | int adb = _spi.write(0); |
ykuroda | 0:2300f3b42cdb | 23 | int dum = _spi.write(0); |
ykuroda | 0:2300f3b42cdb | 24 | _cs = 1; |
ykuroda | 0:2300f3b42cdb | 25 | return adb; |
ykuroda | 0:2300f3b42cdb | 26 | } |
ykuroda | 0:2300f3b42cdb | 27 | |
ykuroda | 0:2300f3b42cdb | 28 | float |
ykuroda | 0:2300f3b42cdb | 29 | MCP3208::volt(int ch) |
ykuroda | 0:2300f3b42cdb | 30 | { |
ykuroda | 0:2300f3b42cdb | 31 | return vref * binary(ch) / 4095; |
ykuroda | 0:2300f3b42cdb | 32 | } |
ykuroda | 0:2300f3b42cdb | 33 |