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

📄 i2cslave.lis

📁 MCU控制程序
💻 LIS
📖 第 1 页 / 共 3 页
字号:
                        .module i2cslave.c
                        .area data(ram, con, rel)
 0000           _localBuffer::
 0000                   .blkb 3
                        .area idata
 0000 212100            .byte 33,33,0
                        .area data(ram, con, rel)
 0003                   .dbfile G:\McuControl\i2cslave.c
 0003                   .dbsym e localBuffer _localBuffer A[3:3]c
 0003           _localBufferLength::
 0003                   .blkb 1
                        .area idata
 0003 20                .byte 32
                        .area data(ram, con, rel)
 0004                   .dbfile G:\McuControl\i2cslave.c
 0004                   .dbsym e localBufferLength _localBufferLength c
                        .area text(rom, con, rel)
 0000                   .dbfile G:\McuControl\i2cslave.c
 0000                   .dbfunc e i2cSetBitrate _i2cSetBitrate fV
                        .even
 0000           _i2cSetBitrate::
 0000                   .dbline -1
 0000                   .dbline 56
 0000           ; //i2c.c 
 0000           ; #include <iom16v.h> 
 0000           ; #include <eeprom.h>
 0000           ; #include "i2c.h" 
 0000           ; 
 0000           ; 
 0000           ; // I2C标准波特率: 
 0000           ; // 低速 100KHz  
 0000           ; // 高速 400KHz  
 0000           ; 
 0000           ; unsigned char localBuffer[] = "!!"; 
 0000           ; unsigned char localBufferLength = 0x20; 
 0000           ; unsigned char i2csendmax;
 0000           ; 
 0000           ; 
 0000           ; unsigned char I2cSendData[I2C_SEND_DATA_BUFFER_SIZE]; 
 0000           ; unsigned char I2cSendDataIndex; 
 0000           ; unsigned char I2cSendDataLength;
 0000           ; 
 0000           ; unsigned char I2cReceiveData[I2C_RECEIVE_DATA_BUFFER_SIZE]; 
 0000           ; unsigned char I2cReceiveDataIndex; 
 0000           ; unsigned char I2cReceiveDataLength;
 0000           ; 
 0000           ; unsigned char i2creceive_b;  //接收到命令
 0000           ; unsigned char i2creadtime_b; //
 0000           ; 
 0000           ; 
 0000           ; // 指向接收处理函数的指针,当本机被选中从接收时调用函数:I2cSlaveReceive 
 0000           ; void (*i2cSlaveReceive)(unsigned char receiveDataLength, unsigned char* recieveData); 
 0000           ; // 指向发送处理函数的指针,当本机被选中从发送时调用函数:I2cSlaveTransmit  
 0000           ; unsigned char (*i2cSlaveTransmit)(unsigned char transmitDataLengthMax, unsigned char* transmitData); 
 0000           ; 
 0000           ; //设置总线速率 
 0000           ; /******************************************
 0000           ; void i2cSetBitrate(unsigned int  bitrateKHz) 
 0000           ; { 
 0000           ;    unsigned char bitrate_div; 
 0000           ;    // SCL freq = F_CPU/(16+2*TWBR)) 
 0000           ;    #ifdef TWPS0 
 0000           ;       // 对于用速率分频的AVR (mega128) 
 0000           ;       // SCL freq = F_CPU/(16+2*TWBR*4^TWPS) 
 0000           ;       // set TWPS to zero 
 0000           ;       TWSR&=~(1<<TWPS0);
 0000           ;       TWSR&=~(1<<TWPS1); 
 0000           ;    #endif 
 0000           ;    // 计算分频   CPU=7.3728M
 0000           ;    bitrate_div = ((F_CPU/1000l)/bitrateKHz); 
 0000           ;    if(bitrate_div >= 16) 
 0000           ;       bitrate_div = (bitrate_div-16)/2; 
 0000           ;           //UDR=bitrate_div;
 0000           ;           TWBR = bitrate_div; 
 0000           ;                 UDR=TWBR;
 0000           ; } **********/
 0000           ; 
 0000           ; void i2cSetBitrate(void) 
 0000           ; { 
 0000                   .dbline 57
 0000           ;       TWSR&=~(1<<TWPS0);
 0000 0898              cbi 0x1,0
 0002                   .dbline 58
 0002           ;       TWSR&=~(1<<TWPS1); 
 0002 0998              cbi 0x1,1
 0004                   .dbline -2
 0004           L2:
 0004                   .dbline 0 ; func end
 0004 0895              ret
 0006                   .dbend
 0006                   .dbfunc e i2cInit _i2cInit fV
                        .even
 0006           _i2cInit::
 0006                   .dbline -1
 0006                   .dbline 63
 0006           ; } 
 0006           ; 
 0006           ; //总线初始化 
 0006           ; void i2cInit(void) 
 0006           ; { 
 0006                   .dbline 70
 0006           ;    //设置总线上拉 
 0006           ;  //  #ifdef _MEGA128_INCLUDED_  
 0006           ;    //#ifdef _MEGA64_INCLUDED_  
 0006           ;  //  PORTD.0=1;   // i2c SCL on ATmega128,64 
 0006           ;  //  PORTD.1=1;   // i2c SDA on ATmega128,64 
 0006           ;  // #else  
 0006           ;   PORTC|=(1<<0);   // i2c SCL on ATmega163,323,16,32,等 
 0006 A89A              sbi 0x15,0
 0008                   .dbline 71
 0008           ;   PORTC|=(1<<1);   // i2c SDA on ATmega163,323,16,32,等 
 0008 A99A              sbi 0x15,1
 000A                   .dbline 74
 000A           ;  // #endif 
 000A           ;    // 清空从发送和从接受 
 000A           ;    i2cSlaveReceive = 0; 
 000A 2224              clr R2
 000C 3324              clr R3
 000E 30920300          sts _i2cSlaveReceive+1,R3
 0012 20920200          sts _i2cSlaveReceive,R2
 0016                   .dbline 75
 0016           ;    i2cSlaveTransmit = 0; 
 0016 30920100          sts _i2cSlaveTransmit+1,R3
 001A 20920000          sts _i2cSlaveTransmit,R2
 001E                   .dbline 77
 001E           ;    // 设置 i2c 波特率为 100KHz 
 001E           ;    i2cSetBitrate();// 从机模式不需要设置
 001E F0DF              xcall _i2cSetBitrate
 0020                   .dbline 79
 0020           ;    // I2C总线使能 
 0020           ;    TWCR|=1<<TWEN; //中断使能
 0020 86B7              in R24,0x36
 0022 8460              ori R24,4
 0024 86BF              out 0x36,R24
 0026                   .dbline 81
 0026           ;    // 状态设置 
 0026           ;    I2cState = I2C_IDLE; 
 0026 2224              clr R2
 0028 20922C00          sts _I2cState,R2
 002C                   .dbline 83
 002C           ;    // 开I2C中断和回应 
 002C           ;           TWCR|=1<<TWIE; 
 002C 86B7              in R24,0x36
 002E 8160              ori R24,1
 0030 86BF              out 0x36,R24
 0032                   .dbline 84
 0032           ;           TWCR|=1<<TWEA; 
 0032 86B7              in R24,0x36
 0034 8064              ori R24,64
 0036 86BF              out 0x36,R24
 0038                   .dbline 87
 0038           ;     
 0038           ;   // asm("sei"); 
 0038           ;    i2cSetLocalDeviceAddr(I2C_ADDR);//设置从地址
 0038 08E4              ldi R16,72
 003A 01D0              xcall _i2cSetLocalDeviceAddr
 003C                   .dbline -2
 003C           L3:
 003C                   .dbline 0 ; func end
 003C 0895              ret
 003E                   .dbend
 003E                   .dbfunc e i2cSetLocalDeviceAddr _i2cSetLocalDeviceAddr fV
 003E           ;     deviceAddr -> R16
                        .even
 003E           _i2cSetLocalDeviceAddr::
 003E                   .dbline -1
 003E                   .dbline 90
 003E           ; } 
 003E           ; void i2cSetLocalDeviceAddr(unsigned char deviceAddr) 
 003E           ; { 
 003E                   .dbline 93
 003E           ;  // 设置本机从地址 (从方式时) 需根据实际修改  末位强制为0,非广播方式
 003E           ;    //TWAR=(deviceAddr&0xFE)|(genCallEn?1:0);
 003E           ;     TWAR=deviceAddr&0xFE;         //地址不需要右移
 003E 802F              mov R24,R16
 0040 8E7F              andi R24,254
 0042 82B9              out 0x2,R24
 0044                   .dbline -2
 0044           L4:
 0044                   .dbline 0 ; func end
 0044 0895              ret
 0046                   .dbsym r deviceAddr 16 c
 0046                   .dbend
 0046                   .dbfunc e i2cSetSlaveReceiveHandler _i2cSetSlaveReceiveHandler fV
 0046           ; i2cSlaveRx_func -> R16,R17
                        .even
 0046           _i2cSetSlaveReceiveHandler::
 0046                   .dbline -1
 0046                   .dbline 104
 0046           ; }
 0046           ; /*****************
 0046           ; void i2cSetLocalDeviceAddr(unsigned char deviceAddr, unsigned char genCallEn) 
 0046           ; { 
 0046           ;  // 设置本机从地址 (从方式时) 需根据实际修改  末位强制为0,非广播方式
 0046           ;    //TWAR=(deviceAddr&0xFE)|(genCallEn?1:0);
 0046           ;     TWAR=deviceAddr&0xFE
 0046           ; } ****************/
 0046           ; 
 0046           ; void i2cSetSlaveReceiveHandler(void (*i2cSlaveRx_func)(unsigned char receiveDataLength, unsigned char* recieveData)) 
 0046           ; { 
 0046                   .dbline 105
 0046           ;    i2cSlaveReceive = i2cSlaveRx_func; 
 0046 10930300          sts _i2cSlaveReceive+1,R17
 004A 00930200          sts _i2cSlaveReceive,R16
 004E                   .dbline -2
 004E           L5:
 004E                   .dbline 0 ; func end
 004E 0895              ret
 0050                   .dbsym r i2cSlaveRx_func 16 pfV
 0050                   .dbend
 0050                   .dbfunc e i2cSetSlaveTransmitHandler _i2cSetSlaveTransmitHandler fV
 0050           ; i2cSlaveTx_func -> R16,R17
                        .even
 0050           _i2cSetSlaveTransmitHandler::
 0050                   .dbline -1
 0050                   .dbline 109
 0050           ; } 
 0050           ; 
 0050           ; void i2cSetSlaveTransmitHandler(unsigned char (*i2cSlaveTx_func)(unsigned char transmitDataLengthMax, unsigned char* transmitData)) 
 0050           ; { 
 0050                   .dbline 110
 0050           ;    i2cSlaveTransmit = i2cSlaveTx_func; 
 0050 10930100          sts _i2cSlaveTransmit+1,R17
 0054 00930000          sts _i2cSlaveTransmit,R16
 0058                   .dbline -2
 0058           L6:
 0058                   .dbline 0 ; func end
 0058 0895              ret
 005A                   .dbsym r i2cSlaveTx_func 16 pfc
 005A                   .dbend
 005A                   .dbfunc e i2cSendStart _i2cSendStart fV
                        .even
 005A           _i2cSendStart::
 005A                   .dbline -1
 005A                   .dbline 114
 005A           ; } 
 005A           ; 
 005A           ; void i2cSendStart(void) 
 005A           ; { 
 005A                   .dbline 116
 005A           ;     
 005A           ;   TWCR=TWCR&TWCR_CMD_MASK|(1<<TWINT)|(1<<TWSTA); 
 005A 86B7              in R24,0x36
 005C 8F70              andi R24,15
 005E 806A              ori R24,160
 0060 86BF              out 0x36,R24
 0062                   .dbline -2
 0062           L7:
 0062                   .dbline 0 ; func end
 0062 0895              ret
 0064                   .dbend
 0064                   .dbfunc e i2cSendStop _i2cSendStop fV
                        .even
 0064           _i2cSendStop::
 0064                   .dbline -1
 0064                   .dbline 120
 0064           ; } 
 0064           ; 
 0064           ; void i2cSendStop(void) 
 0064           ; { 
 0064                   .dbline 122
 0064           ;    // 发送停止条件,保持TWEA以便从接收 
 0064           ;    TWCR=TWCR&TWCR_CMD_MASK|(1<<TWINT)|(1<<TWEA)|(1<<TWSTO); 
 0064 86B7              in R24,0x36
 0066 8F70              andi R24,15
 0068 806D              ori R24,208
 006A 86BF              out 0x36,R24
 006C                   .dbline -2
 006C           L8:
 006C                   .dbline 0 ; func end
 006C 0895              ret
 006E                   .dbend
 006E                   .dbfunc e i2cWaitForComplete _i2cWaitForComplete fV
                        .even
 006E           _i2cWaitForComplete::
 006E                   .dbline -1
 006E                   .dbline 126
 006E           ; } 
 006E           ; 
 006E           ; void i2cWaitForComplete(void) 
 006E           ; { 
 006E           L10:
 006E                   .dbline 128
 006E           L11:
 006E                   .dbline 128
 006E           ;    // 等待i2c 总线操作完成 
 006E           ;    while( !(TWCR&(1<<TWINT)) ); 
 006E 26B6              in R2,0x36
 0070 27FE              sbrs R2,7
 0072 FDCF              rjmp L10
 0074                   .dbline -2
 0074           L9:
 0074                   .dbline 0 ; func end
 0074 0895              ret
 0076                   .dbend
 0076                   .dbfunc e i2cSendByte _i2cSendByte fV
 0076           ;           data -> R16
                        .even
 0076           _i2cSendByte::
 0076                   .dbline -1
 0076                   .dbline 132
 0076           ; } 
 0076           ; 
 0076           ; void i2cSendByte(unsigned char data) 
 0076           ; { 
 0076                   .dbline 134
 0076           ;    // 装载数据到 TWDR 
 0076           ;           TWDR=data; 
 0076 03B9              out 0x3,R16
 0078                   .dbline 136
 0078           ;    // 发送开始 
 0078           ;           TWCR=TWCR&TWCR_CMD_MASK|(1<<TWINT); 
 0078 86B7              in R24,0x36
 007A 8F70              andi R24,15
 007C 8068              ori R24,128
 007E 86BF              out 0x36,R24
 0080                   .dbline -2
 0080           L13:
 0080                   .dbline 0 ; func end
 0080 0895              ret
 0082                   .dbsym r data 16 c
 0082                   .dbend
 0082                   .dbfunc e i2cReceiveByte _i2cReceiveByte fV
 0082           ;        ackFlag -> R16
                        .even
 0082           _i2cReceiveByte::
 0082                   .dbline -1
 0082                   .dbline 140
 0082           ; } 
 0082           ; 
 0082           ; void i2cReceiveByte(unsigned char ackFlag) 
 0082           ; { 
 0082                   .dbline 142
 0082           ;    //开始通过 i2c 接收 
 0082           ;    if( ackFlag ) 
 0082 0023              tst R16
 0084 29F0              breq L15
 0086                   .dbline 143
 0086           ;    { 
 0086                   .dbline 145
 0086           ;       // ackFlag = TRUE: 数据接收后回应ACK  
 0086           ;        TWCR=TWCR&TWCR_CMD_MASK|(1<<TWINT)|(1<<TWEA); 
 0086 86B7              in R24,0x36
 0088 8F70              andi R24,15
 008A 806C              ori R24,192
 008C 86BF              out 0x36,R24
 008E                   .dbline 146
 008E           ;    } 
 008E 04C0              xjmp L16
 0090           L15:
 0090                   .dbline 148
 0090                   .dbline 150
 0090 86B7              in R24,0x36
 0092 8F70              andi R24,15

⌨️ 快捷键说明

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