uraimo / MCP3008.swift

A Swift library for the MCP3008 (and MCP3002,MCP3004) 10 bits SPI ADC
Other
4 stars 1 forks source link

Is this library compatible with MCP3202? #1

Closed paulofaria closed 4 years ago

paulofaria commented 4 years ago

Hello, is this library compatible with MCP3202? Thanks!

uraimo commented 4 years ago

Hey, you'll need to change the bitmask from 3 to 15, since the MCP3202 has 12 bits resolution instead of just 10 bits like the MCP3008 (and it follows that the value range will be wider too, from 0 to 4095). Other than this, you'll have only 2 channels/pins instead of 8.

paulofaria commented 4 years ago

Thank you, very much!

paulofaria commented 4 years ago

Oh, by the way. The channel selection works the same way?

uraimo commented 4 years ago

Looking at the section 6.0 on the datasheet, no... this version of the MCP uses a new flag to select which channel to read and another one the response format, the channel selection line should be modified as:

let tx: [UInt8] = [1, 0xA + (UInt8(channel)<<2)<< 4, 0]
or simply:
let tx: [UInt8] = [1, ((channel==0)?0b1010:0b1110) << 4, 0]

With each bit of those two 4 bit constants (0b<1,2,3,4>) being: 1- 1 to select the single ended mode 2- 0 for input from pin CH0 and 1 for input from pin CH1 3- 1 to select MSB mode, the default for the MCP3008 4- 0 never read

Or at least, it should work this way if I deciphered the datasheet correctly :)