際際滷

際際滷Share a Scribd company logo
Ng動畛ib叩oc叩o: Nguy畛nCh鱈Linh Tili畛u: TUT02.01
Ngy: 9/8/2006 Trang: 1/10
Tutorialno02.01

G畛i畉n: www.picvietnam.com
N畛idung: Chu畉ngiaoti畉pI2CI2CtrongPIC
 MICROSOFTWORD

T坦mt畉t:
Gi畛ithi畛uchungv畛I2C
 畉ci畛mchungv畛I2C:giaoth畛c,畛ach畛
 C叩cch畉紳畛ho畉t畛ng:masterslave,multimaster
ModuleI2CtrongVii畛ukhi畛nPIC
 C畉utr炭cph畉nc畛ng
 Ch畉紳畛ho畉t畛ng:Master,Slave,Multimaster

1. Gi畛ithi畛uchungv畛I2C
Ngynaytrongc叩ch畛th畛ngi畛nt畛hi畛n畉i,r畉tnhi畛uICshaythi畉tb畛ngo畉ivic畉n
ph畉igiaoti畉pv畛ic叩cICshaythi畉tb畛kh叩cgiaoti畉pv畛ith畉紳gi畛ib棚nngoi.V坦im畛cti棚u
畉t動畛chi畛uqu畉Bchoph畉nc畛ngt畛tnh畉tv畛im畉chi畛n董ngi畉n,Phillips達ph叩ttri畛n
m畛tchu畉ngiaoti畉pn畛iti畉p2d但y動畛cg畛ilI2C.I2Clt棚nvi畉tt畉tc畛ac畛mt畛Inter
IntergratedCircuitBusgiaoti畉pgi畛ac叩cICv畛inhau.
L畛chs畛I2CTh棚mvo但y(Ph畉nnys畉渋th棚msau)
I2Cm畉cd湛動畛cph叩ttri畛nb畛iPhilips,nh動ngn坦達動畛cr畉tnhi畛unhs畉nxu畉tIC
tr棚nth畉紳gi畛is畛d畛ng.I2Ctr畛thnhm畛tchu畉nc担ngnghi畛pchoc叩cgiaoti畉pi畛ukhi畛n,c坦
th畛k畛ra但ym畛tvit棚ntu畛ingoiPhilipsnh動:TexasIntrument(TI),MaximDallas,
analogDevice,NationalSemiconductorβBusI2C動畛cs畛d畛nglmbusgiaoti畉pngo畉i
vichor畉tnhi畛ulo畉iICkh叩cnhaunh動c叩clo畉iVii畛ukhi畛n8051,PIC,AVR,ARM,ch鱈p
nh畛nh動RAMt挑nh(StaticRam),EEPROM,b畛chuy畛n畛it動董ngt畛餌s畛(ADC),s畛t動董ngt畛ヂ
(DAC),ICi畛ukhi畛nLCD,LEDβ


H狸nh1.1.BUSI2Cvc叩cthi畉tb畛ngo畉ivi
Ng動畛ib叩oc叩o: Nguy畛nCh鱈Linh Tili畛u: TUT02.01
Ngy: 9/8/2006 Trang: 2/10

1.1. 畉ci畛mgiaoti畉pI2C
M畛tgiaoti畉pI2Cg畛mc坦2d但y:SerialData(SDA)vSerialClock(SCL).SDAl動畛ng
truy畛nd畛li畛u2h動畛ng,c嘆nSCLl動畛ngtruy畛nxung畛ngh畛vch畛theom畛th動畛ng.
Nh動h狸nhv畉渋tr棚n,khim畛tthi畉tb畛ngo畉ivik畉tn畛ivo動畛ngI2Cth狸ch但nSDAc畛an坦s畉渋
n畛iv畛id但ySDAc畛abus,ch但nSCLs畉渋n畛iv畛id但ySCL.

H狸nh1.2.K畉tn畛ithi畉tb畛vobusI2C畛ch畉紳畛chu畉n(Standardmode)
vch畉紳畛nhanh(Fastmode)
M畛id但ySDAhaySCL畛u動畛cn畛iv畛ii畛n叩pd動董ngc畛angu畛nc畉pth担ngquam畛t
i畛ntr畛k辿ol棚n(pullupresistor).S畛餌c畉nthi畉tc畛ac叩ci畛ntr畛k辿onylv狸ch但ngiaoti畉p
I2Cc畛ac叩cthi畉tb畛ngo畉ivith動畛ngld畉ngc畛cm叩ngh畛(opendrainoropencollector).
Gi叩tr畛c畛ac叩ci畛ntr畛nykh叩cnhaut湛yvot畛ngthi畉tb畛vchu畉ngiaoti畉p,th動畛ngdao
畛ngtrongkho畉ng1Kβ畉n4.7K.
Tr畛l畉iv畛ih狸nh1.1,tath畉yc坦r畉tnhi畛uthi畉tb畛(ICs)c湛ng動畛ck畉tn畛ivom畛tbus
I2C,tuynhi棚ns畉渋kh担ngx畉yrachuy畛nnh畉ml畉ngi畛ac叩cthi畉tb畛,b畛im畛ithi畉tb畛s畉渋動畛c
nh畉nrab畛im畛t畛ach畛duynh畉tv畛im畛tquanh畛ch畛/t畛t畛nt畉itrongsu畛tth畛igiank畉t
n畛i.M畛ithi畉tb畛c坦th畛ho畉t担ngnh動lthi畉tb畛nh畉nd畛li畛uhayc坦th畛v畛atruy畛nv畛a
nh畉n.Ho畉t畛ngtruy畛nhaynh畉nc嘆nt湛ythu畛cvovi畛cthi畉tb畛坦lch畛(master)hayt畛
(slave).
M畛tthi畉tb畛haym畛tICkhik畉tn畛iv畛ibusI2C,ngoim畛t畛ach畛(duynh畉t)畛ph但n
bi畛t,n坦c嘆n動畛cc畉uh狸nhlthi畉tb畛ch畛(master)hayt畛(slave).T畉isaol畉ic坦s畛餌ph但nbi畛t
ny?坦lv狸tr棚nm畛tbusI2Cth狸quy畛ni畛ukhi畛nthu畛cv畛thi畉tb畛ch畛(master).Thi畉tb畛
ch畛n畉mvaitr嘆t畉oxung畛ngh畛chotonh畛th畛ng,khigi畛ahaithi畉tb畛ch畛/t畛giaoti畉p
th狸thi畉tb畛ch畛c坦nhi畛mv畛ヂt畉oxung畛ngh畛vqu畉nl箪畛ach畛c畛athi畉tb畛t畛trongsu畛t
qu叩tr狸nhgiaoti畉p.Thi畉tb畛ch畛gi畛vaitr嘆ch畛畛ng,c嘆nthi畉tb畛t畛gi畛vaitr嘆b畛畛ng
trongvi棚cgiaoti畉p.
Ng動畛ib叩oc叩o: Nguy畛nCh鱈Linh Tili畛u: TUT02.01
Ngy: 9/8/2006 Trang: 3/10
 
