IIC ,讀取正常,但一直寫不進去 |
缺席
|
fangsnan
一般會員 發表:2 回覆:1 積分:0 註冊:2005-04-29 發送簡訊給我 |
大家好!我遇到一個怪問題,一直無法解決,我用 IIC 將幾個 Byte 資料寫入 24C02 EEPROM ,之前讀寫都OK,但這兩天卻怎麼也寫不進去,但讀取卻是正常的,請大家幫幫忙!
/*****************************/ //Wait for some time to get proper I2C timing void I2cWait(void) { _nop_( ); _nop_( ); } //Initialize I2C interface //Release I2C bus void I2cInit(void) { ISDA=1; //define I/O=P1.1 ISCL=1; //define I/O=P1.0 } /*****************************/ void I2cStart(void) { ISDA=1; ISCL=1; I2cWait( ); ISDA=0; I2cWait( ); ISCL=0; } void I2cStop(void) { ISDA=0; I2cWait( ); ISCL=1; I2cWait( ); ISDA=1; } /*****************************/ //master transfer data to slave //and return acknowledge bit Bool I2cSentByte(Byte bytedata) { Byte i; Bool ack; ISDA=1; for(i=0; i<8; i ) { //_crol_:include bytedata=_crol_(bytedata,1); if (bytedata & 0x01) ISDA=1; else ISDA=0; ISCL=1; I2cWait( ); ISCL=0; I2cWait( ); } ISDA=1; I2cWait( ); ISCL=1; I2cWait( ); ack=ISDA; ISCL=0; I2cWait( ); return ack; } /*****************************/ // slave transfer data to master Byte I2cReceiveByte(void) { Byte i; Byte bytedata=0; // Receive byte (MSB first) for(i=0; i<8; i ) { ISCL=1; I2cWait( ); bytedata <<= 1; if ( ISDA ) bytedata |= 0x01; ISCL=0; I2cWait( ); } return bytedata; } /*****************************/ //Master send acknowledge bit to slave //acknowledge="0",non-acknowledge="1" void SendAcknowledge(Bool ack) { ISDA=ack; ISCL=1; I2cWait( ); ISCL=0; } /*****************************/ //do 1 times,and un-check acknowledge void I2cByteWrite(Byte device,Byte address,Byte bytedata) { I2cStart( ); I2cSentByte(device); I2cSentByte(address); I2cSentByte(bytedata); I2cStop( ); DelayX1ms(10); } /*****************************/ Byte I2cByteRead(Byte device,Byte address) { Byte bytedata; I2cStart( ); I2cSentByte(device); I2cSentByte(address); I2cStart( ); I2cSentByte(device|0x01); bytedata=I2cReceiveByte( ); SendAcknowledge(1); I2cStop( ); return bytedata; } /*****************************/ //master transfer count data to slave Bool I2cSentData(Byte bytecnt) { Byte i; for(i=0; i if ( I2cSentByte (TrmBuf[i]) == FAILURE ) return FAILURE; } return SUCCESS; } /*****************************/ //slave transfer count data to master Bool I2cReceiveData(Byte bytecnt) { Byte i; for(i=0; i TrmBuf[i]=I2cReceiveByte( ); SendAcknowledge(0); } TrmBuf[i]=I2cReceiveByte( ); SendAcknowledge(1); I2cStop( ); return SUCCESS; } /*****************************/ void DataSetBit(Byte device,Byte addr,Byte bitno) { Byte temp; temp=I2cByteRead(device,addr); temp |= (0x01< } /*****************************/ void DataClearBit(Byte device,Byte addr,Byte bitno) { Byte temp; temp=I2cByteRead(device,addr); temp &= ~(0x01< } |
taishyang
站務副站長 發表:377 回覆:5490 積分:4563 註冊:2002-10-08 發送簡訊給我 |
|
fangsnan
一般會員 發表:2 回覆:1 積分:0 註冊:2005-04-29 發送簡訊給我 |
|
taishyang
站務副站長 發表:377 回覆:5490 積分:4563 註冊:2002-10-08 發送簡訊給我 |
|
stan0718
一般會員 發表:0 回覆:1 積分:0 註冊:2007-05-23 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |