单片机/MCU论坛
直播中

bobo2021

10年用户 72经验值
擅长:控制/MCU
私信 关注
[问答]

stc15w4k32s4单片机串口2只接收1次数据,求指教为什么

#include #include "LCD12864.h"#include "GPS_Analyse.h"#include "string.h"#include "uart.h"#include "delay.h"
extern GPS_INFO   GPS;  //外部结构体
#define Buf1_Max 20                                        //串口1,PC缓存长度,存放命令数据#define Buf2_Max 80                                        //串口2,BD缓存长度,存放GNRMC数据


/*********定义变量********///                                    0    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27//********************************    "    *    2    8    L    5    0    0    0    0    d    0    0    m    0    0    s    0    0    d    0    0    m    0    0    s    *    "unsigned char Location_Buf[29]  = {0x22,0x2a,0x32,0x38,0x4c,0x35,0x30,0x30,0x30,0x30,0x64,0x30,0x30,0x6d,0x30,0x30,0x73,0x30,0x30,0x64,0x30,0x30,0x6d,0x30,0x30,0x73,0x2a,0x22,0x00};//********************************    "    *    1    2    R    5    0    0    -    *    "
unsigned char Times = 0,shijian = 0;unsigned char Val_CSQ = 0;                        //信号质量变量unsigned char Time_Refresh = 0;                //刷新时间控制变量

/*********Uart1相关变量**********/unsigned char PC_NUM = 0;                         //数组下标unsigned char PC_REV_START = 0;                //接收开始标志xdata unsigned char Uart1_Buf[Buf1_Max];       //接收缓存,数组累加变量//xdata unsigned char Temp_Buf1[Buf1_Max];           //
/*********Uart2相关变量**********/unsigned char BD_NUM = 0;                         //数组下标unsigned char BD_REV_START = 0;     //接收开始标志xdata unsigned char Uart2_Buf[Buf2_Max];       //接收缓存,数组累加变量xdata unsigned char Temp_Buf2[Buf2_Max];           //
/*********各类标志**********/bdata unsigned int Flag;                //定时器标志位,用bdata定义可以位寻址***it Timer0_Start = Flag^0;                //定时器0延时启动计数器标志***it Display_CSQ  = Flag^1;         //CQS数值显示标志***it PC_FLAG          = Flag^2;                //PC处理标志***it BD_FLAG          = Flag^3;         //GPS处理标志
***it REV_LOC_ASK  = Flag^6;                //接收坐标应答标志
unsigned char Val_Location = 5;                //接收上位机坐标信息应答标志,初始值不为0/1的其他数

/*********定义接口********/***it Net_Led = P2^0;                                //网络运行状态灯
/*********函数声明********/void Timer0_Init(void);                                //定时器0初始化void CLR_Buf1(void);                                //清空串口1接收的数据void CLR_Buf2(void);                                //清空串口2接收的数据
void Report_Location();                //上PC上报坐标数据
void IO_mode(void);
void main(){        unsigned char i = 0;        IO_mode();        CLR_Buf1();        Uart1_PC_Init();                Uart2_BD_Init();
        Timer0_Init();        Flag = 0;
        LcdInit();
        /*********GSM模块初始设置************/        EA = 1;
        Write_Com(0x8A);        HZkdis("Success!  ");                Delay_ms(3000);                        Lcd_clear();        LcdInit();                Write_Com(0x80);        HZkdis("系统状态");

        while(1)        {                if(Display_CSQ)                        //5秒种刷新1次CSQ值和坐标值                {
                        Val_Location = GPS_RMC_Parse(Uart2_Buf,&GPS);        //如果定位信息解析成功,返回1                        if(Val_Location == 1)                        {                                GPS_Display();                                if(!REV_LOC_ASK)                //如果PC没有返回总控坐标回执,就再发一直发                                {                                        Report_Location();                                        Write_Com(0x98);                                        HZkdis("消息: ");                                }                                CLR_Buf2();                        }                        Display_CSQ = 0;                        Write_Com(0x9e);        Write_Data(0x30 + Val_Location);                //测试Val_Location值的,只第1次为1,之后就是0.不知为什么,求指教                }
        }}
