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

📄 i2cslave.s

📁 MCU控制程序
💻 S
📖 第 1 页 / 共 2 页
字号:
	.module i2cslave.c
	.area data(ram, con, rel)
_localBuffer::
	.blkb 3
	.area idata
	.byte 33,33,0
	.area data(ram, con, rel)
	.dbfile G:\McuControl\i2cslave.c
	.dbsym e localBuffer _localBuffer A[3:3]c
_localBufferLength::
	.blkb 1
	.area idata
	.byte 32
	.area data(ram, con, rel)
	.dbfile G:\McuControl\i2cslave.c
	.dbsym e localBufferLength _localBufferLength c
	.area text(rom, con, rel)
	.dbfile G:\McuControl\i2cslave.c
	.dbfunc e i2cSetBitrate _i2cSetBitrate fV
	.even
_i2cSetBitrate::
	.dbline -1
	.dbline 56
; //i2c.c 
; #include <iom16v.h> 
; #include <eeprom.h>
; #include "i2c.h" 
; 
; 
; // I2C标准波特率: 
; // 低速 100KHz  
; // 高速 400KHz  
; 
; unsigned char localBuffer[] = "!!"; 
; unsigned char localBufferLength = 0x20; 
; unsigned char i2csendmax;
; 
; 
; unsigned char I2cSendData[I2C_SEND_DATA_BUFFER_SIZE]; 
; unsigned char I2cSendDataIndex; 
; unsigned char I2cSendDataLength;
; 
; unsigned char I2cReceiveData[I2C_RECEIVE_DATA_BUFFER_SIZE]; 
; unsigned char I2cReceiveDataIndex; 
; unsigned char I2cReceiveDataLength;
; 
; unsigned char i2creceive_b;  //接收到命令
; unsigned char i2creadtime_b; //
; 
; 
; // 指向接收处理函数的指针,当本机被选中从接收时调用函数:I2cSlaveReceive 
; void (*i2cSlaveReceive)(unsigned char receiveDataLength, unsigned char* recieveData); 
; // 指向发送处理函数的指针,当本机被选中从发送时调用函数:I2cSlaveTransmit  
; unsigned char (*i2cSlaveTransmit)(unsigned char transmitDataLengthMax, unsigned char* transmitData); 
; 
; //设置总线速率 
; /******************************************
; void i2cSetBitrate(unsigned int  bitrateKHz) 
; { 
;    unsigned char bitrate_div; 
;    // SCL freq = F_CPU/(16+2*TWBR)) 
;    #ifdef TWPS0 
;       // 对于用速率分频的AVR (mega128) 
;       // SCL freq = F_CPU/(16+2*TWBR*4^TWPS) 
;       // set TWPS to zero 
;       TWSR&=~(1<<TWPS0);
;       TWSR&=~(1<<TWPS1); 
;    #endif 
;    // 计算分频   CPU=7.3728M
;    bitrate_div = ((F_CPU/1000l)/bitrateKHz); 
;    if(bitrate_div >= 16) 
;       bitrate_div = (bitrate_div-16)/2; 
; 	    //UDR=bitrate_div;
;           TWBR = bitrate_div; 
; 		  UDR=TWBR;
; } **********/
; 
; void i2cSetBitrate(void) 
; { 
	.dbline 57
;       TWSR&=~(1<<TWPS0);
	cbi 0x1,0
	.dbline 58
;       TWSR&=~(1<<TWPS1); 
	cbi 0x1,1
	.dbline -2
L2:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e i2cInit _i2cInit fV
	.even
_i2cInit::
	.dbline -1
	.dbline 63
; } 
; 
; //总线初始化 
; void i2cInit(void) 
; { 
	.dbline 70
;    //设置总线上拉 
;  //  #ifdef _MEGA128_INCLUDED_  
;    //#ifdef _MEGA64_INCLUDED_  
;  //  PORTD.0=1;   // i2c SCL on ATmega128,64 
;  //  PORTD.1=1;   // i2c SDA on ATmega128,64 
;  // #else  
;   PORTC|=(1<<0);   // i2c SCL on ATmega163,323,16,32,等 
	sbi 0x15,0
	.dbline 71
;   PORTC|=(1<<1);   // i2c SDA on ATmega163,323,16,32,等 
	sbi 0x15,1
	.dbline 74
;  // #endif 
;    // 清空从发送和从接受 
;    i2cSlaveReceive = 0; 
	clr R2
	clr R3
	sts _i2cSlaveReceive+1,R3
	sts _i2cSlaveReceive,R2
	.dbline 75
;    i2cSlaveTransmit = 0; 
	sts _i2cSlaveTransmit+1,R3
	sts _i2cSlaveTransmit,R2
	.dbline 77
;    // 设置 i2c 波特率为 100KHz 
;    i2cSetBitrate();// 从机模式不需要设置
	xcall _i2cSetBitrate
	.dbline 79
;    // I2C总线使能 
;    TWCR|=1<<TWEN; //中断使能
	in R24,0x36
	ori R24,4
	out 0x36,R24
	.dbline 81
;    // 状态设置 
;    I2cState = I2C_IDLE; 
	clr R2
	sts _I2cState,R2
	.dbline 83
;    // 开I2C中断和回应 
;           TWCR|=1<<TWIE; 
	in R24,0x36
	ori R24,1
	out 0x36,R24
	.dbline 84
;           TWCR|=1<<TWEA; 
	in R24,0x36
	ori R24,64
	out 0x36,R24
	.dbline 87
;     
;   // asm("sei"); 
;    i2cSetLocalDeviceAddr(I2C_ADDR);//设置从地址
	ldi R16,72
	xcall _i2cSetLocalDeviceAddr
	.dbline -2
L3:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e i2cSetLocalDeviceAddr _i2cSetLocalDeviceAddr fV
;     deviceAddr -> R16
	.even
_i2cSetLocalDeviceAddr::
	.dbline -1
	.dbline 90
; } 
; void i2cSetLocalDeviceAddr(unsigned char deviceAddr) 
; { 
	.dbline 93
;  // 设置本机从地址 (从方式时) 需根据实际修改  末位强制为0,非广播方式
;    //TWAR=(deviceAddr&0xFE)|(genCallEn?1:0);
;     TWAR=deviceAddr&0xFE;         //地址不需要右移
	mov R24,R16
	andi R24,254
	out 0x2,R24
	.dbline -2
L4:
	.dbline 0 ; func end
	ret
	.dbsym r deviceAddr 16 c
	.dbend
	.dbfunc e i2cSetSlaveReceiveHandler _i2cSetSlaveReceiveHandler fV
; i2cSlaveRx_func -> R16,R17
	.even
_i2cSetSlaveReceiveHandler::
	.dbline -1
	.dbline 104
; }
; /*****************
; void i2cSetLocalDeviceAddr(unsigned char deviceAddr, unsigned char genCallEn) 
; { 
;  // 设置本机从地址 (从方式时) 需根据实际修改  末位强制为0,非广播方式
;    //TWAR=(deviceAddr&0xFE)|(genCallEn?1:0);
;     TWAR=deviceAddr&0xFE
; } ****************/
; 
; void i2cSetSlaveReceiveHandler(void (*i2cSlaveRx_func)(unsigned char receiveDataLength, unsigned char* recieveData)) 
; { 
	.dbline 105
;    i2cSlaveReceive = i2cSlaveRx_func; 
	sts _i2cSlaveReceive+1,R17
	sts _i2cSlaveReceive,R16
	.dbline -2
L5:
	.dbline 0 ; func end
	ret
	.dbsym r i2cSlaveRx_func 16 pfV
	.dbend
	.dbfunc e i2cSetSlaveTransmitHandler _i2cSetSlaveTransmitHandler fV
; i2cSlaveTx_func -> R16,R17
	.even
_i2cSetSlaveTransmitHandler::
	.dbline -1
	.dbline 109
; } 
; 
; void i2cSetSlaveTransmitHandler(unsigned char (*i2cSlaveTx_func)(unsigned char transmitDataLengthMax, unsigned char* transmitData)) 
; { 
	.dbline 110
;    i2cSlaveTransmit = i2cSlaveTx_func; 
	sts _i2cSlaveTransmit+1,R17
	sts _i2cSlaveTransmit,R16
	.dbline -2
L6:
	.dbline 0 ; func end
	ret
	.dbsym r i2cSlaveTx_func 16 pfc
	.dbend
	.dbfunc e i2cSendStart _i2cSendStart fV
	.even
_i2cSendStart::
	.dbline -1
	.dbline 114
; } 
; 
; void i2cSendStart(void) 
; { 
	.dbline 116
;     
;   TWCR=TWCR&TWCR_CMD_MASK|(1<<TWINT)|(1<<TWSTA); 
	in R24,0x36
	andi R24,15
	ori R24,160
	out 0x36,R24
	.dbline -2
L7:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e i2cSendStop _i2cSendStop fV
	.even
_i2cSendStop::
	.dbline -1
	.dbline 120
; } 
; 
; void i2cSendStop(void) 
; { 
	.dbline 122
;    // 发送停止条件,保持TWEA以便从接收 
;    TWCR=TWCR&TWCR_CMD_MASK|(1<<TWINT)|(1<<TWEA)|(1<<TWSTO); 
	in R24,0x36
	andi R24,15
	ori R24,208
	out 0x36,R24
	.dbline -2
L8:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e i2cWaitForComplete _i2cWaitForComplete fV
	.even
_i2cWaitForComplete::
	.dbline -1
	.dbline 126
; } 
; 
; void i2cWaitForComplete(void) 
; { 
L10:
	.dbline 128
L11:
	.dbline 128
;    // 等待i2c 总线操作完成 
;    while( !(TWCR&(1<<TWINT)) ); 
	in R2,0x36
	sbrs R2,7
	rjmp L10
	.dbline -2
L9:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e i2cSendByte _i2cSendByte fV
;           data -> R16
	.even
_i2cSendByte::
	.dbline -1
	.dbline 132
; } 
; 
; void i2cSendByte(unsigned char data) 
; { 
	.dbline 134
;    // 装载数据到 TWDR 
;           TWDR=data; 
	out 0x3,R16
	.dbline 136
;    // 发送开始 
;           TWCR=TWCR&TWCR_CMD_MASK|(1<<TWINT); 
	in R24,0x36
	andi R24,15
	ori R24,128
	out 0x36,R24
	.dbline -2
L13:
	.dbline 0 ; func end
	ret
	.dbsym r data 16 c
	.dbend
	.dbfunc e i2cReceiveByte _i2cReceiveByte fV
;        ackFlag -> R16
	.even
_i2cReceiveByte::
	.dbline -1
	.dbline 140
; } 
; 
; void i2cReceiveByte(unsigned char ackFlag) 
; { 
	.dbline 142
;    //开始通过 i2c 接收 
;    if( ackFlag ) 
	tst R16
	breq L15
	.dbline 143
;    { 
	.dbline 145
;       // ackFlag = TRUE: 数据接收后回应ACK  
;        TWCR=TWCR&TWCR_CMD_MASK|(1<<TWINT)|(1<<TWEA); 
	in R24,0x36
	andi R24,15
	ori R24,192
	out 0x36,R24
	.dbline 146
;    } 
	xjmp L16
L15:
	.dbline 148
	.dbline 150
	in R24,0x36
	andi R24,15
	ori R24,128
	out 0x36,R24
	.dbline 151
L16:
	.dbline -2
L14:
	.dbline 0 ; func end
	ret
	.dbsym r ackFlag 16 c
	.dbend
	.dbfunc e i2cGetReceivedByte _i2cGetReceivedByte fc
	.even
_i2cGetReceivedByte::
	.dbline -1
	.dbline 155
;    else 
;    { 
;       // ackFlag = FALSE: 数据接收后无回应 
;        TWCR=TWCR&TWCR_CMD_MASK|(1<<TWINT); 
;    } 
; } 
; 
; unsigned char i2cGetReceivedByte(void) 
; { 
	.dbline 157
;    // 返回接收到的数据 
;    return( TWDR ); 
	in R16,0x3
	.dbline -2
L17:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e i2cGetStatus _i2cGetStatus fc
	.even
_i2cGetStatus::
	.dbline -1
	.dbline 161
; } 
; 
; unsigned char i2cGetStatus(void) 
; { 
	.dbline 163
;    // 返回总线状态 
;    return(TWSR); 
	in R16,0x1
	.dbline -2
L18:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e i2cSlaveReceiveService _i2cSlaveReceiveService fV
;              i -> R20
;    receiveData -> R18,R19
; receiveDataLength -> R16
	.even
_i2cSlaveReceiveService::
	xcall push_gset1
	.dbline -1
	.dbline 167
; } 
; // 从操作 
; void i2cSlaveReceiveService(unsigned char receiveDataLength, unsigned char* receiveData) 
; { 
	.dbline 171
;    unsigned char i; 
;   //此函数在本机被选中为从写入时运行 
;    // 接收到的数据存入本地缓冲区 
;    for(i=0; i<receiveDataLength; i++) 
	clr R20
	xjmp L23
L20:
	.dbline 172
	.dbline 173
	ldi R24,<_localBuffer
	ldi R25,>_localBuffer
	mov R30,R20
	clr R31
	add R30,R24
	adc R31,R25
	movw R26,R18
	ld R2,X+
	movw R18,R26
	std z+0,R2
	.dbline 174
L21:
	.dbline 171
	inc R20
L23:
	.dbline 171
	cp R20,R16
	brlo L20
	.dbline 175
;    { 
;       localBuffer[i] = *receiveData++; 
;    } 
;    localBufferLength = receiveDataLength; //接收的数据个数
	sts _localBufferLength,R16
	.dbline -2
L19:
	xcall pop_gset1
	.dbline 0 ; func end
	ret
	.dbsym r i 20 c
	.dbsym r receiveData 18 pc
	.dbsym r receiveDataLength 16 c
	.dbend
	.dbfunc e i2cSlaveTransmitService _i2cSlaveTransmitService fc
;              i -> R20
;   transmitData -> R18,R19
; transmitDataLengthMax -> R16
	.even
_i2cSlaveTransmitService::
	xcall push_gset1
	.dbline -1
	.dbline 180
; 
; } 
; 
; unsigned char i2cSlaveTransmitService(unsigned char transmitDataLengthMax, unsigned char* transmitData) 
; { 
	.dbline 185
;    unsigned char i; 
; 
;    //此函数在本机被选中为从读出时运行 
;    //要发送的数据存入发送缓冲区 
;    for(i=0; i<localBufferLength; i++) 
	clr R20
	xjmp L28
L25:
	.dbline 186
	.dbline 187
	ldi R24,<_localBuffer
	ldi R25,>_localBuffer
	mov R30,R20
	clr R31
	add R30,R24
	adc R31,R25
	ldd R2,z+0
	movw R30,R18
	st Z+,R2
	movw R18,R30
	.dbline 188
L26:
	.dbline 185
	inc R20
L28:
	.dbline 185
	lds R2,_localBufferLength
	cp R20,R2
	brlo L25
	.dbline 190
;    { 
;       *transmitData++ = localBuffer[i]; 
;    } 
; 
;    localBuffer[0]++; 
	lds R24,_localBuffer
	subi R24,255    ; addi 1
	sts _localBuffer,R24
	.dbline 192
; 
;    return localBufferLength; 
	mov R16,R2
	.dbline -2
L24:
	xcall pop_gset1
	.dbline 0 ; func end
	ret
	.dbsym r i 20 c
	.dbsym r transmitData 18 pc
	.dbsym r transmitDataLengthMax 16 c
	.dbend
	.dbfunc e i2cGetState _i2cGetState fc
	.even
_i2cGetState::
	.dbline -1

⌨️ 快捷键说明

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