#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没有数据
灰小子:
你好,msp430g2553没有usb0rxbuf这个寄存器。
你看看UCB0TXBUF有没有数据
yao wang5:
回复 灰小子:
是这个寄存器UCB0TXBUF,现在用示波器看iic的两路信号是正常的,但是UCB0TXBUF输出的只有0x80和00,寄存器的数值就这两个没有变化
Terry Deng:
参考附件,包括G2553的I2C配置MASTER例程,以及TMP100的驱动例程
yao wang5:
回复 Terry Deng:
主机配程序有问题阿,麻烦看下。。。
yao wang5:
回复 Terry Deng:
#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 }}
#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 }}
这个官方例程进不去中断,是怎么回事?
TI中文支持网



