#include <msp430x14x.h>
void Set_DCO (void);
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P3SEL |= 0x30; // P3.4,5 = USART0 TXD/RXD
BCSCTL1 |= DIVA_3; // ACLK= LFXT1CLK/8
Set_DCO(); // Set DCO
ME1 |= UTXE0 + URXE0; // Enabled USART0 TXD/RXD
UCTL0 |= CHAR; // 8-bit character, SWRST = 1
UTCTL0 |= SSEL1 + SSEL0 + URXSE; // UCLK = SMCLK, start edge detect
UBR00 = 0xD0; // 9600 from 2Mhz
UBR10 = 0x00;
UMCTL0 = 0x00; // No modulation
UCTL0 &= ~SWRST; // Initialize USART state machine
IE1 |= URXIE0; // Enable USART0 RX interrupt
for (;;)
{
while (!(UTCTL0 & TXEPT)); // Confirm no TXing before --> LPM3
_DINT(); // Disable interrupts for flag test
_NOP();
if (!(UTCTL0 & SSEL0))
_BIS_SR(LPM0_bits + GIE); // RX'ing char, enter LPM0, int's active
else
_BIS_SR(LPM3_bits + GIE); // Enter LPM3, int's active
}
}
#pragma vector=UART0RX_VECTOR
__interrupt void usart0_rx (void)
{
if ((IFG1 & URXIFG0)) // Test URXIFG0
{
while (!(IFG1 & UTXIFG0)); // USART0 TX buffer ready?
TXBUF0 = RXBUF0; // RXBUF0 to TXBUF0
_BIC_SR_IRQ(LPM3_bits); // Exit LPM3 after reti
UTCTL0 |= SSEL0; // SSEL0 = 1, no RX activity 为什么这里要重新选择时钟呢?
}
else // Start edge
{
UTCTL0 &= ~URXSE; // Clear URXS signal
UTCTL0 |= URXSE; // Re-enable edge detect
_BIC_SR_IRQ(SCG1 + SCG0); // DCO reamins on after reti
UTCTL0 &= ~SSEL0; // SSEL0= 0, RX activity 为什么这里要重新选择时钟呢?
} }