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的硬件IIC

Other Parts Discussed in Thread: MSP430G2553

你能发个MSP430G2553的硬件IIC主机发送模式的读写数据的代码给我不?

  • 这是www.ti.com上的例程,希望能对你有帮助

    //******************************************************************************

    //  MSP430G2xx3 Demo - USCI_B0 I2C Master TX single bytes to MSP430 Slave

    //

    //  Description: This demo connects two MSP430's via the I2C bus. The master

    //  transmits to the slave. This is the master code. It continuously

    //  transmits 00h, 01h, ..., 0ffh and demonstrates how to implement an I2C

    //  master transmitter sending a single byte using the USCI_B0 TX interrupt.

    //  ACLK = n/a, MCLK = SMCLK = BRCLK = default DCO = ~1.2MHz

    //

    //                                /|\  /|\

    //               MSP430G2xx3      10k  10k     MSP430G2xx3

    //                   slave         |    |        master

    //             -----------------   |    |  -----------------

    //           -|XIN  P1.7/UCB0SDA|<-|---+->|P1.7/UCB0SDA  XIN|-

    //            |                 |  |      |                 |

    //           -|XOUT             |  |      |             XOUT|-

    //            |     P1.6/UCB0SCL|<-+----->|P1.6/UCB0SCL     |

    //            |                 |         |                 |

    //

    //  D. Dang

    //  Texas Instruments Inc.

    //  February 2011

    //   Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10

    //******************************************************************************

    #include "msp430g2553.h"

    unsigned char TXData;

    unsigned char TXByteCtr;

    void main(void)

    {

     WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT

     P1SEL |= BIT6 + BIT7;                     // Assign I2C pins to USCI_B0

     P1SEL2|= BIT6 + BIT7;                     // Assign I2C pins to USCI_B0

     UCB0CTL1 |= UCSWRST;                      // Enable SW reset

     UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC;     // I2C Master, synchronous mode

     UCB0CTL1 = UCSSEL_2 + UCSWRST;            // Use SMCLK, keep SW reset

     UCB0BR0 = 12;                             // fSCL = SMCLK/12 = ~100kHz

     UCB0BR1 = 0;

     UCB0I2CSA = 0x48;                         // Slave Address is 048h

     UCB0CTL1 &= ~UCSWRST;                     // Clear SW reset, resume operation

     IE2 |= UCB0TXIE;                          // Enable TX interrupt

     TXData = 0x00;                            // Holds TX data

     while (1)

     {

       TXByteCtr = 1;                          // Load TX byte counter

       while (UCB0CTL1 & UCTXSTP);             // Ensure stop condition got sent

       UCB0CTL1 |= UCTR + UCTXSTT;             // I2C TX, start condition

       __bis_SR_register(CPUOFF + GIE);        // Enter LPM0 w/ interrupts

                                               // Remain in LPM0 until all data

                                               // is TX'd

       TXData++;                               // Increment data byte

     }

    }

    //------------------------------------------------------------------------------

    // The USCIAB0TX_ISR is structured such that it can be used to transmit any

    // number of bytes by pre-loading TXByteCtr with the byte count.

    //------------------------------------------------------------------------------

    #pragma vector = USCIAB0TX_VECTOR

    __interrupt void USCIAB0TX_ISR(void)

    {

     if (TXByteCtr)                            // Check TX byte counter

     {

       UCB0TXBUF = TXData;                     // Load TX buffer

       TXByteCtr--;                            // Decrement TX byte counter

     }

     else

     {

       UCB0CTL1 |= UCTXSTP;                    // I2C stop condition

       IFG2 &= ~UCB0TXIFG;                     // Clear USCI_B0 TX int flag

       __bic_SR_register_on_exit(CPUOFF);      // Exit LPM0

     }

    }

  • 建议楼主安装CCSv5 IDE,里头集成了关于IIC的例程代码,目前有13个例程如下图所示。

    其中例程i2c_04是master接收数据,例程i2c_05是slave 发送数据