运行时看门狗的周期就不是1s,变成了32ms,发送到123后就复位
//------------------------------------------------------------------------------
// Hardware-related definitions
//------------------------------------------------------------------------------
#define UART_TXD 0x01 // TXD on P2.0 (Timer1_A.OUT0)
#define UART_RXD 0x02 // RXD on P2.1 (Timer1_A.CCI1A)
//------------------------------------------------------------------------------
// Conditions for 9600 Baud SW UART, SMCLK = 1MHz
//------------------------------------------------------------------------------
#define UART_TBIT_DIV_2 (8000000 / (9600 * 2))
#define UART_TBIT (8000000 / 9600)
void main(void){
WDT_init();
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
WDTCTL = WDT_ARST_1000; // Set Watchdog Timer timeout 1s
DCOCTL = 0x00;
BCSCTL1 = CALBC1_8MHZ;
DCOCTL = CALDCO_8MHZ; //MCLK SMCLK 8M ACLK 32.768
P2SEL = UART_TXD + UART_RXD; // Timer function for TXD/RXD pins
P2DIR &= ~UART_RXD;
P2DIR |= UART_TXD;
TimerA_UART_init(); // Start Timer_A UART
__enable_interrupt();
while(1){
WDTCTL = WDT_ARST_1000;
TimerA_UART_print("123456789abcdefghijklmn.\r\n");
}
}
//------------------------------------------------------------------------------
// Function configures Timer_A for full-duplex UART operation
//------------------------------------------------------------------------------
void TimerA_UART_init(void)
{ // 在输出模式 0 中,该位直接控制输出的状态
TA1CCTL0 = OUT; // Set TXD Idle as Mark = '1'
//同步捕捉源。 该位被用于使用定时器时钟同步捕捉输入信号
//CM1捕捉模式,上升沿捕捉
//cap捕捉模式 CCIE捕捉/比较中断使能。 该位启用相应 CCIFG 标志的中断请求
TA1CCTL1 = SCS + CM1 + CAP + CCIE; // Sync, Neg Edge, Capture, Int
TA1CTL = TASSEL_2 + MC_2; // SMCLK, start in continuous mode
}
//------------------------------------------------------------------------------
// Outputs one byte using the Timer_A UART
//------------------------------------------------------------------------------
void TimerA_UART_tx(unsigned char byte)
{
while (TA1CCTL0 & CCIE); // Ensure last char got TX'd
//计算一位时间
TA1CCR0 = TA1R; // Current state of TA counter
TA1CCR0 += UART_TBIT; // One bit time till first bit
//OUT 位值
TA1CCTL0 = OUTMOD0 + CCIE; // Set TXD on EQU0, Int
txData = byte; // Load global variable
txData |= 0x100; // Add mark stop bit to TXData
txData <<= 1; // Add space start bit
}
//------------------------------------------------------------------------------
// Prints a string over using the Timer_A UART
//------------------------------------------------------------------------------
void TimerA_UART_print(char *string)
{
while (*string) {
TimerA_UART_tx(*string++);
}
}
//------------------------------------------------------------------------------
// Timer_A UART - Transmit Interrupt Handler利用输出比较中断发送一个字节数据
//------------------------------------------------------------------------------
#pragma vector = TIMER1_A0_VECTOR
__interrupt void Timer_A0_ISR(void)
{
static unsigned char txBitCnt = 10;
//增加一位时间
TA1CCR0 += UART_TBIT; // Add Offset to CCRx
if (txBitCnt == 0) { // All bits TXed?
TA1CCTL0 &= ~CCIE; // All bits TXed, disable interrupt
txBitCnt = 10; // Re-load bit counter
}
else {
if (txData & 0x01) {
TA1CCTL0 &= ~OUTMOD2; // TX Mark '1'
}
else {
TA1CCTL0 |= OUTMOD2; // TX Space '0'
}
txData >>= 1;
txBitCnt--;
}
}
//------------------------------------------------------------------------------
// Timer_A UART - Receive Interrupt Handler
//------------------------------------------------------------------------------
#pragma vector = TIMER1_A1_VECTOR
__interrupt void Timer_A1_ISR(void)
{
static unsigned char rxBitCnt = 8;
static unsigned char rxData = 0;
switch (__even_in_range(TA1IV, TA1IV_TAIFG)) { // Use calculated branching
case TA1IV_TACCR1: // TACCR1 CCIFG - UART RX
TA1CCR1 += UART_TBIT; // Add Offset to CCRx
if (TA1CCTL1 & CAP) { // Capture mode = start bit edge
TA1CCTL1 &= ~CAP; // Switch capture to compare mode
TA1CCR1 += UART_TBIT_DIV_2; // Point CCRx to middle of D0
}
else {
rxData >>= 1;
//同步捕捉输入 所选择的 CCI 输入信号由EQUx 信号锁存,并可通过该位读取
if (TA1CCTL1 & SCCI) { // Get bit waiting in receive latch
rxData |= 0x80;
}
rxBitCnt--;
if (rxBitCnt == 0) { // All bits RXed?
rxBuffer = rxData; // Store in global variable
rxBitCnt = 8; // Re-load bit counter
TA1CCTL1 |= CAP; // Switch compare to capture mode
// __bic_SR_register_on_exit(LPM0_bits); // Clear LPM0 bits from 0(SR)
}
}
break;
}
}
//------------------------------------------------------------------------------