I2C device on bus

General questions about Firewing...

I2C device on bus

Postby AndrewB » Wed Apr 11, 2018 4:49 pm

Is there a way to check if an I2C device is on the bus?

I have four devices on the bus and three may be removed at some point .

I want to be able to check and count the three if attached.

thanks
AndrewB
 
Posts: 94
Joined: Thu Jan 02, 2014 3:38 pm

Re: I2C device on bus

Postby Jerry Messina » Wed Apr 11, 2018 6:33 pm

Yes.

Send the address byte + WR and check to see if you get an acknowledge.
If the device recognizes its address it'll pull SDA low during the ninth clock. Otherwise, SDA will remain high.

You can terminate the transaction right after reading the ACK bit (so that would be START - ADDR+WR - STOP).
Jerry Messina
 
Posts: 280
Joined: Thu Feb 14, 2013 10:16 am

Re: I2C device on bus

Postby AndrewB » Wed Apr 11, 2018 8:12 pm

Ok

Sorry but I could not find any examples on the site.

Do I do
devices = 0
I2C.Start()
I2C.WriteByte(I2C_device + 1)
if I2C.Acknowledge(IsAcknowledge) = true then
devices = devices + 1
AndrewB
 
Posts: 94
Joined: Thu Jan 02, 2014 3:38 pm

Re: I2C device on bus

Postby Jerry Messina » Wed Apr 11, 2018 11:30 pm

Not quite.

The Acknowledge() function is for sending an ack, not reading one. To read it just look at the NotAcknowledged boolean.
Also, the RD_WRN flag in bit0 of the I2C slave address byte is RD=1, WR=0, so send the address with nothing added to it.

More like this...
Code: Select all
devices = 0
I2C.Start()
I2C.WriteByte(I2C_device)
I2C.Stop()
// check NotAcknowledged flag to see if device responded
if (I2C.NotAcknowledged = I2C.IsAcknowledge) then
   devices = devices + 1
endif
Jerry Messina
 
Posts: 280
Joined: Thu Feb 14, 2013 10:16 am

Re: I2C device on bus

Postby AndrewB » Thu Apr 12, 2018 5:35 pm

I get an error for "if (I2C.NotAcknowledged = I2C.IsAcknowledge) then" as Incompatible types
AndrewB
 
Posts: 94
Joined: Thu Jan 02, 2014 3:38 pm

Re: I2C device on bus

Postby Jerry Messina » Thu Apr 12, 2018 7:59 pm

Try casting IsAcknowledge to a Boolean type
Code: Select all
if (I2C.NotAcknowledged = cbool(I2C.IsAcknowledge)) then


You could also just check that NotAcknowledged = false, but that always screws with my head. NotAcknowledged = false when the line is low, so that means you got an ack. I suppose you should think of it as "NotAcknowledged is true, so you didn't get an ack".

Clear as mud, right?
Jerry Messina
 
Posts: 280
Joined: Thu Feb 14, 2013 10:16 am

Re: I2C device on bus

Postby AndrewB » Tue Jul 24, 2018 10:22 am

Thanks Jerry.
I ended up just noting if 255 was read when checking as this showed that the device was not present.
Seemed to work OK.

I
AndrewB
 
Posts: 94
Joined: Thu Jan 02, 2014 3:38 pm


Return to Questions

Who is online

Users browsing this forum: No registered users and 3 guests

cron

x