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.

MSP430F2272硬件IIC操作问题。

Other Parts Discussed in Thread: TMP100, MSP430G2553, MSP430G2533

void I2C_IO_Init()

{

   P3SEL |=(BIT1+BIT2);

UCB0CTL1 |=UCSWRST;

UCB0CTL0 =(UCMST+UCMODE_3+UCSYNC);

UCB0CTL1 =(UCSSEL_2+UCSWRST);

UCB0BR0 =120;

UCB0BR1 =0;

UCB0CTL1 &=~UCSWRST;

}

//============================================================================================================

void WriteByte(uint8 addr,uint8 data)

{

UCB0I2CSA =0x38; // Slave Address is 38h

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

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

while(!(UC0IFG&UCB0TXIFG)); // Wait USCI_B0 TX  flag Flag reset automaticall

UCB0TXBUF =addr; // Load TX buffer

while(UCB0CTL1&UCTXSTT); //检测从机ACK信号,有ACK信号,则UCTXSTT自动清零

                                //以上顺序不能改变

while(!(UC0IFG&UCB0TXIFG));

UCB0TXBUF =data;

while(!(UC0IFG&UCB0TXIFG));

UCB0CTL1 |=UCTXSTP; // I2C stop condition

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

Delay1us(27056);

}

//============================================================================================================

void Readpage(uint8 *RamAddress,uint8 addr,uint8 bytes)

{

UCB0I2CSA =0x38; // Slave Address is 38h

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

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

while(!(UC0IFG&UCB0TXIFG)); // Wait USCI_B0 TX  flag    Flag reset automatically

UCB0TXBUF =addr; // Load TX buffer

while(UCB0CTL1&UCTXSTT) ;

while(!(UC0IFG&UCB0TXIFG));

UC0IFG &=~UCB0TXIFG;

UCB0CTL1 &=~UCTR;

while(UCB0CTL1&UCTXSTP);

UCB0CTL1 |=UCTXSTT;

while(UCB0CTL1&UCTXSTT);

while(bytes!=1)

{

while(!(UC0IFG&UCB0RXIFG));

*RamAddress =UCB0RXBUF;

UCB0CTL1 &=~UCTXNACK;

RamAddress++;

bytes--;

}

UCB0CTL1 |=UCTXSTP+UCTXNACK; //发送停止信号必须再读取最后一个数据之前

while(!(UC0IFG&UCB0RXIFG));

*RamAddress =UCB0RXBUF;

//UCB1CTL1 |= UCTXSTP+UCTXNACK;   这里放就不可以了

while(UCB0CTL1&UCTXSTP);

Delay1us(27056);

}

//============================================================================================================

void Init_HMC5883()

{

    WriteByte(0x02,0x00);  //

}

代码如上,当执行Init_HMC5883()时(HMC5838L的地址是0x1c),调用void WriteByte(uint8 addr,uint8 data)子函数,程序将会吊死在该子函数的第七行while(!(UC0IFG&UCB0TXIFG));   请教高手帮忙解答一下。

