Picomite issues with MCP3002, dual channel ADC SPI


Author Message
Pluto
Guru

Joined: 09/06/2017
Location: Finland
Posts: 374
Posted: 10:37am 02 Feb 2025      

I made a test program for MCP3004 some years ago. If I remember correctly it was ok. But I dont know if the operation is similar for your MCP3002. Please have a look and see if this makes any sense for solving your problem.

'FN1:
option autorun on
 cls
 
 TEXT 160,40,"MCP3004","CM",1,5,RGB(white)
 TEXT 160,120,"10bit ADC","CM",7,2,RGB(yellow)
 TEXT 160,200,"FN 2021","CM",1,5,RGB(magenta)
 pause 1000
 cls
 Dim integer TX,RX(2),HighByte, LowByte, i
 dim integer DOC 'Digital Output Code
 DIM float Vref=3.296, Vin, Avg, Vavg
 const CS=5 'Chip Select
 const DebugPrint=0
 setpin CS, dout
 pin(CS)=1
 pin(CS)=0
 pin(CS)=1
 'If the device was powered up with the CS pin low, it must be
 'brought high and back low to initiate communication.
 
 '            bit|7|  6     |     5     |4 |2 |1 |0|
 'TX=&b01000000' |0|StartBIT|Single/Diff|D2|D1|D0|0|      D2 is 0 for MCP3004
 TX=&b01000000'             differential CH0-CH1
 'TX=&b01000010'            diff         CH1-CH0
 'TX=&b01000100'            diff         CH2-CH3
 'TX=&b01000110'            diff         CH3-CH2
 'TX=&b01100000'            single          CH0
 'TX=&b01100010'            single          CH1
 'TX=&b01100100'            single          CH2
 'TX=&b01100110'            single          CH3
 'The first clock received with CS low and DIN high will constitute a start bit.
 'The SGL/DIFF bit follows the start bit and will determine if the conversion will be done
 'using single-ended or differential input mode. The next three bits (D0, D1 and D2) are
 'used to select the input channel configuration.
 
 'If necessary, it is possible to bring CS low and clock in leading zeros on the DIN line
 'before the start bit. This is often done when dealing with microcontroller-based
 'SPI ports that must send 8 bits at a time. NOTE THE FIRST "0" IN TX!
 'BY INSERTING ONE "0" BEFORE THE START BIT, THE OUTPUT MSB WILL START DIRECTLY WITH B9
 'OF THE OUTPUT CODE. (No Null Bit before the Output Code.)
 
 'The device will begin to sample the analog input on the fourth rising edge of the clock
 'after the start bit has been received. The sample period will end on the falling edge
 'of the fifth clock following the start bit.
 '
 'Once the D0 bit is input, one more clock is required to complete the sample and hold
 'period (DIN is a “don’t care” for this clock). On the falling edge of the next
 'clock, the device will output a low null bit. The next 10 clocks will output the result
 'of the conversion with MSB first.
 
 
 
 do
   Avg=0
   for i=1 to 100
   MCP3004read
   Avg=Vin+Avg
   Next i
   Vavg=Avg/100
   
   TEXT 160,40,"V="+str$(Vavg,1,4)+"V","CM",7,2,RGB(white)
 loop
 end'
 
 sub MCP3004read
 spi open 1000000,3,8 '8 bit mode
   'Data is always output from the device on the falling edge of the clock.
   'Mode 0,0 or 1,1 are possible.
   PIN(CS)=0
   
   SPI WRITE 1,TX
   
   SPI READ 2,RX()
   if DebugPrint=1 then Print "RX0:";RX(0);" 0b";bin$(RX(0),8)
   if DebugPrint=1 then Print "RX1:";RX(1);" 0b";bin$(RX(1),8)
   'Print "RX2:";RX(2);" 0b";bin$(RX(2),8)
   'Print "RX3:";RX(3);" 0b";bin$(RX(3),8)
   HighByte=RX(0) and &b11111111 'B9:B2 of the output data
   HighByte= HighByte<<2
   LowByte= RX(1) AND &b11000000 'B1:B0 of the output data
   LowByte=LowByte>>6
   if DebugPrint=1 then print "HighByte";HighByte
   if DebugPrint=1 then print "LowByte:";LowByte
   DOC=HighByte+LowByte
   if DebugPrint=1 then Print "Code:"; DOC
   Vin=Vref*DOC/1024
   if DebugPrint=1 then Print "Vin=";Vin
   PIN(CS)=1 'The CS/SHDN pin must be pulled high between conversions.
   spi close
 end sub


Pluto