H狸nh1.3.Truy畛nnh畉nd畛li畛ugi畛ach畛/t畛
Nh狸nh狸nhtr棚ntath畉yxung畛ngh畛ch畛c坦m畛th動畛ngt畛ch畛畉nt畛,c嘆nlu畛ngd畛li畛u
c坦th畛itheohaih動畛ng,t畛ch畛畉nt畛hayng動畛cl畉it畛畉nch畛.
V畛d畛li畛utruy畛ntr棚nbusI2C,m畛tbusI2Cchu畉ntruy畛n8bitd畛li畛uc坦h動畛ngtr棚n
動畛ngtruy畛nv畛it畛c畛l100Kbits/sCh畉紳畛chu畉n(Standardmode).T畛c畛truy畛nc坦
th畛l棚nt畛i400Kbits/sCh畉紳畛nhanh(Fastmode)vcaonh畉tl3,4Mbits/sCh畉紳畛cao
t畛c(Highspeedmode).
M畛tbusI2Cc坦th畛ho畉t畛ng畛nhi畛uch畉紳畛kh叩cnhau:
- M畛tch畛m畛tt畛(onemasteroneslave)
- M畛tch畛nhi畛ut畛(onemastermultislave)
- Nhi畛uch畛nhi畛ut畛(Multimastermultislave)
D湛畛ch畉紳畛no,m畛tgiaoti畉pI2C畛ud畛avoquanh畛ch畛/t畛.Gi畉Bthi畉tm畛tthi畉tb畛A
mu畛ng畛id畛li畛u畉nthi畉tb畛B,qu叩tr狸nh動畛cth畛chi畛nnh動sau:
 Thi畉tb畛A(Ch畛)x叩c畛nh炭ng畛ach畛c畛athi畉tb畛B(t畛),c畛ngv畛ivi畛cx叩c畛nh
畛ach畛,thi畉tb畛As畉渋quy畉t畛nhvi畛c畛chayghivothi畉tb畛t畛
 Thi畉tb畛Ag畛id畛li畛ut畛ithi畉tb畛B
 Thi畉tb畛Ak畉tth炭cqu叩tr狸nhtruy畛nd畛li畛u
KhiAmu畛nnh畉nd畛li畛ut畛B,qu叩tr狸nhdi畛nranh動tr棚n,ch畛kh叩clAs畉渋nh畉nd畛li畛u
t畛B.Tronggiaoti畉pny,Alch畛c嘆nBv畉nlt畛.Chiti畉tvi畛cthi畉tl畉pm畛tgiaoti畉pgi畛a
haithi畉tb畛s畉渋動畛cm担t畉Bchiti畉ttrongc叩cm畛cd動畛i但y.

1.2. STARTandSTOPconditions
STARTvSTOPlnh畛ngi畛uki畛nb畉tbu畛cph畉ic坦khim畛tthi畉tb畛ch畛mu畛nthi畉t
l畉pgiaoti畉pv畛im畛tthi畉tb畛no坦trongm畉ngI2C.STARTli畛uki畛nkh畛i畉u,b叩o
hi畛ub畉t畉uc畛agiaoti畉p,c嘆nSTOPb叩ohi畛uk畉tth炭cm畛tgiaoti畉p.H狸nhd動畛i但ym担t畉B
i畛uki畛nSTARTvSTOP.
Ban畉ukhich動ath畛chi畛nqu叩tr狸nhgiaoti畉p,c畉Bhai動畛ngSDAvSCL畛u畛m畛c
cao(SDA=SCL=HIGH).L炭cnybusI2C動畛ccoild畛i(busfree),s畉nsngchom畛t
giaoti畉p.Haii畛uki畛nSTARTvSTOPlkh担ngth畛thi畉utrongvi畛cgiaoti畉pgi畛ac叩c
thi畉tb畛I2Cv畛inhau
Ng動畛ib叩oc叩o: Nguy畛nCh鱈Linh Tili畛u: TUT02.01
Ngy: 9/8/2006 Trang: 4/10

H狸nh1.4.i畛uki畛nSTARTvSTOPc畛abusI2C
i畛uki畛nSTART:m畛ts畛餌chuy畛n畛itr畉ngth叩it畛caoxu畛ngth畉ptr棚n動畛ngSDA
trongkhi動畛ngSCLang畛m畛ccao(cao=1;th畉p=0)b叩ohi畛um畛ti畛uki畛nSTART
i畛uki畛nSTOP:M畛ts畛餌chuy畛n畛itr畉ngth叩it畛m畛cth畉pl棚ncaotr棚n動畛ngSDA
trongkhi動畛ngSCLang畛m畛ccao.
C畉Bhaii畛uki畛nSTARTvSTOP畛u動畛ct畉orab畛ithi畉tb畛ch畛.Saut鱈nhi畛uSTART,
busI2Ccoinh動angtrongtrangth叩ilmvi畛c(busy).BusI2Cs畉渋r畛i,s畉nsngchom畛t
giaoti畉pm畛isaut鱈nhi畛uSTOPt畛ph鱈athi畉tb畛ch畛.
Saukhic坦m畛ti畛uki畛nSTART,trongquatr狸nhgiaoti畉p,khic坦m畛tt鱈nhi畛uSTART
動畛cl畉pl畉ithayv狸m畛tt鱈nhi畛uSTOPth狸busI2Cv畉nti畉pt畛ctrongtr畉ngth叩ib畉n.T鱈n
hi畛uSTARTvl畉pl畉iSTART畛uc坦ch畛cnnggi畛ngnhaulkh畛it畉om畛tgiaoti畉p.

1.3. 畛nhd畉ngd畛li畛utruy畛n
D畛li畛u動畛ctruy畛ntr棚nbusI2Ctheot畛ngbit,bitd畛li畛u動畛ctruy畛nit畉im畛is動畛n
d動董ngc畛axung畛ngh畛tr棚nd但ySCL,qu叩tr狸nhthay畛ibitd畛li畛ux畉yrakhiSCLang
畛m畛cth畉p.

H狸nh1.5.Qu叩tr狸nhtruy畛n1bitd畛li畛u
M畛ibyted畛li畛u動畛ctruy畛nc坦畛dil8bits.S畛l動畛ngbytec坦th畛truy畛ntrongm畛t
l畉nlkh担ngh畉nch畉.M畛ibyte動畛ctruy畛nitheosaulm畛tbitACK畛b叩ohi畛u達
nh畉nd畛li畛u.Bitc坦tr畛ngs畛caonh畉t(MSB)s畉渋動畛ctruy畛ni畉uti棚n,c叩cb鱈ts畉渋動畛c
truy畛nil畉nl動畛t.Sau8xungclocktr棚nd但ySCL,8bitd畛li畛u達動畛ctruy畛ni.L炭cny
thi畉tb畛nh畉n,saukhi達nh畉n畛8b鱈td畛li畛us畉渋k辿oSDAxu畛ngm畛cth畉pt畉om畛txung
ACK畛ngv畛ixungclockth畛9tr棚nd但ySDA畛b叩ohi畛u達nh畉n畛8bit.Thi畉tb畛truy畛n
khinh畉n動畛cbitACKs畉渋ti畉pt畛cth畛chi畛nqu叩tr狸nhtruy畛nho畉ck畉tth炭c.
Ng動畛ib叩oc叩o: Nguy畛nCh鱈Linh Tili畛u: TUT02.01
Ngy: 9/8/2006 Trang: 5/10

