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.

430g2553 硬件I2C



本来是用IO口模拟的,最近想用硬件的I2C。有人叫我去下一个MSP430Ware - MSP430F5xx 和 MSP430F6xx,说里面例程很多。

但是我没找到在哪里啊?

我也有G2553的官方I2C硬件配置例程,但是配置完不知道如何像模拟的I2C一样读写数据,还望不吝赐教。。万分感激

  • 刚好我正打算写个MSP430G2XX I2C的使用笔记,先把代码给你用。

    //=============================================================================
    //=============================================================================
    // I2C模块发送数据缓存
    static unsigned char   g_I2cTxBuf[I2C_TXBUF_MAX_SIZE] = {0x00, 0x01,0x02,0x03,0x04,0x05,0x06,0x07};
    // I2C模块接收数据缓存
    static unsigned char   g_I2cRxBuf[I2C_RXBUF_MAX_SIZE] = {0,0,0,0,0,0,0,0,};
    static stI2cMach g_stI2cMachine;
    /*******************************************************************************
    ** Function : InitMstI2c                                                        **
    ** Parameter:                                                                   **
    ** Output   :                                                                   **
    ** Retutn   :                                                                   **
    ================================================================================
    ** Author   :                                                                   **
    ** Date     :                                                                   ** 
    *******************************************************************************/
    void InitMstI2c(unsigned char DevAddr)
    {
          g_stI2cMachine.TxCnt = 0;
          g_stI2cMachine.RxCnt = 0;
          g_stI2cMachine.pTxBuf = g_I2cTxBuf;
          g_stI2cMachine.pRxBuf = g_I2cRxBuf;
      
          P1SEL |= BIT6 + BIT7;                     // 分配P1.6   P1.7工作在USCI_B0的SDA和SCL模式
          P1SEL2|= BIT6 + BIT7;                     // 
        
          UCB0CTL1 |= UCSWRST;                      // 配置I2C模块之前需要复位UCB0模块
          UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC;     // 配置成7位主I2C模式,同步模式
          UCB0CTL1 = UCSSEL_2 + UCSWRST;            // 选择SMCLK时钟源
          UCB0BR0 = 10;                             // 设置I2C波特率fSCL   = SMCLK/10 = 100kHz
          UCB0BR1 = 0;
        
          UCB0I2CSA = DevAddr;                      // 设置I2C从地址
          UCB0CTL1 &= ~UCSWRST;                     // 清除复位标志位,恢复到运行模式
    }
    /*******************************************************************************
    ** Function : I2cMSendOneByte                                                   **
    ** Parameter:                                                                   **
    ** Output   :                                                                   **
    ** Retutn   :                                                                   **
    ================================================================================
    ** Author   :                                                                   **
    ** Date     :                                                                   ** 
    *******************************************************************************/
    signed short int I2cMSendOneByte(unsigned   char Val)
    {
            unsigned short int TimeOut = 0;
          
            if (UCB0STAT&UCBBUSY)
            {
                return I2C_ERRO_BUSY;
            } 
          
            TimeOut = I2C_TIME_OUT;
            while ((UCB0CTL1 & UCTXSTP) && (TimeOut--)); // 确保STOP
            if (!TimeOut) 
            {
                return I2C_ERRO_TIMEOUT;
            }
          
            UCB0CTL1 |= UCTR + UCTXSTT;               // I2C模块为发送模式,启动START信号
            TimeOut = I2C_TIME_OUT;
            while ((!(IFG2 & UCB0TXIFG)) && (TimeOut--));
            if (!TimeOut) 
            {
                return I2C_ERRO_TIMEOUT;
            }
          
            UCB0TXBUF = Val;                   // Load TX buffer
            TimeOut = I2C_TIME_OUT;
            while ((!(IFG2 & UCB0TXIFG)) && (TimeOut--));
            if (!TimeOut) 
            {
                return I2C_ERRO_TIMEOUT;
            }
        
            IFG2 &= ~UCB0TXIFG;
          
            UCB0CTL1 |= UCTXSTP;               // 发送I2C STOP 信号
            TimeOut = I2C_TIME_OUT;
            while ((UCB0CTL1 & UCTXSTP) && (TimeOut--)); // 确保STOP
            if (!TimeOut) 
            {
                return I2C_ERRO_TIMEOUT;
            }
            return I2C_SUCCESSFUL;
    }

  • 请问有读的代码吗?