|
一、串口调试出错
串口接收配置需要注意的是开启总中断( __enable_interrupt(); ),不能仅开启串口接收中断。
[cpp] view plain copy
#include
//**********************************************
//RS485芯片方向控制
//***********************************************
void set_rs485_dir(char tx_rx)
{ //1---发送,0接收
if (tx_rx==1)
{
P9OUT|=(BIT6+BIT7);
}
else
{
P9OUT&=~(BIT6+BIT7);
}
P9DIR|=(BIT6+BIT7);
}
void delay(char num)
{
while(num--);
}
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
//串口2 初始化
P9SEL|= BIT4+BIT5; //对于P5 6,7为模块功能
P9DIR|=BIT4;
P9DIR&=~BIT5;
UCA2CTL1 |= UCSWRST; // **Put state machine in reset**
UCA2CTL1 |= UCSSEL_2; // SMCLK
UCA2BR0 = 109;
UCA2BR1 = 0;
UCA2MCTL |= UCBRS_1 + UCBRF_0; // Modulation UCBRSx=1, UCBRFx=0
UCA2CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
UCA2IE |= UCRXIE; // Enable USCI_A0 RX interrupt
__enable_interrupt();//容易忽略
set_rs485_dir(1);//发送
delay(3000);
while (!(UCA2IFG&UCTXIFG)); // USCI_A2 TX buffer ready?
UCA2TXBUF = 0x55; // TX -> RXed character
delay(3000);
set_rs485_dir(0);//接收
delay(3000);
while(1)
{
delay(2000);
}
}
#pragma vector=USCI_A2_VECTOR
__interrupt void USCI_A2_ISR(void)
{
switch(__even_in_range(UCA2IV,4))
{
case 0:break; // Vector 0 - no interrupt
case 2: // Vector 2 - RXIFG
set_rs485_dir(1);//发送
delay(30);
while (!(UCA2IFG&UCTXIFG)); // USCI_A0 TX buffer ready?
UCA2TXBUF = UCA2RXBUF; // TX -> RXed character
set_rs485_dir(0);//接收
delay(30);
break;
case 4:break; // Vector 4 - TXIFG
default: break;
}
}
|
|