新會員
Joined
: 2008/9/2 14:36 最後登入時間
: 2008/11/18 15:06
Group:
註冊會員
Level : 2 HP : 0 / 40 MP : 5 / 1909 EXP : 62
|
請問如何在程式 加入 Address Match 的機制 [Master 端]
if (!I2C_ACKPolling(0xA0)) { fs_value0 = EEPROM_ByteRead(0xA1 , 0x50); fs_value1 = EEPROM_ByteRead(0xA1 , 0x50); fs_value2 = (fs_value1 << 8) | (fs_value0);
** int EEPROM_ByteRead(unsigned char CMD , unsigned char Addr ) { unsigned char Temp_Buffer,Temp_Buffer1,Temp_Buffer2,t1,t2,t3 ; unsigned ReadDelay ; IdleI2C( ) ; StartI2C( ) ; while ( I2CCONbits.SEN) ; IFS0bits.MI2CIF = 0 ; MasterWriteI2C((CMD & 0xFE)) ; // Mask bit 0 -> Write command while(! IFS0bits.MI2CIF ) ; if ( I2CSTATbits.ACKSTAT ) REPORT_NACK IFS0bits.MI2CIF = 0 ; MasterWriteI2C(Addr) ; while(! IFS0bits.MI2CIF ) ;
if ( I2CSTATbits.ACKSTAT ) REPORT_NACK
IFS0bits.MI2CIF = 0 ; RestartI2C( ) ; while ( I2CCONbits.RSEN) ;
IFS0bits.MI2CIF = 0 ; MasterWriteI2C((CMD | 0x01)) ; // Set bit 0 for "Read" command while(! IFS0bits.MI2CIF ) ;
if ( I2CSTATbits.ACKSTAT ) REPORT_NACK
IFS0bits.MI2CIF = 0 ;
Temp_Buffer = MasterReadI2C( ) ;//read 1 byte t1=Temp_Buffer; while(! IFS0bits.MI2CIF ) ;
IFS0bits.MI2CIF = 0 ; NotAckI2C( ) ; while(! IFS0bits.MI2CIF ) ; IFS0bits.MI2CIF = 0 ; ************* #define SLAVE_ADDRESS (0xA0 >> 1) // I2C module compares bit<0..6> of I2CADD with bit<1..7> of I2C Command Byte
int EEPROM_ByteRead(unsigned char , unsigned char ) ; int EEPROM_ByteWrite(unsigned char , unsigned char , unsigned char); int I2C_ByteWrite(unsigned char CMD , unsigned char Addr , unsigned char Data); void Init_I2C(void) ; unsigned char I2C_ACKPolling(unsigned char ) ; ************************************
[Slave 端] ***************** #define SLAVE_ADDRESS (0xA0 >> 1)
// I2C module compares bit<0..6> of I2CADD with bit<1..7> of I2C Command Byte
int EEPROM_ByteRead(unsigned char , unsigned char ) ; int EEPROM_ByteWrite(unsigned char , unsigned char , unsigned char ) ; void Init_I2C(void) ; unsigned char I2C_ACKPolling(unsigned char ) ;
******************* void _ISR _SI2CInterrupt(void) { IFS0bits.SI2CIF = 0 ; DummyByte = I2CRCV ; I2CSTATbits.I2COV = 0;
if (!I2CSTATbits.D_A && I2CSTATbits.S ) // address & stop bit { I2CState = 0 ; // Reset I2C State !! if (I2CSTATbits.R_W ) // Read Command { if(t==0) { I2CTRN =AD15v; t++; } if(t==3) { I2CTRN = AD15v>>8; } if(t==4) t=0; t++; if(t==1) t=0; Nop( ); I2CCONbits.SCLREL = 1; // Release the clock }else // Write Command {
I2CState = 1 ; Nop( ); //I2CBuf[I2CIndexAddress]=I2CRCV; } } else if ( I2CSTATbits.P ) // 如果是 STOP , 直接將 I2CState 清除後結束 { Nop(); I2CState = 0 ;
}//while (WaitLoop < 1000) ; else // Write Command { if ( I2CState == 1 ) // Internal address byte { I2CState = 2 ; I2CIndexAddress = DummyByte ; if ( I2CIndexAddress >= 5 ) I2CIndexAddress = 5 ; }
else if ( I2CState >= 2 ) { I2CBuf2[i++] = DummyByte ; if (i>6)i=0; // tmp_2=DummyByte ; } else Nop(); } }
發表於: 2008/11/10 10:31
Edited by kindpan on 2008年11月10日 11:22:25
|