130 likes | 684 Views
Finite State Machine & PIC-C I2C Communication. I2C Basics. I2C is a Bus. Serial is point-to-point Master and Slave pair Master must start the communication Slave can only respond Slave has a 8-bit address Bit 0 is reserved for direction control. Basic I2C Bus Setup. 5V. MASTER.
E N D
I2C Basics • I2C is a Bus. Serial is point-to-point • Master and Slave pair • Master must start the communication • Slave can only respond • Slave has a 8-bit address • Bit 0 is reserved for direction control
Basic I2C Bus Setup 5V MASTER SLAVE 1 SLAVE 2 Pull-up Resistors SDA SCL
i2C Commands in PIC-C Master Only • I2c_start() • I2c_stop() Slave Only • I2c_isr_state() Master & Slave • I2c_read() • I2c_write()
Communication example MASTER SLAVE I2c_start() I2c_write(slave addr | 0) I2c_write(data) I2c_stop() i2c_read() Data = i2c_read() interrupt state = 0 interrupt state = 1
A slave->master read example MASTER SLAVE I2c_start() I2c_write(slave addr | 0) I2c_write(command) I2c_start() I2c_write(slave addr | 1) Data1 = I2c_read() Data2 = I2c_read(0) I2c_stop() i2c_read() Cmd = i2c_read() I2c_read() I2c_write(data1) I2c_write(data2) interrupt state = 0 interrupt state = 1 No i2c_stop() interrupt state = 0x80 No Interrupt interrupt state = 0x81
Data flow and slave ISR state Data Transmission (Green = Slave -> Master) Slave ISR State
Download examples via SVN from http://svn.e-cpe.org/svn/i2c
Dual-I2C PIC Sensor 1 Sensor 2 GoGo Board I2c 1 (Software Based) Serial I2c 2 (Hardware Based)
Creating dual i2c ports // i2c1 - Master #use i2c(MASTER, SDA=PIN_B1, SCL=PIN_B2, stream=i2c_sensor) // i2c 2 - SLAVE #use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0xB0, FORCE_HW, stream=i2c_gogo) // Example of use I2c_start(i2c_sensor); I2c_read(i2c_gogo); …