marzhs 发表于 2011-4-26 11:50:26

149 的串口调试。 用串口调试助手发送8位数据,然后再接收回来只能成功接收一次 求指

用串口调试助手 发送 8位数据,然后149接收到后,再送回电脑,调试助手却只能显示1次成功的通讯,为什么呢?
程序如下:
#include <MSP430x14x.h>
unsigned char aRxBuff;
unsigned char flag = 0;
/**********************************************************************
                           uart初始化
***********************************************************************/
void UartInit()
{
P3SEL |= 0x30;                            // P3.4,5 = USART0 TXD/RXD
UCTL0=CHAR+SWRST;       //8 位数据,1位停止位
UTCTL0=SSEL0;                //选择UCLK=ACLK
UBR00=0x3;                   //设置波特率为9600bit/s
UBR10=0;
UMCTL0=0x4A;
UCTL0 &= ~SWRST;
ME1 |= UTXE0+URXE0;          //打开模块USART0
IE1 |= URXIE0 ;            //打开USART0接收中断
}
/**********************************************************************
                           系统初始化
***********************************************************************/
void InitSys()
{
   unsigned int iq0;
   BCSCTL1 &= ~XT2OFF;                            //打开XT2振荡器
   do
   {
      IFG1 &= ~OFIFG;                           //清除振荡器失效标志
      for (iq0=0xFF; iq0>0; iq0--);               //延时,等待XT2起振
   }
   while ((IFG1 & OFIFG) != 0);                     //判断XT2是否起振
   BCSCTL2=SELM_2;                                  //选择MCLK 为XT2
   UartInit();                                    //初始化USART0
   _EINT();
}

/**********************************************************************
                               数据发送
***********************************************************************/
void SendUart(unsigned char *pBuffer,unsigned char n_byte)
{
unsigned char q0;
for(q0=0;q0<n_byte;q0++)
{
   while((IFG1&UTXIFG0)==0);//判断是否发送完毕
   TXBUF0=*pBuffer;
   pBuffer++;
}
flag = 0;
}

/**********************************************************************
                        向接收缓冲区中增加一个数据
***********************************************************************/
void AddUsData(unsigned char sq0)
{
    static unsigned char NRxBuff=0;
    while ((IFG1 & UTXIFG0) == 0);
    aRxBuff=sq0;
    NRxBuff++;
    if(NRxBuff == 8)
      flag = 1;
}
/**********************************************************************
                           USART0接收中断函数
***********************************************************************/
#pragma vector=UART0RX_VECTOR
__interrupt void Usart0Rx()
{
AddUsData(RXBUF0);
//flag = 1;
}

void main( void )
{
   WDTCTL=WDTPW+WDTHOLD;                //关闭看门狗
   InitSys();                           //初始化
   while(1)
   {   
   if(flag)
      SendUart(aRxBuff,8);       //发送数据      
   }
}

marzhs 发表于 2011-4-26 14:12:31

原因找到了,这样改就ok了
/**********************************************************************
                        向接收缓冲区中增加一个数据
***********************************************************************/
void AddUsData(unsigned char sq0)
{   
    static unsigned char NRxBuff=0;
    while ((IFG1 & UTXIFG0) == 0);
    aRxBuff=sq0;
    NRxBuff++;
    if(NRxBuff == 8)
      {
      flag = 1;
      NRxBuff=0;
       }   
}
页: [1]
查看完整版本: 149 的串口调试。 用串口调试助手发送8位数据,然后再接收回来只能成功接收一次 求指