'' ================================================================================================= '' '' File....... jm_mcp3208_spi.spin2 '' Purpose.... 8-Channel 12-Bit A/D Converter '' Author..... Jon "JonnyMac" McPhalen '' Copyright (C) 2010-2023 Jon McPhalen '' -- see below for terms of use '' E-mail..... jon.mcphalen@gmail.com '' Started.... 03 JUL 2010 '' Updated.... 19 NOV 2020 '' '' ================================================================================================= { 3v3-5v0 MCP3208  ┌─────────────────┐ │ ch0 ────┤1 CH0 VDD 16├──┫ ch1 ────┤2 CH1 VREF 15├──┘ ch2 ────┤3 CH2 AGND 14├──┐ ch3 ────┤4 CH3 CLK 13├──│──── sck ch4 ────┤5 CH4 DOUT 12├──│──── miso (use 4.7K series resistor if 5v used for MCP3208) ch5 ────┤6 CH5 DIN 11├──│──── mosi ch6 ────┤7 CH6 /CS 10├──│──── cs ch7 ────┤8 CH7 DGND 9├──┫ └─────────────────┘ │  } con { fixed io pins } PGM_RX = 31 { I } ' serial / programming PGM_TX = 30 { O } EE_SDA = 29 { I/O } ' i2c / eeprom EE_SCL = 28 { I/O } con SE = %1 ' single-ended DIF = %0 ' differential SE_0 = %1_1000 ' ch0 single ended SE_1 = %1_1001 SE_2 = %1_1010 SE_3 = %1_1011 SE_4 = %1_1100 SE_5 = %1_1101 SE_6 = %1_1110 SE_7 = %1_1111 D_01 = %1_0000 ' 0+/1- differential D_10 = %1_0001 ' 1+/0- D_23 = %1_0010 ' 2+/3- D_32 = %1_0011 ' 3+/2- D_45 = %1_0000 ' 4+/5- D_54 = %1_0001 ' 5+/4- D_67 = %1_0010 ' 6+/7- D_76 = %1_0011 ' 7+/6- var long cs ' chip select pin long sclk ' clock pin long mosi ' data in long miso ' data out pub null '' This is not an application pub start(cspin, sclkpin, mosipin, misopin) '' Configure MCP3208 on SPI bus longmove(@cs, @cspin, 4) ' copy pins outa[cs] := 1 ' disable (output/high) dira[cs] := 1 outa[sclk] := 0 ' make sclk output/low dira[sclk] := 1 outa[mosi] := 0 ' make mosi output dira[mosi] := 1 if (miso <> mosi) dira[miso] := 0 ' make miso input pub read(ch, mode) : result | mux '' Read MCP3208 channel '' -- ch is channel, 0 to 7 '' -- mode is 1 for single-ended, 0 for differential mux := %1_0000 | ((mode & 1) << 3) | (ch & %111) ' create mux bits return readx(mux) pub readx(mux) : level '' Read MCP3208 channel '' -- mux is encoded mux bits for channel/mode outa[cs] := 0 ' activate adc ' output mux bits, MSBFIRST if (mosi == miso) dira[mosi] := 1 ' make output mux ->= 5 ' prep for msb output repeat 5 ' send mux bits outa[mosi] := (mux <-= 1) ' output a bit outa[sclk] := 1 ' clock the bit outa[sclk] := 0 ' input data bits, MSBPOST if (miso == mosi) dira[miso] := 0 ' make input repeat 13 ' null + 12 data bits outa[sclk] := 1 ' clock a bit outa[sclk] := 0 level := (level << 1) | ina[miso] ' input data bit outa[cs] := 1 ' de-activate adc level &= $FFF pub scale(value, outmin, outmax) : result '' Scales raw (0 to 4095) value to new range: outmin to outmax value := 0 #> value <# 4095 ' constrain input return map(value, 0, 4095, outmin, outmax) ' scale to new range pub map(value, inmin, inmax, outmin, outmax) : result '' Maps value in range inmin..inmax to new value in range outmin..outmax '' -- liberated from Arduino library if (value =< inmin) return outmin elseif (value => inmax) return outmax else return (value - inmin) * (outmax - outmin) / (inmax - inmin) + outmin con {{ Terms of Use: MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. }}