線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2184
推到 Plurk!
推到 Facebook!

IIC ,讀取正常,但一直寫不進去

缺席
fangsnan
一般會員


發表:2
回覆:1
積分:0
註冊:2005-04-29

發送簡訊給我
#1 引用回覆 回覆 發表時間:2006-12-07 10:48:08 IP:59.125.xxx.xxx 未訂閱
大家好!我遇到一個怪問題,一直無法解決,我用 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< I2cByteWrite(device,addr,temp);
}
/*****************************/
void DataClearBit(Byte device,Byte addr,Byte bitno)
{
Byte temp;
temp=I2cByteRead(device,addr);
temp &= ~(0x01< I2cByteWrite(device,addr,temp);
}

編輯記錄
taishyang 重新編輯於 2007-05-23 11:58:27, 註解 將文章分類成[問題]‧
taishyang 重新編輯於 2007-05-23 12:04:10, 註解 無‧
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#2 引用回覆 回覆 發表時間:2006-12-07 12:53:10 IP:59.124.xxx.xxx 未訂閱
你要不要用示波器看一下Timming是否正確? 會不會是硬體的問題?
fangsnan
一般會員


發表:2
回覆:1
積分:0
註冊:2005-04-29

發送簡訊給我
#3 引用回覆 回覆 發表時間:2006-12-11 15:36:17 IP:59.125.xxx.xxx 未訂閱
我用示波器看了,結果發現,讀入資料時,資料會「一直」不斷的寫,持續有訊號輸出,就算我在程式中下了 while(1),也是一樣,不知這是什麼狀況?!
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#4 引用回覆 回覆 發表時間:2006-12-11 16:38:55 IP:59.124.xxx.xxx 未訂閱
你可以將程式Free Run後停下來,看看卡在哪個Loop裡面出不來就知道了
stan0718
一般會員


發表:0
回覆:1
積分:0
註冊:2007-05-23

發送簡訊給我
#5 引用回覆 回覆 發表時間:2007-05-23 11:50:05 IP:59.125.xxx.xxx 訂閱
你可否整個程式傳給我~
我這樣才看的出你那有問題,你po上去的都是副程式~
有時有要看其他因為,有可能對它呼叫的有問題

我的信箱:tsung_han_h@yahoo.com.tw
系統時間:2024-05-19 23:49:11
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!