This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

各位前辈,我用的MSP430F5438A单片机,串口发一串数据(123456789),在电脑串口助手上来的数是乱码,求助各位大师



代码如下:



#include "msp430x54x.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "PIN_DEF.H"

#define  FLL_FACTOR     549                                          // FLL_FACTOR: DCO倍频系数   
char  event, RXBuffer[2];      


//***************************************************************************//
//                                                                           //
//  RS232/485接收中断服务程序                                                //
//                                                                           //
//***************************************************************************//
#pragma vector=USCI_A3_VECTOR
__interrupt void USCI_A3_ISR(void)
{
  switch(__even_in_range(UCA3IV,4))
  {
  case 0:break;                                                     // Vector 0 - no interrupt
  case 2:                                                            // Vector 2 - RXIFG
 
    UCA3TXBUF = UCA3RXBUF;
    while (!(UCA1IFG&UCTXIFG));
      event |= 0x01;                              
      break;                                                 
  case 4:break;                                                   // Vector 4 - TXIFG
  default: break;                                       
  } 
}



//***************************************************************************
//                                                                           //
//                 初始化主时钟: MCLK = XT1×(FLL_FACTOR+1)                  //
//                                                                           //
//***************************************************************************

void Init_CLK(void)
{
  WDTCTL = WDTPW + WDTHOLD;                            // 关看门狗
  P7SEL |= 0x03;                              // 端口选择外部低频晶振XT1
  UCSCTL6 &=~XT1OFF;                             // 使能外部晶振
  UCSCTL6 |= XCAP_3;                       // 设置内部负载电容
  UCSCTL3 |= SELREF_2;                      // DCOref = REFO
  UCSCTL4 |= SELA_2;                          // ACLK   = XT1 
  __bis_SR_register(SCG0);                    // 关闭FLL控制回路
  UCSCTL0 = 0x0000;                         // 设置DCOx, MODx
  UCSCTL1 = DCORSEL_7;                       // 设置DCO振荡范围
  UCSCTL2 = FLLD__1 + FLL_FACTOR;             // Fdco = ( FLL_FACTOR + 1)×FLLRef = (649 + 1) * 32768 = 21.2992MHz
  __bic_SR_register(SCG0);                   // 打开FLL控制回路                                                           
  __delay_cycles(1024000);                 
  do
  {
    UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + XT1HFOFFG + DCOFFG); // 清除 XT2,XT1,DCO 错误标志                                                           
    SFRIFG1 &= ~OFIFG;                                   
  }while(SFRIFG1&OFIFG);                              // 检测振荡器错误标志
}


//***************************************************************************//
//                                                                           //
//  Init_Port(void): 设置IO端口                                              //
//                                                                           //
//***************************************************************************//
void Init_Port(void)
{
  P5DIR  |= POWER;                      // 主电源
  MAIN_POWER_ON;                      
  P7DIR  |= LED_PWR;                 // 发光二极管电源
  P7OUT  &=~LED_PWR;               
  INTERNAL_PULL_UP;              // 使能键盘端口内部上拉电阻
  ROW_IN_COL_OUT;              // 设置行输入,列输出0
  P11DIR |= 0x07;                           // ACLK, MCLK, SMCLK set out to pins
  P11SEL |= 0x07;                           // P11.0,1,2 for debugging purposes.
}



//***************************************************************************//
//                                                                           //
//  Init_RSUART(void): 初始化RS232/485端口                                   //
//                                                                           //
//***************************************************************************//
void Init_RSUART(void)
{
  RS_PORT_SEL |= TXD + RXD;               // 选择引脚功能
  RS_PORT_DIR |= TXD;                    // 选择引脚功能
  UCA3CTL1 = UCSWRST;                  // 状态机复位
  UCA3CTL1 |= UCSSEL_1;                  // CLK = ACLK

  UCA3BR0 = 0x03;                     // 32.768kHz/9600=3.41
  UCA3BR1 = 0x00;                         
  UCA3MCTL = UCBRS_3 + UCBRF_0;            // UCBRSx=3, UCBRFx=0
  UCA3CTL1 &= ~UCSWRST;                      // 启动状态机
  UCA3IE |= UCRXIE;                        // 允许接收中断
//  RS485_IN;                     
}


//***************************************************************************//
//                                                                           //
//  RS232TX_PROC(void): RS232端口发送程序                                    //
//                                                                           //
//***************************************************************************//
void RS232TX_PROC(char *tx_buf)
{
  unsigned char i,length;                                 
  length = strlen(tx_buf);                              
  for(i=0;i<length;i++)
  {
    UCA3TXBUF = *tx_buf++;    
    while (!(UCA3IFG&UCTXIFG));
  }
}


void main( void )
{
  WDTCTL = WDTPW + WDTHOLD;                                        
  Init_CLK_HFXT2();                                                      
  Init_Port();                                                     
  Init_RSUART();                                                   
  _EINT();                                                        
  while(1)
  {
    if((P6IN&0x0F)!=0x0F)                                            // 检测按键按下
    {
      RS232TX_PROC("hello world");                           
      __delay_cycles(5000000);                                     
    }   
  }
}

  • 这种情况,建议先检查msp430的串口配置和串口助手上的配置是否一样。

    如果配置都一样,建议先调试msp430串口发送部分的代码,调通了再加入串口接收的代码

    也可以用示波器或者逻辑分析仪看看波形