H狸nh1.6.D畛li畛utruy畛ntr棚nbusI2C

H狸nh1.7.BitACKtr棚nbusI2C
Truy畛nNh畉n





 








H狸nh1.8.L動u畛thu畉tto叩nqu叩tr狸nhtruy棚ngnh畉nd畛li畛u
Buffer=databit
BufferFull?
ReadBuffer
SendACK
NO
YES
SDA=data_bit
Clock=8?
ACK?
Next_byte
or STOP
STOP
NO
NO
YES
YES
Ki畛mtraxem
達truy畛n畛
8bitch動a
Ng動畛ib叩oc叩o: Nguy畛nCh鱈Linh Tili畛u: TUT02.01
Ngy: 9/8/2006 Trang: 6/10
M畛tbytetruy畛nic坦k竪mtheobitACKli畛uki棚nb畉tbu畛c,nh畉m畉mb畉ochoqu叩
tr狸nhtruy畛nnh畉n動畛cdi畛nrach鱈nhx叩c.Khikh担ngnh畉n動畛c炭ng畛ach畛haykhi
mu畛nk畉tth炭cqu叩tr狸nhgiaoti畉p,thi畉tb畛nh畉ns畉渋g畛im畛txungNotACK(SDA畛m畛c
cao)畛b叩ochothi畉tb畛ch畛bi畉t,thi畉tb畛ch畛s畉渋t畉oxungSTOP畛k畉tth炭chayl畉pl畉im畛t
xungSTART畛b畉t畉uqu叩tr狸nhm畛i.

1.4. 畛nhd畉ng畛ach畛thi畉tb畛
M畛ithi畉tb畛ngo畉ivithamgiavobusi2c畛uc坦m畛t畛ach畛duynh畉t,nh畉mph但nbi畛t
gi畛ac叩cthi畉tb畛v畛inhau.畛di畛ach畛l7bit,i畛u坦c坦ngh挑altr棚nm畛tbusI2Cta
c坦th畛ph但nbi畛tt畛ia128thi畉tb畛.Khithi畉tb畛ch畛mu畛ngiaoti畉pv畛ingo畉ivinotr棚nbus
I2C,n坦s畉渋g畛i7bit畛ach畛c畛athi畉tb畛坦rabusngaysauxungSTART.Byte畉uti棚n動畛c
g畛is畉渋baog畛m7bit畛ach畛vm畛tb鱈tth畛8i畛ukhi畛nh動畛ngtruy畛n.

H狸nh1.8.C畉utr炭cbyted畛li畛u畉uti棚n
M畛im畛tthi畉tb畛ngo畉ivis畉渋c坦m畛t畛ach畛ri棚ngdonhs畉nxu畉tran坦quy畛nh.畛ach畛
坦c坦th畛lc畛畛nhhaythay畛i.Ri棚ngbiti畛ukhi畛nh動畛ngs畉渋quy畛nhchi畛utruy畛nd畛
li畛u.N畉ubitnyb畉ng0c坦ngh挑albyted畛li畛uti畉ptheosaus畉渋動畛ctruy畛nt畛ch畛畉n
t畛,c嘆nng動畛cl畉in畉ub畉ng1th狸c叩cbytetheosaubyte畉uti棚ns畉渋ld畛li畛ut畛cont畛g畛i
畉nconch畛.Vi畛cthi畉tl畉pgi叩tr畛chobitnydoconch畛thihnh,cont畛s畉渋t湛ytheogi叩tr畛
坦mc坦s畛餌ph畉nh畛it動董ng畛ng畉nconch畛.

1.5. Truy畛nd畛li畛utr棚nbusI2C,ch畉紳畛MasterSlave
Vi畛ctruy畛nd畛li畛udi畛nragi畛aconch畛vcont畛.D畛li畛utruy畛nc坦th畛theo2h動畛ng,
t畛ch畛畉nt畛hayng動畛cl畉i.H動畛ngtruy畛n動畛cquy畛nhb畛ibitth畛8 trongbyte畉u
ti棚n動畛ctruy畛ni.

H狸nh1.9.Qu叩tr狸nhtruy畛nd畛li畛u
Ng動畛ib叩oc叩o: Nguy畛nCh鱈Linh Tili畛u: TUT02.01
Ngy: 9/8/2006 Trang: 7/10
 Truy畛nd畛li畛ut畛ch畛畉nt畛(ghid畛li畛u):Thi畉tb畛ch畛khimu畛nghid畛li畛u畉n
cont畛,qu叩tr狸nhth畛chi畛nl:
 Thi畉tb畛ch畛t畉oxungSTART
 Thi畉tb畛ch畛g畛i畛ach畛c畛athi畉tb畛t畛mn坦c畉ngiaoti畉pc湛ngv畛ibit
=0rabusv畛ixungACKph畉nh畛it畛cont畛
 Khinh畉n動畛cxungACKb叩o達nh畉ndi畛n炭ngthi畉tb畛t畛,conch畛b畉t
畉ug畛id畛li畛u畉ncont畛theot畛ngbytem畛t.Theosaum畛ibyteny畛u
lm畛txungACK.S畛l動畛ngbytetruy畛nlkh担ngh畉nch畉.
 K畉tth炭cqu叩tr狸nhtruy畛n,conch畛saukhitruy畛nbytecu畛is畉渋t畉oxung
STOPb叩ohi畛uk畉tth炭c.

H狸nh1.10.Ghid畛li畛ut畛ch畛畉nt畛
 Truy畛nd畛li畛ut畛t畛畉nch畛(畛cd畛li畛u):Thi畉tb畛ch畛mu畛n畛cd畛li畛ut畛thi畉tb畛
t畛,qu叩tr狸nhth畛chi畛nnh動sau:
 Khibusr畛i,thi畉tb畛ch畛t畉oxungSTART,b叩ohi畛ub畉t畉ugiaoti畉p
 Thi畉tb畛ch畛g畛i畛ach畛c畛athi畉tb畛t畛c畉ngiaoti畉pc湛ngv畛ibit =1v
畛ixungACKt畛ph鱈athi畉tb畛t畛
 SauxungACKd畉uti棚n,thi畉tb畛t畛s畉渋g畛it畛ngbyterabus,thi畉tb畛ch畛s畉渋
nh畉nd畛li畛uvtr畉Bv畛xungACK.S畛l動畛ngbytekh担ngh畉nch畉紳
 Khimu畛nk畉tth炭cqu叩tr狸nhgiaoti畉p,thi畉tb畛ch畛g畛ixungNotACKv
t畉oxungSTOP畛k畉tth炭c.

H狸nh1.11.畛cd畛li畛ut畛thi畉tb畛t畛
 Qu叩tr狸nhk畉th畛pghiv畛cd畛li畛u:gi畛ahaixungSTARTvSTOP,thi畉tb畛ch畛c坦
th畛th畛chi畛nvi畛c畛chayghinhi畛ul畉n,v畛im畛thaynhi畛uthi畉tb畛.畛th畛chi畛n
vi畛c坦,saum畛tqu叩tr狸nhghihay畛c,thi畉tb畛ch畛l畉pl畉im畛txungSTARTvl畉i
g畛il畉i畛ach畛c畛athi畉tb畛t畛vb畉t畉um畛tqu叩tr狸nhm畛i.
Ng動畛ib叩oc叩o: Nguy畛nCh鱈Linh Tili畛u: TUT02.01
Ngy: 9/8/2006 Trang: 8/10

