03 I2c and Finite State Machine

Download Report

Transcript 03 I2c and Finite State Machine

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
MASTER
SLAVE 1
SDA
SCL
SLAVE 2
Pull-up Resistors
5V
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()
interrupt state = 0
I2c_write(slave addr | 0)
i2c_read()
interrupt state = 1
I2c_write(data)
I2c_stop()
Data = i2c_read()
A slave->master read example
MASTER
SLAVE
I2c_start()
interrupt state = 0
I2c_write(slave addr | 0)
i2c_read()
interrupt state = 1
I2c_write(command)
Cmd = i2c_read()
No i2c_stop()
I2c_start()
interrupt state = 0x80
I2c_write(slave addr | 1)
Data1 = I2c_read()
No Interrupt
I2c_read()
I2c_write(data1)
interrupt state = 0x81
Data2 = I2c_read(0)
I2c_stop()
I2c_write(data2)
Data flow and slave ISR state
Data Transmission (Green = Slave -> Master)
0xB0
0x01
0xB1
1
0x80
0x11
0x22
Slave ISR State
0
0x81
Download examples via SVN
from
http://svn.e-cpe.org/svn/i2c
Finite State Machine
Finite State Machine 2
Dual-I2C
Sensor 1
Sensor 2
PIC
I2c 1
(Software Based)
GoGo Board
I2c 2
(Hardware Based)
Serial
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);
…