/******************************************************************************** 函数名 : Report_Location* 描述   : MCU收到BD的坐标数据后,发送给PC*******************************************************************************/void Report_Location(void){//        经度        Location_Buf[7]  = GPS.longitude_Degree/100 + 0x30; Location_Buf[8]  = GPS.longitude_Degree/10%10 + 0x30; Location_Buf[9] = GPS.longitude_Degree%10 + 0x30;         Location_Buf[11] = GPS.longitude_Cent/10 + 0x30;    Location_Buf[12] = GPS.longitude_Cent%10 + 0x30;         Location_Buf[14] = GPS.longitude_Second/10 + 0x30;        Location_Buf[15] = GPS.longitude_Second%10 + 0x30;        //        纬度        Location_Buf[17] = GPS.latitude_Degree/10 + 0x30; Location_Buf[18] = GPS.latitude_Degree%10 + 0x30;         Location_Buf[20] = GPS.latitude_Cent/10 + 0x30;   Location_Buf[21] = GPS.latitude_Cent%10 + 0x30;                Location_Buf[23] = GPS.latitude_Second/10 + 0x30; Location_Buf[24] = GPS.latitude_Second%10 + 0x30;                UART1_PC_SendString(Location_Buf);                REV_LOC_ASK = 0;}/******************************************************************************** 函数名 : Uart2_BD_Receive* 描述   : 串口2中断服务入口函数,处理BD模块发来的数据*******************************************************************************/void Uart2_BD_Receive(void) interrupt 8{        unsigned char ch;        IE2 &= ~0X01;        if (S2CON & S2RI)        {                S2CON &= ~S2RI;         //清除S2RI位                ch = S2BUF;                    if ((ch == 'R') && (BD_FLAG == 0))  //如果收到字符'R',便开始接收                  {                        Uart2_Buf[0] = '$';Uart2_Buf[1] = 'G';Uart2_Buf[2] = 'N';                        BD_REV_START = 1;                        BD_NUM = 3;                }                if (BD_REV_START == 1)  //标志位为1,开始接收                            {                        Uart2_Buf[BD_NUM++] = ch;  //字符存到数组中                                                SBUF=ch;                                                while(TI!=1);        //等待发送成功                        TI=0;                                                if (ch == '*')                     //如果接收到*号,说明接收完成。                        {                                Uart2_Buf[BD_NUM] = '';                                BD_REV_START = 0;                                BD_FLAG  = 1;                                BD_NUM = 0;                        }                }        }        if(S2CON&S2TI)        {                S2CON &= ~S2TI;         //TI清0,重新接收         }        IE2 |= 0X01;        }/******************************************************************************** 函数名 : Uart1 * 描述   : 串口1中断服务入口函数,处理PC发来的数据*******************************************************************************/void Uart1() interrupt 4{        unsigned char ch;    if (RI)    {        RI = 0;                 //清除RI位                ch = SBUF;                    if ((ch == '"') && (PC_FLAG == 0))  //如果收到起始符0XFF,便开始接收                  {                        PC_REV_START = 1;                }                if (PC_REV_START == 1)                  //标志位为1,开始接收                            {                        Uart1_Buf[PC_NUM++] = ch;  //字符存到数组中                        if (ch == '')             //如果接收到0xEE号,说明接收完成。                        {                                PC_REV_START = 0;                                PC_FLAG  = 1;                                PC_NUM = 0;                        }                }
    }    if (TI)    {        TI = 0;                 //清除TI位    }}
void CLR_Buf1(void){        unsigned int k;        for(k=0;k        {                Uart1_Buf[k] = 0x00;        }    PC_NUM = 0;             //接收字符串的起始存储位置}
void CLR_Buf2(void){        unsigned int k;        for(k=0;k        {                Uart2_Buf[k] = 0x00;        }    BD_NUM = 0;             //接收字符串的起始存储位置}
/******************************************************************************** 函数名 : Timer0_ISR* 描述   : 定时器0中断服务入口函数,20ms中断一次,产生1秒1次(20X50)的闪烁效果*******************************************************************************/void Timer0_ISR() interrupt 1{        static unsigned char Time_Count=0;         TR0=0;//关定时器        Time_Count++;        if(Time_Count>=50)                //开关后,网络状态灯闪烁        {                Time_Count = 0;                Net_Led =~Net_Led;                Time_Refresh++;                if(Time_Refresh == 5)                {                        Display_CSQ = 1;                        Time_Refresh = 0;                }        }        if(Timer0_Start)        {                Times++;        }        if(Times > (50*shijian))        {                Timer0_Start = 0;                Times = 0;                Time_Count = 0;        }        TR0=1;//开定时器}void Timer0_Init(void)                //20毫秒@22.1184MHz{        AUXR &= 0x7F;        //12T模式        TMOD &= 0xF0;        //设置定时器模式 16位重载        TL0 = 0x00;                //设定定时器初值        TH0 = 0x70;                //设定定时器初值        TF0 = 0;                //清除TF0标志        TR0 = 1;                //定时器0开始计时        ET0 = 1;            //使能定时器0中断}void IO_mode(void){        P0M0 = 0;        P0M1 = 0;        P1M0 = 0;        P1M1 = 0;        P2M1 = 0;        P2M0 = 0;        P3M0 = 0;        P3M1 = 0;        P4M0 = 0;        P4M1 = 0;        P5M0 = 0;        P5M1 = 0;        P6M0 = 0;        P6M1 = 0;        P7M0 = 0;        P7M1 = 0;}

回帖(7)

人中狼

2018-4-28 00:38:05
判断条件是,if ((ch == 'R') && (BD_FLAG == 0))  //如果收到字符'R',便开始接收
但最后是BD_FLAG  = 1;
举报

bobo2021

2018-4-28 09:56:47
这么乱,整理一下
#include
#include "LCD12864.h"
#include "GPS_Analyse.h"
#include "string.h"
#include "uart.h"
#include "delay.h"
extern GPS_INFO   GPS;  //外部结构体
#define Buf1_Max 20                                        //串口1,PC缓存长度,存放命令数据#define Buf2_Max 80                                        //串口2,BD缓存长度,存放GNRMC数据


/*********定义变量********///                                    0    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27//********************************    "    *    2    8    L    5    0    0    0    0    d    0    0    m    0    0    s    0    0    d    0    0    m    0    0    s    *    "unsigned char Location_Buf[29]  = {0x22,0x2a,0x32,0x38,0x4c,0x35,0x30,0x30,0x30,0x30,0x64,0x30,0x30,0x6d,0x30,0x30,0x73,0x30,0x30,0x64,0x30,0x30,0x6d,0x30,0x30,0x73,0x2a,0x22,0x00};//********************************    "    *    1    2    R    5    0    0    -    *    "
unsigned char Times = 0,shijian = 0;unsigned char Val_CSQ = 0;                        //信号质量变量unsigned char Time_Refresh = 0;                //刷新时间控制变量

/*********Uart1相关变量**********/unsigned char PC_NUM = 0;                         //数组下标unsigned char PC_REV_START = 0;                //接收开始标志xdata unsigned char Uart1_Buf[Buf1_Max];       //接收缓存,数组累加变量//xdata unsigned char Temp_Buf1[Buf1_Max];           //
/*********Uart2相关变量**********/unsigned char BD_NUM = 0;                         //数组下标unsigned char BD_REV_START = 0;     //接收开始标志xdata unsigned char Uart2_Buf[Buf2_Max];       //接收缓存,数组累加变量xdata unsigned char Temp_Buf2[Buf2_Max];           //
/*********各类标志**********/bdata unsigned int Flag;                //定时器标志位,用bdata定义可以位寻址***it Timer0_Start = Flag^0;                //定时器0延时启动计数器标志***it Display_CSQ  = Flag^1;         //CQS数值显示标志***it PC_FLAG          = Flag^2;                //PC处理标志***it BD_FLAG          = Flag^3;         //GPS处理标志
***it REV_LOC_ASK  = Flag^6;    //接收坐标应答标志
unsigned char Val_Location = 5;   //接收上位机坐标信息应答标志,初始值不为0/1的其他数

/*********定义接口********/
***it Net_Led = P2^0;                                //网络运行状态灯
/*********函数声明********/
void Timer0_Init(void);       //定时器0初始化
void CLR_Buf1(void);          //清空串口1接收的数据
void CLR_Buf2(void);          //清空串口2接收的数据
void Report_Location();      //上PC上报坐标数据
void IO_mode(void);
void main()
{        
unsigned char i = 0;        
IO_mode();        
CLR_Buf1();      
Uart1_PC_Init();              
Uart2_BD_Init();
Timer0_Init();        
Flag = 0;
        LcdInit();
        /*********GSM模块初始设置************/        
EA = 1;
        Write_Com(0x8A);        HZkdis("Success!  ");                Delay_ms(3000);                        
Lcd_clear();      
LcdInit();               
Write_Com(0x80);        HZkdis("系统状态");

        while(1)
        {
                if(Display_CSQ)                        //5秒种刷新1次CSQ值和坐标值                {
                        Val_Location = GPS_RMC_Parse(Uart2_Buf,&GPS);        //如果定位信息解析成功,返回1
                        if(Val_Location == 1)
                       {
                                GPS_Display();
                               if(!REV_LOC_ASK)                //如果PC没有返回总控坐标回执,就再发一直发
                                {
                                       Report_Location();
                                       Write_Com(0x98);
                                       HZkdis("消息: ");
                                }
                               CLR_Buf2();
                        }
                        Display_CSQ = 0;
                        Write_Com(0x9e);
       Write_Data(0x30 + Val_Location);                //测试Val_Location值的,只第1次为1,之后就是0.不知为什么,求指教                }
        }}
/******************************************************************************** 函数名 : Report_Location* 描述   : MCU收到BD的坐标数据后,发送给PC*******************************************************************************/void Report_Location(void){//        经度        Location_Buf[7]  = GPS.longitude_Degree/100 + 0x30; Location_Buf[8]  = GPS.longitude_Degree/10%10 + 0x30; Location_Buf[9] = GPS.longitude_Degree%10 + 0x30;         Location_Buf[11] = GPS.longitude_Cent/10 + 0x30;    Location_Buf[12] = GPS.longitude_Cent%10 + 0x30;         Location_Buf[14] = GPS.longitude_Second/10 + 0x30;        Location_Buf[15] = GPS.longitude_Second%10 + 0x30;        //        纬度        Location_Buf[17] = GPS.latitude_Degree/10 + 0x30; Location_Buf[18] = GPS.latitude_Degree%10 + 0x30;         Location_Buf[20] = GPS.latitude_Cent/10 + 0x30;   Location_Buf[21] = GPS.latitude_Cent%10 + 0x30;                Location_Buf[23] = GPS.latitude_Second/10 + 0x30; Location_Buf[24] = GPS.latitude_Second%10 + 0x30;                UART1_PC_SendString(Location_Buf);                REV_LOC_ASK = 0;}/******************************************************************************** 函数名 : Uart2_BD_Receive* 描述   : 串口2中断服务入口函数,处理BD模块发来的数据*******************************************************************************/void Uart2_BD_Receive(void) interrupt 8{        unsigned char ch;        IE2 &= ~0X01;        if (S2CON & S2RI)        {                S2CON &= ~S2RI;         //清除S2RI位                ch = S2BUF;                    if ((ch == 'R') && (BD_FLAG == 0))  //如果收到字符'R',便开始接收                  {                        Uart2_Buf[0] = '$';Uart2_Buf[1] = 'G';Uart2_Buf[2] = 'N';                        BD_REV_START = 1;                        BD_NUM = 3;                }                if (BD_REV_START == 1)  //标志位为1,开始接收                            {                        Uart2_Buf[BD_NUM++] = ch;  //字符存到数组中                                                SBUF=ch;                                                while(TI!=1);        //等待发送成功                        TI=0;                                                if (ch == '*')                     //如果接收到*号,说明接收完成。                        {                                Uart2_Buf[BD_NUM] = '';                                BD_REV_START = 0;                                BD_FLAG  = 1;                                BD_NUM = 0;                        }                }        }        if(S2CON&S2TI)        {                S2CON &= ~S2TI;         //TI清0,重新接收         }        IE2 |= 0X01;        }/******************************************************************************** 函数名 : Uart1 * 描述   : 串口1中断服务入口函数,处理PC发来的数据*******************************************************************************/void Uart1() interrupt 4{        unsigned char ch;    if (RI)    {        RI = 0;                 //清除RI位                ch = SBUF;                    if ((ch == '"') && (PC_FLAG == 0))  //如果收到起始符0XFF,便开始接收                  {                        PC_REV_START = 1;                }                if (PC_REV_START == 1)                  //标志位为1,开始接收                            {                        Uart1_Buf[PC_NUM++] = ch;  //字符存到数组中                        if (ch == '')             //如果接收到0xEE号,说明接收完成。                        {                                PC_REV_START = 0;                                PC_FLAG  = 1;                                PC_NUM = 0;                        }                }
    }    if (TI)    {        TI = 0;                 //清除TI位    }}
void CLR_Buf1(void){        unsigned int k;        for(k=0;k void CLR_Buf2(void){        unsigned int k;        for(k=0;k /******************************************************************************** 函数名 : Timer0_ISR* 描述   : 定时器0中断服务入口函数,20ms中断一次,产生1秒1次(20X50)的闪烁效果*******************************************************************************/void Timer0_ISR() interrupt 1{        static unsigned char Time_Count=0;         TR0=0;//关定时器        Time_Count++;        if(Time_Count>=50)                //开关后,网络状态灯闪烁        {                Time_Count = 0;                Net_Led =~Net_Led;                Time_Refresh++;                if(Time_Refresh == 5)                {                        Display_CSQ = 1;                        Time_Refresh = 0;                }        }        if(Timer0_Start)        {                Times++;        }        if(Times > (50*shijian))        {                Timer0_Start = 0;                Times = 0;                Time_Count = 0;        }        TR0=1;//开定时器}void Timer0_Init(void)                //20毫秒@22.1184MHz{        AUXR &= 0x7F;        //12T模式        TMOD &= 0xF0;        //设置定时器模式 16位重载        TL0 = 0x00;                //设定定时器初值        TH0 = 0x70;                //设定定时器初值        TF0 = 0;                //清除TF0标志        TR0 = 1;                //定时器0开始计时        ET0 = 1;            //使能定时器0中断}void IO_mode(void){        P0M0 = 0;        P0M1 = 0;        P1M0 = 0;        P1M1 = 0;        P2M1 = 0;        P2M0 = 0;        P3M0 = 0;        P3M1 = 0;        P4M0 = 0;        P4M1 = 0;        P5M0 = 0;        P5M1 = 0;        P6M0 = 0;        P6M1 = 0;        P7M0 = 0;        P7M1 = 0;}
举报

bobo2021

2018-4-28 09:57:52
这么乱自已都看晕了
举报

bobo2021

2018-4-28 09:58:37
#include
#include "LCD12864.h"
#include "GPS_Analyse.h"
#include "string.h"
#include "uart.h"
#include "delay.h"

extern GPS_INFO   GPS;  //外部结构体

#define Buf1_Max 20                                        //串口1,PC缓存长度,存放命令数据
#define Buf2_Max 80                                        //串口2,BD缓存长度,存放GNRMC数据



/*********定义变量********/
//                                    0    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27
//********************************    "    *    2    8    L    5    0    0    0    0    d    0    0    m    0    0    s    0    0    d    0    0    m    0    0    s    *    "
unsigned char Location_Buf[29]  = {0x22,0x2a,0x32,0x38,0x4c,0x35,0x30,0x30,0x30,0x30,0x64,0x30,0x30,0x6d,0x30,0x30,0x73,0x30,0x30,0x64,0x30,0x30,0x6d,0x30,0x30,0x73,0x2a,0x22,0x00};


unsigned char Times = 0,shijian = 0;
unsigned char Val_CSQ = 0;                        //信号质量变量
unsigned char Time_Refresh = 0;                //刷新时间控制变量


/*********Uart1相关变量**********/
unsigned char PC_NUM = 0;                         //数组下标
unsigned char PC_REV_START = 0;                //接收开始标志
xdata unsigned char Uart1_Buf[Buf1_Max];       //接收缓存,数组累加变量
//xdata unsigned char Temp_Buf1[Buf1_Max];           //

/*********Uart2相关变量**********/
unsigned char BD_NUM = 0;                         //数组下标
unsigned char BD_REV_START = 0;     //接收开始标志
xdata unsigned char Uart2_Buf[Buf2_Max];       //接收缓存,数组累加变量
xdata unsigned char Temp_Buf2[Buf2_Max];           //

/*********各类标志**********/
bdata unsigned int Flag;                //定时器标志位,用bdata定义可以位寻址
***it Timer0_Start = Flag^0;                //定时器0延时启动计数器标志
***it Display_CSQ  = Flag^1;         //CQS数值显示标志
***it PC_FLAG          = Flag^2;                //PC处理标志
***it BD_FLAG          = Flag^3;         //GPS处理标志

***it REV_LOC_ASK  = Flag^6;                //接收坐标应答标志

unsigned char Val_Location = 5;                //接收上位机坐标信息应答标志,初始值不为0/1的其他数


/*********定义接口********/
***it Net_Led = P2^0;                                //网络运行状态灯

/*********函数声明********/
void Timer0_Init(void);                                //定时器0初始化
void CLR_Buf1(void);                                //清空串口1接收的数据
void CLR_Buf2(void);                                //清空串口2接收的数据

void Report_Location();                //上PC上报坐标数据

void IO_mode(void);
举报

bobo2021

2018-4-28 09:59:09

void main()
{
        unsigned char i = 0;
        IO_mode();
        CLR_Buf1();
        Uart1_PC_Init();       
        Uart2_BD_Init();

        Timer0_Init();
        Flag = 0;

        LcdInit();

        /*********GSM模块初始设置************/
        EA = 1;

        Write_Com(0x8A);
        HZkdis("Success!  ");
       
        Delay_ms(3000);       
       
        Lcd_clear();
        LcdInit();
       
        Write_Com(0x80);
        HZkdis("系统状态");


        while(1)
        {
                if(Display_CSQ)                        //5秒种刷新1次CSQ值和坐标值
                {

                        Val_Location = GPS_RMC_Parse(Uart2_Buf,&GPS);        //如果定位信息解析成功,返回1
                        if(!REV_LOC_ASK)                //如果PC没有返回总控坐标回执,就再发一直发
                        {
                                Report_Location();
                                Write_Com(0x98);
                                HZkdis("消息: ");
                        }
                        CLR_Buf2();
                        if(Val_Location == 1)
                        {
                                GPS_Display();
//                                if(!REV_LOC_ASK)                //如果PC没有返回总控坐标回执,就再发一直发
//                                {
//                                        Report_Location();
//                                        Write_Com(0x98);
//                                        HZkdis("消息: ");
//                                }
                                CLR_Buf2();
                        }
                        Display_CSQ = 0;
                        Write_Com(0x9e);        Write_Data(0x30 + Val_Location);                //测试Val_Location值的,只第1次为1,之后就是0.不知为什么,求指教
                }

        }
}

/*******************************************************************************
* 函数名 : Report_Location
* 描述   : MCU收到BD的坐标数据后,发送给PC
*******************************************************************************/
void Report_Location(void)
{
//        经度
        Location_Buf[7]  = GPS.longitude_Degree/100 + 0x30; Location_Buf[8]  = GPS.longitude_Degree/10%10 + 0x30; Location_Buf[9] = GPS.longitude_Degree%10 + 0x30;
        Location_Buf[11] = GPS.longitude_Cent/10 + 0x30;    Location_Buf[12] = GPS.longitude_Cent%10 + 0x30;
        Location_Buf[14] = GPS.longitude_Second/10 + 0x30;        Location_Buf[15] = GPS.longitude_Second%10 + 0x30;
       
//        纬度
        Location_Buf[17] = GPS.latitude_Degree/10 + 0x30; Location_Buf[18] = GPS.latitude_Degree%10 + 0x30;
        Location_Buf[20] = GPS.latitude_Cent/10 + 0x30;   Location_Buf[21] = GPS.latitude_Cent%10 + 0x30;       
        Location_Buf[23] = GPS.latitude_Second/10 + 0x30; Location_Buf[24] = GPS.latitude_Second%10 + 0x30;
       
        UART1_PC_SendString(Location_Buf);
       
        REV_LOC_ASK = 0;
}
举报

bobo2021

2018-4-28 09:59:41
/*******************************************************************************
* 函数名 : Uart2_BD_Receive
* 描述   : 串口2中断服务入口函数,处理BD模块发来的数据
*******************************************************************************/
void Uart2_BD_Receive(void) interrupt 8
{
        unsigned char ch;
        IE2 &= ~0X01;
        if (S2CON & S2RI)
        {
                S2CON &= ~S2RI;         //清除S2RI位
                ch = S2BUF;   
                if ((ch == 'R') && (BD_FLAG == 0))  //如果收到字符'R',便开始接收  
                {
                        Uart2_Buf[0] = '$';Uart2_Buf[1] = 'G';Uart2_Buf[2] = 'N';
                        BD_REV_START = 1;
                        BD_NUM = 3;
                }
                if (BD_REV_START == 1)  //标志位为1,开始接收            
                {
                        Uart2_Buf[BD_NUM++] = ch;  //字符存到数组中
                       
                        SBUF=ch;                       
                        while(TI!=1);        //等待发送成功
                        TI=0;
                       
                        if (ch == '*')                     //如果接收到*号,说明接收完成。
                        {
                                Uart2_Buf[BD_NUM] = '';
                                BD_REV_START = 0;
                                BD_FLAG  = 1;
                                BD_NUM = 0;
                        }
                }
        }
        if(S2CON&S2TI)
        {
                S2CON &= ~S2TI;         //TI清0,重新接收
        }
        IE2 |= 0X01;       
}
/*******************************************************************************
* 函数名 : Uart1
* 描述   : 串口1中断服务入口函数,处理PC发来的数据
*******************************************************************************/
void Uart1() interrupt 4
{
        unsigned char ch;
    if (RI)
    {
        RI = 0;                 //清除RI位
                ch = SBUF;   
                if ((ch == '"') && (PC_FLAG == 0))  //如果收到起始符0XFF,便开始接收  
                {
                        PC_REV_START = 1;
                }
                if (PC_REV_START == 1)                  //标志位为1,开始接收            
                {
                        Uart1_Buf[PC_NUM++] = ch;  //字符存到数组中
                        if (ch == '')             //如果接收到0xEE号,说明接收完成。
                        {
                                PC_REV_START = 0;
                                PC_FLAG  = 1;
                                PC_NUM = 0;
                        }
                }

    }
    if (TI)
    {
        TI = 0;                 //清除TI位
    }
}
/*******************************************************************************
* 函数名 : CLR_Buf1
* 描述   : 清除串口1缓存数据
*******************************************************************************/
void CLR_Buf1(void)
{
        unsigned int k;
        for(k=0;k         {
                Uart1_Buf[k] = 0x00;
        }
    PC_NUM = 0;             //接收字符串的起始存储位置
}
/*******************************************************************************
* 函数名 : CLR_Buf2
* 描述   : 清除串口2缓存数据
*******************************************************************************/
void CLR_Buf2(void)
{
        unsigned int k;
        for(k=0;k         {
                Uart2_Buf[k] = 0x00;
        }
    BD_NUM = 0;             //接收字符串的起始存储位置
}

/*******************************************************************************
* 函数名 : Timer0_ISR
* 描述   : 定时器0中断服务入口函数,20ms中断一次,产生1秒1次(20X50)的闪烁效果
*******************************************************************************/
void Timer0_ISR() interrupt 1
{
        static unsigned char Time_Count=0;
        TR0=0;//关定时器
        Time_Count++;
        if(Time_Count>=50)                //开关后,网络状态灯闪烁
        {
                Time_Count = 0;
                Net_Led =~Net_Led;
                Time_Refresh++;
                if(Time_Refresh == 5)
                {
                        Display_CSQ = 1;
                        Time_Refresh = 0;
                }
        }
        if(Timer0_Start)
        {
                Times++;
        }
        if(Times > (50*shijian))
        {
                Timer0_Start = 0;
                Times = 0;
                Time_Count = 0;
        }
        TR0=1;//开定时器
}
void Timer0_Init(void)                //20毫秒@22.1184MHz
{
        AUXR &= 0x7F;        //12T模式
        TMOD &= 0xF0;        //设置定时器模式 16位重载
        TL0 = 0x00;                //设定定时器初值
        TH0 = 0x70;                //设定定时器初值
        TF0 = 0;                //清除TF0标志
        TR0 = 1;                //定时器0开始计时
        ET0 = 1;            //使能定时器0中断
}
void IO_mode(void)
{
        P0M0 = 0;        P0M1 = 0;        P1M0 = 0;        P1M1 = 0;
        P2M1 = 0;        P2M0 = 0;        P3M0 = 0;        P3M1 = 0;
        P4M0 = 0;        P4M1 = 0;        P5M0 = 0;        P5M1 = 0;
        P6M0 = 0;        P6M1 = 0;        P7M0 = 0;        P7M1 = 0;
}
举报

bobo2021

2018-4-28 20:25:45
引用: 人中狼 发表于 2018-4-28 10:02
判断条件是,if ((ch == 'R') && (BD_FLAG == 0))  //如果收到字符'R',便开始接收
但最后是BD_FLAG  = 1;

大神一眼就找到问题的根源。今天我找了一天,发现就是这个问题,导致收不到数据。
举报

更多回帖

×
20
完善资料,
赚取积分