I2C assembly program: /*****;FileName: IicSMasU.a51;Describe: 51 series analog I2C bus master driver;Date: 2003/11/12;*****/$include (IicSMasU.inc)public _IicTxdRxdpublic SlvAddrpublic SubAddr/***************;Name: bit _IicTxdRxd(TxdByte,RxdByte,&IicDataBuf);Describe: Send data to the controlled device/receive data from the controlled device;Input: TxdByte=number of bytes of data to be sent; RxdByte=number of bytes of data to be received; IicDataBuf=first byte of the send/receive buffer; (SlvAddr(controlled device address),SubAddr(unit address));Output: C(Retry=1):operation failure flag; IicDataBuf=first byte of received data (valid when master receiving);Calling instructions:; A. Write current address: SlvAddr (write)!!! (devices with SubAddr cannot use this subroutine); TxdByte=(number of bytes of data to be sent (SubAddr is the first data to be sent)); RxdByte=0; B. Write specified address: SlvAddr (write), SubAddr; TxdByte=(1+number of bytes of data to be sent); RxdByte=0; C. Read current address: SlvAddr (read); TxdByte=0; RxdByte=number of bytes of data to be received; D. Read specified address: SlvAddr (read), SubAddr; TxdByte=1; RxdByte=number of bytes of data to be received; Nesting level: 1; Change: A, C, R1, R4~R7; ******************************/BitSegIicSM SEGMENT BIT overlayableRSEG BitSegIicSMRetry: dbit 1 Indicates that the last data transmission of I2C failed and the operation should be repeated BITEA: DBIT 1 Store interrupt status DataSegIicSM SEGMENT DATA overlayableRSEG DataSegIicSMSlvAddr: ds 1 controlled device address SubAddr: ds 1 unit address TxdByte equ r7 the number of bytes of data to be sent (the first transfer parameter) RxdByte equ r5 the number of bytes of data to be received (the second transfer parameter) WaitXTm macro X delay X machine cycles if X=0exitmendifif X=1nopendifif X=2nopnopendifif X=3nopnopnopendifif X>255error \"the number of X is too much\"elsemov r6,#X/2DJNZ r6,$endifendmCodeSegIicSM SEGMENT CODERSEG CodeSegIicSM_IicTxdRxd:SETB Retry Set error flag /*****; Send start condition;*****/SendStart:SETB SDASETB SCLWaitXTm IicDelayCLR SDA generates a start signal WaitXTm IicDelayCLR SCL ends the start condition /*****; Send the controlled device address, data; *****/SendSlaAdr:MOV A,SlvAddrCJNE TxdByte,#0,SendSlaAdr1SETB ACC.0 Read operation when TxdByte=0 SendSlaAdr1:SETB C Release the SDA line when detecting the response bit CALL XmByteJC IicErr No response error JB ACC.0,ReceiveData Read operation when SlaAdr.0=1; Write operation MOV A,SubAddrSendData:SETB C Release the SDA line when detecting the response bit CALL XmByteJC IicErr No response error MOV A,@R1INC R1DJNZ TxdByte,SendDataDEC R1MOV A,RxdByteJNZ SendStart RxdByte>0 Read operation when JMP SendStop/*****;Name: RcvByte;Describe: Receive 1 byte of data (receive 8 bits, send +1 bit (non) acknowledge bit);Input: C=1: send non-acknowledge bit (notify slave to stop communication); C=0: send acknowledge bit (notify slave to continue sending subsequent bytes);Output: A=received data, C=(non) acknowledge bit;Use: A,R4,C,R6(WaitXTm);*****/RcvByte:MOV A,#0FFH Release SDA line to allow input/*****;Name: XmByte;Describe: Send 1 byte of data (send 8 bits, receive 1 bit (non) acknowledge bit);Input: A=data to be sent, C=1 (release SDA line when detecting acknowledge bit);Output: C=1:slave non-acknowledge (slave does not respond); C=0:slave acknowledge (slave receives successfully);Use: A,R4,C,R6(WaitXTm)
You Might Like
Recommended ContentMore
Open source project More
Popular Components
Searched by Users
Just Take a LookMore
Trending Downloads
Trending ArticlesMore