Hello, I'm a beginner. I want to chip of MC9SDZ60 MCU to communicate with IIC bus. One of them always works as master, and anthor one works as slaver.
Now master can write to slaver successly, but it goes wrong when master reads from slaver. I used oscilloscope to see the waveform of master's reading from slaver. I found all bytes of data receives right, the 9th of last byte is NAK, but SDA and SCL line is still low.
I'm a beginner ot Freescale MCU, and this is my first time to use DZ60's IIC module, I really cannot found the problem , please help me. The code is followed.
/*Master IIC code */unsigned char IIC_Index = 0;unsigned char IIC_BUF[50];unsigned char IIC_Stat;interrupt IIC_ISR(){ DisableInterrupts; IIC_IICIF = 1; if ( !IICS_ARBL ) { if ( IICS_IAAS ) { IIC_Index = 0; if ( IICS_SRW ) { IICC_TX = 1; IIC_Stat = __iic_slaver_transfering; } else { IICC_TXAK = 0; IICS_TX = 0; IICD; IIC_Stat = __iic_slaver_receiving; EnableInterrupts; return; } }// End of if ( IICS_IAAS ); if ( IIC_stat == __iic_slaver_transfering ) { IICD = IIC_BUF[IIC_Index++]; if ( IIC_Index >= IIC_DataSize ) { IIC_Stat = __iic_slaver_transfered; IIC_DataSize = 0; } } else ( IIC_Stat == __iic_slaver_receiving ) { IIC_BUF[IIC_Index] = IICD; if ( IIC_Index == 0 ) switch ( IIC_BUF[0] ) { case _iic_test: IIC_DataSize = 2; } if ( ++IIC_Index >= IIC_DataSize ) { IIC_Stat = __iic_slaver_received; IIC_DataSize = 0; } } // end of if ( !IICS_ARBL ) else { IICS_ARBL = 1; EnableInterrupts; return; } EnableInterrpts; }
// Master codeextern unsigned char IIC_BUF[];#define IIC_START() IIC_TX = 1; \ IICC_MST = 1;#define IIC_WRITE_BYTE(tmp) IICD = tmp;#define IIC_READ_DUMMY() IICD;#define IIC_STOP() IICC_MST = 0;void IIC_WaitRespone(){ while( IICS_IICF == 0 ); IICS_IICF = 1; while( IICS_RXAK == 1 );}void IIC_WaitInterFlg(){ while( IICS_IICF == 0 ); IICS_IICF = 1;}void IIC_Read (unsigned char addr, unsigned char length){ unsigned char addrTmp; unsigned char iLoop = 0; addrTmp = ( addrTmp << addr ) | 0x01; while ( !IIC_WaitResponse() ); IICC_TX = 0; IICC_TXAK = 0; IIC_READ_DUMMY(); IIC_WaitInterFlg(); for ( iLoop = 0; iLoop < length; iLoop++ ) { if ( iLoop = ( length -1 ) ) { IICC_TXAK = 1; } IIC_READ_BYTE(IIC_BUF[iLoop]); IIC_WaitInterFlg(); } IIC_STOP(); return;}
Thanks!
祝好,
刘宇
Solved! Go to Solution.
I haven't used this particulr chip, but have you remembered to change the data line port pin direction from output to input?
I used oscilloscope to see the waveform of master's reading from slaver. I found all bytes of data receives right, the 9th bit of last byte is NAK, but SDA and SCL line is still low.
I haven't used this particulr chip, but have you remembered to change the data line port pin direction from output to input?
There have been many discussions of implementing this interface in the forum. Some of that information may be of use to you. Do a search on "IIC" and "I2C" using the Search function at the upper right of this web page.
---Tom
Hello Tem
Thanks for your suggestion. It's a good way to solve the problem, and with your help, I have solved my problem of IIC communication. 谢谢
刘宇