Maybe you want to create some cool new UI using an actual slider (i.e., a variable resistor) to control something (like motor speed.) Well, that’s where the ADC0838 comes in. Below is some Parallax Spin code that will read in data on the first channel of that chip (and display the value back over a serial connection.) The chip actually has eight channels (you select which one you’re interested in through the built in MUX), but this code is hardwired, so to speak, to just read the first one.
Notes:
VCC, VDD mean + voltage
VSS is Ground
connect Vref (chip pin 12) to +
test 5v input: + to CH0 (chip pin 1)
SE bar (chip pin 13) to gnd
ground COM (Chip pin 9) for single ended
tie Agnd (Chip pin 11) to gnd
tie Dgnd (Chip pin 10) to gnd
ADC0838 8-Channel Mux CH0 +-1-|___|-20-+ Vcc CH1 + 2 19 + V+ CH2 + 3 18 + CS bar CH3 + 4 17 + DI CH4 + 5 16 + CLK CH5 + 6 15 + SARS CH6 + 7 14 + DO Ch7 + 8 13 + SE bar COM + 9 12 + Vref Dgnd +-10------11-+ Agnd
Single Ended MUX Mode (table 2)
SGL/DIF bar is 1
ODD/SIGN is 0
Select 1 is 0
Select 0 is 0
Setup (address MUX)
1- CS bar goes low
2- Start pulsing CLK (data read on rising edge), while sending data into DI
Rising 1
Start Bit: 1
Rising 2
SGL/DIF bar: 1
Rising 3
ODD/SIGN: 0
Rising 4
Select Bit 1: 0
Rising 5
Select Bit 0: 0
Output
1- CS bar stays low
2- pulse CLK while reading data on DO on falling edge
Falling 1..7
MSB 7..1
Falling 9..13
LSB 1..7
Here’s the SPIN code:
'**********************************
'* ADC 0838 Channel 0 Debug *
'**********************************
'adapted from Bryan Kobe June
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
' pin configuration
clk = 1
cs = 0
dataout = 2
datain = 3
'P0: CS bar (chip pin 18)
'P1: CLK (chip pin 16)
'P2: DO (chip pin 14) [data out from the chip to the propeller, its an INPUT to the propeller]
'P3: DI (chip pin 17) [data from propeller TO the chip, its an OUTPUT from the propeller]
OBJ
bs2 : "BS2_Functions"
debug : "FullDuplexSerial"
num : "Numbers"
VAR
byte ADC ' Analog to Digital Channel Din Value. Set using CON values.
long datar ' 8 Bit return value from conversion
PUB main
dira[cs]~~ 'set Chip Select to output
outa[cs] := 0 'set Chip Select bar to low
dira[clk]~~ 'set clk pin to output
outa[clk]:=0 'set clock pin low
dira[datain]~~ 'set DI data in pin to an output
dira[dataout]~ 'set DO data out pin to an input
debug.start(31,30,0,57600)
debug.str(string("ADC0838 Channel Debug"))
repeat
debug.str(string("Ch.0 = "))
debug.str(num.ToStr(GetADC(0), Num#dec))
waitcnt(5_000_000 + cnt+50000)
PRI GetADC( chan ) : value
ADC := %11000 ' channel 0
datar := write(ADC) ' write MUX Address to start conversion for ADC channel and set result to the datar value
return datar
PRI write( ADCaddr ) : ADC_value
outa[cs] := 0 'set Chip Select bar LOW, activating the ADC chip
bs2.SHIFTOUT(datain, clk, ADCaddr, BS2#MSBFIRST, 5 ) 'shift out the addressing byte to the ADC via DI pin on ADC
ADC_value := bs2.SHIFTIN(dataout, clk, BS2#MSBPOST, 8 ) 'shift in the byte from the ADC via the DO pin on the ADC
'the first byte is a start bit, to initiate reading sequence
outa[cs]:=1 'set Chip Select bar HIGH, de-activating the ADC chip
return ADC_value 'return value of the ADC