Picomite issues with MCP3002, dual channel ADC SPI


Author Message
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6181
Posted: 02:15am 02 Feb 2025      

Including the start bit, you need 4 bits to set the configuration, one null bit and 10 data bits
By adding a low bit before the start bit, you have 16 bits nicely aligned

You can choose to use 8 bit spi or 16 bit spi. Either will work,
'
OPTION EXPLICIT
OPTION DEFAULT INTEGER
DIM data0, data1

SETPIN 27, DOUT                 ' pin 27 ADC chip enable active low
PIN(27) = 1                     ' ADC chip disabled
SETPIN 11, 20, 19, SPI2         ' assign SPI I/O pins, RX, TX, CLK
PAUSE 20
'using 16 bit spi
SPI2 OPEN 100000, 0, 16         ' speed 1MHz, SPI mode 0, 16 bits data
DO
PIN(27) = 0                     ' ADC chip enabled
data0 = SPI2(&b0110100000000000)' get data from ADC channel 0
data1 = SPI2(&b0111100000000000)' get data from ADC channel 1
PIN(27)=1                       ' ADC chip disabled
PRINT data0,data1
PAUSE 5000
LOOP UNTIL INKEY$<>""
SPI2 CLOSE                      ' close the channel

'using 8 bit spi
SPI2 OPEN 100000, 0, 8         ' speed 1MHz, SPI mode 0, 8 bits data
DO
PIN(27) = 0                     ' ADC chip enabled
data0 = SPI2(&b01101000)<<8 + SPI2(0)' get data from ADC channel 0
data0 = data0 AND &h3FF
data1 = SPI2(&b01111000)<<8 + SPI2(0)' get data from ADC channel 1
data1 = data1 AND &h3FF
PIN(27)=1                       ' ADC chip disabled
PRINT data0,data1
PAUSE 5000
LOOP UNTIL INKEY$<>""
SPI2 CLOSE                      ' close the channel



You may need to toggle the CS pin between the two readings.

I don't have the MCP3002 so untested.

Jim