24CXX系列讀寫程序(EMC指令版)大話篇
話說程序匠人,自進論壇以來,天天勤練,日日苦修(花了我東家的不少上網(wǎng)費?。。浴洞笤捚废盗?,嬴得了無數(shù)MM得芳心……終于將積分修到500分以上(呵呵,以后可以貼圖片了,如果那位MM想一睹匠人的“浴”照,說一聲,小匠一定滿足)……
—— 身后突然傳來一聲呵斥:“休不休啊你?”
—— 匠人心頭一驚,驀然回首,那人(不是MM,是斑竹)正在燈火闌珊處(手中正握著那把失而復(fù)得的大砍刀)……
—— 匠人暗自慶幸還沒有把對斑竹不敬的話語說出來……
……在論壇中,小匠結(jié)識了許多高手好友,并得到不少幫助,感激不盡。但也有一些MM好報打不平,覺得小匠在《大話篇》中老是受斑竹的欺負……
—— 一道寒光映入眼簾,匠人發(fā)現(xiàn)自己好象說漏了嘴……
—— 再看斑竹手中的刀,已經(jīng)從刀鞘中抽出了兩公分……
……其實,那都是大伙的誤解……其實,小匠一直非常感謝斑竹的厚道和寬容,沒有將小匠的一些大話帖子DELETE掉……
—— 匠人好象聽到了砍刀緩緩入鞘的金屬聲……
—— 暗呼:“好險!”
—— 匠人再次悄悄回頭,只見斑竹大人已經(jīng)遠遠去了(頭上頂著一頂精致的蘭花大高帽)
……最近,連續(xù)看到好幾篇討論24CXX系列應(yīng)用的帖子。正好,小匠最近用EMC的指令也做了一段程序。不如無私奉獻一下(如果哪位MM有疑問,可來函、來電、來EMAIL、來FAX、來人,或者約下第一次親密約會,探討探討……)
?
;******************************************
;** 24CXX? 接口I2C總線讀/寫的程序
;**? (所有時序均基于4MHZ晶體震蕩器頻率)
;******************************************
/*
;****************************
;隨機讀寫測試程序(示范程序)
;****************************
TEST:
??? MOV??? A,@0XAE??????? ;A2=A1=A0=1
??? MOV??? SLAVE_24,A??? ;設(shè)置器件碼
??? MOV??? A,@2??????? ;R/W LOC. = 2
??? MOV??? ADDR_24,A??? ;設(shè)置地址碼
??? MOV??? A,@55??????? ;
??? MOV??? DATA_24,A??? ;寫55到E2PROM
??? CALL??? WRBYTE??????? ;寫一個字節(jié)
??? CALL??? DL10MS??????? ;延時10MS,等待寫操作完成(注意,此語句非常重要)
??? CALL???? RDBYTE??????? ;讀回原數(shù)據(jù)
??? MOV??? A,@55??????? ;
??? XOR??? A,DATA_24??? ;
??? JBS??? R3,Z??????? ;讀出數(shù)據(jù)=寫入數(shù)據(jù)跳
WRONG:
??? JMP??? WRONG??????? ;校驗失敗
CORRECT:
??? JMP??? CORRECT??????? ;校驗通過
DL10MS:
??? RET???
;****************************
;存儲器定義
;****************************
PROT_I2C??? EQU??? 0X05??? ;I2C操作口
??? SDA??? EQU??? 3??? ;數(shù)據(jù)腳
??? SCL??? EQU??? 2??? ;時鐘腳
ADDR_24??? EQU??? 0X1B??????? ;地址寄存器
DATA_24??? EQU??? 0X1C??????? ;寫入/讀自I2C的數(shù)據(jù)寄存器
SLAVE_24 EQU??? 0X1D??????? ;從器件地址寄存器(1010XXX0)
DBUF_24??? EQU??? 0X1E??????? ;發(fā)送/接受自SDA口的數(shù)據(jù)緩沖器
COUNT_24 EQU??? 0X1F??????? ;位計數(shù)器
TT_24??? EQU??? 0X20??????? ;標志寄存器
??? REND_24??? EQU??? 1??? ;讀完畢標志(0=未完畢,1=完畢)
NUM_24??? EQU??? 0X21??????? ;頁寫/頁讀字節(jié)數(shù)
*/
;****************************
;宏定義
;****************************
;=================
;設(shè)置SCL,SDA為輸出口
;=================
SDA_OUT??? MACRO
??? MOV??? A,@0B00000001??
??? IOW??? PROT_I2C
ENDM
;=================
;設(shè)置SCL為輸出口,SDA為輸入口
;=================
SDA_IN??? MACRO
??? MOV??? A,@0B00001001
??? IOW??? PROT_I2C
ENDM
;****************************
;字節(jié)寫程序
;功能:??? 寫一個字節(jié)到EEPROM器件
;入口:??? DATA_24??? =要寫的數(shù)據(jù)
;??? ADDR_24??? =數(shù)據(jù)地址
;??? SLAVE_24=從器件地址1010XXX0)
;****************************
WRBYTE:
??? MOV??? A,SLAVE_24??????????????
??? MOV??? DBUF_24,A???????
??? CALL??? BSTART??????????? ;送起始位
??? CALL??? TX??????????? ;送從器件地址并檢測應(yīng)答信號
??? MOV??? A,ADDR_24???????
??? MOV??? DBUF_24,A???????
??? CALL??? TX??????????? ;送數(shù)據(jù)地址并檢測應(yīng)答信號
??? MOV??? A,DATA_24???????
??? MOV??? DBUF_24,A???????
??? CALL??? TX??????????? ;送數(shù)據(jù)并檢測應(yīng)答信號
??? CALL??? BSTOP??????????? ;送停止位
??? RET
;****************************
;字節(jié)讀程序
;功能:??? 從EEPROM器件讀一個字節(jié)
;入口:??? ADDR_24??? =數(shù)據(jù)地址
;??? SLAVE_24=從器件地址(1010XXX0)
;出口:??? DATA_24??? =讀到的數(shù)據(jù)
;****************************
RDBYTE:
??? MOV??? A,SLAVE_24
??? MOV??? DBUF_24,A
??? CALL??? BSTART??????????? ;送起始位
??? CALL??? TX??????????? ;送從器件地址并檢測應(yīng)答信號
??? MOV??? A,ADDR_24???????
??? MOV??? DBUF_24,A
??? CALL??? TX??????????? ;送數(shù)據(jù)地址并檢測應(yīng)答信號
;轉(zhuǎn)入讀狀態(tài)???
??? CALL??? BSTART??????????? ;送起始位???
??? MOV??? A,SLAVE_24??????????
??? MOV??? DBUF_24,A
??? BS???? DBUF_24,0???????????
??? CALL??? TX??????????? ;送從器件地址并檢測應(yīng)答信號
??? BS??? TT_24,REND_24???????
??? CALL??? RX??????????? ;讀數(shù)據(jù)并發(fā)送應(yīng)答信號
??? CALL??? BSTOP??????????? ;送停止位
??? MOV??? A,DBUF_24???????
??? MOV??? DATA_24,A??????? ;保存數(shù)據(jù)到DATA_24
??? RET
;****************************
;頁寫程序
;功能:??? 寫一個字節(jié)到EEPROM器件
;入口:??? R4??? =要寫的數(shù)據(jù)在RAM中存放首地址
;??? NUM_24??? =字節(jié)數(shù)
;??? ADDR_24??? =數(shù)據(jù)地址
;??? SLAVE_24=從器件地址1010XXX0)
;****************************
WRPAGE:
??? MOV??? A,SLAVE_24??????????????
??? MOV??? DBUF_24,A???????
??? CALL??? BSTART??????????? ;送起始位
??? CALL??? TX??????????? ;送從器件地址并檢測應(yīng)答信號
??? MOV??? A,ADDR_24???????
??? MOV??? DBUF_24,A???????
??? CALL??? TX??????????? ;送數(shù)據(jù)地址并檢測應(yīng)答信號
WRPAGE1:
??? MOV??? A,R0???????
??? MOV??? DBUF_24,A???????
??? CALL??? TX??????????? ;送數(shù)據(jù)并檢測應(yīng)答信號
??? INC??? R4
??? DJZ??? NUM_24
??? JMP??? WRPAGE1??????????? ;未寫完繼續(xù)
??? CALL??? BSTOP??????????? ;送停止位
??? RET
;****************************
;頁讀程序
;功能:??? 從EEPROM器件讀一個字節(jié)
;入口:??? ADDR_24??? =數(shù)據(jù)地址
;??? SLAVE_24=從器件地址(1010XXX0)
;??? NUM_24??? =字節(jié)數(shù)
;出口:??? R4??? =讀到的數(shù)據(jù)在RAM中存放首地址
;****************************
RDPAGE:
??? MOV??? A,SLAVE_24
??? MOV??? DBUF_24,A
??? CALL??? BSTART??????????? ;送起始位
??? CALL??? TX??????????? ;送從器件地址并檢測應(yīng)答信號
??? MOV??? A,ADDR_24???????
??? MOV??? DBUF_24,A
??? CALL??? TX??????????? ;送數(shù)據(jù)地址并檢測應(yīng)答信號
;轉(zhuǎn)入讀狀態(tài)???
??? CALL??? BSTART??????????? ;送起始位???
??? MOV??? A,SLAVE_24??????????
??? MOV??? DBUF_24,A
??? BS???? DBUF_24,0???????????
??? CALL??? TX??????????? ;送從器件地址并檢測應(yīng)答信號
RDPAGE1:
??? MOV??? A,@1
??? XOR??? A,NUM_24
??? BS??? TT_24,REND_24???????
??? JBS??? R3,Z??????????? ;NUM_24=1跳;判斷是否最后一個字節(jié),決定應(yīng)答信號???
??? BC??? TT_24,REND_24???????
??? CALL??? RX??????????? ;讀數(shù)據(jù)并發(fā)送應(yīng)答信號
??? MOV??? A,DBUF_24???????
??? MOV??? R0,A??????????? ;保存數(shù)據(jù)到R0
??? INC??? R4
??? DJZ??? NUM_24
??? JMP??? RDPAGE1??????????? ;未讀完繼續(xù)
??? CALL??? BSTOP??????????? ;送停止位
??? RET
;****************************
;I2C操作子程序(時序)集合
;****************************
;=================
;發(fā)送起始位程序
;說明:??? 當SCL=1時,向SDA發(fā)一個下降沿
;=================
BSTART:
??? SDA_OUT??????????????? ;設(shè)置SDA為輸出口
??? BS??? PROT_I2C,SDA??????? ;SDA=1
??? NOP??????????????? ;延時0.6US
??? BS??? PROT_I2C,SCL??????? ;SCL=1
??? NOP??????????????? ;延時0.6US
??? BC??? PROT_I2C,SDA??????? ;SDA=0
??? NOP??????????????? ;延時0.6US
??? BC??? PROT_I2C,SCL??????? ;SCL=0
??? NOP??????????????? ;延時0.6US
??? RET
;=================
;發(fā)送結(jié)束位程序
;說明:??? 當SCL=1時,向SDA發(fā)一個上升沿
;=================
BSTOP:
??? SDA_OUT??????????????? ;設(shè)置SDA為輸出口
??? BC??? PROT_I2C,SDA??????? ;SDA=0
??? NOP??????????????? ;延時0.6US
??? BS??? PROT_I2C,SCL??????? ;SCL=1
??? NOP??????????????? ;延時0.6US
??? BS??? PROT_I2C,SDA??????? ;SDA=1
??? NOP??????????????? ;延時0.6US
??? RET
;=================
;位輸入子程序
;說明:??? 當SCL=1時,從SDA上讀電平
;出口:??? C=從SDA上讀入的位值???
;=================
BITIN:
??? SDA_IN??????????????? ;設(shè)置SDA為輸入口
??? BS??? PROT_I2C,SDA??????? ;SDA=1
??? NOP??????????????? ;延時0.6US
??? BS??? PROT_I2C,SCL??????? ;SCL=1
??? NOP??????????????? ;延時0.6US
??? BC??? R3,C
??? JBC??? PROT_I2C,SDA???????
??? BS??? R3,C??????????? ;C=SDA
??? NOP??????????????? ;延時0.6US
??? BC??? PROT_I2C,SCL??????? ;SCL=0
??? NOP??????????????? ;延時0.6US
??? RET
;=================
;位輸出子程序
;說明:??? 每當SCL=0時,改寫SDA上的電平
;入口:??? C=要寫到SDA上的位值
;=================
BITOUT:
??? SDA_OUT??????????????? ;設(shè)置SDA為輸出口
??? JBS??? R3,C
??? JMP??? BIT0
??? BS??? PROT_I2C,SDA??????? ;SDA=C=1
??? JMP??? CLK1???????
BIT0:
??? BC??? PROT_I2C,SDA??????? ;SDA=C=0
CLK1:
??? NOP??????????????? ;延時0.6US
??? BS??? PROT_I2C,SCL??????? ;SCL=1
BIT2:
??? NOP
??? NOP
??? BC??? PROT_I2C,SCL??????? ;SCL=0
??? RET
;============================
;接收數(shù)據(jù)子程序
;入口:??? TT_24.REND_24??? =讀完畢標志
;出口:??? DBUF_24??? =接受到的數(shù)據(jù)(8_BIT)
;============================
RX:
??? MOV??? A,@8??????????? ;循環(huán)次數(shù)=8
??? MOV??? COUNT_24,A
??? CLR??? DBUF_24
RXLP:
??? CALL??? BITIN??????????? ;輸入1_BIT
??? RLC??? DBUF_24??????????? ;左移(帶C)
??? DJZ??? COUNT_24??????? ;循環(huán)結(jié)束?
??? JMP??? RXLP
;設(shè)置應(yīng)答信號位,如果讀完畢則送1(NO_ACK)停止接受,否則送0(ACK)繼續(xù)接受
??? BS??? R3,C???????
??? JBS??? TT_24,REND_24??????? ;讀完畢信號->C
??? BC??? R3,C???????
??? CALL??? BITOUT??????????? ;應(yīng)答???????????
??? RET
;============================
;發(fā)送數(shù)據(jù)子程序
;入口:??? DBUF_24??? =要發(fā)送的數(shù)據(jù)(8_BIT)
;============================
TX:
??? MOV??? A,@8??????????? ;循環(huán)次數(shù)=8
??? MOV??? COUNT_24,A
TXLP:
??? RLC??? DBUF_24??????????? ;左移(帶C)
??? CALL??? BITOUT??????????? ;輸出1_BIT
??? DJZ??? COUNT_24??????? ;循環(huán)結(jié)束?
??? JMP??? TXLP???????
??? CALL??? BITIN??????????? ;讀應(yīng)答信號
??? RET
;****************************
;I2C總線讀/寫的程序全部結(jié)束 !
;****************************
簽名:
如果你的‘芯’是一座作坊,
我愿做那不知疲倦的程序匠……
?freego 發(fā)表于 2002-4-21 12:53 侃單片機 ←返回版面????
小弟提問?
1、匠人兄在“TX”中讀得應(yīng)答位,小弟沒看懂如果讀得值為“1”即沒有應(yīng)答該如何處理?
2、匠人兄在“WRPAGE”中將寫入次數(shù)循環(huán)“NUM_24”次不知有否考慮當寫入字節(jié)數(shù)超越頁界(如每頁16BYTES、32BYTES)時數(shù)據(jù)地址溢出而覆蓋頁首的問題?
???
?程序匠人 發(fā)表于 2002-4-21 15:30 侃單片機 ←返回版面????
答復(fù)freego
1、匠人兄在“TX”中讀得應(yīng)答位,小弟沒看懂如果讀得值為“1”即沒有應(yīng)答該如何處理?
答:這段程序中,沒有對可能的錯誤做處理(因為我的設(shè)計中,I2C總線上只掛了一個器件),在“TX”中讀得應(yīng)答位,只是為了輸出一個CLOCK。
2、匠人兄在“WRPAGE”中將寫入次數(shù)循環(huán)“NUM_24”次不知有否考慮當寫入字節(jié)數(shù)超越頁界(如每頁16BYTES、32BYTES)時數(shù)據(jù)地址溢出而覆蓋頁首的問題?
答:既然是頁寫/頁讀,當然只針對頁內(nèi)進行操作了。如果要跨頁,我不是還有單字節(jié)寫/讀程序嗎?只要再做一段循環(huán)程序去調(diào)用即可。
歡迎大家把這段程序繼續(xù)完善。???
簽名:
如果你的‘芯’是一座作坊,
我愿做那不知疲倦的程序匠……
?zhhch 發(fā)表于 2002-4-21 23:40 侃單片機 ←返回版面????
向程序匠人學(xué)習(xí),現(xiàn)貼上我寫24xx讀寫程序,以供交流。我有emc_wice專業(yè)漢化版(本人漢化),歡迎索取!
該程序系51程序改寫而來,我現(xiàn)在正用著呢!
;=====================寫EEPROM程序 ========================================
;功能:從KEY_BUF取數(shù)據(jù),寫入EEPROM中SUB_ADR,連續(xù)寫B(tài)YTE_CNT個字節(jié)
;入口:SUB_ADR??????? 出口:NONE???????????????????
;影響資源:A,DATA_BUF,P6,STU,BIT_CNT,BYTE_CNT,SUB_ADR?????????????????????????????????????????????
;===============================================
WR_UEE:??????????????????????????? ;將KEY_BUF中客密碼寫EEPROM
??????? MOV???? SUB_ADR,@U_EE
??????? MOV???? BYTE_CNT,@0X06??????????? ;設(shè)置字節(jié)計數(shù)器為6
??????? JMP???? WR_EE
;------------------------------------------------------------------------???????
WR_SEE:??????????????????????????? ;將KEY_BUF中主密碼寫EEPROM
??????? MOV???? SUB_ADR,@S_EE
??????? MOV???? BYTE_CNT,@0X07??????????? ;設(shè)置字節(jié)計數(shù)器為7
;------------------------------------------------------------------------???????
WR_EE:
??????? MOV???? EEP6_BUF,EE??? ;保護EE口
??????? BS????? EE,PW????????? ;EEPROM上電
??????? BC????? EE,WP????????? ;清除EEPROM的寫保護???????
??????? CALL??? BSTART???????? ;啟動IIC
??????? MOV???? A,@WREE??????? ;發(fā)送寫器件地址
??????? MOV???? DATA_BUF,A
??????? CALL??? SENDBYTE
??????? JBC???? STU,GP????????
??????? JMP???? WR_RET??????? ;無應(yīng)答返回
??????? MOV???? A,SUB_ADR???? ;發(fā)送寫單元地址
??????? MOV???? DATA_BUF,A
??????? CALL??? SENDBYTE
??????? JBC???? STU,GP
??????? JMP???? WR_RET??????? ;無應(yīng)答返回
??????? MOV???? A,@KEY_BUF??? ;設(shè)置待寫數(shù)據(jù)區(qū)首址
??????? MOV???? R4,A
WR_NEXT:
??????? MOV???? A,R0
??????? MOV???? DATA_BUF,A??? ;取待寫數(shù)據(jù)
??????? CALL??? SENDBYTE
??????? JBC???? STU,GP
??????? JMP???? WR_RET??????? ;無應(yīng)答返回
??????? INC???? R4
??????? DJZ???? BYTE_CNT
??????? JMP???? WR_NEXT
???????
??????? CALL??? BSTOP???????? ;停止IIC
??????? CALL??? DELAY10?????? ;延時10MS,等待數(shù)據(jù)寫完
WR_RET:
??????? BC????? EE,PW????????? ;EEPROM下電
;---------------------------------------------------------???????
??????? IOR???? EE???????????? ;設(shè)置SDA為輸出
??????? AND???? A,@0B11110111? ;**************************
??????? IOW???? EE
;---------------------------------------------------------
??????? MOV???? EE,EEP6_BUF??? ;恢復(fù)EE口現(xiàn)場
??????? RET
;=====================讀EEPROM程序 ========================================
;功能:讀EEPROM,寫入PSW_BUF,連續(xù)寫B(tài)YTE_CNT個字節(jié)
;入口:SUB_ADR?????? 出口:NONE???????????????????
;影響資源:A,DATA_BUF,P6,STU,BIT_CNT,BYTE_CNT,SUB_ADR?????????????????????????????????????????????
;==============================================
RD_UEE:??????????????????????????? ;讀6位客密碼到PSW_BUF
??????? MOV???? SUB_ADR,@U_EE
??????? MOV???? BYTE_CNT,@0X05??????????? ;設(shè)置字節(jié)計數(shù)器為6-1
??????? JMP???? RD_EE
;------------------------------------------------------------------------???????
RD_SEE:??????????????????????????? ;讀7位密碼到PSW_BUF
??????? MOV???? SUB_ADR,@S_EE
??????? MOV???? BYTE_CNT,@0X06??????????? ;設(shè)置字節(jié)計數(shù)器為7-1
;------------------------------------------------------------------------???????
RD_EE:???
??????? MOV???? EEP6_BUF,EE??? ;保護EE口???
??????? BS????? EE,PW????????? ;EEPROM上電
??????? BS????? EE,WP????????? ;設(shè)置EEPROM的寫保護???????
??????? CALL??? BSTART???????? ;啟動IIC
??????? MOV???? A,@WREE??????? ;發(fā)送寫器件地址
??????? MOV???? DATA_BUF,A
??????? CALL??? SENDBYTE
??????? JBC???? STU,GP????????
??????? JMP???? RD_RET???????? ;無應(yīng)答返回
??????? MOV???? A,SUB_ADR????? ;發(fā)送寫單元地址
??????? MOV???? DATA_BUF,A
??????? CALL??? SENDBYTE
??????? JBC???? STU,GP
??????? JMP???? RD_RET???????? ;無應(yīng)答返回
??????? CALL??? BSTART???????? ;重新啟動IIC
??????? MOV???? A,@RDEE??????? ;發(fā)送讀器件地址
??????? MOV???? DATA_BUF,A
??????? CALL??? SENDBYTE
??????? JBC????? STU,GP????????
??????? JMP???? RD_RET???????? ;無應(yīng)答返回
???????
??????? MOV???? A,@PSW_BUF???? ;設(shè)置讀出數(shù)據(jù)存放首址
??????? MOV???? R4,A
RD_NEXT:
??????? BC????? STU,GP???????? ;連續(xù)接收BYTE_CNT-1字節(jié)數(shù)據(jù)
??????? CALL??? RCVBYTE??????? ;并發(fā)送應(yīng)答信號
??????? MOV???? A,DATA_BUF
??????? MOV???? R0,A
??????? INC???? R4
??????? DJZ???? BYTE_CNT
??????? JMP???? RD_NEXT
???????
??????? BS????? STU,GP???????? ;接收最后一個字節(jié)數(shù)據(jù)
??????? CALL??? RCVBYTE??????? ;并發(fā)送非應(yīng)答信號
??????? MOV???? A,DATA_BUF
??????? MOV???? R0,A
??????? CALL??? BSTOP????????? ;停止IIC
???????
RD_RET:
??????? BC????? EE,PW?????????? ;EEPROM下電
;---------------------------------------------------------???????
??????? IOR???? EE???????????? ;設(shè)置SDA為輸出
??????? AND???? A,@0B11110111? ;**************************
??????? IOW???? EE
;---------------------------------------------------------????????
??????? MOV???? EE,EEP6_BUF???? ;恢復(fù)EE現(xiàn)場
??????? RET
???????
;============================延時子程序====================================
;功能:短延時???????? 入口:NONE???????????? 出口:NONE????????????????????
;影響資源:A,TEMP ,STATUS?????????????????????????????????????????????????????
;==================================================
DELAY:????????????????????????
??????? MOV???????? A,@0X01
??????? MOV???????? TEMP,A
??????? DJZ???????? TEMP
??????? JMP???????? $-1
??????? RET???????????
;============================延時子程序10MS================================
;功能:延時???????? 入口:NONE???????????? 出口:NONE????????????????????
;影響資源:A,TEMP ,STATUS?????????????????????????????????????????????????????
;=================================================
DELAY10:????????????????????????
??????? MOV???????? A,@0X20
??????? MOV???????? TEMP,A
??????? MOV???????? A,@250
??????? MOV???????? TEMP1,A
??????? DJZ???????? TEMP1
??????? JMP???????? $-1
??????? DJZ???????? TEMP
??????? JMP???????? $-5
??????? RET???????????????????
;===============================啟用IIC總線================================
;功能:開啟IIC總線???????????????????????????????????????????????????????
;入口:NONE?????? 出口:NONE?? SCL=0,SDA=0
;影響資源:A,P6,STU??????????????????????????????????????????????????????
;==============================================
BSTART:
??????? BS????? EE,SDA??????? ;拉高SDA和SCL
??????? BS????? EE,SCL
??????? CALL??? DELAY
??????? BC????? EE,SDA??????? ;拉低SDA,啟動IIC
??????? CALL??? DELAY
??????? BC????? EE,SCL??????? ;拉低SCL
??????? RET
;===============================關(guān)閉IIC總線================================
;功能:關(guān)閉IIC總線???????????????????????????????????????????????????????
;入口:NONE?????? 出口:NONE? ,SCL=1,SDA=1?????????????????????????????????????????????
;影響資源:A,P6,STU??????????????????????????????????????????????????????
;====================================================
BSTOP:???????????
??????? BC????? EE,SDA??????? ;拉低SDA
??????? BS????? EE,SCL??????? ;拉高SCL
??????? CALL??? DELAY
??????? BS????? EE,SDA??????? ;拉高SDA,停止IIC
??????? CALL??? DELAY????
??????? RET
;======================送出一個字節(jié)數(shù)據(jù),高位在前===========================
;功能:從MCU發(fā)送一個字節(jié)給EEPROM,并接收應(yīng)答信號?????????????????????????????????????
;入口:發(fā)送位數(shù)放在DATA_BUF?????? 出口:GP=0 表應(yīng)答正確 GP=1 表應(yīng)答錯誤?????????????????????????
;影響資源:A,DATA_BUF,P6,STU,BIT_CNT?????????????????????????????????????????????
;=============================================
SENDBYTE:???????????????
??????? MOV???? A,@0X08??????? ;準備位計數(shù)器為8
??????? MOV???? BIT_CNT,A
SEND_B:???????
??????? RLC???? DATA_BUF?????? ;從DATA_BUF取數(shù)據(jù)送C
??????? JBC???? STU,C????????? ;C->SDA
??????? BS????? EE,SDA
??????? JBS???? STU,C?????????
??????? BC????? EE,SDA
??????? CALL??? DELAY
??????? BS????? EE,SCL???????? ;拉高SCL,通知接受數(shù)據(jù)
??????? CALL??? DELAY
??????? BC????? EE,SCL???????? ;拉低SCL,鉗住總線
??????? DJZ???? BIT_CNT
??????? JMP???? SEND_B???????? ;8位未發(fā)送完,繼續(xù)發(fā)送
???????
??????? BS????? EE,SDA???????? ;拉高SDA
;---------------------------------------------------------???????
??????? IOR???? EE???????????? ;設(shè)置SDA為輸入
??????? OR????? A,@0B00001000? ;**************************
??????? IOW???? EE
;---------------------------------------------------------???????
??????? BS????? EE,SCL???????? ;拉高SCL,接收應(yīng)答信號
??????? CALL??? DELAY
??????? JBC???? EE,SDA???????? ;SDA->GP:=0,OK,=1,ERR
??????? BS????? STU,GP
??????? JBS???? EE,SDA?
??????? BC????? STU,GP
;---------------------------------------------------------???????
??????? IOR???? EE???????????? ;設(shè)置SDA為輸出
??????? AND???? A,@0B11110111? ;**************************
??????? IOW???? EE
;---------------------------------------------------------???????
??????? BC????? EE,SCL???????? ;拉低SCL
??????? BC????? EE,SDA???????? ;拉低SDA
??????? RET
;=====================接收一個字節(jié)數(shù)據(jù),高位在前===========================
;功能:接收一個字節(jié)從EEPROM,并發(fā)送應(yīng)答信號
;入口:接收數(shù)據(jù)放在DATA_BUF?????? 出口:NONE???????????????????
;影響資源:A,DATA_BUF,P6,STU,BIT_CNT?????????????????????????????????????????????
;=============================================
RCVBYTE:???????????????
??????? MOV???? A,@0X08??????? ;準備位計數(shù)器為8
??????? MOV???? BIT_CNT,A
??????? BS????? EE,SDA
;---------------------------------------------------------???????
??????? IOR???? EE???????????? ;設(shè)置SDA為輸入
??????? OR????? A,@0B00001000? ;**************************
??????? IOW???? EE
;---------------------------------------------------------???????
RCV_B:?
??????? BC????? EE,SCL???????? ;拉低SCL
??????? CALL??? DELAY
??????? BS????? EE,SCL???????? ;拉高SCL
??????? CALL??? DELAY
??????? JBC???? EE,SDA????????? ;SDA->C
??????? BS????? STU,C
??????? JBS???? EE,SDA?????????
??????? BC????? STU,C
??????? RLC???? DATA_BUF?????? ;DATA_BUF<-C
??????? DJZ???? BIT_CNT
??????? JMP???? RCV_B????????? ;8位未接收完,繼續(xù)接收
???????
??????? BC????? EE,SCL???????? ;拉低SCL,準備發(fā)送應(yīng)答信號
??????? JBC???? STU,GP???????? ;GP->SDA,0發(fā)應(yīng)答信號,1發(fā)非應(yīng)答信號
??????? BS????? EE,SDA
??????? JBS???? STU,GP?
??????? BC????? EE,SDA
;---------------------------------------------------------???????
??????? IOR???? EE???????????? ;設(shè)置SDA為輸出
??????? AND???? A,@0B11110111? ;**************************
??????? IOW???? EE
;---------------------------------------------------------???????
??????? BS????? EE,SCL???????? ;拉高SCL,送出應(yīng)答信號
??????? CALL??? DELAY
??????? BC????? EE,SCL???????? ;拉低SCL,鉗住總線,等待后續(xù)操作
??????? BC????? EE,SDA
??????? RET????????
?
評論
查看更多