⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dt-107-005cn.txt

📁 8051单片机使用ASM对IIC器件访问的底层函数
💻 TXT
📖 第 1 页 / 共 2 页
字号:
;******************************************************************************
;**
;**  DESCRIPTION:
;**
;**  This file contains general utility routines, written in the 80C51 assembly
;**  language, which are used to interface the 80C51 to standard two-wire(IIC)
;**  Serial Products. The interface between the 80C51 and IIC Device consists 
;**  of a clock (SCL) and a bidirectional data line (SDA). 
;**  The communication interface uses 2 pins from Port 1(P10 = SCL and P11 = SDA).
;**  The following table lists all the subroutines in this file with a brief
;**  description:
;**
;**          Start: Generate the start condition
;**          Stop: Generate the stop condition
;**          ResetIIC: Issues the appropriate commands to force device reset
;**          ProgPage: Transfer multible bytes from RAM buffer to IIC Device
;**          ProgByte: Transfer byte from RAM buffer to IIC Device
;**          SeqRead: Read multiple bytes, starting from current address pointer
;**          RandomRead: Read a byte from a specific memory location
;**          ACKPoll: Return when the write cycle completes.
;**          OutACK: Process the acknowledge output cycle
;**          GetACK: process the acknowledge from the slave device
;**
;**	   NOTE 1. These rountines can run at 12.0MHz, While IIC Maxspeed is 400K
;**             2. The IIC Device must based on 16-bits address, so the sequence:
;**
;**         START   Slave  Address  Address   Data    STOP
;**              \XXXXXXXW AAAAAAAA AAAAAAAA DDDDDDDD /
;**               	  ACK      ACK      ACK      ACK
;**
;******************************************************************************
;******************************************************************************
;*                           PROGRAM CONSTANTS
;******************************************************************************
SDAbit      EQU     00H         ; PORT-1 BITS FUNCTIONING AS BIDIRECTIONAL
SCLbit      EQU     01H         ; SERIAL DATA (SDA) AND SERIAL CLOCK OUTPUT (SCL)

MaxDelay    EQU     0FFH        ; NUMBER OF TIMES TO CHECK ACKNOWLEDGE POLLING

DeviceID    EQU     6FH         ; DEVICE SLAVE ADDRESS
                                ; X1208\1209 SLAVE ADDRESS: 1101111B

;*******************************************************************************
;******************************************************************************
;** Name: SeqRead
;** Description: Read sequentially from the IIC Device.
;** Function: This subroutine extracts contents of the IIC Device and stores
;**            them into the specified RAM buffer. The total number of bytes to
;**            read should be provided along with the buffer address. This
;**            routine assumes that the address pointer has already been
;**            initialized using the RandomRead routine.
;**                 !!!!!!!!!!   RETURN FROM StopRead   !!!!!!!!!!
;**
;** Calls:              Start, SlavAddr, InByte, StopRead
;** Input:              R0 = RAM Buffer Base Address
;**                     R1 = Number of bytes to read
;** Output:             None
;** Register Usage:     A, R0, R1
;******************************************************************************
      ORG     0400H                   ; IIC.ASM MAPPED IN: 400~4ACH

SeqRead:
	acall   Start                   ; START
	setb    c                       ; [C=1] READ OPERATION BIT
	acall   SlavAddr                ; SEND THE SLAVE ADDRESS BYTE
SeqReadLoop:
	acall   InByte                  ; START READING FROM THE CURRENT ADDRESS
	mov     @R0,A                   ; TOTAL NUMBER OF BYTES TO READ OUT OF
	inc     R0                      ; IIC Device
	djnz    R1,SeqReadNxt
	ajmp    StopRead                ; END OF READ OPERATION
SeqReadNxt:
	acall   OutACK                  ; SEND AN ACKNOWLEDGE TO THE DEVICE
	ajmp    SeqReadLoop

;******************************************************************************
;** Name: RandomRead
;** Description: Reads content of the IIC Device at a specific location.
;** Function: This subroutine sends out the command to read the content of a
;**             memory location specified in the DPTR.
;**                 !!!!!!!!!!   RETURN FROM StopRead   !!!!!!!!!!
;**
;** Calls:              Start, InByte, SlavAddr, OutByte StopRead
;** Input:              DPTR = Address of the byte
;** Output:             A = Read value
;** Register Usage:     A
;******************************************************************************
RandomRead:
	acall   Start                   ; START
	clr     C                       ; [C=0] WRITE OPERATION BIT
	acall   SlavAddr                ; SEND THE SLAVE ADDRESS BYTE
	mov     A,DPH                   ; LOAD THE UPPER BYTE OF THE ADDRESS
	acall   OutByte                 ; ADDRESS SHIFT OUT TO THE DEVICE
	mov     A,DPL                   ; LOAD THE LOWER BYTE OF THE PAGE
	acall   OutByte                 ; ADDRESS SHIFT OUT TO THE DEVICE
	acall   Start                   ; START
	setb    C                       ; [C=1] READ OPERATION BIT
	acall   SlavAddr                ; SEND THE SLAVE ADDRESS BYTE
	acall   InByte                  ; SHIFT IN A BYTE FROM THE DEVICE
	ajmp    StopRead                ; END OPERATION