H狸nh1.12.Qu叩tr狸nhph畛ih畛p畛c/ghid畛li畛u
Ch畉紳畛giaoti畉pMasterSlavelch畉紳畛c董b畉ntrongm畛tbusI2C,tonb畛bus動畛c
qu畉nl箪b畛im畛tmasterduynh畉t.Trongch畉紳畛nys畉渋kh担ngx畉yrat狸nhtr畉ngxung畛t
bushaym畉t畛ngb畛xungclockv狸ch畛c坦m畛tmasterduynh畉tc坦th畛t畉oxungclock.
1.6. Ch畉紳畛MultiMaster
Tr棚nbusI2Cc坦th畛c坦nhi畛uh董nm畛tmasteri畛ukhi畛nbus.Khi坦busI2Cs畉渋ho畉t
畛ng畛ch畉紳畛MultiMaster.
V畉n畛nys畉渋動畛cbnth畉osau.
2. ModuleI2CtrongVii畛ukhi畛nPIC
V畛inh畛ngti畛n鱈cheml畉i,kh畛igiaoti畉pI2C達動畛ct鱈chh畛pc畛ngtrongkh叩nhi畛u
lo畉iVii畛ukhi畛nkh叩cnhau.Trongc叩clo畉iVii畛ukhi畛nPICd嘆ngMidrangeph畛bi畉n
t畉iVi畛tNam,ch畛t畛16F88m畛ic坦h畛tr畛Bph畉nc畛ngI2C,c嘆nc叩clo畉i16F84,16F628th狸
kh担ngc坦.V畛inh畛nglo畉iVii畛ukhi畛nkh担ngc坦h畛tr畛Bph畉nc畛nggiaoti畉pI2C,畛s畛
d畛ngtac坦th畛d湛ngph畉nm畛ml畉ptr狸nh,khi坦tas畉渋vi畉tm畛tch動董ngtrinhdi畛ukhi畛n2
ch但nb畉tk畛蛎c畛aVii畛ukhi畛n畛n坦th畛chi畛ngiaoti畉pI2C(c叩chmSTART,STOP,
WRITE,READ).Trongbivi畉tnyta畛c畉p畉nvi畛cs畛d畛nggiaoti畉pI2Cc畛ac叩clo畉i
PICc坦t鱈chh畛pkh畛iI2Cs畉ntrongn坦,mc畛ヂth畛lVii畛ukhi畛nPIC16F877A.

2.1. 畉ci畛mph畉nc畛ng
H狸nhd動畛i但ych畛rac畉utr炭cph畉nc畛ngc畛akh畛ii畛ukhi畛nGiaoti畉pn畛iti畉p畛ngb畛
(MSSP)ho畉t畛ng畛ch畉紳畛I2C.Kh畛iI2Cc坦d畉y畛ch畛cnng,ho畉t畛ng畛c畉B2ch畉紳畛l
MASTER(ch畛)vSLAVE(t畛),c坦ng畉tx畉yrakhic坦i畛uki畛nSTARThaySTOPx畉yra,
nh畉m畛nhr探動畛ngI2Cc坦d畛ihaykh担n董ng(ch畛cnngMultimaster).Ch畉紳畛畛ach畛c坦
th畛l7bithay10bit.
Kh畛iI2Cc坦6thanhghii畛ukhi畛nho畉t畛ng,坦l:
 SSPCON:Thanhghii畛ukhi畛n
 SSPCON2:Thanhghii畛ukhi畛nth畛2
 SSPSTAT:Thanhghitr畉ngth叩i
Ng動畛ib叩oc叩o: Nguy畛nCh鱈Linh Tili畛u: TUT02.01
Ngy: 9/8/2006 Trang: 9/10
 SSPBUF:Thanhghib畛畛mtruy畛nnh畉n
 SSPSR:Thanhghid畛ch
 SSPADD:Thanhghi畛ach畛
C叩cthanhghiSSPCON,SSPBUF,SSPADDvSSPSON2c坦th畛truyc畉p畛c/ghi動畛c.
ThanhghiSSPSRkh担ngth畛truyc畉ptr畛cti畉p,lthanhghidichd畛li畛urahayvo.C叩c
thanhghiSSPCON,SSPCON2vSSPSTAT動畛c畛nh畛ach畛bit,m畛ibitc坦ch畛cnng
ri棚ng.ngh挑ac畛at畛ngthanhghivc畛am畛ibittrongt畛ngthanhghi達動畛c畛c畉pk畛溝
trongtili畛uDatasheetc畛aPICvtrongtili畛uTUT04.02.PVN.MAFDc畛ab畉nM畉nh,t担i
kh担ng畛c畉pth棚m畛但y.Trongtili畛unaygt担is畉渋t畉ptrungvovi畛cs畛d畛ngkh畛iI2C
c畛aPIC畛c叩cch畉紳畛MastervSlavetrongph畉nm畛mbi棚nd畛chCchoPIClCCS


H狸nh2.1.C畉utr炭ckh畛iI2CtrongPIC

2.2. C叩chth畛cs畛d畛ngModuleI2CtrongCCS
Trongvi畛cl畉ptr狸nhchoPICs畛d畛nggiaoti畉pI2Cc畛an坦trongc叩c畛ngd畛ng,ng動畛i
l畉ptr狸nhc坦th畛th畛chi畛nm畛tc叩chd畛dngv畛itr狸nhd畛chCCS.N坦id畛dng畛但ylch畛v畛
m畉tc炭ph叩pl畛nh,takh担ngc畉ns畛d畛ngnhi畛uc但ul畛nhkh坦nh畛nh動trongl畉ptr狸nhASM.
Ng動畛ib叩oc叩o: Nguy畛nCh鱈Linh Tili畛u: TUT02.01
Ngy: 9/8/2006 Trang: 10/10
Vi畛ckh畛it畉o,ch畛nch畉紳畛ho畉t畛ngvth畛chi畛ngiaoti畉pc畛aI2C達c坦c叩chmd畛ng
s畉nc畛aCCSth畛chi畛n.C叩chmli畛tk棚d動畛i但ylc畛aphi棚nb畉nCCS3.242,坦l:
 i2c_isr_state():Th担ngb叩otr畉ngth叩igiaoti畉pI2C
 i2c_start():T畉oi畛uki畛nSTART
 i2c_stop():T畉oi畛uki畛nSTOP
 i2c_read():畛cgi叩tr畛t畛thi畉tb畛I2C,tr畉Bv畛gi叩tr畛8bit
 i2c_write():Ghigi叩tr畛8bit畉nthi畉tb畛I2C
畛s畛d畛ngkh畛iI2Ctas畛d畛ngkhaib叩osau:
#usei2c(ch畉_畛,t畛c畛,sda=PIN_C4,scl=PIN_C3)
 Ch畉紳畛:MasterhaySlave
 T畛c畛:Slow(100KHz)hayFast(400KHz)
 SDAvSCLlc叩cch但ni2ct動董ng畛ngc畛aPIC
