msp430串口接收调试

一、串口调试出错 

        串口接收配置需要注意的是开启总中断( __enable_interrupt(); ),不能仅开启串口接收中断。

#include <msp430f5438a.h>
//**********************************************
//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;
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值