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 Michael Chuah

Committer:
mcx
Date:
Thu Oct 04 06:46:49 2018 +0000
Revision:
4:d2fa630c69e1
Parent:
3:1c34c302cbfb
For 6_Sensors

Who changed what in which revision?

UserRevisionLine numberNew 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 4:d2fa630c69e1 12 _spi.frequency(2000000);
mcx 4:d2fa630c69e1 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 4:d2fa630c69e1 23 unsigned long ret = _spi.write((0x18|ch)<<2);
mcx 3:1c34c302cbfb 24 // 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 4:d2fa630c69e1 25 int adb = _spi.write(0)>>4; // mbed fixed my fix (-_-).
mcx 4:d2fa630c69e1 26 // 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 27 _cs = 1;
ykuroda 0:2300f3b42cdb 28 return adb;
ykuroda 0:2300f3b42cdb 29 }
ykuroda 0:2300f3b42cdb 30
ykuroda 0:2300f3b42cdb 31 float
ykuroda 0:2300f3b42cdb 32 MCP3208::volt(int ch)
ykuroda 0:2300f3b42cdb 33 {
ykuroda 1:0876e83ba21b 34 return _vref * binary(ch) / 4095;
ykuroda 0:2300f3b42cdb 35 }
ykuroda 0:2300f3b42cdb 36