#include "msp430g2553.h"
unsigned int RxByteCtr;
unsigned int RxWord;
void 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 I2C复位
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode 主设备选择UCMST+I2C UCMODE_3+同步模式UCSYNC
UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset SMCLK+I2C复位
UCB0BR0 = 12; // fSCL = SMCLK/12 = ~100kHz 波特率控制 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 产生START条件
__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 = TIMER0_A0_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
}
}
这是例程1的程序,其中有点不明白,他用定时器0在干吗,有什么作用吗,还有,在while(1)里面,把CCIE开启是干嘛的?他设置了模式2,这个到了0xffff中断就可以了,为什么还要设置CCIE?
此帖出自
小平头技术问答
一周热门 更多>