Saukhaib叩otr棚n,tac坦th畛s畛d畛ngc叩chmn棚utr棚n畛th畛chi畛n,x畛l箪c叩cgiaoti畉p
i2cv畛ic叩cthi畉tb畛ngo畉ivikh叩c.
C嘆nti畉pn畛aβ
3. K畉tlu畉n

More Related Content

Giao tiep i2c

  • 1. Ng動畛ib叩oc叩o: Nguy畛nCh鱈Linh Tili畛u: TUT02.01 Ngy: 9/8/2006 Trang: 1/10 Tutorialno02.01 G畛i畉n: www.picvietnam.com N畛idung: Chu畉ngiaoti畉pI2CI2CtrongPIC MICROSOFTWORD T坦mt畉t: Gi畛ithi畛uchungv畛I2C 畉ci畛mchungv畛I2C:giaoth畛c,畛ach畛 C叩cch畉紳畛ho畉t畛ng:masterslave,multimaster ModuleI2CtrongVii畛ukhi畛nPIC C畉utr炭cph畉nc畛ng Ch畉紳畛ho畉t畛ng:Master,Slave,Multimaster 1. Gi畛ithi畛uchungv畛I2C Ngynaytrongc叩ch畛th畛ngi畛nt畛hi畛n畉i,r畉tnhi畛uICshaythi畉tb畛ngo畉ivic畉n ph畉igiaoti畉pv畛ic叩cICshaythi畉tb畛kh叩cgiaoti畉pv畛ith畉紳gi畛ib棚nngoi.V坦im畛cti棚u 畉t動畛chi畛uqu畉Bchoph畉nc畛ngt畛tnh畉tv畛im畉chi畛n董ngi畉n,Phillips達ph叩ttri畛n m畛tchu畉ngiaoti畉pn畛iti畉p2d但y動畛cg畛ilI2C.I2Clt棚nvi畉tt畉tc畛ac畛mt畛Inter IntergratedCircuitBusgiaoti畉pgi畛ac叩cICv畛inhau. L畛chs畛I2CTh棚mvo但y(Ph畉nnys畉渋th棚msau) I2Cm畉cd湛動畛cph叩ttri畛nb畛iPhilips,nh動ngn坦達動畛cr畉tnhi畛unhs畉nxu畉tIC tr棚nth畉紳gi畛is畛d畛ng.I2Ctr畛thnhm畛tchu畉nc担ngnghi畛pchoc叩cgiaoti畉pi畛ukhi畛n,c坦 th畛k畛ra但ym畛tvit棚ntu畛ingoiPhilipsnh動:TexasIntrument(TI),MaximDallas, analogDevice,NationalSemiconductorβBusI2C動畛cs畛d畛nglmbusgiaoti畉pngo畉i vichor畉tnhi畛ulo畉iICkh叩cnhaunh動c叩clo畉iVii畛ukhi畛n8051,PIC,AVR,ARM,ch鱈p nh畛nh動RAMt挑nh(StaticRam),EEPROM,b畛chuy畛n畛it動董ngt畛餌s畛(ADC),s畛t動董ngt畛ヂ (DAC),ICi畛ukhi畛nLCD,LEDβ H狸nh1.1.BUSI2Cvc叩cthi畉tb畛ngo畉ivi
  • 2. Ng動畛ib叩oc叩o: Nguy畛nCh鱈Linh Tili畛u: TUT02.01 Ngy: 9/8/2006 Trang: 2/10 1.1. 畉ci畛mgiaoti畉pI2C M畛tgiaoti畉pI2Cg畛mc坦2d但y:SerialData(SDA)vSerialClock(SCL).SDAl動畛ng truy畛nd畛li畛u2h動畛ng,c嘆nSCLl動畛ngtruy畛nxung畛ngh畛vch畛theom畛th動畛ng. Nh動h狸nhv畉渋tr棚n,khim畛tthi畉tb畛ngo畉ivik畉tn畛ivo動畛ngI2Cth狸ch但nSDAc畛an坦s畉渋 n畛iv畛id但ySDAc畛abus,ch但nSCLs畉渋n畛iv畛id但ySCL. H狸nh1.2.K畉tn畛ithi畉tb畛vobusI2C畛ch畉紳畛chu畉n(Standardmode) vch畉紳畛nhanh(Fastmode) M畛id但ySDAhaySCL畛u動畛cn畛iv畛ii畛n叩pd動董ngc畛angu畛nc畉pth担ngquam畛t i畛ntr畛k辿ol棚n(pullupresistor).S畛餌c畉nthi畉tc畛ac叩ci畛ntr畛k辿onylv狸ch但ngiaoti畉p I2Cc畛ac叩cthi畉tb畛ngo畉ivith動畛ngld畉ngc畛cm叩ngh畛(opendrainoropencollector). Gi叩tr畛c畛ac叩ci畛ntr畛nykh叩cnhaut湛yvot畛ngthi畉tb畛vchu畉ngiaoti畉p,th動畛ngdao 畛ngtrongkho畉ng1Kβ畉n4.7K. Tr畛l畉iv畛ih狸nh1.1,tath畉yc坦r畉tnhi畛uthi畉tb畛(ICs)c湛ng動畛ck畉tn畛ivom畛tbus I2C,tuynhi棚ns畉渋kh担ngx畉yrachuy畛nnh畉ml畉ngi畛ac叩cthi畉tb畛,b畛im畛ithi畉tb畛s畉渋動畛c nh畉nrab畛im畛t畛ach畛duynh畉tv畛im畛tquanh畛ch畛/t畛t畛nt畉itrongsu畛tth畛igiank畉t n畛i.M畛ithi畉tb畛c坦th畛ho畉t担ngnh動lthi畉tb畛nh畉nd畛li畛uhayc坦th畛v畛atruy畛nv畛a nh畉n.Ho畉t畛ngtruy畛nhaynh畉nc嘆nt湛ythu畛cvovi畛cthi畉tb畛坦lch畛(master)hayt畛 (slave). M畛tthi畉tb畛haym畛tICkhik畉tn畛iv畛ibusI2C,ngoim畛t畛ach畛(duynh畉t)畛ph但n bi畛t,n坦c嘆n動畛cc畉uh狸nhlthi畉tb畛ch畛(master)hayt畛(slave).T畉isaol畉ic坦s畛餌ph但nbi畛t ny?坦lv狸tr棚nm畛tbusI2Cth狸quy畛ni畛ukhi畛nthu畛cv畛thi畉tb畛ch畛(master).Thi畉tb畛 ch畛n畉mvaitr嘆t畉oxung畛ngh畛chotonh畛th畛ng,khigi畛ahaithi畉tb畛ch畛/t畛giaoti畉p th狸thi畉tb畛ch畛c坦nhi畛mv畛ヂt畉oxung畛ngh畛vqu畉nl箪畛ach畛c畛athi畉tb畛t畛trongsu畛t qu叩tr狸nhgiaoti畉p.Thi畉tb畛ch畛gi畛vaitr嘆ch畛畛ng,c嘆nthi畉tb畛t畛gi畛vaitr嘆b畛畛ng trongvi棚cgiaoti畉p.
  • 3. Ng動畛ib叩oc叩o: Nguy畛nCh鱈Linh Tili畛u: TUT02.01 Ngy: 9/8/2006 Trang: 3/10 H狸nh1.3.Truy畛nnh畉nd畛li畛ugi畛ach畛/t畛 Nh狸nh狸nhtr棚ntath畉yxung畛ngh畛ch畛c坦m畛th動畛ngt畛ch畛畉nt畛,c嘆nlu畛ngd畛li畛u c坦th畛itheohaih動畛ng,t畛ch畛畉nt畛hayng動畛cl畉it畛畉nch畛. V畛d畛li畛utruy畛ntr棚nbusI2C,m畛tbusI2Cchu畉ntruy畛n8bitd畛li畛uc坦h動畛ngtr棚n 動畛ngtruy畛nv畛it畛c畛l100Kbits/sCh畉紳畛chu畉n(Standardmode).T畛c畛truy畛nc坦 th畛l棚nt畛i400Kbits/sCh畉紳畛nhanh(Fastmode)vcaonh畉tl3,4Mbits/sCh畉紳畛cao t畛c(Highspeedmode). M畛tbusI2Cc坦th畛ho畉t畛ng畛nhi畛uch畉紳畛kh叩cnhau: - M畛tch畛m畛tt畛(onemasteroneslave) - M畛tch畛nhi畛ut畛(onemastermultislave) - Nhi畛uch畛nhi畛ut畛(Multimastermultislave) D湛畛ch畉紳畛no,m畛tgiaoti畉pI2C畛ud畛avoquanh畛ch畛/t畛.Gi畉Bthi畉tm畛tthi畉tb畛A mu畛ng畛id畛li畛u畉nthi畉tb畛B,qu叩tr狸nh動畛cth畛chi畛nnh動sau: Thi畉tb畛A(Ch畛)x叩c畛nh炭ng畛ach畛c畛athi畉tb畛B(t畛),c畛ngv畛ivi畛cx叩c畛nh 畛ach畛,thi畉tb畛As畉渋quy畉t畛nhvi畛c畛chayghivothi畉tb畛t畛 Thi畉tb畛Ag畛id畛li畛ut畛ithi畉tb畛B Thi畉tb畛Ak畉tth炭cqu叩tr狸nhtruy畛nd畛li畛u KhiAmu畛nnh畉nd畛li畛ut畛B,qu叩tr狸nhdi畛nranh動tr棚n,ch畛kh叩clAs畉渋nh畉nd畛li畛u t畛B.Tronggiaoti畉pny,Alch畛c嘆nBv畉nlt畛.Chiti畉tvi畛cthi畉tl畉pm畛tgiaoti畉pgi畛a haithi畉tb畛s畉渋動畛cm担t畉Bchiti畉ttrongc叩cm畛cd動畛i但y. 1.2. STARTandSTOPconditions STARTvSTOPlnh畛ngi畛uki畛nb畉tbu畛cph畉ic坦khim畛tthi畉tb畛ch畛mu畛nthi畉t l畉pgiaoti畉pv畛im畛tthi畉tb畛no坦trongm畉ngI2C.STARTli畛uki畛nkh畛i畉u,b叩o hi畛ub畉t畉uc畛agiaoti畉p,c嘆nSTOPb叩ohi畛uk畉tth炭cm畛tgiaoti畉p.H狸nhd動畛i但ym担t畉B i畛uki畛nSTARTvSTOP. Ban畉ukhich動ath畛chi畛nqu叩tr狸nhgiaoti畉p,c畉Bhai動畛ngSDAvSCL畛u畛m畛c cao(SDA=SCL=HIGH).L炭cnybusI2C動畛ccoild畛i(busfree),s畉nsngchom畛t giaoti畉p.Haii畛uki畛nSTARTvSTOPlkh担ngth畛thi畉utrongvi畛cgiaoti畉pgi畛ac叩c thi畉tb畛I2Cv畛inhau
  • 4. Ng動畛ib叩oc叩o: Nguy畛nCh鱈Linh Tili畛u: TUT02.01 Ngy: 9/8/2006 Trang: 4/10 H狸nh1.4.i畛uki畛nSTARTvSTOPc畛abusI2C i畛uki畛nSTART:m畛ts畛餌chuy畛n畛itr畉ngth叩it畛caoxu畛ngth畉ptr棚n動畛ngSDA trongkhi動畛ngSCLang畛m畛ccao(cao=1;th畉p=0)b叩ohi畛um畛ti畛uki畛nSTART i畛uki畛nSTOP:M畛ts畛餌chuy畛n畛itr畉ngth叩it畛m畛cth畉pl棚ncaotr棚n動畛ngSDA trongkhi動畛ngSCLang畛m畛ccao. C畉Bhaii畛uki畛nSTARTvSTOP畛u動畛ct畉orab畛ithi畉tb畛ch畛.Saut鱈nhi畛uSTART, busI2Ccoinh動angtrongtrangth叩ilmvi畛c(busy).BusI2Cs畉渋r畛i,s畉nsngchom畛t giaoti畉pm畛isaut鱈nhi畛uSTOPt畛ph鱈athi畉tb畛ch畛. Saukhic坦m畛ti畛uki畛nSTART,trongquatr狸nhgiaoti畉p,khic坦m畛tt鱈nhi畛uSTART 動畛cl畉pl畉ithayv狸m畛tt鱈nhi畛uSTOPth狸busI2Cv畉nti畉pt畛ctrongtr畉ngth叩ib畉n.T鱈n hi畛uSTARTvl畉pl畉iSTART畛uc坦ch畛cnnggi畛ngnhaulkh畛it畉om畛tgiaoti畉p. 1.3. 畛nhd畉ngd畛li畛utruy畛n D畛li畛u動畛ctruy畛ntr棚nbusI2Ctheot畛ngbit,bitd畛li畛u動畛ctruy畛nit畉im畛is動畛n d動董ngc畛axung畛ngh畛tr棚nd但ySCL,qu叩tr狸nhthay畛ibitd畛li畛ux畉yrakhiSCLang 畛m畛cth畉p. H狸nh1.5.Qu叩tr狸nhtruy畛n1bitd畛li畛u M畛ibyted畛li畛u動畛ctruy畛nc坦畛dil8bits.S畛l動畛ngbytec坦th畛truy畛ntrongm畛t l畉nlkh担ngh畉nch畉.M畛ibyte動畛ctruy畛nitheosaulm畛tbitACK畛b叩ohi畛u達 nh畉nd畛li畛u.Bitc坦tr畛ngs畛caonh畉t(MSB)s畉渋動畛ctruy畛ni畉uti棚n,c叩cb鱈ts畉渋動畛c truy畛nil畉nl動畛t.Sau8xungclocktr棚nd但ySCL,8bitd畛li畛u達動畛ctruy畛ni.L炭cny thi畉tb畛nh畉n,saukhi達nh畉n畛8b鱈td畛li畛us畉渋k辿oSDAxu畛ngm畛cth畉pt畉om畛txung ACK畛ngv畛ixungclockth畛9tr棚nd但ySDA畛b叩ohi畛u達nh畉n畛8bit.Thi畉tb畛truy畛n khinh畉n動畛cbitACKs畉渋ti畉pt畛cth畛chi畛nqu叩tr狸nhtruy畛nho畉ck畉tth炭c.
  • 5. Ng動畛ib叩oc叩o: Nguy畛nCh鱈Linh Tili畛u: TUT02.01 Ngy: 9/8/2006 Trang: 5/10 H狸nh1.6.D畛li畛utruy畛ntr棚nbusI2C H狸nh1.7.BitACKtr棚nbusI2C Truy畛nNh畉n H狸nh1.8.L動u畛thu畉tto叩nqu叩tr狸nhtruy棚ngnh畉nd畛li畛u Buffer=databit BufferFull? ReadBuffer SendACK NO YES SDA=data_bit Clock=8? ACK? Next_byte or STOP STOP NO NO YES YES Ki畛mtraxem 達truy畛n畛 8bitch動a
  • 6. Ng動畛ib叩oc叩o: Nguy畛nCh鱈Linh Tili畛u: TUT02.01 Ngy: 9/8/2006 Trang: 6/10 M畛tbytetruy畛nic坦k竪mtheobitACKli畛uki棚nb畉tbu畛c,nh畉m畉mb畉ochoqu叩 tr狸nhtruy畛nnh畉n動畛cdi畛nrach鱈nhx叩c.Khikh担ngnh畉n動畛c炭ng畛ach畛haykhi mu畛nk畉tth炭cqu叩tr狸nhgiaoti畉p,thi畉tb畛nh畉ns畉渋g畛im畛txungNotACK(SDA畛m畛c cao)畛b叩ochothi畉tb畛ch畛bi畉t,thi畉tb畛ch畛s畉渋t畉oxungSTOP畛k畉tth炭chayl畉pl畉im畛t xungSTART畛b畉t畉uqu叩tr狸nhm畛i. 1.4. 畛nhd畉ng畛ach畛thi畉tb畛 M畛ithi畉tb畛ngo畉ivithamgiavobusi2c畛uc坦m畛t畛ach畛duynh畉t,nh畉mph但nbi畛t gi畛ac叩cthi畉tb畛v畛inhau.畛di畛ach畛l7bit,i畛u坦c坦ngh挑altr棚nm畛tbusI2Cta c坦th畛ph但nbi畛tt畛ia128thi畉tb畛.Khithi畉tb畛ch畛mu畛ngiaoti畉pv畛ingo畉ivinotr棚nbus I2C,n坦s畉渋g畛i7bit畛ach畛c畛athi畉tb畛坦rabusngaysauxungSTART.Byte畉uti棚n動畛c g畛is畉渋baog畛m7bit畛ach畛vm畛tb鱈tth畛8i畛ukhi畛nh動畛ngtruy畛n. H狸nh1.8.C畉utr炭cbyted畛li畛u畉uti棚n M畛im畛tthi畉tb畛ngo畉ivis畉渋c坦m畛t畛ach畛ri棚ngdonhs畉nxu畉tran坦quy畛nh.畛ach畛 坦c坦th畛lc畛畛nhhaythay畛i.Ri棚ngbiti畛ukhi畛nh動畛ngs畉渋quy畛nhchi畛utruy畛nd畛 li畛u.N畉ubitnyb畉ng0c坦ngh挑albyted畛li畛uti畉ptheosaus畉渋動畛ctruy畛nt畛ch畛畉n t畛,c嘆nng動畛cl畉in畉ub畉ng1th狸c叩cbytetheosaubyte畉uti棚ns畉渋ld畛li畛ut畛cont畛g畛i 畉nconch畛.Vi畛cthi畉tl畉pgi叩tr畛chobitnydoconch畛thihnh,cont畛s畉渋t湛ytheogi叩tr畛 坦mc坦s畛餌ph畉nh畛it動董ng畛ng畉nconch畛. 1.5. Truy畛nd畛li畛utr棚nbusI2C,ch畉紳畛MasterSlave Vi畛ctruy畛nd畛li畛udi畛nragi畛aconch畛vcont畛.D畛li畛utruy畛nc坦th畛theo2h動畛ng, t畛ch畛畉nt畛hayng動畛cl畉i.H動畛ngtruy畛n動畛cquy畛nhb畛ibitth畛8 trongbyte畉u ti棚n動畛ctruy畛ni. H狸nh1.9.Qu叩tr狸nhtruy畛nd畛li畛u
  • 7. Ng動畛ib叩oc叩o: Nguy畛nCh鱈Linh Tili畛u: TUT02.01 Ngy: 9/8/2006 Trang: 7/10 Truy畛nd畛li畛ut畛ch畛畉nt畛(ghid畛li畛u):Thi畉tb畛ch畛khimu畛nghid畛li畛u畉n cont畛,qu叩tr狸nhth畛chi畛nl: Thi畉tb畛ch畛t畉oxungSTART Thi畉tb畛ch畛g畛i畛ach畛c畛athi畉tb畛t畛mn坦c畉ngiaoti畉pc湛ngv畛ibit =0rabusv畛ixungACKph畉nh畛it畛cont畛 Khinh畉n動畛cxungACKb叩o達nh畉ndi畛n炭ngthi畉tb畛t畛,conch畛b畉t 畉ug畛id畛li畛u畉ncont畛theot畛ngbytem畛t.Theosaum畛ibyteny畛u lm畛txungACK.S畛l動畛ngbytetruy畛nlkh担ngh畉nch畉. K畉tth炭cqu叩tr狸nhtruy畛n,conch畛saukhitruy畛nbytecu畛is畉渋t畉oxung STOPb叩ohi畛uk畉tth炭c. H狸nh1.10.Ghid畛li畛ut畛ch畛畉nt畛 Truy畛nd畛li畛ut畛t畛畉nch畛(畛cd畛li畛u):Thi畉tb畛ch畛mu畛n畛cd畛li畛ut畛thi畉tb畛 t畛,qu叩tr狸nhth畛chi畛nnh動sau: Khibusr畛i,thi畉tb畛ch畛t畉oxungSTART,b叩ohi畛ub畉t畉ugiaoti畉p Thi畉tb畛ch畛g畛i畛ach畛c畛athi畉tb畛t畛c畉ngiaoti畉pc湛ngv畛ibit =1v 畛ixungACKt畛ph鱈athi畉tb畛t畛 SauxungACKd畉uti棚n,thi畉tb畛t畛s畉渋g畛it畛ngbyterabus,thi畉tb畛ch畛s畉渋 nh畉nd畛li畛uvtr畉Bv畛xungACK.S畛l動畛ngbytekh担ngh畉nch畉紳 Khimu畛nk畉tth炭cqu叩tr狸nhgiaoti畉p,thi畉tb畛ch畛g畛ixungNotACKv t畉oxungSTOP畛k畉tth炭c. H狸nh1.11.畛cd畛li畛ut畛thi畉tb畛t畛 Qu叩tr狸nhk畉th畛pghiv畛cd畛li畛u:gi畛ahaixungSTARTvSTOP,thi畉tb畛ch畛c坦 th畛th畛chi畛nvi畛c畛chayghinhi畛ul畉n,v畛im畛thaynhi畛uthi畉tb畛.畛th畛chi畛n vi畛c坦,saum畛tqu叩tr狸nhghihay畛c,thi畉tb畛ch畛l畉pl畉im畛txungSTARTvl畉i g畛il畉i畛ach畛c畛athi畉tb畛t畛vb畉t畉um畛tqu叩tr狸nhm畛i.
  • 8. Ng動畛ib叩oc叩o: Nguy畛nCh鱈Linh Tili畛u: TUT02.01 Ngy: 9/8/2006 Trang: 8/10 H狸nh1.12.Qu叩tr狸nhph畛ih畛p畛c/ghid畛li畛u Ch畉紳畛giaoti畉pMasterSlavelch畉紳畛c董b畉ntrongm畛tbusI2C,tonb畛bus動畛c qu畉nl箪b畛im畛tmasterduynh畉t.Trongch畉紳畛nys畉渋kh担ngx畉yrat狸nhtr畉ngxung畛t bushaym畉t畛ngb畛xungclockv狸ch畛c坦m畛tmasterduynh畉tc坦th畛t畉oxungclock. 1.6. Ch畉紳畛MultiMaster Tr棚nbusI2Cc坦th畛c坦nhi畛uh董nm畛tmasteri畛ukhi畛nbus.Khi坦busI2Cs畉渋ho畉t 畛ng畛ch畉紳畛MultiMaster. V畉n畛nys畉渋動畛cbnth畉osau. 2. ModuleI2CtrongVii畛ukhi畛nPIC V畛inh畛ngti畛n鱈cheml畉i,kh畛igiaoti畉pI2C達動畛ct鱈chh畛pc畛ngtrongkh叩nhi畛u lo畉iVii畛ukhi畛nkh叩cnhau.Trongc叩clo畉iVii畛ukhi畛nPICd嘆ngMidrangeph畛bi畉n t畉iVi畛tNam,ch畛t畛16F88m畛ic坦h畛tr畛Bph畉nc畛ngI2C,c嘆nc叩clo畉i16F84,16F628th狸 kh担ngc坦.V畛inh畛nglo畉iVii畛ukhi畛nkh担ngc坦h畛tr畛Bph畉nc畛nggiaoti畉pI2C,畛s畛 d畛ngtac坦th畛d湛ngph畉nm畛ml畉ptr狸nh,khi坦tas畉渋vi畉tm畛tch動董ngtrinhdi畛ukhi畛n2 ch但nb畉tk畛蛎c畛aVii畛ukhi畛n畛n坦th畛chi畛ngiaoti畉pI2C(c叩chmSTART,STOP, WRITE,READ).Trongbivi畉tnyta畛c畉p畉nvi畛cs畛d畛nggiaoti畉pI2Cc畛ac叩clo畉i PICc坦t鱈chh畛pkh畛iI2Cs畉ntrongn坦,mc畛ヂth畛lVii畛ukhi畛nPIC16F877A. 2.1. 畉ci畛mph畉nc畛ng H狸nhd動畛i但ych畛rac畉utr炭cph畉nc畛ngc畛akh畛ii畛ukhi畛nGiaoti畉pn畛iti畉p畛ngb畛 (MSSP)ho畉t畛ng畛ch畉紳畛I2C.Kh畛iI2Cc坦d畉y畛ch畛cnng,ho畉t畛ng畛c畉B2ch畉紳畛l MASTER(ch畛)vSLAVE(t畛),c坦ng畉tx畉yrakhic坦i畛uki畛nSTARThaySTOPx畉yra, nh畉m畛nhr探動畛ngI2Cc坦d畛ihaykh担n董ng(ch畛cnngMultimaster).Ch畉紳畛畛ach畛c坦 th畛l7bithay10bit. Kh畛iI2Cc坦6thanhghii畛ukhi畛nho畉t畛ng,坦l: SSPCON:Thanhghii畛ukhi畛n SSPCON2:Thanhghii畛ukhi畛nth畛2 SSPSTAT:Thanhghitr畉ngth叩i
  • 9. Ng動畛ib叩oc叩o: Nguy畛nCh鱈Linh Tili畛u: TUT02.01 Ngy: 9/8/2006 Trang: 9/10 SSPBUF:Thanhghib畛畛mtruy畛nnh畉n SSPSR:Thanhghid畛ch SSPADD:Thanhghi畛ach畛 C叩cthanhghiSSPCON,SSPBUF,SSPADDvSSPSON2c坦th畛truyc畉p畛c/ghi動畛c. ThanhghiSSPSRkh担ngth畛truyc畉ptr畛cti畉p,lthanhghidichd畛li畛urahayvo.C叩c thanhghiSSPCON,SSPCON2vSSPSTAT動畛c畛nh畛ach畛bit,m畛ibitc坦ch畛cnng ri棚ng.ngh挑ac畛at畛ngthanhghivc畛am畛ibittrongt畛ngthanhghi達動畛c畛c畉pk畛溝 trongtili畛uDatasheetc畛aPICvtrongtili畛uTUT04.02.PVN.MAFDc畛ab畉nM畉nh,t担i kh担ng畛c畉pth棚m畛但y.Trongtili畛unaygt担is畉渋t畉ptrungvovi畛cs畛d畛ngkh畛iI2C c畛aPIC畛c叩cch畉紳畛MastervSlavetrongph畉nm畛mbi棚nd畛chCchoPIClCCS H狸nh2.1.C畉utr炭ckh畛iI2CtrongPIC 2.2. C叩chth畛cs畛d畛ngModuleI2CtrongCCS Trongvi畛cl畉ptr狸nhchoPICs畛d畛nggiaoti畉pI2Cc畛an坦trongc叩c畛ngd畛ng,ng動畛i l畉ptr狸nhc坦th畛th畛chi畛nm畛tc叩chd畛dngv畛itr狸nhd畛chCCS.N坦id畛dng畛但ylch畛v畛 m畉tc炭ph叩pl畛nh,takh担ngc畉ns畛d畛ngnhi畛uc但ul畛nhkh坦nh畛nh動trongl畉ptr狸nhASM.
  • 10. Ng動畛ib叩oc叩o: Nguy畛nCh鱈Linh Tili畛u: TUT02.01 Ngy: 9/8/2006 Trang: 10/10 Vi畛ckh畛it畉o,ch畛nch畉紳畛ho畉t畛ngvth畛chi畛ngiaoti畉pc畛aI2C達c坦c叩chmd畛ng s畉nc畛aCCSth畛chi畛n.C叩chmli畛tk棚d動畛i但ylc畛aphi棚nb畉nCCS3.242,坦l: i2c_isr_state():Th担ngb叩otr畉ngth叩igiaoti畉pI2C i2c_start():T畉oi畛uki畛nSTART i2c_stop():T畉oi畛uki畛nSTOP i2c_read():畛cgi叩tr畛t畛thi畉tb畛I2C,tr畉Bv畛gi叩tr畛8bit i2c_write():Ghigi叩tr畛8bit畉nthi畉tb畛I2C 畛s畛d畛ngkh畛iI2Ctas畛d畛ngkhaib叩osau: #usei2c(ch畉_畛,t畛c畛,sda=PIN_C4,scl=PIN_C3) Ch畉紳畛:MasterhaySlave T畛c畛:Slow(100KHz)hayFast(400KHz) SDAvSCLlc叩cch但ni2ct動董ng畛ngc畛aPIC Saukhaib叩otr棚n,tac坦th畛s畛d畛ngc叩chmn棚utr棚n畛th畛chi畛n,x畛l箪c叩cgiaoti畉p i2cv畛ic叩cthi畉tb畛ngo畉ivikh叩c. C嘆nti畉pn畛aβ 3. K畉tlu畉n