You are on page 1of 10

Tc gi: Nguyn Ch Linh

http://codientu.org

Tmtt:
GiithiuchungvI2C
cimchungvI2C:giaothc,ach
C cchhotng:masterslave,multimaster
ModuleI2CtrongViiukhinPIC
Cutr cphncng
Chhotng:Master,Slave,Multimaster

1.

GiithiuchungvI2C

Ngynaytrongcchthnginthini,rtnhiuICshaythitbngoivicn
phigiaotipviccICshaythitbkhcgiaotipvithgiibnngoi.Vimctiu
tchiuquchophncngttnhtvimchinngin,Phillipsphttrin
mtchungiaotipnitip2dycgilI2C.I2CltnvitttcacmtInter
IntergratedCircuitBusgiaotipgiaccICvinhau.

LchsI2CThmvoy(Phnnysthmsau)

I2CmcdcphttrinbiPhilips,nhngncrtnhiunhsnxutIC
trnthgiisdng.I2Ctrthnhmtchuncngnghipchoccgiaotipiukhin,c
thkraymtvitntuingoiPhilipsnh:TexasIntrument(TI),MaximDallas,
analogDevice,NationalSemiconductorBusI2Ccsdnglmbusgiaotipngoi
vichortnhiuloiICkhcnhaunhccloiViiukhin8051,PIC,AVR,ARM,chp
nhnhRAMtnh(StaticRam),EEPROM,bchuynitngts(ADC),stngt
(DAC),ICiukhinLCD,LED

Hnh1.1.BUSI2Cvccthitbngoivi

Tc gi: Nguyn Ch Linh

http://codientu.org

1.1.

cimgiaotipI2C

MtgiaotipI2Cgmc2dy:SerialData(SDA)vSerialClock(SCL).SDAlng
truyndliu2hng,cnSCLlngtruynxungnghvchtheomthng.
Nhhnhvtrn,khimtthitbngoiviktnivongI2CthchnSDAcans
nividySDAcabus,chnSCLsnividySCL.

Hnh1.2.KtnithitbvobusI2Cchchun(Standardmode)

vchnhanh(Fastmode)

MidySDAhaySCLucniviinpdngcanguncpthngquamt
intrkoln(pullupresistor).Scnthitcaccintrkonylvchngiaotip
I2Ccaccthitbngoivithngldngccmngh(opendrainoropencollector).
Gitrcaccintrnykhcnhautyvotngthitbvchungiaotip,thngdao
ngtrongkhong1Kn4.7K.

Trlivihnh1.1,tathycrtnhiuthitb(ICs)cngcktnivomtbus
I2C,tuynhinskhngxyrachuynnhmlngiaccthitb,bimithitbsc
nhnrabimtachduynhtvimtquanhch/ttntitrongsutthigiankt
ni.Mithitbcthhotngnhlthitbnhndliuhaycthvatruynva
nhn.Hotngtruynhaynhncntythucvovicthitblch(master)hayt
(slave).

MtthitbhaymtICkhiktnivibusI2C,ngoimtach(duynht)phn
bit,ncnccuhnhlthitbch(master)hayt(slave).Tisaolicsphnbit
ny?lvtrnmtbusI2Cthquyniukhinthucvthitbch(master).Thitb
chnmvaitrtoxungnghchotonhthng,khigiahaithitbch/tgiaotip
ththitbchcnhimvtoxungnghvqunlachcathitbttrongsut
qutrnhgiaotip.Thitbchgivaitrchng,cnthitbtgivaitrbng
trongvicgiaotip.

Tc gi: Nguyn Ch Linh

http://codientu.org

Hnh1.3.Truynnhndliugiach/t

Nhnhnhtrntathyxungnghchcmthngtchnt,cnlungdliu
cthitheohaihng,tchnthayngclitnch.

