To query other sensors and devices you will need to add an 'I2C Master' to your Project Designer project. From within this you can build up the list of all the devices you have got connected to the bus. Think of this as a 'phone directory'.
At this point you may get an error if the devices don't have unique addresses. The easiest solution is to add one, or more, software I2C busses so that you can move conflicting devices onto different busses to resolve the conflict and end up with several busses each with its own set of now unique addresses.
The I2C sub-system is set up to use 100kbps (kilo bits per second) as the default communication speed. If you wish to change this then you can call setBitRate
in appInitSoftware - ie after the bus has been initialised.
The I2C interface requires pull-up resistors on the SCL and SDA lines. For a hardware bus the library achieves this by using the internal pull-up resistors on the processor pins however for a software bus you must add the extra resistors (4.7k should work).
If the device you want to add is directly supported by Project Designer then WebbotLib will do all the I2C calls for you. If your device is not in the list then add a 'Generic I2C Device'. You can then use functions it provides to read/write the registers normally found in a slave device (check its datasheet). This should allow you to get up and running quickly. If your device needs more complex communications then you can use the low-level start, get, put, stop functions to code the communications yourself from scratch.
- get - This is a low-level function to read, and return, a byte from the bus.
- put - This is a low-level function to write a byte across the bus.
- setBitRate - Sets the communications speed of the I2C bus.
- start - A low level function to start communicating with a given slave device if you are writing your own I2C communications from scratch.
- stop - This is a low-level call to indicate that the communication session has finished (ie 'hang up the phone').