;******************************************************************************
;** Name: StopRead
;** Description: Terminate read operation.
;** Function: This subroutine sends out the command to end reading content of a
;**             IIC Device.
;**                 !!!!!!!!!!   RETURN FROM Stop   !!!!!!!!!!
;** Calls:              ClockPulse, Stop
;** Input:              None
;** Output:             None
;** Register Usage:     None
;******************************************************************************
StopRead:
	setb    P1.SDAbit               ; MAKE SURE THAT THE DATA LINE IS HIGH
	acall   ClockPulse
	jmp     Stop                    ; END OPERATION

;******************************************************************************
;** Name: ProgPage
;** Description: Update a page of the Serial Memory.
;** Function: This subroutine transfers the contents of the given buffer to the
;**            Serial Memory. The caller program must supply the page number
;**            of the Serial Memory to update and the base address of the
;**            RAM buffer.
;**                 !!!!!!!!!!   RETURN FROM Stop   !!!!!!!!!!
;** Calls:              Start, SlavAddr, OutByte, Stop
;** Input:              R0 = RAM Buffer Base Address, DPTR = Address of programm
;**                     R1 = Number of bytes to write
;** Output:             None
;** Register Usage:     A, R0, R1
;******************************************************************************

ProgPage:
	acall   Start                   ; START
	clr     C                       ; [C=0] WRITE OPERATION BIT
	acall   SlavAddr                ; SEND THE SLAVE ADDRESS BYTE
	mov     A,DPH                   ; LOAD THE HIGHER BYTE OF ADDRESS
      acall   OutByte
	mov     A,DPL                   ; LOAD THE LOWER BYTE OF ADDRESS
	acall   OutByte                 ; AND SHIFT OUT TO THE DEVICE
ProgPageNxt:
	mov     A,@R0                   ; TO THE Serial MEMORY
	acall   OutByte                 ; R0 SHOULD BE POINTING TO THE BUFFER
	inc     R0                      ; TOTAL NUMBER OF BYTES TRANSFERRED
	djnz    R1,ProgPageNxt          ; TO THE Serial SHOULD NOT EXCEED
      				        ; THE PAGE SIZE
	ajmp    Stop                    ; END OF THE OPERATION

;******************************************************************************
;** Name: ProgByte
;** Description: Write a byte to IIC Device.
;** Function: This subroutine transfers the contents of Acc to the IIC
;**            Device. The upper byte of the word address  is contained in 
;**            (DPH) and the lower byte is contained in (DPL).  A two byte 
;**            address is required for both page and byte write commands.
;**                 !!!!!!!!!!   RETURN FROM Stop   !!!!!!!!!!
;**
;** Calls:              Start, SlavAddr, Outbyte, Stop
;** Input:              Acc = byte to be written, DPTR = Address of programm
;** Output:             None
;** Register Usage:     B
;******************************************************************************
ProgByte:
	xch     A,B 	              ; SAVE DATA BYTE
	acall   Start                   ; START
	clr     c                       ; [C=0] WRITEOPERATION BIT
	acall   SlavAddr                ; SEND THE SLAVE ADDRESS BYTE
	mov     A,DPH                   ; LOAD THE UPPER BYTE OF THE WORD ADDRESS
	acall   OutByte                 ; SEND OUT THE LOWER BYTE OF THE ADDRESS
	mov     A,DPL                   ; LOAD THE LOWER BYTE OF THE WORD ADDRESS
	acall   OutByte                 ; SEND OUT THE LOWER BYTE OF THE ADDRESS
	xch     A,B                     ; LOAD THE DATA TO BE SENT IN THE ACC
	acall   OutByte                 ; SEND OUT THE DATA TO THE SERIAL MEMORY
	ajmp    Stop                    ; END OF THE OPERATION                  

;*******************************************************************************
;** Name: SlavAddr
;** Description: Build the slave address for the IIC Device.
;** Function: This subroutine concatenates the bit fields for Device ID,
;**            the high address bits and the command bit. The resultant
;**            byte is then transmitted to the IIC Device.
;**                 !!!!!!!!!!    RETURN FROM OutByte   !!!!!!!!!!
;** Calls:              OutByte
;** Input:              C = COMMAND BIT (=0 WRITE, =1 READ)
;** Output:             None
;** Register Usage:     A
;*******************************************************************************
SlavAddr:
	mov     A,dID			  ; IF IS FIXED DEVICE USE:'#DeviceID' 
	rlc     A                       ; MERGE THE COMMAND BIT
	ajmp    OutByte                 ; SEND THE SLAVE ADDRESS

;*******************************************************************************
;** Name: OutByte
;** Description: Sends a byte to the IIC Device
;** Function: This subroutine shifts out a byte, MSB first, through the
;**             assigned SDA/SCL lines on port 1.
;**                 !!!!!!!!!!   RETURN FROM GetACK    !!!!!!!!!!!
;** Calls:              ClockPulse, GetACK
;** Input:              A = Byte to be sent
;** Return Value:       None
;** Register Usage:     A

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -