This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

[参考译文] MSP430F5529:检查从器件是否存在

Guru**** 663810 points
Other Parts Discussed in Thread: MSP430F5529, MSP430F2619
请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/microcontrollers/msp-low-power-microcontrollers-group/msp430/f/msp-low-power-microcontroller-forum/1125919/msp430f5529-check-if-slave-is-present

器件型号:MSP430F5529
主题中讨论的其他器件: MSP430F2619

您好!

我在 I2C 总线上有一个 MSP430F5529。 在我的系统运行期间、某些组件可以与该总线连接或断开。 在与从器件通信之前、我想检查总线上是否有特定的从器件。 我找到了一个有用的函数(TI_USCI_I2C_SLAVE_Present)、该函数在文档 slaa382a 中引用、并在 GitHub 上生成了一些 TI 生成的源代码、尽管适用于 MSP430F2619。 我一直在尝试为  MSP430F5529调整此函数、但我一直在努力使此代码正常工作。  

 MSP430F2619的原始代码

unsigned char TI_USCI_I2C_slave_present(unsigned char slave_address){
  unsigned char ie2_bak, slaveadr_bak, ucb0i2cie, returnValue;
  ucb0i2cie = UCB0I2CIE;                      // restore old UCB0I2CIE
  ie2_bak = IE2;                              // store IE2 register
  slaveadr_bak = UCB0I2CSA;                   // store old slave address
  UCB0I2CIE &= ~ UCNACKIE;                    // no NACK interrupt
  UCB0I2CSA = slave_address;                  // set slave address
  IE2 &= ~(UCB0TXIE + UCB0RXIE);              // no RX or TX interrupts
  __disable_interrupt();
  UCB0CTL1 |= UCTR + UCTXSTT + UCTXSTP;       // I2C TX, start condition
  while (UCB0CTL1 & UCTXSTP);                 // wait for STOP condition
  
  returnValue = !(UCB0STAT & UCNACKIFG);
  __enable_interrupt();
  IE2 = ie2_bak;                              // restore IE2
  UCB0I2CSA = slaveadr_bak;                   // restore old slave address
  UCB0I2CIE = ucb0i2cie;                      // restore old UCB0CTL1
  return returnValue;                         // return whether or not 
                                              // a NACK occured
}

 我当前尝试使用 MSP430F5529  

unsigned char TI_USCI_I2C_slave_present(unsigned char slave_address){
  unsigned char slaveadr_bak, ucb0i2cie, returnValue;
  ucb0i2cie = UCB0IE;                      // restore old UCB0I2CIE
  slaveadr_bak = UCB0I2CSA;                   // store old slave address
  UCB0I2CSA = slave_address;                  // set slave address
  UCB0IFG &= ~(UCTXIFG + UCRXIFG);       // Clear any pending interrupts
  UCB0IE &= ~ ( UCNACKIE + UCRXIE + UCTXIE);  // no NACK, RX, or TX interrupts
  __disable_interrupt();
  UCB0CTL1 |= UCTR + UCTXSTT + UCTXSTP;       // I2C TX, start condition
  while (UCB0CTL1 & UCTXSTP);                 // wait for STOP condition
  
  // !!! Not sure what registers need to be used here for MSP430F5529 !!!
  returnValue = !(UCB0IV & USCI_I2C_UCNACKIFG);
  //returnValue = !(UCB0IFG & UCNACKIFG); //

  __enable_interrupt();
  UCB0I2CSA = slaveadr_bak;                   // restore old slave address
  UCB0IE = ucb0i2cie;                      // restore old UCB0CTL1
  return returnValue;                         // return whether or not
                                              // a NACK occured
}

运行调试器时、我似乎没有获得变量"returnValue"所需的值、但我不确定我做了什么错误。 我有一个测试设置和一个逻辑分析仪。 我的目标是验证总线上是否存在现有的从器件、然后验证不存在的从器件。

感谢您的任何帮助!

j

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    你(们)好  

    您需要检查寄存器 UCBxIFG 中的 NACK 位

    返回值=!(UCB0IFG 和 UCNACKIFG);

    而不是  

    //!!! 不确定这里需要为 MSP430F5529使用哪些寄存器!!!
    返回值=!(UCB0IV 和 USCI_I2C_UCNACKIFG);