' {$STAMP BS2} cs CON 0 'chip select is pin 0 clk CON 1 'clock is pin 1 dat CON 2 'dat is pin 2 'make sure there is a pull-up resistor (10k) on the data pin refv CON 5 'reference voltage refvbin CON 1023 'the max value for 10 bits pt CON 1 value VAR Word 'the reading results is stored here voltage VAR Nib 'the calculated voltage is here value = 0 HIGH cs 'chip not selected HIGH clk 'clock is high INPUT dat 'dat is on input PAUSE 250 falseread: 'the first reading is always a false reading, this step ignores it LOW cs 'select that chip 'idle clock cycle 1, two are needed LOW clk PAUSE pt HIGH clk PAUSE pt 'idle cycle 2 LOW clk PAUSE pt HIGH clk PAUSE pt 'end idle clock cycles, start null bit LOW clk PAUSE pt HIGH clk PAUSE pt 'end null bit 'start shift in SHIFTIN dat, clk, MSBPOST, [value\10] 'shift in serial data 'dat and clk pin are defined 'msb means most significant bit 'msbpost means read bit after clock cycle 'the 10 bit data is stored into "value" 'change the 10 to the resolution of your chip HIGH cs 'unselect chip PAUSE 250 GOTO mainloop: mainloop: LOW cs 'select that chip 'idle clock cycle 1, two are needed LOW clk PAUSE pt HIGH clk PAUSE pt 'idle cycle 2 LOW clk PAUSE pt HIGH clk PAUSE pt 'end idle clock cycles, start null bit LOW clk PAUSE pt HIGH clk PAUSE pt 'end null bit 'start shift in SHIFTIN dat, clk, MSBPOST, [value\10] 'shift in serial data 'dat and clk pin are defined 'msbpost means read bit after clock cycle 'the 10 bit data is stored into "value" 'change the 10 to the resolution of your chip HIGH cs 'unselect chip voltage = (value * 100 / 1023) * refv / 100 'this is supposed to calculate voltage 'debug will display the value on your computer screen DEBUG HOME DEBUG "Binary Read: " DEBUG BIN value DEBUG " ", CR DEBUG "Decimal Read: " DEBUG DEC value DEBUG " ", CR DEBUG "Voltage: " DEBUG DEC voltage, CR 'this is rounded down PAUSE 10 'loop delay value = 0 'resets the value variable voltage = 0 'resets the voltage variable GOTO mainloop 'repeat