当前位置:首页 > 单片机 > 单片机
[导读] I2C_示例程序;----------------------------------------------------------; File Name: I2C.ASM; Author: Andy Yu; Date: July 22, 1998;---------------------------------------------------------

I2C_示例程序

;----------------------------------------------------------
; File Name: I2C.ASM
; Author: Andy Yu
; Date: July 22, 1998
;----------------------------------------------------------

#define I2C_ASM
include i2c.inc
publICInitI2CBusMaster
public _i2c_bLOCk_write
public _i2c_block_read
public SendData
public TxmtStartBit
public Txmt_Slave_Addr
public IsSlaveActive
public _Bus_Busy
public _Txmt_Progress
public _Rcv_Progress
public _Txmt_SuCCess
public _Rcv_Success
public _Ack_Error
public _10BitAddr
public _Slave_RW
public _SlaveActive
public DataByte
public SlaveAddr
public tempCount
public TempStore


;-------------------
; General Equate
;-------------------
TRUE EQU 1
FALSE EQU 0
LSB EQU 0
MSB EQU 7
Carry EQU [0ah].0
;-------------------
; Port Assignment
;-------------------
PortCTRlAddr EQU (PortAddr+1)
I2CPORT EQU [PortAddr]
SCL EQU [PortAddr+1].SCL_Bit ;PBC
SDA EQU [PortAddr+1].SDA_Bit ;PBC
PSCL EQU [PortAddr].SCL_Bit ;PB
PSDA EQU [PortAddr].SDA_Bit ;PB
;-----------------------------------------------
; Data Section
;-----------------------------------------------
I2CData .section 'data'
I2CStatus label byte
_Bus_Busy dbit
_Abort dbit
_Txmt_Progress dbit
_Rcv_Progress dbit
_Txmt_Success dbit
_Rcv_Success dbit
_Fatal_Error dbit
_Ack_Error dbit
I2CCtrl label byte
_10BitAddr dbit
_Slave_RW dbit
_Last_Byte_Rcv dbit
_reserve3 dbit
_reserve4 dbit
_reserve5 dbit
_SlaveActive dbit
_Time_Out dbit
DataByte db ?
DataByteCopy db ?
BitCount db ?
SlaveAddr db ? ; reserve 2 bytes to support 10 bit addr
tempCount db ?
TempStore db ?
DelayCount db ?
;-----------------------------------------------
; Code Section
;-----------------------------------------------
I2CCode .section 'code'
;-------------------
; IsSlaveAcitve
;-------
IsSlaveActive proc
clr _Slave_RW
call TxmtStartBit
call Txmt_Slave_Addr
clr _SlaveActive
snz _Ack_Error
set _SlaveActive
call TxmtStopBit
ret
IsSlaveActive endp
;-------------------
; _i2c_block_write
;-------
_i2c_block_write proc
call TxmtStartBit ; start
clr _Slave_RW ; slave address
call Txmt_Slave_Addr
snz _Txmt_Success
jmp ending
MOV A,TempStore ; subaddress
MOV DataByte,A
call SendData
_block_wr1_loop:
snz _Txmt_Success
jmp ending
MOV A,_IAR
MOV DataByte,A
inc _MP
call SendData
sdz tempCount
jmp _block_wr1_loop
ending:
call TxmtStopBit ; stop
ret
_i2c_block_write endp
;-------------------
; _i2c_block_read
;-------
_i2c_block_read proc
call TxmtStartBit
set _Slave_RW
clr _Last_Byte_Rcv
call Txmt_Slave_Addr
sz _Txmt_Success
jmp _block_rd1_loop
call TxmtStopBit
ret A,FALSE
_block_rd1_loop:
call GetData
MOV A,DataByte
MOV _IAR,A
inc _MP
sdz tempCount
jmp _block_rd1_loop
;
set _Last_Byte_Rcv
call GetData
MOV A,DataByte
MOV _IAR,A
call TxmtStopBit
ret A,TRUE
_i2c_block_read endp
;-------------------
; InitI2CBusMaster
;-------
InitI2CBusMaster proc
MOV A,I2CPORT ; clear latches
and A,NOT I2CPORTMASK
MOV I2CPORT,A
RELEASE_BUS
clr I2CStatus
clr I2CCtrl
ret
InitI2CBusMaster endp
;-------------------
; TxmtStartBit
;-------
TxmtStartBit proc
set SDA
set SCL
DELAY_tSETUP_START
clr SDA
DELAY_tHOLD_START
set _Bus_Busy
ret
TxmtStartBit endp
;-------------------
; TxmtStopBit
;-------
TxmtStopBit proc
clr SCL
clr SDA
set SCL
DELAY_tSETUP_STOP
set SDA
DELAY_tBUS_FREE
clr _Bus_Busy
ret
TxmtStopBit endp
;-------------------
; SendData
;-------
SendData proc
TxmtByte:
MOV A,DataByte
MOV DataByteCopy,A
set _Txmt_Progress
clr _Txmt_Success
MOV A,08h ; for BitCount=8 downto 1
MOV BitCount,A
TxmtNextBit:
clr SCL
rlc DataByteCopy
clr SDA
sz Carry
set SDA
DELAY_tLOW
set SCL
DELAY_tHIGH
sdz BitCount
jmp TxmtNextBit ; end for
;--- check ACK
clr SCL
set SDA
DELAY_tLOW
set SCL
DELAY_tHIGH
sz PSDA
jmp TxmtErrorAck
clr SCL
clr _Txmt_Progress
set _Txmt_Success
clr _Ack_Error
ret
TxmtErrorAck:
RELEASE_BUS
set SDA
set SCL
clr _Bus_Busy
clr _Txmt_Progress
clr _Txmt_Success
set _Ack_Error
ret
SendData endp
;-------------------
; GetData
;-------
GetData proc
RcvByte:
set _Rcv_Progress
clr _Rcv_Success
MOV A,08h
MOV BitCount,A
RcvNextBit:
clr SCL
set SDA
DELAY_tLOW
set SCL
DELAY_tHIGH
clr Carry
sz PSDA
set Carry
rlc DataByte
sdz BitCount
jmp RcvNextBit
; generate ACK
clr SCL
clr SDA
sz _Last_Byte_Rcv
set SDA
DELAY_tLOW
set SCL
DELAY_tHIGH
RcvEnd:
clr SCL
clr _Rcv_Progress
set _Rcv_Success
clr _Ack_Error
ret
GetData endp
;-------------------
; Txmt_Slave_Addr
;-------
Txmt_Slave_Addr proc
clr _Ack_Error
snz _10BitAddr
jmp SevenBitAddr
snz _Slave_RW
jmp TenBitAddrWR
TenBitAddrRd:
clr _Slave_RW
call TenBitAddrWR
snz _Txmt_Success
ret A,FALSE
call TxmtStartBit
set _Slave_RW
MOV A,SlaveAddr[1]
MOV DataByte,A
set DataByte.LSB
call SendData
jmp _AddrSendTest
;
snz _Txmt_Success
jmp _AddrSendFail
TenBitAddrWR:
MOV A,SlaveAddr[1]
MOV DataByte,A
clr DataByte.LSB
call SendData
snz _Txmt_Success
jmp _AddrSendFail
MOV A,SlaveAddr
MOV DataByte,A
jmp EndTxmtAddr
SevenBitAddr:
MOV A,SlaveAddr
MOV DataByte,A
clr DataByte.LSB
sz _Slave_RW
set DataByte.LSB
EndTxmtAddr:
call SendData
_AddrSendTest:
snz _Txmt_Success
jmp _AddrSendFail
ret A,TRUE
_AddrSendFail:
snz _Ack_Error
ret A,FALSE
call TxmtStopBit
ret A,FALSE
Txmt_Slave_Addr endp

