SMBus与Silicon Labs C8051f320和ADXL345的通信

时间:2014-04-26 17:02:29

标签: c i2c 8051

我正在尝试通过SMBus从C8051F320 MCU与ADXL345加速度计进行通信

8051是师父 ADXL345是Slave

测试我的代码我在地址 0x00 使用ADXL345 device_ID 的简单读取

但遗憾的是它没有工作

这是我的代码

void SMBus_ISR (void) interrupt 7
 {
  bit FAIL = 0;                       // Used by the ISR to flag failed
                             // transfers




    if (SMB0CN_ARBLOST == 0)                   // Check for errors
     {

// Normal operation
switch (SMB0CN & 0xF0)    //        // Status vector
  {
  // Master Transmitter/Receiver: START condition transmitted.
 case SMB_MTSTA:

  SMB0DAT = TARGET;          // Load address of the target slave

  SMB0CN_STA = 0;                   // Manually clear START bit
  ADDR_SEND = 1;

  break;

  // Master Transmitter: Data byte transmitted
  case SMB_MTDB:


  if (SMB0CN_ACK)                   // Slave ACK?
  {
  if (ADDR_SEND && counter < index)
  {

      //LED = ~LED;
        SMB0DAT = outgoing_str[counter];
        counter++;


  }
  else
     ADDR_SEND = 0;
     //SMB0CN_STO = 1;
     SMB_BUSY = 0;
     TARGET = SLAVE_ADDR_READ;
     SMB_Read();
  }
  else{
      LED =~LED;
    }
  break;

 // Master Receiver: byte received
 case SMB_MRDB:
  SMB_DATA_IN = SMB0DAT;     // Store received byte
  SMB_BUSY = 0;              // Free SMBus interface
  SMB0CN_ACK = 0;                   // Send NACK to indicate last byte
    if(SMB_DATA_IN>0){


    LED2 = ~LED2;}


  break;



} // end switch
}
  else{

  FAIL = 1;
   }

  if (FAIL)                           // If the transfer failed,
  {
    SMB0CF &= ~0x80;                 // Reset communication
    SMB0CF |= 0x80;
    SMB0CN_STA = 0;
    SMB0CN_STO = 0;
    SMB0CN_ACK = 0;

    SMB_BUSY = 0;                    // Free SMBus

    FAIL = 0;


  NUM_ERRORS++;                    // Indicate an error occurred
   }

SMB0CN_SI = 0;                             // Clear interrupt flag
}

以下是SMBus写入和读取初始化方法

void SMB_Write (void)
{
  while (SMB_BUSY);                   // Wait for SMBus to be free.
  SMB_BUSY = 1;                       // Claim SMBus (set to busy)
  SMB_RW = 0;                         // Mark this transfer as a WRITE
  SMB0CN_STA = 1;                            // Start transfer
}

void SMB_Read (void)
{
 while (SMB_BUSY);                   // Wait for bus to be free.
 SMB_BUSY = 1;                       // Claim SMBus (set to busy)
 SMB_RW = 1;                         // Mark this transfer as a READ

 SMB0CN_STA = 1;                            // Start transfer

 while (SMB_BUSY);                   // Wait for transfer to complete
 }

其他方法来自SDK中的示例,所以我猜他们没问题

0 个答案:

没有答案