Sending and Receiving Data
Those familiar only with serial ("UART") communications will find this surprising: there are no separate send and receive operations for the ssi. object. When you are sending something out you are also receiving something in at the same time (and vise versa). On the ssi. object's level, these two operations are simultaneous — with each clock pulse generated on the CLK line, one data bit will be output onto the DO line, and one bit will be recorded from the DI line. The same is true when DO and DI lines are merged.
Of course, on the operational level, interactions with a slave SSI device usually consist of sending something (command or data) to the slave device first and then receiving something (data) from it later. Typically, when your device is sending a command, the slave side won't be sending anything meaningful back at the same time, but the SSI channel will still record the data on the DI line. It is your application that should know how to interpret the data received from the slave device.
In the similar matter, whenever you are receiving from the slave device, you are also sending something. Normally, when the slave device is sending you data it will ignore everything you are sending to it.
There are two ways to send and receive data: word-by-word, or as a "string".
For "word-by-word transactions", use ssi.value method. It will simultaneously clock out and clock in up to 16 bits of data. If you are sending less than 16 bits, then the specified number of the rightmost bits will be sent and received.
** Tibbo Basic **
Dim x As word ... ssi.value(&hCA5A,14) 'When we want to send something out, we can ignore the "data" we get back x=ssi.value(&hFFFF,14) 'When we are expecting to receive data, we send out 'all 1s' (good practice) x=ssi.value(&hCA5A,14) 'Of course we can do both at the same time
For "string transactions", use ssi.str method. String transactions are more sophisticated: you can send (and receive) a string consisting of multiple bytes. These bytes may be handled as "pure" 8-bit words or 9-bit words consisting of eight data bits plus one acknowledgement bit, as required for I2C communications (http://en.wikipedia.org/wiki/I2C). For each byte it outputs, the ssi. object will check for the slave's acknowledgement. Data output will not continue if there was no acknowledgement. You can detect this by comparing the length of the returned string and the length of the string you were trying to send — if they do not match, then the data exchange ended prematurely.
** Tibbo Basic **
Dim Input_String, Output_String As String ... Output_String="\x40\x12\xFF" Input_String=ssi.str(Output_String,1) 'output data, with acknowledgements enabled If Len(Output_String)<>Len(Input_String) Then 'something went wrong End If ...
If you are going to talk to an I2C device, be sure to check out More on I2C topic.