i2c

21

Upload: tuanvu-ho

Post on 01-Sep-2014

55 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: I2C
Page 2: I2C

Câu lạc bộ Nghiên cứu Khoa học Khoa Điện – Điện tử

I2CPIC16F 887 TRAINING COURCE

13/10/2010

Page 3: I2C

I²C (Inter-Integrated Circuit)[Eye-to-see]

Serial Data LineMCU

MasterMCU Slave

SensorSlave

EEPROMSlave

ADCSlave

Serial Clock SCL

SDA

Rp Rp

I2C

BUS

RPULL-UP: 1.8 kΩ – 47 kΩ 1K8 4K7 10K

VCC

+ 5V+ 3.3V

Device address: 7-bit or 10-bit addressing

128 devices127 Slaves

1 Master

BUT:16 reserved addresses

maximum 112 nodes can communicate on the same bus

Page 4: I2C

I²C (Inter-Integrated Circuit)

MODE

One Master – One Slave

One Master – Multi-slave

Multi-master – Multi-slave

SPEED

Standard mode: 100 kbit/s

Low-speed mode: 10kbit/s

Fast mode: 400 kbit/s

Fast mode plus: 1 Mbit/s

High Speed mode: 3.4 Mbit/s

Page 5: I2C

I²C (Inter-Integrated Circuit)

Idle StatusIdle Status

Timing diagram

Start sequence

Stop sequenceTransper data

Page 6: I2C

I²C (Inter-Integrated Circuit)

Read 1 byte

AK (ACK) : Acknowledge = “0”NAK (NACK) : Not Acknowledge = “1”

Start sequence

AK AK AK

ReadWrite NAKReStart sequence Stop sequence

Page 7: I2C

I²C (Inter-Integrated Circuit)

Read multi-byteAK

AK AK NAK

Stop sequence

Page 8: I2C

I²C (Inter-Integrated Circuit)

Write 1 byte

AK AK AK

WriteStart sequence

Stop sequence

Page 9: I2C

I²C (Inter-Integrated Circuit)

Write multi-byteStop sequence

Page 10: I2C

I²C (Inter-Integrated Circuit)

Real-Time Clock DS1307 Module

Page 12: I2C

I²C (Inter-Integrated Circuit)In PIC 16F887

MASTER SYNCHRONOUS SERIAL PORT (MSSP) MODULE

MCU can be Master or Slave on I2C Bus7-bit and 10-bit addressing

DS1307 Slave address: 0x68

Page 13: I2C

I²C (Inter-Integrated Circuit)

// Send Start sequence to Slavevoid i2c_start(); // Send Stop sequence to Slavevoid i2c_stop();// Initialize i2c modulevoid i2c_init();unsigned char i2c_read(unsigned char ack);

// ack = 1: Not Acknowledge// ack = 0: Acknowledge

unsigned char i2c_write(unsigned char d);// d: 8-bit data

I2C functions in i2c.h (Hi-tech PIC)

Page 14: I2C

I²C (Inter-Integrated Circuit)

void i2c_init()

RC3 = 1;RC4 = 1;TRISC3 = 1;TRISC4 = 1; //control by MSSPSSPADD = 9;// Baudrate = Fosc/4/(SSPADDR+1)SMP = 1; // slew rate control disabled for 100KHz baudrateSSPCON2 = 0;SSPCON = 0x28; //I2C Master Hardware mode

i2c_init function in i2c.c (Hi-tech PIC)

Page 15: I2C

I²C (Inter-Integrated Circuit)

…#include “i2c.h”

// DS1307 7-bit address = 0b 110 1000 = 0x68#define SLAVE_ADD 0x68#define READ 1#define WRITE 0

void main()… i2c_init();…

How to write Hi-tech C code for I2C module

Page 16: I2C

I²C (Inter-Integrated Circuit)

Example: Read multi-byte

//Send Start conditioni2c_start();//Send Slave address + WRITE commandi2c_write((SLAVE_ADD<<1)|WRITE));// ((SLAVE_ADD<<1)| WRITE) = ((0x68<<1)|0) = 0xD0//Send register address (register of Slave – the first register need to read data)i2c_write(0x00);//ReStart conditioni2c_stop();i2c_start();

How to write Hi-tech C code for I2C module

Page 17: I2C

I²C (Inter-Integrated Circuit)

Example: Read multi-byte <cont.>//Send Slave address + READ commandi2c_write((SLAVE_ADD<<1)|READ));// ((SLAVE_ADD<<1)| READ) = ((0x68<<1)|1) = 0xD1

//read data//unsigned char i2c_read(unsigned char ack);data_array[0] = i2c_read(0); //1st byte and ACKdata_array[1] = i2c_read(0); //2nd byte and ACKdata_array[2] = i2c_read(1); //3rd byte and NACK

//Send Stop conditioni2c_stop();

How to write Hi-tech C code for I2C module

Page 18: I2C
Page 19: I2C

TC74: Serial Digital Thermal SensorDigital Temperature Sensing in SOT-23-5 orTO-220 Packages

Outputs Temperature as an 8-Bit Digital Word

Simple SMBus/I2C™ Serial Port Interface

Supply Voltage of 2.7V to 5.5V