7:58 pm
March 12, 2015
We recently had a question about using the I2C library to create a Slave Device. We realized there are a few details to be aware of and there is no packaged Slave example so this post will hopefully clear up any questions.
First of all, you need to make sure you are attaching your request interrupt using the "I2C Attach Request Interrupt" VI not "I2C Attach Receive Interrupt". Since this is the Slave side, it only responds to requests sent by the Master, so attach request must is used. Also be sure to set your slave address properly.
In the interrupt handler you have attached, there are a couple ways to send data back to the Master. There is an "I2C Write" polymorphic VI which has 3 methods: Write Byte, Write Array, and Write String. When used in Slave Mode, it is important that the Write Mode is set to "Queue Only". This inhibits the beginTransmission and endTramsission call from occurring in the Arduino library. If beginTransmission is called, it automatically forces master mode within the Arduino Wire library. This will cause the Slave to never respond if it is used in the Slave side interrupt handler. Here is an example of the Slave side interrupt to return a byte of data to the Master. Note that "Send Stop" must be set to True to release the bus after the Slave is done sending.
However, for Write String (as of Compiler version 1.0.0.21) there is no Write Mode input and Write String automatically calls beginTransmission. To get around this, the attached Arduino Wire library can be used which forces Slave mode when the Slave Address is set to 0. Since the I2C write function sends data to the Slave Address specified (when used on the Master side) then it makes sense that the library should force Slave mode when the Slave address is undefined, or set to 0. Here is an example of the Slave side interrupt to return a string of data to the Master. Note that "Send Stop" must be set to True to release the bus after the Slave is done sending and the Slave Address is set to 0.
5
1 Guest(s)