我使用的是mSP430F4152
UCA0_uart与电脑连接,1200 8 N 1
UCB0_iic与MCP4152连接.
开发工具IAR for MSP430 5.30.1 不限时间试用版
unsigned char TXData;
unsigned char TXByteCtr;
unsigned char *PRxData; // Pointer to RX data
unsigned char RXByteCtr;
volatile unsigned char RxBuffer[5]; // Allocate 128 byte of RAM
const char string1[14] = { "Hello World\r\n" };
unsigned int j;
void IIC_init(void)
{
P1OUT |= BIT4;
P6DIR |= 0x06;
P6OUT |= 0x60;
P1OUT &=~ BIT4;
P6SEL |= 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 = (0xD0>>1); // Slave Address is 0D0h
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
IFG2=0;
IE2 |= UCB0TXIE; // Enable TX interrupt
IE2 |= UCB0RXIE; // Enable RX interrupt
}
void uart_init(void)
{
P6SEL |= BIT5+BIT6; // P6.5,6 = USCI_A0 RXD/TXD
UCA0CTL1 |= UCSSEL_1; // CLK = ACLK
UCA0BR0 = 0x1B; // 32k/1200
UCA0BR1 = 0x00; //
UCA0MCTL = UCBRS1; // Modulation
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
IE2 &= ~UCA0TXIE;
IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt
IFG2 &= ~UCA0TXIFG;
}
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
{
if (UCA0RXBUF == 'u') // 'u' received?
{
j = 0;
IE2 |= UCA0TXIE; // Enable USCI_A0 TX interrupt
UCA0TXBUF = string1[j++];
}
}
//------------------------------------------------------------------------------
// The USCIAB0TX_ISR is structured such that it can be used to transmit any
// number of bytes by pre-loading TXByteCtr with the byte count.
//------------------------------------------------------------------------------
#pragma vector = USCIAB0TX_VECTOR
__interrupt void USCIAB0TX_ISR(void)
{
// unsigned char itemp;
// itemp= IFG2;
if (IFG2&UCB0TXIFG)
{
if (TXByteCtr) // Check TX byte counter
{
UCB0TXBUF = TXData; // Load TX buffer
TXByteCtr--; // Decrement TX byte counter
}
else
{
UCB0CTL1 |= UCTXSTP; // I2C stop condition
}
IFG2 &= ~UCB0TXIFG; // Clear USCI_B0 TX int flag
}
if(IFG2&UCB0RXIFG)
{
RXByteCtr--; // Decrement RX byte counter
if (RXByteCtr)
{
*PRxData++ = UCB0RXBUF; // Move RX data to address PRxData
if (RXByteCtr == 1) // Only one byte left?
UCB0CTL1 |= UCTXSTP; // Generate I2C stop condition
}
else
{
*PRxData = UCB0RXBUF; // Move final RX data to PRxData
}
IFG2 &= ~UCB0RXIFG;
}
if(IFG2&UCA0TXIFG)
{
UCA0TXBUF = string1[j++]; // TX next character
if (j == sizeof string1 - 1) // TX over?
{
IE2 &= ~UCA0TXIE; // Disable USCI_A0 TX interrupt
}
IFG2 &=~ UCA0TXIFG;
}
IFG2 &= UCA0RXIFG ;
}
现象是串口接收到字母"u"后,会发送完字符串,但是不会停止发送,而是一直在发送,暂停程序后发现标记位UCA0TXIFG没有得到清除. j已经上升到很大的数值.单步在中断中运行后标记位UCA0TXIFG会被清零,恢复正常.
(iic处于0.5秒写一个字节,读4个字节的状态,由定时器控制节拍)