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

📄 i2c_demo.s

📁 本人改编和测试过的用于AVR mega128关于I2C总线的应用实例--使用EEPROM的读写方法。采用ICCAVR编译器实验。
💻 S
字号:
	.module I2C_Demo.c
	.area text(rom, con, rel)
	.dbfile E:\MyWorks\SOFT\I2C_E2PROM\I2C_Demo.c
	.dbfunc e EEPROM_Write _EEPROM_Write fV
;           Data -> y+5
;          M_ADD -> R10,R11
;          H_ADD -> R12
	.even
_EEPROM_Write::
	xcall push_xgset003C
	movw R10,R18
	mov R12,R16
	sbiw R28,1
	.dbline -1
	.dbline 72
; // ***************   I2C driver Demo V 1.0   *************
; // IO模拟方式的I2C接口操作
; // *******************************************************
; 
; #include <iom1281v.h>
; #include <MACROS.h>
; 
; #include <DC_Defines.h>
; 
; #include "I2C.h"
; 
; 
; 
; void EEPROM_Write(unsigned char, unsigned int, unsigned char);
; unsigned char EEPROM_Read(unsigned char, unsigned int);
; 
; 
; // **************************************************************** //
; // ***                     Init_I2C();							*** //
; // *** This routine will setup the I2C port direction registers *** //
; // **************************************************************** //
; 
; // ***************************************************************** //
; // ***                     I2C_Start();	 		  		 	   	 *** //
; // *** This routine will set the I2C start condition on the bus, *** //
; // *** All commands must be preceded by a START condition 		 *** //
; // ***************************************************************** //
; 
; // ***************************************************************** //
; // ***                     I2C_Stop();	 		  		 	   	 *** //
; // *** This routine will set the I2C stop condition on the bus,  *** //
; // *** All commands must end with a STOP condition 		         *** //
; // ***************************************************************** //
; 
; // *********************************************************************** //
; // ***                Write_I2C_Control(0x0A,0,0);	  		  	       *** //
; // *** This routine will write the I2C device code, the device address *** //
; // *** setup on the hardware pins A0,A1 & A2, and also the W/R bit     *** //
; // *** So for an external EEPROM, such as the 24LC04B you would need   *** //
; // *** a device code of 1010 (0x0A),   	  	  		  	  			   *** //
; // *** hardware address 0 (if pins A0,A1 & A2 are left unconnected,    *** //
; // *** and the last parameter is R/W. Write is active low			   *** //
; // *********************************************************************** //
; 
; // *********************************************************************** //
; // ***                          I2C_Ackn();							   *** //
; // *** This routine will clock the ACK bit from the I2C slave device   *** //
; // *** it will return TRUE for a fail, and FALSE for a correct ACK bit *** //
; // *********************************************************************** //
; 
; // ************************************************************** //
; // ***                     Write_I2C_Byte();				  *** //
; // *** This routine will clock a byte to the slave I2C device *** //
; // ************************************************************** //
; 
; // ************************************************************************** //
; // ***                       Read_I2C_Byte();					          *** //
; // *** This routine will read and return a byte from the I2C slave device *** //
; // ************************************************************************** //
; 
; 
; 
; // *********************************************************************** //
; // ***                 Example of using Imagecraft I2C driver          *** //
; // ***              to write to an external 8 bit address EEPROM       *** //
; // *** H_ADD is the hardware address set on the device A0,A1 & A2 pins *** //
; // *** M_ADD is the devices internal memory address                    *** //
; // *** Data is user data to be writen 								   *** //
; // *********************************************************************** //
; 
; void EEPROM_Write(unsigned char H_ADD, unsigned int M_ADD, unsigned char Data)
; {
	.dbline 73
;  I2C_Start();			   			   // Set I2C start condition
	xcall _I2C_Start
	.dbline 75
; 
;  Write_I2C_Control(0x0A,H_ADD,0);	   // Send the EEPROM control Byte
	clr R2
	std y+0,R2
	mov R18,R12
	ldi R16,10
	xcall _Write_I2C_Control
	.dbline 77
; 
;  Write_I2C_Byte(M_ADD>>8);				   // Send the EEPROM internal Address higher byte.
	movw R16,R10
	mov R16,R17
	clr R17
	xcall _Write_I2C_Byte
	.dbline 79
;  
;  Write_I2C_Byte(M_ADD);            // Send the EEPROM internal Address lower byte.
	mov R16,R10
	xcall _Write_I2C_Byte
	.dbline 81
;  
;  Write_I2C_Byte(Data);				   // Send the EEPROM Data
	ldd R16,y+5
	xcall _Write_I2C_Byte
	.dbline 83
;  
;  I2C_Stop();	  					   // Set I2C Stop condition
	xcall _I2C_Stop
	.dbline -2
L1:
	.dbline 0 ; func end
	adiw R28,1
	xjmp pop_xgset003C
	.dbsym l Data 5 c
	.dbsym r M_ADD 10 i
	.dbsym r H_ADD 12 c
	.dbend
	.dbfunc e EEPROM_Read _EEPROM_Read fc
;           Temp -> R10
;          M_ADD -> R12,R13
;          H_ADD -> R10
	.even
_EEPROM_Read::
	xcall push_xgset003C
	movw R12,R18
	mov R10,R16
	sbiw R28,1
	.dbline -1
	.dbline 97
; }
; 
; 
; 
; // *********************************************************************** //
; // ***              Example of using Imagecraft I2C driver             *** //
; // ***            to Read an external 8 bit address EEPROM             *** //
; // *** H_ADD is the hardware address set on the device A0,A1 & A2 pins *** //
; // *** M_ADD is the devices internal memory address                    *** //
; // *** Data is user data to be writen 								   *** //
; // *********************************************************************** //
; 
; unsigned char EEPROM_Read(unsigned char H_ADD, unsigned int M_ADD)
; {
	.dbline 100
;  unsigned char Temp;			   		// Temp RAM for EEPROM Read
;  
;  I2C_Start();			   			   // Set I2C start condition
	xcall _I2C_Start
	.dbline 102
; 
;  Write_I2C_Control(0x0A,H_ADD,0);	   // Send the EEPROM control Byte
	clr R2
	std y+0,R2
	mov R18,R10
	ldi R16,10
	xcall _Write_I2C_Control
	.dbline 105
;  									   // Dummy write to set address
; 
;  Write_I2C_Byte(M_ADD>>8);				// Send the EEPROM internal Address Higher byte.
	movw R16,R12
	mov R16,R17
	clr R17
	xcall _Write_I2C_Byte
	.dbline 106
;  Write_I2C_Byte(M_ADD);     // Send the EEPROM internal Address lower byte.
	mov R16,R12
	xcall _Write_I2C_Byte
	.dbline 108
;  
;  I2C_Start();			   			   // Set I2C start condition
	xcall _I2C_Start
	.dbline 110
;  
;  Write_I2C_Control(0x0A,H_ADD,1);	   // Send the EEPROM control Byte
	ldi R24,1
	std y+0,R24
	mov R18,R10
	ldi R16,10
	xcall _Write_I2C_Control
	.dbline 112
;  
;  Temp = Read_I2C_Byte();			   // Read data from EEPROM
	xcall _Read_I2C_Byte
	mov R10,R16
	.dbline 114
;  
;  I2C_Stop();	  					   // Set I2C Stop condition
	xcall _I2C_Stop
	.dbline 116
; 
;  return Temp;						   // Return data from EEPROM
	mov R16,R10
	.dbline -2
L2:
	.dbline 0 ; func end
	adiw R28,1
	xjmp pop_xgset003C
	.dbsym r Temp 10 c
	.dbsym r M_ADD 12 i
	.dbsym r H_ADD 10 c
	.dbend
	.dbfunc e main _main fV
;              k -> R12
;           data -> y+1
;              j -> R10
;              i -> R22,R23
;              c -> R20,R21
	.even
_main::
	sbiw R28,2
	.dbline -1
	.dbline 125
; }
; 
; 
; 
; 
; //AT24C512B使用2BYTE片内地址。
; unsigned char bf[26];
; void main (void)
; {
	.dbline 131
;   unsigned int i;
;   unsigned char j, k;
;   unsigned int c;			  // Temp Ram used for write delay
;   volatile unsigned char data;
;   
;   DDRG |=BIT(PG1);
	sbi 0x13,1
	.dbline 132
;   PORTG |=BIT(PG1); //VCC =high.
	sbi 0x14,1
	.dbline 134
;   
;   I2C_DIR |= BIT(PD0)|BIT(PD1);  	 			  // Set Port B to Outputs
	in R24,0xa
	ori R24,3
	out 0xa,R24
	.dbline 136
;   
;   Init_I2C();	   			  // Setup the hardware port
	xcall _Init_I2C
	xjmp L5
L4:
	.dbline 139
;   
;   while (true)
;   {
	.dbline 140
;     for (k=0,i=0x1050, j='A'; 1; i++,j++ )
	clr R12
	ldi R22,4176
	ldi R23,16
	ldi R24,65
	mov R10,R24
	xjmp L10
L7:
	.dbline 141
;     {
	.dbline 142
;     	EEPROM_Write( 1 , i , j );
	std y+0,R10
	movw R18,R22
	ldi R16,1
	xcall _EEPROM_Write
	.dbline 144
;     	
;     	for(c=0;c<5000;c++);		  // Delay for EEPROM Write
	clr R20
	clr R21
	xjmp L14
L11:
	.dbline 144
L12:
	.dbline 144
	subi R20,255  ; offset = 1
	sbci R21,255
L14:
	.dbline 144
	cpi R20,136
	ldi R30,19
	cpc R21,R30
	brlo L11
X0:
	.dbline 146
;     	
;     	data = EEPROM_Read( 1, i );	  // Read device 1, memory address 1
	movw R18,R22
	ldi R16,1
	xcall _EEPROM_Read
	std y+1,R16
	.dbline 147
;     	bf[k++] =data;
	mov R2,R12
	clr R3
	mov R24,R2
	subi R24,255    ; addi 1
	mov R12,R24
	ldi R24,<_bf
	ldi R25,>_bf
	mov R30,R2
	clr R31
	add R30,R24
	adc R31,R25
	mov R2,R16
	std z+0,R2
	.dbline 148
;     	if (j=='Z') break;
	mov R24,R10
	cpi R24,90
	brne L15
X1:
	.dbline 148
	xjmp L9
L15:
	.dbline 150
;     	
;     }
L8:
	.dbline 140
	subi R22,255  ; offset = 1
	sbci R23,255
	inc R10
L10:
	.dbline 140
	xjmp L7
L9:
	.dbline 152
;     
;     for(c=0;c<5000;c++);		  // Delay for EEPROM Write
	clr R20
	clr R21
	xjmp L20
L17:
	.dbline 152
L18:
	.dbline 152
	subi R20,255  ; offset = 1
	sbci R21,255
L20:
	.dbline 152
	cpi R20,136
	ldi R30,19
	cpc R21,R30
	brlo L17
X2:
	.dbline 153
;   }
L5:
	.dbline 138
	xjmp L4
X3:
	.dbline -2
L3:
	.dbline 0 ; func end
	adiw R28,2
	ret
	.dbsym r k 12 c
	.dbsym l data 1 c
	.dbsym r j 10 c
	.dbsym r i 22 i
	.dbsym r c 20 i
	.dbend
	.area bss(ram, con, rel)
	.dbfile E:\MyWorks\SOFT\I2C_E2PROM\I2C_Demo.c
_bf::
	.blkb 26
	.dbsym e bf _bf A[26:26]c
; 
; }

⌨️ 快捷键说明

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