'MAX127 Demo func main() dim ch0 as float dim ch1 as float const sda 0 const scl 1 loop: ch0 =MAX127read(sda,scl,0,0,1,1) print {.} "raw=", ch0," ",{.2} "Volts=",ch0*.00245*2 pause 500 goto loop endfunc '--------------------------------------------------------- 'Read one of the channels from Max127 ' sda data pin. Must be held high with 10k resistor ' scl clock pin. Must be held high with 10k resistor ' addr must mach A0,A1,A2 pins (0-7) ' chnl Max127 channel (0-7) ' rng 0=5v 1=10v ' bip 0=Unipolar 1-Bipolar ' ' Returns value as float 0-4095 uniploar -2047 - 2047 Bipolar '--------------------------------------------------------- func MAX127read(sda,scl,addr,chnl,rng,bip) as float dim value dim control addr = 80 + addr control=chnl & 7 * 16 control.bit(7)=1 control.bit(3)=rng control.bit(2)=bip '----------------------------------------- 'Set up control byte '----------------------------------------- addr.bit(0)=0 I2c_start(sda,scl) 'address I2c_sendbyte(sda,scl,addr) 'EEprom write I2c_getack(sda,scl) 'control I2c_sendbyte(sda,scl,control.byte(0)) I2c_getack(sda,scl) I2c_stop(sda,scl) pauseus 25 '---------------------------------------- 'Go read the conversion '---------------------------------------- addr.bit(0)=1 I2c_start(sda,scl) 'address byte I2c_sendbyte(sda,scl,addr) I2c_getack(sda,scl) value.byte(1) = I2c_getbyte(sda,scl) I2c_sendack(sda,scl) value.byte(0) = I2c_getbyte(sda,scl) I2c_sendnack(sda,scl) I2c_stop(sda,scl) value = value / 16 if value.bit(11) = 1 and bip = 1 then value = value -1 ^ 4095 exit value * -1 else exit value endif endfunc include \lib\DiosI2c.lib