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.

MSP430g2553使用硬件I2C时读取正常几次,突然数据总线就一直忙(USBBUSY=1),为什么?

Other Parts Discussed in Thread: MSP430G2553

#include "msp430g2553.h"
#include  "math.h"    //Keil library
#include  "stdio.h"
#define CPU_F ((double)1000000)
#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0)) //延时x微秒
#define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0)) //延时x毫秒
#define uint unsigned int
#define uchar unsigned char
uchar TXByteCtr,RXByteCtr;
uchar TxData[2];
uchar RxBuf[6]={0x00};

void Init_I2C(void)
{
  UCB0CTL1 |= UCSWRST;                      // Enable SW reset
  UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC;     // I2C Master, synchronous mode
  UCB0CTL1 = UCSSEL_2 + UCSWRST;            // Use SMCLK, keep SW reset
  UCB0BR0 = 11;                             // fSCL = SMCLK/12 = ~100kHz
  UCB0BR1 = 0;
  P1SEL |= BIT6 + BIT7;                     // Assign I2C pins to USCI_B0
  P1SEL2|= BIT6 + BIT7;                     // Assign I2C pins to USCI_B0
  UCB0I2CSA = SlaveAddress;
  UCB0CTL1 &= ~UCSWRST;                     // Clear SW reset, resume operation
}

void I2C_WriteMode(void)
{
 UCB0CTL1 |= UCTR;
 IFG2 &=~ UCB0TXIFG;
 IE2 &=~ UCB0RXIE;
 IE2 |= UCB0TXIE;
}
void I2C_ReadMode(void)
{
 UCB0CTL1 &=~ UCTR;
 IFG2 &=~ UCB0RXIFG;
 IE2 &=~ UCB0TXIE;
 IE2 |= UCB0RXIE;                                // 关闭发送中断,开启接收中断
}
/*----------------------------------------------------------------------------
功能:向I2C接口发送1个字节数据
输入:待发送字节个数 num,7-bit从机地址slave_addr
输出:
说明:在发送第1个字节过程中需要置位停止位,待发送数据放在TxData[0]
----------------------------------------------------------------------------*/
void I2C_Txbyte(uchar Reg_addr,uchar Reg_data)
{
  while(UCB0STAT & UCBBUSY);
  I2C_WriteMode();
  TxData[1]=Reg_addr;
  TxData[0]=Reg_data;
  TXByteCtr=2;                                     // Load TX byte counter
  UCB0CTL1 |= UCTXSTT;                 // I2C TX, start condition
  //while (UCB0CTL1 & UCTXSTT);
  __bis_SR_register(CPUOFF + GIE);                   // Enter LPM0 w/ interrupts
  while (UCB0CTL1 & UCTXSTP);
}

void I2C_Rxbyte(uchar Reg_addr)
{
 while(UCB0STAT & UCBBUSY);
 I2C_WriteMode();
 TxData[0]=Reg_addr;
 TXByteCtr=1;                                    // Load TX byte counter
 UCB0CTL1 |= UCTXSTT;                 // I2C TX, start condition
 __bis_SR_register(CPUOFF + GIE);                   // Enter LPM0 w/ interrupts
 while (UCB0CTL1 & UCTXSTP);
 I2C_ReadMode();
 RXByteCtr=1;
 UCB0CTL1 |= UCTXSTT;
 __bis_SR_register(CPUOFF + GIE);
 while (IFG2 & UCB0RXIFG);
 while (UCB0CTL1 & UCTXSTP);
}

 

#pragma vector = USCIAB0TX_VECTOR
__interrupt void USCIAB0TX_ISR(void)
{
  if ( IFG2 & UCB0TXIFG )
  {
   if (TXByteCtr)                            // Check TX byte counter
   {
    TXByteCtr--;
    UCB0TXBUF =TxData[TXByteCtr];                 // Load TX buffer                               // Decrement TX byte counter
   }
   else
   {
    UCB0CTL1 |= UCTXSTP;                    // I2C stop condition
    IFG2&=~UCB0TXIFG;                     // Clear USCI_B0 TX int flag
    //IE2 &=~UCB0TXIE;
    __bic_SR_register_on_exit(CPUOFF);      // Exit LPM0
   }
  }
  if ( IFG2 & UCB0RXIFG )
    {
     if (RXByteCtr)
     {
      RXByteCtr--;
      if(RXByteCtr==0)
      {
       UCB0CTL1 |= UCTXSTP+UCTXNACK;//必须在接受最后一个数据之前发送停止和NACK
      }
      RxBuf[RXByteCtr]=UCB0RXBUF;
      UCB0CTL1 &=~UCTXNACK;
     }
     else
     {
      //UCB0CTL1 |= UCTXSTP;
      IFG2&=~UCB0RXIFG;
       __bic_SR_register_on_exit(CPUOFF);      // Exit LPM0
     }
    }
}