I2c data reading problem of MC9S12XHZ512

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

I2c data reading problem of MC9S12XHZ512

跳至解决方案
2,382 次查看
SARY
Contributor III

hi

   I have interfaced RTC (I2C based) to above microcontroller.I am able to write data into RTC but can read only once.

After reading data once it goes to busy state and then don't read or write,

Although I have set repaet start condition.I am using code for reading is like.

 

IIC0_IBCR_TXAK = 0;
IIC0_IBCR |= 0x30;

IIC0_IBDR = M41T00_ID;
while (!IIC0_IBSR_IBIF);
IIC0_IBSR_IBIF=1;
while(IIC0_IBSR_RXAK);


IIC0_IBDR = Address;
while (!IIC0_IBSR_IBIF);
IIC0_IBSR_IBIF=1;
while(IIC0_IBSR_RXAK);
IIC0_IBCR_RSTA = 1; 


IIC0_IBDR = 0xD1;
while (!IIC0_IBSR_IBIF);
IIC0_IBSR_IBIF=1;
while (IIC0_IBSR_RXAK);

 

 IIC0_IBCR_TX_RX = 0;
 IIC0_IBCR_TXAK = 1; 
RD_data = IIC0_IBDR;
 while (!IIC0_IBSR_IBIF);
 IIC0_IBSR_IBIF=1; 


  RD_data = IIC0_IBDR;
 IIC0_IBCR_MS_SL = 0;
 IIC0_IBDR=0x00;

 

 

Anyone has any idea,please let me know.

Thanks in advance

SARY

标签 (1)
0 项奖励
回复
1 解答
1,058 次查看
SARY
Contributor III

hi

   thanks for ur reply.the changes you said also works in the same manner as earlier.

Anyway ,problem has been solved. it was just because of last I2c stop command I m sending.

If we want to read data again and again.then ,in the read function we should not use

// IIC0_IBCR_MS_SL = 0;

After ,commenting above line its working fine.

Thanks once again

SARY

在原帖中查看解决方案

0 项奖励
回复
4 回复数
1,058 次查看
kef
Specialist I

IIC0_IBCR_TXAK = 0;


IIC0_IBSR_IBIF=1;


IIC0_IBCR |= 0x30;

IIC0_IBDR = M41T00_ID;
while (!IIC0_IBSR_IBIF);
IIC0_IBSR_IBIF=1;

 

if(IIC0_IBSR_RXAK) error;

 

IIC0_IBDR = Address;
while (!IIC0_IBSR_IBIF);
IIC0_IBSR_IBIF=1;

 

if(IIC0_IBSR_RXAK) error;

 

IIC0_IBCR_RSTA = 1;

IIC0_IBDR = M41T00_ID | 1;
while (!IIC0_IBSR_IBIF);
IIC0_IBSR_IBIF=1;

 

if(IIC0_IBSR_RXAK) error;

 

IIC0_IBCR_TX_RX = 0;


RD_data = IIC0_IBDR;   // dummy read to start data transfer from slave
while (!IIC0_IBSR_IBIF);

IIC0_IBCR_MS_SL = 0; //stop

RD_data = IIC0_IBDR; // read data

0 项奖励
回复
1,059 次查看
SARY
Contributor III

hi

   thanks for ur reply.the changes you said also works in the same manner as earlier.

Anyway ,problem has been solved. it was just because of last I2c stop command I m sending.

If we want to read data again and again.then ,in the read function we should not use

// IIC0_IBCR_MS_SL = 0;

After ,commenting above line its working fine.

Thanks once again

SARY

0 项奖励
回复
1,058 次查看
kef
Specialist I

1) IBIF flag stays set until you clear it. Now have a look at starting lines of your code

 

IIC0_IBCR_TXAK = 0;
IIC0_IBCR |= 0x30;

IIC0_IBDR = M41T00_ID;
while (!IIC0_IBSR_IBIF);   
<-- IBIF was set probably in previous IIC transfer. It won't wait second time.

 

2) while(RXAK); doesn't make sense at all. RXAK isn't something you can wait for. After Tx transfer is complete and maybe while IBIF is set, you can check RXAK to see if receiving node acknowledged your transfer or not:

 

3) Shouldn't RSTA be issued after you switch direction from Tx to Rx? I see you are sending address, then RSTA, then sending 0xD1, then changing direction.

 

4) In master mode, read from data register initiates transfer. You should ignore data first time read from data register. Also look at

 

  RD_data = IIC0_IBDR;
 IIC0_IBCR_MS_SL = 0;


This doesn't look right. You initiate transfer and immediately are trying to generate stop. If this is the last byte you want to read and generate stop, then you should first generate stop condition and then read data register.

 

 IIC0_IBCR_MS_SL = 0;
  RD_data = IIC0_IBDR;

0 项奖励
回复
1,058 次查看
SARY
Contributor III

hi Kef

  thanks for your response.

But I want to explain the points as you mentioned ,

1. IBIF flags automatically set to zero ,and I m clearing it by writting 1 to it(in the same way as it can be done )

   This seems to be fine.

2.As far as RXAK is concerned ,I am just checking it for acknowledgement  purpose.To confirm that data has been received.

3.I  changed RSTA in the way you said.I set it '1 ' after sending 0xD1.

But,then it does not read data at all.

4.As you mentioned the last point ,I have made those changes .

IIC0_IBCR_MS_SL = 0;
 RD_data = IIC0_IBDR;

but its working in the same manner.

But ignoring data first time does not help,it does not read data at all.

 

 

So my problem remains the same.

I want to read data again and again from the same address (as I have to read time again & again).

which is not happening.

 

If you know any other solution ,please let me know.

 

Thanks

SARY

 

 

0 项奖励
回复