单片机串口收发字符串出现乱码,请问是什么原因导致的?如何解决?

2019-07-15 08:12发布

大家有空帮我看下我这为什么只能接收一个字符 而接收字符串 串口就会出现乱码问题, QQ图片2018.png

一下是我的代码
#include "N76E003.h"
#include "SFR_Macro.h"
#include "Common.h"
#include "Delay.h"
#include "Function_define.h"
#include "string.h"
#include "intrins.h"


#define BUFFER_SIZE                16
#define   N     2


char  UART_BUFFER[BUFFER_SIZE],temp,i;
UINT16 u16CNT=0,u16CNT1=0;
bit riflag;


/**
* FUNCTION_PURPOSE: serial interrupt, echo received data.
* FUNCTION_INPUTS: P0.7(RXD) serial input
* FUNCTION_OUTPUTS: P0.6(TXD) serial output
*/
void SerialPort0_ISR(void) interrupt 4
{
        if(RI)
        {
                RI = 0;
                UART_BUFFER[u16CNT++] = SBUF;
                if(u16CNT==N)
                {
                        riflag = 1;
                }
        }
}


/************************************************************************************************************
*    Main function
************************************************************************************************************/
void main (void)
{
                P12_PushPull_Mode;
                P06_Quasi_Mode;
                P07_Quasi_Mode;
        
                SCON = 0xD0;                        // Special setting the mode 3 and
                TMOD |= 0x20;            //Timer1 Mode1
   
                set_SMOD;                //UART0 Double Rate Enable
                set_T1M;
               clr_BRCK;                //Serial port 0 baud rate clock source = Timer1
                TH1 = 256 - (1000000/115200+1);               /*16 MHz */
                set_TR1;
        
                set_RB8;                                        //This bit is for setting the stop bit 2 high/low status,
        
                clr_TI;
                 set_ES;           //enable UART interrupt
                set_EA;           //enable global interrupt


               while(1)
                {
                        if(riflag==1)
                        {
                                ES = 0;
                                for(temp=0;temp<N;temp++)
                                {
                                        SBUF=UART_BUFFER[temp];
                                         while(!TI);
                                        TI=0;
                                }
                                temp=0;
                                ES = 1;
                                riflag = 0;
                        }
                }
}


帮指点一下,谢谢

0条回答

一周热门 更多>