#include "msp430g2533.h"
#include"stdio.h"
//#include "I2C_TMP.h"
unsigned int RxByteCtr;
unsigned int RxWord;
volatile int j;
void UART0_send_byte(unsigned int data) //发送一位
{
for( j=10000;j>0;j--);
UCA0TXBUF=data;
}
void UART0_send_str(char *s) //发送字符串
{
while(*s != '\0')
{
UART0_send_byte(*s++);
}
}
void main(void)
{ char pBuf[10];
int temp;
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 复位uscib0状态机
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode 使得uscib0工作在主机模式
UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset 选择时钟源为smclk
UCB0BR0 = 12; // fSCL = SMCLK/12 = ~100kHz 进行12分频
UCB0BR1 = 0;
UCB0I2CSA = 0x48; // Set slave address 根据tmp101的接线设置从机地址
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation 清除复位状态
IE2 |= UCB0RXIE; // Enable RX interrupt 使能iic接收中断
TACTL = TASSEL_2 + MC_2; // SMCLK, contmode
pBuf[0] = 0xFF; //设置tmp101的12位精度
_EINT();
while (1)
{
DCOCTL = 0; // Select lowest DCOx and MODx settings
BCSCTL1 = CALBC1_1MHZ; // Set DCO
DCOCTL = CALDCO_1MHZ;
P1SEL |= BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
P1SEL2 |= BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 104; // 1MHz 9600
UCA0BR1 = 0; // 1MHz 9600
UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
// _EINT();
while (1)
{_EINT();
RxByteCtr = 2; // Load RX byte counter
UCB0CTL1 |= UCTXSTT; // I2C start condition
for(j=10000;j>0;j--);
UCB0CTL1 &= ~UCTR; // I2C RX
_EINT();
// __bis_SR_register(CPUOFF + GIE); // Enter LPM0, enable interrupts 进入低功耗模式使能全局中断
// Remain in LPM0 until all data
// is RX'd
temp =RxWord ;
sprintf(pBuf,"%x\n",temp);
UART0_send_str(pBuf);
__disable_interrupt();
TACCTL0 |= CCIE; // TACCR0 interrupt enabled
// __bis_SR_register(CPUOFF + GIE); // Enter LPM0, enable interrupts
// interrupt occurs
_EINT();
TACCTL0 &= ~CCIE; // TACCR0 interrupt disabled
}
}
}
#pragma vector = TIMER0_A0_VECTOR
__interrupt void TA0_ISR(void)
{
__bic_SR_register_on_exit(CPUOFF); // Exit LPM0
}
#pragma vector = USCIAB0TX_VECTOR
__interrupt void USCIAB0TX_ISR(void)
{
RxByteCtr--; // Decrement RX byte counter
RxWord = UCB0RXBUF ;
if (RxByteCtr)
{
//RxWord = UCB0RXBUF ;
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
}
}
现在的问题是输出dfc8,寄存器usb0rxbuf没有数据