-->
Page 1 of 2

Reading a register in Lua ESP8266-01

PostPosted: Thu Aug 06, 2015 5:48 am
by lampshade
Hey everyone,

i didnt get a simple read from a register function working. I got a ESP8266-01, that runs nodemcu. I just want to write to a conversion register of a MCP4725 DAC and than again read the value. But my output is alwas 255 :(

heres my code:
write
Code: Select allid=0
sda=3
scl=4
i2c.setup(id, sda, scl, i2c.SLOW)
voltage=1
volt=(4096*voltage)/3.27   --the conversion
msb = bit.rshift(volt, 8)
lsb = volt-bit.lshift(msb,8)
print(lsb)
print(msb)
i2c.start(id)
i2c.address(id, 0x60,i2c.TRANSMITTER)
--i2c.write(id, 0x40)
i2c.write(id,msb)
i2c.write(id,lsb)
i2c.stop(id)


than i want to read the register with this code:
Code: Select allid=0
sda=3
scl=4
i2c.setup(id, sda, scl, i2c.SLOW)
i2c.start(id)
i2c.address(id, 0x60,i2c.RECEIVER)
--i2c.write(id,0x40) also tried this
c=i2c.read(id,2)
h,i=string.byte(c,1,2)
print("MSB=",h)
print("LSB=",i)
i2c.stop(id)


hopefully someone could tell what im doing wrong

Edit: Ok saw my fault wrong read_reg function

now its this:
Code: Select alli2c.start(id)
      i2c.address(id, 0x60 ,i2c.TRANSMITTER)
      i2c.write(id,0x40)
      i2c.stop(id)
      i2c.start(id)
      i2c.address(id, 0x60,i2c.RECEIVER)
      c=i2c.read(id,2)
c,h=string.byte(c,1,2)
print(c)     
print(h)
i2c.stop(id)


but anyway its now full of 0s

Re: Reading a register in Lua ESP8266-01

PostPosted: Thu Aug 06, 2015 7:01 am
by GerryKeely
Try add the following line when writing to the MCP4725 DAC

i2c.write(id, reg_val) -- reg_val = conversion register of a MCP4725 DAC

just before i2c.stop

regards
Gerry

Re: Reading a register in Lua ESP8266-01

PostPosted: Thu Aug 06, 2015 7:13 am
by jankop
Where is connected A0 of the MCP4725? Is device address realy 60h and not 61h?

Re: Reading a register in Lua ESP8266-01

PostPosted: Thu Aug 06, 2015 7:33 am
by lampshade
jankop wrote:Where is connected A0 of the MCP4725? Is device address realy 60h and not 61h?


A0 isnt connected at all so yes im sure its 0x60