MOV P4M1, #00H
MOV P5M0, #00H
MOV P5M1, #00H
MOV P_SW2,#80H
MOV A,#10000001B ;Enable I2C slave mode
MOV DPTR,#I2CCFG
MOVX @DPTR,A
MOV A,#01011010B ;Set the slave device address to 5A
;That is, I2CSLADR[7:1]=010_1101B, MA=0B.
;Since MA is 0, the device address sent by the host must be
; the same as I2CSLADR[7:1] to access this I2C slave device.
;If the host needs to write data, it will send 5AH(0101_1010B)
MOV DPTR,#I2CSLADR
MOVX @DPTR,A
MOV A,#00000000B
MOV DPTR,#I2CSLST
MOVX @DPTR,A
MOV A,#01111000B ;Enable interrupt of slave mode
MOV DPTR,#I2CSLCR
MOVX @DPTR,A
SETB ISDA ;User variable initialization
SETB ISMA
CLR A
MOV ADDR,A
MOV R0,A
MOVX A,@R0
MOV DPTR,#I2CTXD
MOVX @DPTR,A
SETB EA
SJMP $
END
21.5.5 I
2
C Slave Mode (Polling Mode)
C language code
//Operating frequency for test is 11.0592MHz
#include "reg51.h"
#include "intrins.h"
sfr P_SW2 = 0xba;
#define I2CCFG (*(unsigned char volatile xdata *)0xfe80)
#define I2CMSCR (*(unsigned char volatile xdata *)0xfe81)
#define I2CMSST (*(unsigned char volatile xdata *)0xfe82)
#define I2CSLCR (*(unsigned char volatile xdata *)0xfe83)
#define I2CSLST (*(unsigned char volatile xdata *)0xfe84)
#define I2CSLADR (*(unsigned char volatile xdata *)0xfe85)