;*********************************************************

;----------------------------------------------------------
; File Name: I2C.INC
; Author: Andy Yu
; Date: July 22, 1998
;----------------------------------------------------------
;
; modify these equates to suit your application
;
PortAddr EQU 14H ; SCL=PB0, SDA=PB1 14H is PB's port addr
SCL_Bit EQU 0
SDA_Bit EQU 1
I2CPORTMASK EQU 03h
_IAR EQU [00h]
_MP EQU [01h]
;
; fill delay codes to fit specific I2C slaves
;
DELAY_tSETUP_START macro ; START Condition Setup Time (600ns HT24C0x)
endm
DELAY_tHOLD_START macro ; START Condition Hold Time (600ns HT24C0x)
endm
DELAY_tSETUP_STOP macro ; STOP Condition Setup Time (600ns HT24C0x)
endm
DELAY_tBUS_FREE macro ; Bus Free Time (1300ns HT24C0x)
endm
DELAY_tLOW macro ; Clock Low Time (600ns HT24C0x)
endm
DELAY_tHIGH macro ; Clock High Time (1300ns HT24C0x)
endm
;----------------------------------------------------------
ifndef I2C_ASM
extern InitI2CBusMaster : near
extern _i2c_block_write : near
extern _i2c_block_read : near
extern SendData : near
extern TxmtStartBit : near
extern Txmt_Slave_Addr : near
extern IsSlaveActive : near
extern _Bus_Busy : bit
extern _Txmt_Progress : bit
extern _Rcv_Progress : bit
extern _Txmt_Success : bit
extern _Rcv_Success : bit
extern _Ack_Error : bit
extern _10BitAddr : bit
extern _Slave_RW : bit
extern _SlaveActive : bit
extern DataByte : byte
extern SlaveAddr : byte
extern tempCount : byte
extern TempStore : byte
endif
;-------------------
; LOAD_ADDR_8
;-------
LOAD_ADDR_8 macro SLAVE_ADDRESS
clr _10BitAddr
MOV A,(SLAVE_ADDRESS and 0ffh)
MOV SlaveAddr,A
endm
;-------------------
; I2C_TEST_DEVICE
;-------
I2C_TEST_DEVICE macro
call IsSlaveActive
endm
;-------------------
; RELEASE_BUS
;-------
RELEASE_BUS macro
set SDA ; set to hi-impedence
set SCL
endm
;-------------------
; I2C_WRITE_SUB
;-------
I2C_WRITE_SUB macro _BYTES_, _SourcePointer_, _SubAddress_
MOV A,_SubAddress_
MOV TempStore,A
MOV A,_BYTES_ ; loop data
MOV tempCount,A
MOV A,_SourcePointer_
MOV _MP,A
call _i2c_block_write
endm
;-------------------
; I2C_READ
;-------
I2C_READ macro _BYTES_, _DestPointer_
MOV A,_BYTES_-1
MOV tempCount,A
MOV A,_DestPointer_
MOV _MP,A
call _i2c_block_read
endm
;-------------------
; I2C_READ_SUB
;-------
I2C_READ_SUB macro _BYTES_, _DestPointer_, _SubAddress_
clr _Slave_RW
call TxmtStartBit
call Txmt_Slave_Addr
MOV A,_SubAddress_
MOV DataByte,A
call SendData
I2C_READ _BYTES_, _DestPointer_
endm

;*************************************************

;----------------------------------------------------------
; File Name: TEST.ASM
; Author: Andy Yu
; Date: July 22, 1998
; Purpose: test i2c routines
;----------------------------------------------------------
include i2c.inc
include ht48c50.inc

MySlaveAddr EQU 0a2h

tdata .section 'data'
PagEDAta db 8 dup(?)
Addr db ?
Add1 db ?
PageCount db ?
Temp db ?
Count db ?

main .section at 0 'code'
jmp start
;
start:
call InitI2CBusMaster
;
LOAD_ADDR_8 MySlaveAddr
;
I2C_TEST_DEVICE
snz _SlaveActive
jmp Error
;
; write HT24c01 with their address
;
clr Addr
MOV A,16 ; for PageCount = 16 downto 1
MOV PageCount,A
PageWriteLp:
call FillPageData ; Fill PageData with address
I2C_WRITE_SUB 8, offset PageData, Addr
snz _Txmt_Success
jmp Error
AckPoll:
I2C_TEST_DEVICE
snz _SlaveActive
jmp AckPoll
MOV A,8
addm A,Addr ; Addr += 8
sdz PageCount
jmp PageWriteLp
;
; read back check
;
clr Addr
MOV A,16; for PageCount = 16 downto 1
MOV PageCount,A
PageReadLp:
I2C_READ_SUB 8, offset PageData, Addr
snz _Rcv_Success
jmp Error
MOV a,0a0h
MOV add1,a
call CheckPageData
snz ACC.0
jmp Error
MOV a,8
addm A,add1 ; Add1 += 8
sdz PageCount
jmp PageReadLp
;
jmp $; success
Error:
jmp $ ; fail
FillPageData proc
; PageData=Add,Add+1,...,Add+7
MOV A,offset PageData ; MP0 = offset PageData
MOV MP0,A
MOV A,0a0h ; Temp = Addr
MOV Temp,A
MOV A,8
MOV Count,A ; for Count = 8 downto 1
FillLp:
MOV A,Temp
MOV R0,A
MOVPAC,A
inc Temp
inc MP0
sdz Count
jmp FillLp ; end for
ret
FillPageData endp
CheckPageData proc
; PageData must be Addr,Addr+1,...,Addr+7
MOV A,offset PageData ; MP0 = offset PageData
MOV MP0,A
MOV A,add1 ; Temp = Addr
MOV Temp,A
MOV A,8
MOV Count,A ; for Count = 16 downto 1
CheckLp:
MOV A,R0
XOR A,Temp
snz Z
ret A,0
inc Temp
inc mp0
sdz count
jmp checkLp
ret A,1
CheckPageData endp
;**********************************************