另外,我多方搜索,始终找不到TI给出的关于MSP430F2系列的硬件IIC程序。我们公司现在要开发一款新型罗盘,需要用的这IIC接口,模拟出来的IIC无法使用。专家能否提供一下MSP430F2系列的硬件IIC,最好能提供中断和查询方式各一个成功的例子?急急急。

  • 1.如果HMC5838L的地址是0x1c,那么在设置从机地址时UCB0I2CSA = 0x1C;

    2.死在第七行,说明从机没有发送ACK,如果单步调试,可以看到这个时候的NACKIFG被置位,为了保证程序的健壮性这个时候还需要判断是否是NACKIFG,如果是则要stop和restart,或者使用延时来强行跳过;

  • //******************************************************************************

    //  MSP430F22x4 Demo - USCI_B0 I2C Master to TMP100, Set P5.1 if Temp > 28C

    //

    //  Description: I2C interface to TMP100 temperature sensor in 9-bit mode.

    //  Timer_A CCR0 interrupt is used to wake up and read the two bytes of

    //  the TMP100 temperature register every 62ms. If the temperature is greater

    //  than 28C, P1.0 is set, else reset. CPU is operated in LPM0. I2C speed

    //  is ~100kHz.

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

    //

    //         /|\           /|\ /|\

    //          |   TMP100   10k 10k     MSP430F22x4

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

    //          +--|Vcc SDA|<-|---+->|P3.1/UCB0SDA    XIN|-

    //          |  |       |  |      |                   |

    //          +--|A1,A0  |  |      |               XOUT|-

    //             |       |  |      |                   |

    //          +--|Vss SCL|<-+------|P3.2/UCB0SCL   P1.0|---> LED

    //         \|/  -------          |                   |

    //

    //  Andreas Dannenberg

    //  Texas Instruments Inc.

    //  September 2006

    //  Built with CCE Version: 3.2.0 and IAR Embedded Workbench Version: 3.41A

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

    #include "msp430x22x4.h"

    unsigned int RxByteCtr;

    unsigned int RxWord;

    void main(void)

    {

     WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT

     P1DIR |= 0x01;                            // P1.0 output

     P3SEL |= 0x06;                            // 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 = 0x4e;                         // Set slave address

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

     IE2 |= UCB0RXIE;                          // Enable RX interrupt

     TACTL = TASSEL_2 + MC_2;                  // SMCLK, contmode

     while (1)

     {

       RxByteCtr = 2;                          // Load RX byte counter

       UCB0CTL1 |= UCTXSTT;                    // I2C start condition

       __bis_SR_register(CPUOFF + GIE);        // Enter LPM0, enable interrupts

                                               // Remain in LPM0 until all data

                                               // is RX'd

       if (RxWord < 0x1d00)                    // >28C?

         P1OUT &= ~0x01;                       // No, P1.0 = 0

       else

         P1OUT |= 0x01;                        // Yes, P1.0 = 1

       __disable_interrupt();

       TACCTL0 |= CCIE;                        // TACCR0 interrupt enabled

       __bis_SR_register(CPUOFF + GIE);        // Enter LPM0, enable interrupts

                                               // Remain in LPM0 until TACCR0

                                               // interrupt occurs

       TACCTL0 &= ~CCIE;                       // TACCR0 interrupt disabled

     }

    }

    #pragma vector = TIMERA0_VECTOR

    __interrupt void TA0_ISR(void)

    {

     __bic_SR_register_on_exit(CPUOFF);        // Exit LPM0

    }

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

    // 2+ number of bytes by pre-loading RxByteCtr with the byte count.

    #pragma vector = USCIAB0TX_VECTOR

    __interrupt void USCIAB0TX_ISR(void)

    {

     RxByteCtr--;                              // Decrement RX byte counter

     if (RxByteCtr)

     {

       RxWord = (unsigned int)UCB0RXBUF << 8;  // Get received byte

       if (RxByteCtr == 1)                     // Only one byte left?

         UCB0CTL1 |= UCTXSTP;                  // Generate I2C stop condition

     }

     else

     {

       RxWord |= UCB0RXBUF;                    // Get final received byte,

                                               // Combine MSB and LSB

       __bic_SR_register_on_exit(CPUOFF);      // Exit LPM0

     }

    }

  • 发了一个中断方式使用IIC配置和读取TMP100数据的例程,希望能够帮到你

  • 用USCI_B实现IIC时,其有非常多的标志位与时序需要遵守,所以用中断方式是比较容易实现的,查询实现起来稍微麻烦一点,建议抓波形然后和DATASHEET中的对比。

  • 请教一个问题:对应I2C的中断变量有几个啊?上面TI官方例程里面打开的是接收中断, (IE2 |= UCB0RXIE;                          // Enable RX interrupt)

    中断服务程序却是用的USART0TX_VECTOR,无论发送还是接收数据都只用这一个发送中断函数吗??在msp430g2553里面也是这样吗???

  • USCI_Ax and USCI_Bx share the same interrupt vectors. In I2C mode the state change interrupt flags

    UCSTTIFG, UCSTPIFG, UCIFG, UCALIFG from USCI_Bx and UCAxRXIFG from USCI_Ax are routed to

    one interrupt vector. The I2C transmit and receive interrupt flags UCBxTXIFG and UCBxRXIFG from

    USCI_Bx and UCAxTXIFG from USCI_Ax share another interrupt vector.

  • UCSTTIFG, UCSTPIFG, UCIFG, UCALIFG from USCI_Bx and UCAxRXIFG from USCI_Ax 共享 USCIAB0TX_VECTOR

    UCBxTXIFG and UCBxRXIFG from USCI_Bx and UCAxTXIFG from USCI_Ax 共享 USCIAB0RX_VECTOR

  • USCI_A和USCI_B一共就两个中断向量,多个中断标志位是共享两个中断向量的

  • 把这个例程烧到msp430g2533,中断函数不执行是怎么回事?谢谢