版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
读写24LCxx系列的EEPROM的实例程序读写24LCxx系列的EEPROM的实例程序;********************************************************
;*
*
;*
?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;
;***************************************
;
﹚竡跑计?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
;
;
;
;*???代刚祘Α穦?盢?舱计?(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. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 二零二五版无人驾驶技术研发公司股权转让及合作生产合同3篇
- 二零二五年网络安全违约责任承担详细合同解析3篇
- 二零二五年钢板桩租赁与施工方案优化合同3篇
- 二零二五版水电安装工程节能改造与施工合同2篇
- 二零二五版房产代持权违约责任合同范本3篇
- 二零二五年窗帘艺术中心窗帘定制合同3篇
- 二零二五年度高品质地暖系统安装与维护服务合同书2篇
- 二零二五版海洋工程建设项目担保合同3篇
- 二零二五年度酒店窗帘改造升级合同2篇
- 二零二五版服务器租赁与云存储解决方案合同3篇
- 2024年公务员考试《公共基础知识》全真模拟试题1000题及答案
- DB3301T 0382-2022 公共资源交易开评标数字见证服务规范
- 幼儿教育专业国家技能人才培养工学一体化课程设置方案
- 2025年会计从业资格考试电算化考试题库及答案(共480题)
- 江苏省无锡市2023-2024学年八年级上学期期末数学试题(原卷版)
- DL-T 5876-2024 水工沥青混凝土应用酸性骨料技术规范
- GB/T 44889-2024机关运行成本统计指南
- 2024年6月英语六级考试真题及答案(第2套)
- 职业院校技能大赛(高职组)市政管线(道)数字化施工赛项考试题库(含答案)
- 危险化学品目录(2024版)
- 华为经营管理-华为的股权激励(6版)
评论
0/150
提交评论