top
About Buy News Guide Ideas
The phatIO project has finished, and I'm no longer selling ready made boards - but you're welcome to look around - send any questions to the email below

Back to Table of Contents

#TWI (Two Wire Interface)

phatIOs TWI (Two Wire Interface also called I2C) functionality allows communication with devices connected to the TWI bus on pins 12 (SCL - clock) and 13 (SDA - data).

The Ideas section contains several TWI examples, for example (ht16k33)

Note that the current implementation doesn’t read data - this will be fixed in a future version.

##(twi_start <address>) [twi_start]

Initialize the twi interface and send a start message to the twi device with the given address. Function will return 0 on success or the error status on failure.

##(twi_write <data> ...) [twi_data] Send bytes to the current TWI device (called after a (twi_start)). Each argument is evaluated as an unsigned byte and sent on the twi bus. So:

(twi_write 0x22F3 (+ 3 0x80))

Would send 0xF3 (the low byte of 0x22F3) and 0x83 (3 + 0x80) to the current

Function will return 0 on success or the error status on failure.

##(twi_stop) [twi_stop] Stops the current twi session.

##(twi <address> <data> ...) [twi] This function allows a complete twi transmission to be done in a single function call, equivalent to:

(twi_start address)
(twi_data arg2)
(twi_data arg3)
(twi_data arg4)
...
(twi_stop)

Note that the twi function can be used in two modes

  1. Each argument to the (twi) function is evaluated to produce an unsigned byte and the value is sent on the twi bus.

  2. To allow for arbitrary code to be executed within a TWI session (loops and control statements), if (twi_data) is called during evaluation of one of the arguments, it is assumed that none of the further arguments to ‘twi’ should be sent - although all will be evaluated, for example:

    (= i 0) (twi address 0x0A 0x0B (while (< i 3) (+= i 1)) 0x0C )

would send 0x0A, 0x0B, 0x03, 0x0C to the twi device where 0x03 is the return value of the while loop. However:

(= i 0)
(twi address 
	0x0A 0x0B
	(while (< i 3)
		(twi_data i)
		(+= i 1))
	0x0C
)

would send 0x0A, 0x0B, 0x00, 0x01, 0x02 to the twi device. 0x01, 0x02, and 0x03 are sent manually by the call to twi_data in the while loop, and hence 0x0C isn’t sent.

Function will return 0 on success or the error status on failure.

@Contact  |   Legal & Privacy  |    RSS  |   Development Twitter  |   Official Twitter
© phatIO 2012, All Rights Reserved.