本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

9月2日消息,不造车的华为或将催生出更大的独角兽公司,随着阿维塔和赛力斯的入局,华为引望愈发显得引人瞩目。

关键字: 阿维塔 塞力斯 华为

加利福尼亚州圣克拉拉县2024年8月30日 /美通社/ -- 数字化转型技术解决方案公司Trianz今天宣布,该公司与Amazon Web Services (AWS)签订了...

关键字: AWS AN BSP 数字化

伦敦2024年8月29日 /美通社/ -- 英国汽车技术公司SODA.Auto推出其旗舰产品SODA V,这是全球首款涵盖汽车工程师从创意到认证的所有需求的工具,可用于创建软件定义汽车。 SODA V工具的开发耗时1.5...

关键字: 汽车 人工智能 智能驱动 BSP

北京2024年8月28日 /美通社/ -- 越来越多用户希望企业业务能7×24不间断运行,同时企业却面临越来越多业务中断的风险,如企业系统复杂性的增加,频繁的功能更新和发布等。如何确保业务连续性,提升韧性,成...

关键字: 亚马逊 解密 控制平面 BSP

8月30日消息,据媒体报道,腾讯和网易近期正在缩减他们对日本游戏市场的投资。

关键字: 腾讯 编码器 CPU

8月28日消息,今天上午,2024中国国际大数据产业博览会开幕式在贵阳举行,华为董事、质量流程IT总裁陶景文发表了演讲。

关键字: 华为 12nm EDA 半导体

8月28日消息,在2024中国国际大数据产业博览会上,华为常务董事、华为云CEO张平安发表演讲称,数字世界的话语权最终是由生态的繁荣决定的。

关键字: 华为 12nm 手机 卫星通信

要点: 有效应对环境变化,经营业绩稳中有升 落实提质增效举措,毛利润率延续升势 战略布局成效显著,战新业务引领增长 以科技创新为引领,提升企业核心竞争力 坚持高质量发展策略,塑强核心竞争优势...

关键字: 通信 BSP 电信运营商 数字经济

北京2024年8月27日 /美通社/ -- 8月21日,由中央广播电视总台与中国电影电视技术学会联合牵头组建的NVI技术创新联盟在BIRTV2024超高清全产业链发展研讨会上宣布正式成立。 活动现场 NVI技术创新联...

关键字: VI 传输协议 音频 BSP

北京2024年8月27日 /美通社/ -- 在8月23日举办的2024年长三角生态绿色一体化发展示范区联合招商会上,软通动力信息技术(集团)股份有限公司(以下简称"软通动力")与长三角投资(上海)有限...

关键字: BSP 信息技术
关闭
关闭