VdliutruyntrnbusI2C,mtbusI2Cchuntruyn8bitdliuchngtrn
ngtruynvitcl100Kbits/sChchun(Standardmode).Tctruync
thlnti400Kbits/sChnhanh(Fastmode)vcaonhtl3,4Mbits/sChcao
tc(Highspeedmode).

MtbusI2Ccthhotngnhiuchkhcnhau:

- Mtchmtt(onemasteroneslave)

- Mtchnhiut(onemastermultislave)

- Nhiuchnhiut(Multimastermultislave)

Dchno,mtgiaotipI2Cudavoquanhch/t.GithitmtthitbA
mungidliunthitbB,qutrnhcthchinnhsau:

ThitbA(Ch)xcnhngachcathitbB(t),cngvivicxcnh
ach,thitbAsquytnhvicchayghivothitbt

ThitbAgidliutithitbB

ThitbAktthcqutrnhtruyndliu

KhiAmunnhndliutB,qutrnhdinranhtrn,chkhclAsnhndliu
tB.Tronggiaotipny,AlchcnBvnlt.Chititvicthitlpmtgiaotipgia
haithitbscmtchitittrongccmcdiy.

1.2.

STARTandSTOPconditions

STARTvSTOPlnhngiukinbtbucphickhimtthitbchmunthit
lpgiaotipvimtthitbnotrongmngI2C.STARTliukinkhiu,bo
hiubtucagiaotip,cnSTOPbohiuktthcmtgiaotip.Hnhdiymt
iukinSTARTvSTOP.

Banukhichathchinqutrnhgiaotip,chaingSDAvSCLumc
cao(SDA=SCL=HIGH).LcnybusI2Cccoildi(busfree),snsngchomt
giaotip.HaiiukinSTARTvSTOPlkhngththiutrongvicgiaotipgiacc
thitbI2Cvinhau

Tc gi: Nguyn Ch Linh

http://codientu.org

Hnh1.4.iukinSTARTvSTOPcabusI2C

iukinSTART:mtschuynitrngthitcaoxungthptrnngSDA
trongkhingSCLangmccao(cao=1;thp=0)bohiumtiukinSTART

iukinSTOP:MtschuynitrngthitmcthplncaotrnngSDA
trongkhingSCLangmccao.

ChaiiukinSTARTvSTOPuctorabithitbch.SautnhiuSTART,
busI2Ccoinhangtrongtrangthilmvic(busy).BusI2Csri,snsngchomt
giaotipmisautnhiuSTOPtphathitbch.

SaukhicmtiukinSTART,trongquatrnhgiaotip,khicmttnhiuSTART
clplithayvmttnhiuSTOPthbusI2Cvntiptctrongtrngthibn.Tn
hiuSTARTvlpliSTARTucchcnnggingnhaulkhitomtgiaotip.

1.3.

nhdngdliutruyn

DliuctruyntrnbusI2Ctheotngbit,bitdliuctruynitimisn
dngcaxungnghtrndySCL,qutrnhthayibitdliuxyrakhiSCLang
mcthp.

Hnh1.5.Qutrnhtruyn1bitdliu

Mibytedliuctruyncdil8bits.Slngbytecthtruyntrongmt
lnlkhnghnch.MibytectruynitheosaulmtbitACKbohiu
nhndliu.Bitctrngscaonht(MSB)sctruyniutin,ccbtsc
truynilnlt.Sau8xungclocktrndySCL,8bitdliuctruyni.Lcny
thitbnhn,saukhinhn8btdliuskoSDAxungmcthptomtxung
ACKngvixungclockth9trndySDAbohiunhn8bit.Thitbtruyn
khinhncbitACKstiptcthchinqutrnhtruynhocktthc.

http://lab3i.com

Tc gi: Nguyn Ch Linh

Hnh1.6.DliutruyntrnbusI2C

Hnh1.7.BitACKtrnbusI2C

Truyn

Nhn

SDA=data_bit

Buffer=databit

Kimtraxem
truyn
8bitcha

Clock=8?

