51 MCU drives I2C (24C02) assembly language program

Publisher:心满意足Latest update time:2015-08-26 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
;Program description: Write the contents of 55h~58h inside the MPU to 24c02 via the I2C bus from external interrupt 0, and then read the written contents to 
;MPU internal data memory 60H~~63H 
 
SCL EQU P1.1  
SDA EQU P1.0  
org 0000h 
jmp begin 
org 0003H 
ljmp write 
org 0013H 
ljmp read 
 
;Main program 
begin: 
mov 55H,#41H 
mov 56h,#42h 
MOV 57h,#43H 
MOV 58H, #44H 
mov tcon,#0 
setb ex0 
setb ex1 
setb of 
main: 
nop 
jmp main 
 
;********************************************************** 
;Program to read 24C02 data 
read:     
ACALL START ; start signal 
MOV A,#0A0H ; send the slave address 
ACALL SEND ;Send control word 
ACALL ACKI; receive response 
JC OUTEND ; No response, transfer to end 
MOV A,#00H; Set the internal address of the slave device to be read 
ACALL SEND 
ACALL ACKI 
JC OUTEND 
ACALL START ; start signal 
ORL A,#0A1H; Set the control word and prepare to receive 
ACALL SEND ;Send control word 
ACALL ACKI 
JC OUTEND 
MOV R2,#4 ; Prepare to receive the number of bytes of data 
MOV R1,#60H ; The received byte is stored at address 60H in the master device 
IN0: 
ACALL REV ; receive a byte 
ACALL ACKO; the host sends a response signal to 24C02 
MOV @R1,A ; store the received data in the storage unit 
INC R1 
DEC R2 
CJNE R2,#1,IN0 ; Determine whether it is the last received byte 
ACALL REV ;Receive the last byte 
ACALL ACKN; 
MOV @R1,A ; store the received data in the storage unit 
OUTEND: 
ACALL STOP ;Stop signal 
jnb p3.3,$ 
rarely 
 
 
;********************************************************** 
;Program to write 24C02 data 
write: 
LCALL START ; start signal 
MOV A,#0A0H; set control word, ready to send 
ACALL SEND ;Send control word 
ACALL ACKI ;Receive acknowledgment 
JC WRITEEND ; The slave device does not respond, and the transfer ends 
MOV A,#00H ; Set the internal address of the slave device 
ACALL SEND ;Send the internal address 
ACALL ACKI 
JC WRITEEND 
MOV R2,#4; Set the number of bytes to send 
MOV R1,#55H ;Header address of data to be sent in the master device 
OUT1: 
MOV A,@R1 
ACALL SEND; send data 
ACALL ACKI; 
JC WRITEEND 
INC R1 
DJNZ R2,OUT1 ;Sending is not finished, continue sending 
WRITEEND: 
ACALL STOP ;Stop signal 
jnb p3.2,$; prevent further interruption 
rarely 
;******************************************************** 
;******************** 
;I2C simulation program 
; Start signal 
START: 
CLR SCL  
SETB SDA 
ACALL DELAY   
SETB SCL 
ACALL DELAY  
CLR SDA  
ACALL DELAY   
CLR SCL  
RIGHT  
; Stop signal: 
 STOP:CLR SCL  
 CLR SDA  
 ACALL DELAY  
 SETB SCL  
 ACALL DELAY  
 SETB SDA  
 ACALL DELAY  
 RIGHT  
; Receive a byte 
 REV: MOV R0,#08H  
 REV0: SETB SCL  
 LCALL DELAY 
 MOV C,SDA 
 RLC A  
 CLR SCL  
 LCALL DELAY 
 DJNZ R0,REV0  
 RIGHT  
; Send a byte  
SEND: 
MOV R0, #08H  
CLR SCL  
SEND0: 
RLC A   
MOV SDA,C   
SETB SCL   
ACALL DELAY  
CLR SCL  
DJNZ R0,SEND0 
SETB SDA  
RIGHT  
; Receive an acknowledge bit: 
AKI: 
SETB SCL   
ACALL DELAY   
MOV C,SDA  
CLR SCL  
RIGHT  
; Send a receive acknowledge bit 
 ACKO: 
 CLR SDA 
 ACALL DELAY 
 SETB SCL 
  ACALL DELAY 
 CLR SCL 
  ACALL DELAY 
 SETB SDA 
  ACALL DELAY 
 RIGHT  
 ;Send a non-receive receive acknowledge bit 
 ACKN: 
 SETB SDA 
 ACALL DELAY 
 SETB SCL 
 ACALL DELAY 
 CLR SCL 
 ACALL DELAY 
DELAY: 
NOP 
NOP 
NOP 
RIGHT 
END
Reference address:51 MCU drives I2C (24C02) assembly language program

Previous article:LCD1602 LCD screen password lock assembly program for 51 single chip microcomputer
Next article:51 MCU serial port test C language program

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号