![PIC單片機(jī)的I2C應(yīng)用程序1_第1頁](http://file4.renrendoc.com/view/fb0efdfd59f37010185109883a257f04/fb0efdfd59f37010185109883a257f041.gif)
![PIC單片機(jī)的I2C應(yīng)用程序1_第2頁](http://file4.renrendoc.com/view/fb0efdfd59f37010185109883a257f04/fb0efdfd59f37010185109883a257f042.gif)
![PIC單片機(jī)的I2C應(yīng)用程序1_第3頁](http://file4.renrendoc.com/view/fb0efdfd59f37010185109883a257f04/fb0efdfd59f37010185109883a257f043.gif)
![PIC單片機(jī)的I2C應(yīng)用程序1_第4頁](http://file4.renrendoc.com/view/fb0efdfd59f37010185109883a257f04/fb0efdfd59f37010185109883a257f044.gif)
![PIC單片機(jī)的I2C應(yīng)用程序1_第5頁](http://file4.renrendoc.com/view/fb0efdfd59f37010185109883a257f04/fb0efdfd59f37010185109883a257f045.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
PIC單片機(jī)的I2C應(yīng)用程序1File:an734.asmFunctionality:;ThiscodeimplementsthebasicfunctionsforanI2CslavedeviceusingtheSSPmodule.AllI2CfunctionsarehandledinanISR.Byteswrittentotheslavearestoredinabuffer.Afteranumberofbyteshavebeenwritten,themasterdevicecanthenreadthebytesbackfromthebuffer.;VariablesandConstantsusedintheprogram:Thestartaddressforthereceivebufferisstoredinthevariable'RXBuffer'.Thelengthofthebufferisdenotedbytheconstantvalue'RX_BUF_LEN'.Thecurrentbufferindexisstoredinthevariable'Index'.;;--------------------------------------------------------------------;ThefollowingfilesshouldbeincludedintheMPLABproject:;an734.asm--Mainsourcecodefile;;16f872.lkr--Linkerscriptfile(changethisfileforthedeviceyouareusing);---------------------------------------------------------------------#include<p16f872.inc>;Changetodevicethatyouareusing.;---------------------------------------------------------------------;ConstantDefinitions;---------------------------------------------------------------------#defineNODE_ADDR0x02;I2CaddressofthisnodeChangethisvaluetoaddressthatyouwishtouse.;---------------------------------------------------------------------;BufferLengthDefinition;---------------------------------------------------------------------#defineRX_BUF_LEN32;Lengthofreceivebuffer;---------------------------------------------------------------------;Variabledeclarations;---------------------------------------------------------------------udataWREGsaveres1STATUSsaveres1FSRsaveres1PCLATHsaveres1Indexres1;IndextoreceivebufferTempres1;RXBufferresRX_BUF_LEN;Holdsrec'dbytesfrommaster;device.;---------------------------------------------------------------------;Vectors;---------------------------------------------------------------------STARTUPcodenopgotoStartup;nop;0x0002nop;0x0003gotoISR;0x0004PROGcode;---------------------------------------------------------------------;Macros;---------------------------------------------------------------------memsetmacroBuf_addr,Value,LengthmovlwLength;ThismacroloadsarangeofdatamemorymovwfTemp;withaspecifiedvalue.ThestartingmovlwBuf_addr;addressandnumberofbytesarealsomovwfFSR;specified.SetNextmovlwValuemovwfINDFincfFSR,FdecfszTemp,FgotoSetNextendmLFSRmacroAddress,Offset;ThismacroloadsthecorrectvaluemovlwAddress;intotheFSRgivenaninitialdatamovwfFSR;memoryaddressandoffsetvalue.movfOffset,WaddwfFSR,Fendm;---------------------------------------------------------------------;MainCode;---------------------------------------------------------------------StartupbcfSTATUS,RP1bsfSTATUS,RP0callSetupMainclrwdt;CleartheWDTgotoMain;Loopforever.;---------------------------------------------------------------------;InterruptCode;---------------------------------------------------------------------ISRmovwfWREGsave;SaveWREGmovfSTATUS,W;GetSTATUSregisterbankselSTATUSsave;Switchbanks,ifneeded.movwfSTATUSsave;SavetheSTATUSregistermovfPCLATH,W;movwfPCLATHsave;SavePCLATHmovfFSR,W;movwfFSRsave;SaveFSRbankselPIR1btfssPIR1,SSPIF;IsthisaSSPinterrupt?goto$;No,justtraphere.bcfPIR1,SSPIFcallSSP_Handler;Yes,serviceSSPinterrupt.bankselFSRsavemovfFSRsave,W;movwfFSR;RestoreFSRmovfPCLATHsave,W;movwfPCLATH;RestorePCLATHmovfSTATUSsave,W;movwfSTATUS;RestoreSTATUSswapfWREGsave,F;swapfWREGsave,W;RestoreWREGretfie;Returnfrominterrupt.;---------------------------------------------------------------------Setup;Initializesprogramvariablesandperipheralregisters.;---------------------------------------------------------------------bankselPCONbsfPCON,NOT_PORbsfPCON,NOT_BORbankselIndex;ClearvariousprogramvariablesclrfIndexclrfPORTBclrfPIR1bankselTRISBclrfTRISBmovlw0x36;SetupSSPmodulefor7-bitbankselSSPCONmovwfSSPCON;address,slavemodemovlwNODE_ADDRbankselSSPADDmovwfSSPADDclrfSSPSTATbankselPIE1;EnableinterruptsbsfPIE1,SSPIEbsfINTCON,PEIE;EnableallperipheralinterruptsbsfINTCON,GIE;EnableglobalinterruptsbcfSTATUS,RP0return;---------------------------------------------------------------------SSP_Handler;---------------------------------------------------------------------;TheI2Ccodebelowchecksfor5states:;---------------------------------------------------------------------State1:I2Cwriteoperation,lastbytewasanaddressbyte.SSPSTATbits:S=1,D_A=0,R_W=0,BF=1;State2:I2Cwriteoperation,lastbytewasadatabyte.SSPSTATbits:S=1,D_A=1,R_W=0,BF=1;State3:I2Creadoperation,lastbytewasanaddressbyte.SSPSTATbits:S=1,D_A=0,R_W=1,BF=0;State4:I2Creadoperation,lastbytewasadatabyte.SSPSTATbits:S=1,D_A=1,R_W=1,BF=0;State5:SlaveI2ClogicresetbyNACKfrommaster.SSPSTATbits:S=1,D_A=1,R_W=0,BF=0;Forconvenience,WriteI2CandReadI2Cfunctionshavebeenused.;----------------------------------------------------------------------bankselSSPSTATmovfSSPSTAT,W;GetthevalueofSSPSTATandlwb'00101101';MaskoutunimportantbitsinSSPSTAT.bankselTemp;PutmaskedvalueinTempmovwfTemp;forcomparisionchecking.State1:;Writeoperation,lastbytewasanmovlwb'00001001';address,bufferisfull.xorwfTemp,W;btfssSTATUS,Z;AreweinState1?gotoState2;No,checkfornextstate.....memsetRXBuffer,0,RX_BUF_LEN;Clearthereceivebuffer.clrfIndex;Clearthebufferindex.callReadI2C;DoadummyreadoftheSSPBUF.returnState2:;Writeoperation,lastbytewasdata,movlwb'00101001';bufferisfull.xorwfTemp,WbtfssSTATUS,Z;AreweinState2?gotoState3;No,checkfornextstate.....LFSRRXBuffer,Index;Pointtothebuffer.callReadI2C;GetthebytefromtheSSP.movwfINDF;Putitinthebuffer.incfIndex,F;Incrementthebufferpointer.movfIndex,W;Getthecurrentbufferindex.sublwRX_BUF_LEN;Subtractthebufferlength.btfscSTATUS,Z;Hastheindexexceededthebufferlength?clrfIndex;Yes,clearthebufferindex.returnState3:;Readoperation,lastbytewasanmovlwb'00001100';address,bufferisempty.xorwfTemp,WbtfssSTATUS,Z;AreweinState3?gotoState4;No,checkfornextstate.....clrfIndex;Clearthebufferindex.LFSRRXBuffer,Index;PointtothebuffermovfINDF,W;Getthebytefrombuffer.callWriteI2C;WritethebytetoSSPBUFincfIndex,F;Incrementthebufferindex.returnState4:;Readoperation,lastbytewasdata,movlwb'00101100';bufferisempty.xorwfTemp,WbtfssSTATUS,Z;AreweinState4?gotoState5;No,checkfornextstate....movfIndex,W;Getthecurrentbufferindex.sublwRX_BUF_LEN;Subtractthebufferlength.btfscSTATUS,Z;Hastheindexexceededthebufferlength?clrfIndex;Yes,clearthebufferindex.LFSRRXBuffer,Index;PointtothebuffermovfINDF,W;GetthebytecallWriteI2C;WritetoSSPBUFincfIndex,F;Incrementthebufferindex.returnState5:movlwb'00101000';ANACKwasreceivedwhentransmittingxorwfTemp,W;databackfromthemaster.SlavelogicbtfssSTATUS,Z;isresetinthiscase.R_W=0,D_A=1gotoI2CErr;andBF=0return;Ifwearen抰inState5,thensomethingis;wrong.I2CErrno
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 教育行業(yè)在線教育平臺的課程評價(jià)體系方案
- 造價(jià)咨詢合同
- 2025年天津貨運(yùn)從業(yè)資格證模擬試題答案解析大全
- 2025年寧德貨物運(yùn)輸駕駛員從業(yè)資格考試系統(tǒng)
- 電子消費(fèi)券采購合同(2篇)
- 電力電量分配合同(2篇)
- 電池焊接維修合同(2篇)
- 2024年高考?xì)v史二輪復(fù)習(xí)“12+2+3”專項(xiàng)練第46題選做題專練
- 2024-2025學(xué)年四年級語文上冊第五單元19奇妙的國際互聯(lián)網(wǎng)教案2蘇教版
- 2024-2025學(xué)年高中化學(xué)第二章化學(xué)反應(yīng)與能量第二節(jié)化學(xué)能與電能2發(fā)展中的化學(xué)電源課時訓(xùn)練含解析新人教版必修2
- 肝癌個案護(hù)理課件
- 《公路橋梁抗震設(shè)計(jì)規(guī)范》(2231-01-2020)
- 新技術(shù)和新項(xiàng)目準(zhǔn)入制度及要點(diǎn)解讀
- 員工待崗管理辦法
- 26個英文字母書寫(手寫體)Word版
- 新學(xué)期新氣象PPT
- 教育的第三只眼
- GB/T 13813-2023煤礦用金屬材料摩擦火花安全性試驗(yàn)方法和判定規(guī)則
- 多功能健身車的設(shè)計(jì)-機(jī)械設(shè)計(jì)制造及其自動化本科畢業(yè)設(shè)計(jì)
- 動物檢疫技術(shù)-動物檢疫的方法方式(動物防疫與檢疫技術(shù))
- DB31 SW-Z 017-2021 上海市排水檢測井圖集
評論
0/150
提交評論