打印

msp430 IIC 例子求助

[复制链接]
1101|5
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
jsaw|  楼主 | 2017-1-11 14:58 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1.    UCB0CTL1 |= UCTXSTT;                    // I2C start condition
这句执行就马上进入IIC中断吗?还是执行了下一句进入休眠后等到有数据才进入中断的?

2.为什么中断服务程序对应的中断向量是发送数据的?不应该是接收数据的吗

#include <msp430.h>

unsigned int RxByteCtr;
unsigned int RxWord;

int main(void)
{
  WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
  P1DIR |= BIT0;                            // P1.0 output
  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 = 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
  }
}

#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector = TIMER0_A0_VECTOR
__interrupt void TA0_ISR(void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(TIMER0_A0_VECTOR))) TA0_ISR (void)
#else
#error Compiler not supported!
#endif
{
  __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.
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector = USCIAB0TX_VECTOR
__interrupt void USCIAB0TX_ISR(void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(USCIAB0TX_VECTOR))) USCIAB0TX_ISR (void)
#else
#error Compiler not supported!
#endif
{
  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
  }
}

相关帖子

沙发
火山LF| | 2017-1-11 18:56 | 只看该作者
我用的轮询的硬件I2C,你需要的话,我可以发我代码给你看看,来个邮箱
中断的我还在调,还没调出来

使用特权

评论回复
板凳
火山LF| | 2017-1-11 19:02 | 只看该作者
不过看你是直接配置寄存器的,我用的是库函数,推荐你个博文,对理解会好一些:
http://blog.csdn.net/xukai871105 ... ad52626f6f40fdeadaa

我是看了博文写出来的,然后需要添加一点,在UCB0CTL1 |= UCTXSTT; 需要等待UCTXSTT这个位置起来之后才可以继续下面的动作
你用中断的话,我师父说中断的是不需要等待这个位的置起的,看你选择什么方法咯

使用特权

评论回复
地板
外方爱| | 2017-1-12 09:12 | 只看该作者
void __attribute__ ((interrupt(USCIAB0TX_VECTOR))) USCIAB0TX_ISR (void)这个是发送的中断啊

使用特权

评论回复
5
jsaw|  楼主 | 2017-1-12 10:35 | 只看该作者
这个是TI自带的IIC读取温度的例子

使用特权

评论回复
6
smilingangel| | 2017-1-15 22:31 | 只看该作者
这个看下原版程序中的注释及程序设计说明的

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

39

主题

92

帖子

3

粉丝