I2C problem
Posted: Sat Nov 29, 2014 4:57 pm
Hello.
I used the latest nodeMCU firmware and coded a small program to work with i2c device.
I have MCP23017 connected to GPIO0 and GPIO2 (data+clock).
I connected few leds to port A of MCP23017 and set the address to 0x20.
I tried few writes and everything was just fine.
But than I coded a simple main.lua like this:
When executed, it works as expected.
But than I added a delay after the writing to the i2c, before the end statement of the for loop, so now the for loop looks like this:
With this delay, the script runs until 10-15 and than the device restarts.
Even when removing the delay and with the longer "print" statement, the script runs only until i=~100.
Shortening the print statement and removing the delay, the script again runs until 255.
Any advice, what am I doing wrong?
I used the latest nodeMCU firmware and coded a small program to work with i2c device.
I have MCP23017 connected to GPIO0 and GPIO2 (data+clock).
I connected few leds to port A of MCP23017 and set the address to 0x20.
I tried few writes and everything was just fine.
But than I coded a simple main.lua like this:
Code: Select all
id=0
sda=8
scl=9
-- initialize i2c, set pin1 as sda, set pin0 as scl
i2c.setup(id,sda,scl,i2c.SLOW)
-- user defined function: read from reg_addr content of dev_addr
function read_reg(dev_addr, reg_addr)
i2c.start(id)
i2c.address(id, dev_addr ,i2c.TRANSMITTER)
i2c.write(id,reg_addr)
i2c.stop(id)
i2c.start(id)
i2c.address(id, dev_addr,i2c.RECEIVER)
c=i2c.read(id,1)
i2c.stop(id)
return c
end
function write_reg(dev_addr, reg_addr, reg_val)
i2c.start(id)
i2c.address(id, dev_addr, i2c.TRANSMITTER)
i2c.write(id, reg_addr)
i2c.write(id, reg_val)
i2c.stop(id)
end
-- get content of register 0xAA of device 0x77
write_reg(0x20, 0x00, 0x00) -- set bank A to output
for i=0,255 do
print(i)
write_reg(0x20, 0x12, i)
end
When executed, it works as expected.
But than I added a delay after the writing to the i2c, before the end statement of the for loop, so now the for loop looks like this:
Code: Select all
for i=0,255 do
print("Setting bank A to value " .. i)
write_reg(0x20, 0x12, i)
tmr.delay(200000)
end
With this delay, the script runs until 10-15 and than the device restarts.
Even when removing the delay and with the longer "print" statement, the script runs only until i=~100.
Shortening the print statement and removing the delay, the script again runs until 255.
Any advice, what am I doing wrong?