NO

BufferFull?

YES
ACK?

YES
NO

SendACK

YES
Next_byte
orSTOP

ReadBuffer

STOP

Hnh1.8.Luthuttonqutrnhtruyngnhndliu

NO

hhttp://lab3i.co

Tc gi: Nguyn Ch Linh

MtbytetruynickmtheobitACKliukinbtbuc,nhmmbochoqu
trnhtruynnhncdinrachnhxc.Khikhngnhncngachhaykhi
munktthcqutrnhgiaotip,thitbnhnsgimtxungNotACK(SDAmc
cao)bochothitbchbit,thitbchstoxungSTOPktthchaylplimt
xungSTARTbtuqutrnhmi.

1.4.

nhdngachthitb

Mithitbngoivithamgiavobusi2cucmtachduynht,nhmphnbit
giaccthitbvinhau.diachl7bit,iucnghaltrnmtbusI2Cta
cthphnbittia128thitb.Khithitbchmungiaotipvingoivinotrnbus
I2C,nsgi7bitachcathitbrabusngaysauxungSTART.Byteutinc
gisbaogm7bitachvmtbtth8iukhinhngtruyn.

Hnh1.8.Cutrcbytedliuutin

Mimtthitbngoiviscmtachringdonhsnxutranquynh.ach
cthlcnhhaythayi.Ringbitiukhinhngsquynhchiutruynd
liu.Nubitnybng0cnghalbytedliutiptheosausctruyntchn
t,cnngclinubng1thccbytetheosaubyteutinsldliutcontgi
nconch.Victhitlpgitrchobitnydoconchthihnh,contstytheogitr
mcsphnhitngngnconch.

1.5.

TruyndliutrnbusI2C,chMasterSlave

Victruyndliudinragiaconchvcont.Dliutruyncththeo2hng,
tchnthayngcli.Hngtruyncquynhbibitth8
trongbyteu
tinctruyni.

Hnh1.9.Qutrnhtruyndliu

hhttp://lab3i.co

Tc gi: Nguyn Ch Linh

Truyndliutchnt(ghidliu):Thitbchkhimunghidliun
cont,qutrnhthchinl:

ThitbchtoxungSTART

Thitbchgiachcathitbtmncngiaotipcngvibit

=0rabusvixungACKphnhitcont

KhinhncxungACKbonhndinngthitbt,conchbt
ugidliunconttheotngbytemt.Theosaumibytenyu
lmtxungACK.Slngbytetruynlkhnghnch.

Ktthcqutrnhtruyn,conchsaukhitruynbytecuistoxung
STOPbohiuktthc.

Hnh1.10.Ghidliutchnt

Truyndliuttnch(cdliu):Thitbchmuncdliutthitb
t,qutrnhthchinnhsau:

Khibusri,thitbchtoxungSTART,bohiubtugiaotip

Thitbchgiachcathitbtcngiaotipcngvibit
ixungACKtphathitbt

=1v

SauxungACKdutin,thitbtsgitngbyterabus,thitbchs
nhndliuvtrvxungACK.Slngbytekhnghnch

Khimunktthcqutrnhgiaotip,thitbchgixungNotACKv
toxungSTOPktthc.

Hnh1.11.cdliutthitbt

Qutrnhkthpghivcdliu:giahaixungSTARTvSTOP,thitbchc
ththchinvicchayghinhiuln,vimthaynhiuthitb.thchin
vic,saumtqutrnhghihayc,thitbchlplimtxungSTARTvli
giliachcathitbtvbtumtqutrnhmi.

hhttp://lab3i.co

Tc gi: Nguyn Ch Linh

Hnh1.12.Qutrnhphihpc/ghidliu

ChgiaotipMasterSlavelchcbntrongmtbusI2C,tonbbusc
qunlbimtmasterduynht.Trongchnyskhngxyratnhtrngxungt
bushaymtngbxungclockvchcmtmasterduynhtcthtoxungclock.

