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

📄 i2cslave.s

📁 MCU控制程序
💻 S
📖 第 1 页 / 共 2 页
字号:
	.dbline 197
; } 
; 
; /**********************************************/
; eI2cStateType i2cGetState(void) 
; { 
	.dbline 198
;    return I2cState; 
	lds R16,_I2cState
	.dbline -2
L29:
	.dbline 0 ; func end
	ret
	.dbend
	.area vector(rom, abs)
	.org 68
	jmp _twi_isr
	.area text(rom, con, rel)
	.dbfile G:\McuControl\i2cslave.c
	.dbfunc e twi_isr _twi_isr fV
;         status -> R20
	.even
_twi_isr::
	xcall push_lset
	xcall push_gset1
	.dbline -1
	.dbline 205
; }  
; 
; // I2C (TWI) 中断服务程序 
; //interrupt [TWI] void twi_isr(void) 
; #pragma interrupt_handler twi_isr:18
; void twi_isr(void)
; { 
	.dbline 208
;    //读状态位 
;    unsigned char status; 
;           status = TWSR & TWSR_STATUS_MASK; 
	in R20,0x1
	andi R20,248
	.dbline 209
;    switch(status) 
	clr R21
	cpi R20,136
	ldi R30,0
	cpc R21,R30
	brne X3
	xjmp L38
X3:
	ldi R24,136
	ldi R25,0
	cp R24,R20
	cpc R25,R21
	brge X4
	xjmp L50
X4:
L49:
	cpi R20,104
	ldi R30,0
	cpc R21,R30
	brne X5
	xjmp L34
X5:
	ldi R24,104
	ldi R25,0
	cp R24,R20
	cpc R25,R21
	brlt L52
L51:
	cpi R20,0
	cpc R20,R21
	brne X6
	xjmp L48
X6:
X0:
	cpi R20,0
	ldi R30,0
	cpc R21,R30
	brge X7
	xjmp L31
X7:
L53:
	cpi R20,96
	ldi R30,0
	cpc R21,R30
	brne X8
	xjmp L34
X8:
	xjmp L31
L52:
	cpi R20,120
	ldi R30,0
	cpc R21,R30
	brne X9
	xjmp L34
X9:
	ldi R24,120
	ldi R25,0
	cp R24,R20
	cpc R25,R21
	brlt L55
L54:
	cpi R20,112
	ldi R30,0
	cpc R21,R30
	brne X10
	xjmp L34
X10:
	xjmp L31
L55:
	cpi R20,128
	ldi R30,0
	cpc R21,R30
	brne X11
	xjmp L35
X11:
	xjmp L31
L50:
	cpi R20,184
	ldi R30,0
	cpc R21,R30
	brne X12
	xjmp L43
X12:
	ldi R24,184
	ldi R25,0
	cp R24,R20
	cpc R25,R21
	brlt L57
L56:
	cpi R20,168
	ldi R30,0
	cpc R21,R30
	brne X13
	xjmp L40
X13:
	ldi R24,168
	ldi R25,0
	cp R24,R20
	cpc R25,R21
	brlt L59
L58:
	cpi R20,160
	ldi R30,0
	cpc R21,R30
	brne X14
	xjmp L39
X14:
	xjmp L31
L59:
	cpi R20,176
	ldi R30,0
	cpc R21,R30
	brne X15
	xjmp L40
X15:
	xjmp L31
L57:
	cpi R20,200
	ldi R30,0
	cpc R21,R30
	brne X16
	xjmp L46
X16:
	ldi R24,200
	ldi R25,0
	cp R24,R20
	cpc R25,R21
	brlt L61
L60:
	cpi R20,192
	ldi R30,0
	cpc R21,R30
	brne X17
	xjmp L46
X17:
	xjmp L31
L61:
	cpi R20,248
	ldi R30,0
	cpc R21,R30
	brne X18
	xjmp L32
X18:
	xjmp L31
X1:
	.dbline 210
;    { 
L34:
	.dbline 222
;    
; /********************************************/
;    // 从接收状态码 
;    case TW_SR_SLA_ACK:               // 0x60: 自己的SLA+W 已经被接收,ACK 已返回 
;    case TW_SR_ARB_LOST_SLA_ACK:// 0x68: SLA+R/W 作为主机的仲裁失败;自己的SLA+W 已经被接收,ACK 已返回 
;    case TW_SR_GCALL_ACK:            // 0x70: 接收到广播地址,ACK 已返回 
;    case TW_SR_ARB_LOST_GCALL_ACK: // 0x78: SLA+R/W 作为主机的仲裁失败;接收到广播地址,ACK 已返回 
;     
;       // 被选中为从写入 (数据将从主机接收) 
;       // 设置状态 
;       //UDR=0x66;
;       I2cState = I2C_SLAVE_RX; 
	ldi R24,5
	sts _I2cState,R24
	.dbline 224
;       // 缓冲准备 
;       I2cReceiveDataIndex = 0; 
	clr R2
	sts _I2cReceiveDataIndex,R2
	.dbline 226
; 	  // 接收数据,回应 ACK 
;       TWCR=TWCR&TWCR_CMD_MASK|(1<<TWINT)|(1<<TWEA); 
	in R24,0x36
	andi R24,15
	ori R24,192
	out 0x36,R24
	.dbline 227
;       break; 
	xjmp L32
L35:
	.dbline 233
; //----------------------------读取数据,有应答位-----------------------	  
;    case TW_SR_DATA_ACK:            // 0x80: 以前以自己的 SLA+W 被寻址;数据已经被接收,ACK 已返回 
; //   case TW_SR_GCALL_DATA_ACK:   // 0x90: 以前以广播方式被寻址;数据已经被接收,ACK 已返回 
;        
;        
;       I2cReceiveData[I2cReceiveDataIndex++] = TWDR;
	lds R2,_I2cReceiveDataIndex
	clr R3
	mov R24,R2
	subi R24,255    ; addi 1
	sts _I2cReceiveDataIndex,R24
	ldi R24,<_I2cReceiveData
	ldi R25,>_I2cReceiveData
	mov R30,R2
	clr R31
	add R30,R24
	adc R31,R25
	in R2,0x3
	std z+0,R2
	.dbline 237
; 	  //UDR=I2cReceiveData[I2cReceiveDataIndex-1];
; 	
;       //检查接收缓冲区状态 是否溢出
;       if(I2cReceiveDataIndex < 7)// 
	lds R24,_I2cReceiveDataIndex
	cpi R24,7
	brsh L36
	.dbline 238
;       { 
	.dbline 240
;          // 接收数据,回应 ACK 
;          i2cReceiveByte(TRUE); 
	ldi R16,1
	xcall _i2cReceiveByte
	.dbline 242
; 		 
;       } 
	xjmp L32
L36:
	.dbline 244
;       else 
;       { 
	.dbline 246
;          // 接收数据,回应 NACK 
;          i2cReceiveByte(FALSE); 
	clr R16
	xcall _i2cReceiveByte
	.dbline 248
; 		 
;       } 
	.dbline 249
;       break; 
	xjmp L32
L38:
	.dbline 255
; //########################################################################	  
;    case TW_SR_DATA_NACK:            // 0x88: 以前以自己的 SLA+W 被寻址;数据已经被接收,NOT ACK 已返回 
; //   case TW_SR_GCALL_DATA_NACK:   // 0x98: 以前以广播方式被寻址;数据已经被接收,NOT ACK 已返回 
;     
;       // 接收数据,回应 NACK 
;       i2cReceiveByte(FALSE); 
	clr R16
	xcall _i2cReceiveByte
	.dbline 256
;       break; 
	xjmp L32
L39:
	.dbline 259
; //---------------------无应答位------------------------------------------	  
;    case TW_SR_STOP:               // 0xA0: 在以从机工作时接收到STOP或重复START 
;     TWCR=TWCR&TWCR_CMD_MASK|(1<<TWINT)|(1<<TWEA); 
	in R24,0x36
	andi R24,15
	ori R24,192
	out 0x36,R24
	.dbline 264
;       // i2c 接收完成 
; //      if(i2cSlaveReceive)  
; //                    i2cSlaveReceive(I2cReceiveDataIndex, I2cReceiveData); 
;       // 设置状态 
;       I2cState = I2C_IDLE;           //i2c接受结束,可以处理数据
	clr R2
	sts _I2cState,R2
	.dbline 265
; 	  i2creceive_b=1;                //i2c命令处理申请
	ldi R24,1
	sts _i2creceive_b,R24
	.dbline 266
; 	  break; 
	xjmp L32
L40:
	.dbline 275
; 
;    // ---------------------从发送------------------------------------ 
;    case TW_ST_SLA_ACK:               // 0xA8: 自己的SLA+R 已经被接收,ACK 已返回 
;    case TW_ST_ARB_LOST_SLA_ACK:// 0xB0: SLA+R/W 作为主机的仲裁失败;自己的SLA+R 已经被接收,ACK 已返回 
;     
;       // 被选中为从读出 (数据将从传回主机) 
;       // 设置状态 
; 	  //UDR=0x65;
;       I2cState = I2C_SLAVE_TX;
	ldi R24,4
	sts _I2cState,R24
	.dbline 289
; 	 /* if(catt[0]==catt[1])
; 	  {
; 	    I2cReceiveData[0]=catt[0];
; 	  }
; 	  else if(catt[0]==catt[2])
; 	  {I2cReceiveData[0]=catt[0];
; 	  }
; 	  else if(catt[1]==catt[2])
; 	  {I2cReceiveData[0]=catt[2];
; 	  }*/ 
; 	  // I2cSendData[0]=catt;
;       // 数据请求 需要发送的字节长度
;      // if(i2cSlaveTransmit) I2cSendDataLength = i2cSlaveTransmit(I2C_SEND_DATA_BUFFER_SIZE, I2cSendData); //更改发送数据长度
;       I2cSendDataIndex = 0;
	clr R2
	sts _I2cSendDataIndex,R2
	.dbline 290
; 	  if(i2creadtime_b)
	lds R2,_i2creadtime_b
	tst R2
	breq L41
	.dbline 291
; 			{
	.dbline 292
; 			  i2csendmax=7; //发送时间数据7字节
	ldi R24,7
	sts _i2csendmax,R24
	.dbline 293
; 			}
	xjmp L42
L41:
	.dbline 294
; 	  else  {
	.dbline 295
; 	          i2csendmax=3;//正常发送3字节
	ldi R24,3
	sts _i2csendmax,R24
	.dbline 296
; 		    }	
L42:
L43:
	.dbline 301
; 	//  I2cSendData[0]=EEPROMread(0xff);
;    case TW_ST_DATA_ACK:            // 0xB8: TWDR 里数据已经发送,接收到ACK 
;     
;       // 发送数据位 
; 	         TWDR=I2cSendData[I2cSendDataIndex++];
	lds R2,_I2cSendDataIndex
	clr R3
	mov R24,R2
	subi R24,255    ; addi 1
	sts _I2cSendDataIndex,R24
	ldi R24,<_I2cSendData
	ldi R25,>_I2cSendData
	mov R30,R2
	clr R31
	add R30,R24
	adc R31,R25
	ldd R2,z+0
	out 0x3,R2
	.dbline 307
; 			// UDR=I2cSendData[I2cSendDataIndex-1];
; 			// if(I2cSendDataIndex==1) 
; 			 //{
; 			  //I2cSendData[0]=catt;
; 			 // UDR=0x55;//}
; 	  PORTA|=0x80;//PA7置高,取消申请
	sbi 0x1b,7
	.dbline 308
; 	  if(I2cSendDataIndex <  i2csendmax) //发送字节
	lds R2,_i2csendmax
	lds R3,_I2cSendDataIndex
	cp R3,R2
	brsh L44
	.dbline 310
;          // 回应 ACK 
;                 TWCR=TWCR&TWCR_CMD_MASK|(1<<TWINT)|(1<<TWEA); 
	in R24,0x36
	andi R24,15
	ori R24,192
	out 0x36,R24
	xjmp L45
L44:
	.dbline 313
;       else 
;          // 回应 NACK 
;                 TWCR=TWCR&TWCR_CMD_MASK|(1<<TWINT);
	in R24,0x36
	andi R24,15
	ori R24,128
	out 0x36,R24
L45:
	.dbline 314
; 				i2creadtime_b=0; 
	clr R2
	sts _i2creadtime_b,R2
	.dbline 315
;       break; 
	xjmp L32
L46:
	.dbline 323
; 	//##################################################################  
;    case TW_ST_DATA_NACK:            // 0xC0: TWDR 里数据已经发送接收到NOT ACK 
;    case TW_ST_LAST_DATA:            // 0xC8: TWDR 的一字节数据已经发送(TWAE = “0”);接收到ACK 
;        
;       // 全部完成 
;       // 从方式开放 
; 	  //UDR=0xff;
;       TWCR=TWCR&TWCR_CMD_MASK|(1<<TWINT)|(1<<TWEA); 
	in R24,0x36
	andi R24,15
	ori R24,192
	out 0x36,R24
	.dbline 325
;       // 设置状态 
;       I2cState = I2C_IDLE; 
	clr R2
	sts _I2cState,R2
	.dbline 328
; 	  //i2creceive_b=1;
; 	  //UDR=0x55;
; 	  i2creadtime_b=0; 
	sts _i2creadtime_b,R2
	.dbline 330
; 	
;       break; 
	xjmp L32
X2:
	.dbline 336
; 
; 
;    case TW_NO_INFO:                 // 0xF8: 没有相关的状态信息;TWINT = “0” 
;       // 无操作 
;        
;       break; 
L48:
	.dbline 340
;    case TW_BUS_ERROR:               // 0x00: 由于非法的START 或STOP 引起的总线错误 
;     
;       // 内部硬件复位,释放总线 
;       TWCR=TWCR&TWCR_CMD_MASK|(1<<TWINT)|(1<<TWSTO)|(1<<TWEA); 
	in R24,0x36
	andi R24,15
	ori R24,208
	out 0x36,R24
	.dbline 342
;       // 设置状态 
;       I2cState = I2C_IDLE; 
	clr R2
	sts _I2cState,R2
	.dbline 344
; 	  //i2creceive_b=1;
;       break; 
L31:
L32:
	.dbline -2
L30:
	xcall pop_gset1
	xcall pop_lset
	.dbline 0 ; func end
	reti
	.dbsym r status 20 c
	.dbend
	.area bss(ram, con, rel)
	.dbfile G:\McuControl\i2cslave.c
_i2cSlaveTransmit::
	.blkb 2
	.dbsym e i2cSlaveTransmit _i2cSlaveTransmit pfc
_i2cSlaveReceive::
	.blkb 2
	.dbsym e i2cSlaveReceive _i2cSlaveReceive pfV
_i2creadtime_b::
	.blkb 1
	.dbsym e i2creadtime_b _i2creadtime_b c
_i2creceive_b::
	.blkb 1
	.dbsym e i2creceive_b _i2creceive_b c
_I2cReceiveDataLength::
	.blkb 1
	.dbsym e I2cReceiveDataLength _I2cReceiveDataLength c
_I2cReceiveDataIndex::
	.blkb 1
	.dbsym e I2cReceiveDataIndex _I2cReceiveDataIndex c
_I2cReceiveData::
	.blkb 16
	.dbsym e I2cReceiveData _I2cReceiveData A[16:16]c
_I2cSendDataLength::
	.blkb 1
	.dbsym e I2cSendDataLength _I2cSendDataLength c
_I2cSendDataIndex::
	.blkb 1
	.dbsym e I2cSendDataIndex _I2cSendDataIndex c
_I2cSendData::
	.blkb 16
	.dbsym e I2cSendData _I2cSendData A[16:16]c
_i2csendmax::
	.blkb 1
	.dbsym e i2csendmax _i2csendmax c
_I2cDeviceAddrRW:
	.blkb 1
	.dbfile G:\McuControl/i2c.h
	.dbsym s I2cDeviceAddrRW _I2cDeviceAddrRW c
_I2cState:
	.blkb 1
	.dbsym s I2cState _I2cState c

⌨️ 快捷键说明

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