




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
讀寫(xiě)24LCxx系列的EEPROM的實(shí)例程序讀寫(xiě)24LCxx系列的EEPROM的實(shí)例程序;********************************************************
;*
*
;*
?PIC16F877A?I2CMaster家Α???﹃?ΑEEPROM?絛ㄒ祘Α
*
;*
*
;*Writtenby:
RichardYang
*
;*
Sr.CorporateApplicationEngineer
*
;*
MicrochipTechnologyInc.
*
;*Date:
Oct.2nd'2002
*
;*Revision:1.00
*
;********************************************************
;*************************************************
;ThissourcecodeprovidesademonstrationoftheMSSPperipheral
;onthePIC16F87xMCU.
;
;
;***********
ThesubroutinesforEEPROM
*****************
;
;EE_Random_Read
;EEPROMRandomaddressReadfromEEPROM
;EE_SEQU_Read
;EEPROMSequentialReadformEEPROM
;EE_Ack_Check
;PollingcurrentstatusofEEPROM
;EE_Page_Write
;PageWritefunctionforEEPROM
;EE_Byte_Write
;WriteabytetoEEPROMwithaddresssetting
;
;***********
ThesubroutinesforI2C
*****************
;
;Init_I2C_Master
;InitialI2CModuleforMasterMode,7-bitaddress
;StartI2C
;SendaSTARTCondition!!
;StopI2C
;SendsSTOPCondition
;RstartI2C
;SendaRepeatStartconditionaltoI2C
;Non_Ack
;SendaNon-acknowledgesignaltoI2C
;An_Ack
;SendaacknowledgesignaltoI2C
;Sebd_Byte
;SendabytetoI2Cbus
;RecI2C
;EnableReadabyteformslavedevice
;I2C_Done
:WaittheI2Ccompletedthecurrectprocess
;
;***************************************************
listp=16f877a
#include;
;***************************************
;
﹚竡跑計(jì)?RAM???
;
CBLOCK0x20
I2C_Ctrl
I2C_Addr
I2C_Data
I2C_Page_Length
D_Count
ENDC
cblock0x40
I2C_Page_Buffer:8
I2C_SEQU_Buffer:8
endc
;
;***************************************
;
﹚竡い耞?既?竟??w_temp
EQU0x72
status_tempEQU0x73
pclath_tempEQU0x74
;
;***************************************
;
﹚竡I2C???の竲?
;
EEPROM_CMDequ0xA0
;DeviceadressofSlavePoint
EE_Read
equ.1
EE_Writeequ.0;
#define
SCLPORTC,3
;I2CSCLpin
#define
SDAPORTC,4
;I2CSDApin;;********************************************
;Locatesstartupcode@theresetvector
;********************************************
Reset_Addr
org
0x00
nop
gotoMain_Init
;
org
0x04
gotoISR
;
;*************************************************************
;****TheStartAddressofISRis0x004
;****"PUSH"&"POP"?ㄏノ絛ㄒ:続ノ?鉤PIC16F877ΤSHAREBANK?PIC
;**************************************************************
ISR
Push
movwf
w_temp
;saveoffcurrentWregistercontents
movfSTATUS,w
;movestatusregisterintoWregister
movwfstatus_temp
;saveoffcontentsofSTATUSregister
movfPCLATH,W
movwfpclath_temp
;
;
Putyourinterruptcodehere
;
Pop
movfpclath_temp,W
movwfPCLATH
movf
status_temp,w
;retrievecopyofSTATUSregister
movwfSTATUS
;restorepre-isrSTATUSregistercontents
swapf
w_temp,f
swapf
w_temp,w
;restorepre-isrWregistercontents
;
retfie
;returnfrominterrupt
;Main_Init
pageselInit_I2C_Master
;SetPAGEtoPCLATHRegister
call
Init_I2C_Master
;InittheMSSPforI2CMaster
;
bankselI2C_Ctrl
movlwEEPROM_CMD
;LoadEEPROMcommandaddress@0xA0
movwfI2C_Ctrl
;
;
Main
;
;
;
;*???代剛祘Α穦?盢?艙計(jì)?(0x40-0x47)糶??RMA0x40-0x47??竚
;*?ノPageWrite?よΑ盢8??じ艙?戈?糶?EEPROM?
;*秈︽?篈?盎代EEPROM琌??ЧΘ糶??笆??程?盢?糶
;*??戈??SequentialRead?よΑ眖EEPROM弄?ㄓ??I2C_SEQU_Bufferい
;Test_Page_RW
movlw0x08
movwfI2C_Page_Length
movlwI2C_Page_Buffer
_Fill_RAMmovwfFSR
movwfINDF
incfFSR,W
decfszI2C_Page_Length,F
goto_Fill_RAM
;
Test_Page_Write
;
bankselI2C_Ctrl
movlwEEPROM_CMD
;LoadEEPROMcommandaddress@0xA0
movwfI2C_Ctrl
movlw0x10
;SelectEEPROMlocationat0x00
movwfI2C_Addr
movlw.8
movwfI2C_Page_Length
callEE_Page_Write
;
callEE_Ack_Check
;
bankselI2C_Ctrl
movlwEEPROM_CMD
;LoadEEPROMcommandaddress@0xA0
movwfI2C_Ctrl
movlw0x10
movwfI2C_Addr
movlw.8
movwfI2C_Page_Length
callEE_SEQU_Read
;
goto$
;
;
;
;*???代剛祘Α穦?糶????じ艙?戈?糶?EEPROMい
;*?秈︽?篈?盎代琌??ЧΘ糶??笆??程?盢?糶
;*??戈??眖EEPROM弄?ㄓ??I2C_Data既?竟い
;
Test_1byte_RW
bankselI2C_Ctrl
movlwEEPROM_CMD
;LoadEEPROMcommandaddress@0xA0
movwfI2C_Ctrl
movlw0x00
;SelectEEPROMlocationat0x00
movwfI2C_Addr
movlw0xAA
;Writedata0x5Atolocation0x00ofEEPROM
movwfI2C_Data
callEE_Byte_Write
;
Test2
callEE_Ack_Check
;PollingAcknowledgefornextaccess
;
bankselI2C_Data
movlw0x00
;ClearI2Cdatabuffer
movwfI2C_Data
;
Test3
bankselI2C_Ctrl
movlwEEPROM_CMD
;LoadEEPROMcommandaddress@0xA0
movwfI2C_Ctrl
movlw0x00
movwfI2C_Addr
callEE_Random_Read
;
goto
$
;;
;******************************************************
;*
RandomReadaBytefromEEPROM
;*
;*Input:
;*
-I2C_Ctrl:ControlByteofEEPROM
;*
-I2C_Addr:LocationofEEPROM
;*Output:
;*
-I2C_Data:ReadDatafromEEPROM
;******************************************************
;
;SendCommandforRANDOMREAD:
;"Start+0xA0+EE_Address+ReStart+0xA1+Read_Data+NAck+Stop"
;EE_Random_Read
callStartI2C
;SetSSPCON2.SEN
;
bcf
I2C_Ctrl,0
;setforwriteCommand
movfI2C_Ctrl,W
;SendSlaveAddresstoI2CBus
callSend_Byte
;
movfI2C_Addr,W
;SendouttheRendomaddressofEEPROM
callSend_Byte
;
callRstartI2C
;SendaRepeatStarttoI2C
;
bsf
I2C_Ctrl,0
;setforReadCommand
movfI2C_Ctrl,W
;SendSlaveAddresstoI2CBus
callSend_Byte
;
callRecI2C
;EnableI2CReceive
;
BANKSELSSPBUF
movfSSPBUF,W;SavetoI2C_DataFirst!!
movwfI2C_Data
callNon_Ack
;InitialNACKResponse!!
callStopI2C
;InitialSTOPCondition
return
;
;***************************************************************
;*
SequentialReadfromEEPROM
;*
;*Input:
;*
-I2C_Ctrl:ControlByteofEEPROM
;*
-I2C_Addr:StartLocationofEEPROM
;*
-I2C_Page_Length:Howmanybyteneedtoread
;*Output:
;*
-I2C_SEQU_Buffer:SequentialReadDatabuffer
;*
;***************************************************************
;
;SendCommandforRANDOMREAD:
;"Start+0xA0+EE_Address+ReStart+0xA1+Read_Data+NAck+Stop"
;
EE_SEQU_Read
callStartI2C
;SetSSPCON2.SEN
;
bcf
I2C_Ctrl,0
;setforwriteCommand
movfI2C_Ctrl,W
;SendSlaveAddresstoI2CBus
callSend_Byte
;
movfI2C_Addr,W
;SendouttheRendomaddressofEEPROM
callSend_Byte
;
callRstartI2C
;SendaRepeatStarttoI2C
;
bsf
I2C_Ctrl,0
;setforReadCommand
movfI2C_Ctrl,W
;SendSlaveAddresstoI2CBus
callSend_Byte
;
movlwI2C_SEQU_Buffer
movwfFSR
;
_Sequ_LoopcallRecI2C
;EnableI2CReceive
BANKSELSSPBUF
movfSSPBUF,W
;SavetoI2C_DataFirst!!
movwfINDF
incfFSR,F
decfszI2C_Page_Length,F
goto_Cont_Read
goto_End_Read
_Cont_ReadcallAn_Ack
goto_Sequ_Loop
;
_End_ReadcallNon_Ack
;InitialNACKResponse!!
callStopI2C
;InitialSTOPCondition
return
;
;******************************************************
;*
EEPROMAcknowledgePolling
;*
;*
--
TheroutinewillpollingtheACK
;*
responsefromEEPROM
;*
--
ACK=0return
;*
--
ACK=1sendRestart&loopcheck
;*
;******************************************************
;
EE_Ack_Check
callStartI2C
;SetSSPCON2.SEN
bcf
I2C_Ctrl,0
;ClearforWriteCommand
movfI2C_Ctrl,W
;SendSlaveAddresstoI2CBus
callSend_Byte
_Ack_Polling
BANKSELSSPCON2
btfssSSPCON2,ACKSTAT;CheckACKSTATbit,0=ACK,1=NACK
gotoACK_Return
;Ack=0;EEPROMisReady
_Ack_Hi
;Ack=1;EEPROMisBusyforWrite
callDelay_mS
;Delay500uSfornextAckpolling
callRstartI2C
;SendaRepeatStarttoI2C
bcf
I2C_Ctrl,0
;ClearforWriteCommand
movfI2C_Ctrl,W
;SendSlaveAddresstoI2CBus
callSend_Byte
goto_Ack_Polling
ACK_Return
callStopI2C
;InitialSTOPCondition
return
;
;**********************************************************
;*
PageWrite1to8BytestoEEPROM
;*
;*
Input:
;*
-I2C_Ctrl:ControlByteofEEPROM
;*
-I2C_Addr:LocationofEEPROM
;*
-I2C_Page_Buffer:RAMlocationofData
;*
-I2C_Page_Length:Datalengthcount
;*
;**********************************************************
;
;SendCommandforPAGEWRITE:
;"Start+0xA0+EE_Address+W_Data0+..+W_DataN+Stop"
;
EE_Page_Write
movfI2C_Page_Length,W
btfscSTATUS,Z
return
;
callStartI2C
;SetSSPCON2.SEN
;
bcf
I2C_Ctrl,0
;ClearforWriteCommand
movfI2C_Ctrl,W
;SendSlaveAddresstoI2CBus
callSend_Byte
;
movfI2C_Addr,W
;SendouttheCommand
callSend_Byte
;
movlwI2C_Page_Buffer
movwfFSR
_W1
movfINDF,W
callSend_Byte
incfFSR,F
decfszI2C_Page_Length,F
goto_W1
;
callStopI2C
;InitialSTOPCondition
return
;
;
;*******************************************************
;*
WriteaBytetoEEPROM
;*
;*Input:
;*
-I2C_Ctrl:ControlByteofEEPROM
;*
-I2C_Addr:LocationofEEPROM
;*
-I2C_Data:DatatoEEPROM
;*
;*******************************************************
;
;SendCommandforBYTEWRITE:
;"Start+0xA0+EE_Address+Write_Data+Stop"
;
EE_Byte_Write
callStartI2C
;SetSSPCON2.SEN
;
bcf
I2C_Ctrl,0
;ClearforWriteCommand
movfI2C_Ctrl,W
;SendSlaveAddresstoI2CBus
callSend_Byte
;
movfI2C_Addr,W
;SendouttheCommand
callSend_Byte
;
movfI2C_Data,W
;SendouttheData
callSend_Byte
;
callStopI2C
;InitialSTOPCondition
return
;
;**********************************************************************
;
;
AsfollowingsubroutinesperformcommonlyusedI2Cfunctions.
;
YoucanusethesesubroutinesforyourI2Caccess
;
;**********************************************************************
;
;***InitialI2Cmodulefot7-bitI2CMasterModewith100Kcps@4MHz
;
Init_I2C_Master
BANKSELTRISC
;InitialPortC,bit3&4asInput
bsf
SCL
;RC3=SCL
,
RC4=SDA
bsf
SDA
;
BANKSELPORTC
bsf
SCL
;SCL=Hi
bsf
SDA
;SDA=Hi
;
movlwb'00101000'
;I2CMasterMode,ClockRate:FOSC/(4*SSPADD+1)
movwfSSPCON
;
;
bankselSSPADD
movlw
.9
;Thisgives100KHzI2Cclock@4MHz
movwf
SSPADD
;(4MHz/4)/(9+1)=100KHz
;
movlw
b'10000000'
;Disableslewratecontrol,
movwf
SSPSTAT
;andclearstatusbits
;
movlw
b'00000000'
;SetSCL,SDAintoReadystatus
movwf
SSPCON2
;
return
;
;******
SendSTARTconditiontobus
*******
;
StartI2C
;InitiatetheI2CSTARTcondition.
bankselSSPCON2
bsf
SSPCON2,SEN
gotoI2C_Done
;
;******
SendSTOPconditiontobus
*******
;
StopI2C
bankselSSPCON2
bsf
SSPCON2,PEN
gotoI2C_Done
;
;******
SendRESTARTconditiontobus
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 食品企業(yè)應(yīng)對(duì)飛行檢查注意事項(xiàng)及策略
- 教育培訓(xùn)課程定制與實(shí)施協(xié)議
- 生物制藥技術(shù)工藝知識(shí)習(xí)題集
- 物理電學(xué)原理試題庫(kù)
- 小學(xué)教育中的多元化評(píng)價(jià)方式
- 古漢語(yǔ)基礎(chǔ)練習(xí):初中英語(yǔ)語(yǔ)法技巧輔導(dǎo)
- 活動(dòng)安排表格-工作日程表管理
- 污染治理措施的有效性評(píng)估與改進(jìn)
- 國(guó)際商務(wù)文化與交流測(cè)試題
- 教育學(xué)歷及相關(guān)資格證書(shū)證明(5篇)
- 2025天津市龍網(wǎng)科技發(fā)展有限公司招聘9人筆試參考題庫(kù)附帶答案詳解
- 2024年甘肅省西部計(jì)劃真題
- 2024北京重點(diǎn)校八年級(jí)(下)期末道德與法治匯編:人民當(dāng)家作主章節(jié)綜合
- CJ/T 189-2007鋼絲網(wǎng)骨架塑料(聚乙烯)復(fù)合管材及管件
- 工會(huì)考試試題及答案大全
- 中職電子技術(shù)試題及答案
- 麻醉精神藥品培訓(xùn)考核試題(含答案)
- 豬丁型冠狀病毒感染對(duì)ST細(xì)胞凋亡與自噬的機(jī)制解析
- 家居建材倉(cāng)庫(kù)管理制度
- 國(guó)家開(kāi)放大學(xué)2025年《創(chuàng)業(yè)基礎(chǔ)》形考任務(wù)2答案
- 商業(yè)發(fā)票模板(INVOICE)
評(píng)論
0/150
提交評(píng)論