1.6.

ChMultiMaster

TrnbusI2Ccthcnhiuhnmtmasteriukhinbus.KhibusI2Cshot
ngchMultiMaster.

Vnnyscbnthosau.

2.

ModuleI2CtrongViiukhinPIC

Vinhngtinchemli,khigiaotipI2Cctchhpcngtrongkhnhiu
loiViiukhinkhcnhau.TrongccloiViiukhinPICdngMidrangephbin
tiVitNam,cht16F88michtrphncngI2C,cnccloi16F84,16F628th
khngc.VinhngloiViiukhinkhngchtrphncnggiaotipI2C,s
dngtacthdngphnmmlptrnh,khitasvitmtchngtrinhdiukhin2
chnbtkcaViiukhinnthchingiaotipI2C(cchmSTART,STOP,
WRITE,READ).TrongbivitnytacpnvicsdnggiaotipI2Ccaccloi
PICctchhpkhiI2Csntrongn,mcthlViiukhinPIC16F877A.

2.1.

cimphncng

HnhdiychracutrcphncngcakhiiukhinGiaotipnitipngb
(MSSP)hotngchI2C.KhiI2Ccdychcnng,hotngc2chl
MASTER(ch)vSLAVE(t),cngtxyrakhiciukinSTARThaySTOPxyra,
nhmnhrngI2Ccdihaykhnng(chcnngMultimaster).Chachc
thl7bithay10bit.

KhiI2Cc6thanhghiiukhinhotng,l:

SSPCON:Thanhghiiukhin

SSPCON2:Thanhghiiukhinth2

SSPSTAT:Thanhghitrngthi

hhttp://lab3i.co

Tc gi: Nguyn Ch Linh

SSPBUF:Thanhghibmtruynnhn

SSPSR:Thanhghidch

SSPADD:Thanhghiach

CcthanhghiSSPCON,SSPBUF,SSPADDvSSPSON2cthtruycpc/ghic.
ThanhghiSSPSRkhngthtruycptrctip,lthanhghidichdliurahayvo.Cc
thanhghiSSPCON,SSPCON2vSSPSTATcnhachbit,mibitcchcnng
ring.nghacatngthanhghivcamibittrongtngthanhghiccpk
trongtiliuDatasheetcaPICvtrongtiliuTUT04.02.PVN.MAFDcabnMnh,ti
khngcpthmy.TrongtiliunaygtistptrungvovicsdngkhiI2C
caPICccchMastervSlavetrongphnmmbindchCchoPIClCCS

Hnh2.1.CutrckhiI2CtrongPIC

2.2.

CchthcsdngModuleI2CtrongCCS

TrongviclptrnhchoPICsdnggiaotipI2Ccantrongccngdng,ngi
lptrnhcththchinmtcchddngvitrnhdchCCS.Niddngylchv
mtcphplnh,takhngcnsdngnhiuculnhkhnhnhtronglptrnhASM.

hhttp://lab3i.co

Tc gi: Nguyn Ch Linh

Vickhito,chnchhotngvthchingiaotipcaI2Cccchmdng
sncaCCSthchin.CchmlitkdiylcaphinbnCCS3.242,l:

i2c_isr_state():ThngbotrngthigiaotipI2C

i2c_start():ToiukinSTART

i2c_stop():ToiukinSTOP

i2c_read():cgitrtthitbI2C,trvgitr8bit

i2c_write():Ghigitr8bitnthitbI2C

sdngkhiI2Ctasdngkhaibosau:

#usei2c(ch_,tc,sda=PIN_C4,scl=PIN_C3)

Ch:MasterhaySlave

Tc:Slow(100KHz)hayFast(400KHz)

SDAvSCLlccchni2ctngngcaPIC

Saukhaibotrn,tacthsdngcchmnutrnthchin,xlccgiaotip
i2cviccthitbngoivikhc.

Cntipna

3.

Ktlun