Author | Wouter van Ooijen, Copyright © 1998, 1999 Wouter van Ooijen |
Adapted-by | Joep Suijs. |
Compiler | >=2.4g |
Software implementation of I2C (single) master. Define i2c pins like: alias i2c_scl is pin_c3 alias i2c_scl_direction is pin_c3_direction alias i2c_sda is pin_c4 alias i2c_sda_direction is pin_c4_direction const word _i2c_bus_speed = 1 ; * 100kHz
i2c_initialize()
i2c_initialize - initialize the software i2c bus The output latch values are always low, the lines are switched open-collector fashion by manipulating the direction registers.
i2c_stop()
i2c_stop - output a stop condition
i2c_start()
i2c_start - output a start condition
i2c_restart()
i2c_restart - output a (re)start condition A restart is a start without a previous stop. This procedure is provided as part of the API to maintain compatibility with the i2c hardware master lib.
_i2c_bit_out(bit in x)
_i2c_bit_out- output a single bit (for internal use only)
_i2c_bit_in(bit out x)
_i2c_bit_in- input a single bit (for internal use only)
_i2c_wait()
_i2c_wait - wait for a change to settle (for internal use only) The delay is determined by i2c_bus_speed and is 1/4 of the cycle time. Given the delay resolution of 1 us and all actions required by i2c_software routines, the actual bus speed will be lower then the configured maximum.
i2c_transmit_byte( byte in x ) return bit
i2c_transmit_byte - output one byte return true = okay, false = no ack received
i2c_receive_byte(bit in ack) return byte
i2c_receive_byte - read one byte The ACK flag indicated if the byte read must be acked. In general, all bytes read by the master are acked, except the last one. Failing to NACK the last byte read will give unpredictable results. (Often it will provide repeatable sequences, where one out of 2 to 4 of the reads from a specific device is correct. You are warned!)