Help Me ! My IIC communication cannot work

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Help Me ! My IIC communication cannot work

Jump to solution
1,601 Views
Tianshu
Contributor I

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!

祝好,

      刘宇

Labels (1)
0 Kudos
Reply
1 Solution
865 Views
DRichards
Contributor III

I haven't used this particulr chip, but have you remembered to change the data line port pin direction from output to input?

View solution in original post

0 Kudos
Reply
5 Replies
865 Views
Tianshu
Contributor I

 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.

0 Kudos
Reply
866 Views
DRichards
Contributor III

I haven't used this particulr chip, but have you remembered to change the data line port pin direction from output to input?

0 Kudos
Reply
865 Views
Tianshu
Contributor I
Hello  DRichards:
          Thanks for your suggestion. I have solved my problem. I found my slaver didn't change translation direction after sending the last byte.  When I set IICC_TX to zero and read the register IICD, the SDA line changed to be high, and the communication worked.
           I'm a beginner of HCS08, thanks for you help.
         谢谢,
祝好
0 Kudos
Reply
865 Views
J2MEJediMaster
Specialist I

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

0 Kudos
Reply
865 Views
Tianshu
Contributor I

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. 谢谢

刘宇

 

0 Kudos
Reply