串口收到指令检测单片机IO状态,再向上位机发送数据,串口发的和串口助手收到的不一致啊

2019-07-15 09:15发布

#define         MAIN_Fosc                        11059200L        //定义主时钟
#include        "STC15Fxxxx.H"
#define uchar  unsigned char
#define                Baudrate1                        9600L
#define                UART1_BUF_LENGTH        32
char buf;
u8        TX1_Cnt;        //发送计数
u8        RX1_Cnt;        //接收计数
bit        B_TX1_Busy;        //发送忙标志
bit Send_ed;

u8         idata RX1_Buffer[UART1_BUF_LENGTH];        //接收缓冲
unsigned char ref;


void        UART1_config(u8 brt);        // 选择波特率, 2: 使用timer2做波特率, 其它值: 使用Timer1做波特率.
//void         PrintString1(u8 *puts);


//========================================================================
// 函数: void main(void)
// 描述: 主函数。
// 参数: none.
// 返回: none.
// 版本: VER1.0
// 日期: 2014-11-28
// 备注:
//========================================================================

void main(void)
{
    P0M1 = 0x00;   P0M0 = 0xFF;   //设置为强推挽输出
    P1M1 = 0x00;   P1M0 = 0xFF;   //设置为强推挽输出
    P2M1 = 0x00;   P2M0 = 0xFF;   //设置为强推挽输出
    P3M1 = 0x00;   P3M0 = 0x3C;   //设置为强推挽输出
    P4M1 = 0x00;   P4M0 = 0x7F;   //设置为强推挽输出
    P5M1 = 0x00;   P5M0 = 0x3F;   //设置为强推挽输出
        P44=0;
        P04=0;
        P54=0;
        P55=0;
        P45=0;
        P53=0;
        P16=1;
        UART1_config(1);        // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.
        EA = 1;        //允许总中断
//        PrintString1("STC15F2K60S2 UART1 Test Prgramme! ");        //SUART1发送一个字符串
        ES=1;    //允许串行中断
        Send_ed=1;
        while (1)
        {               
                buf = SBUF;
                        switch(buf)
                {
              case 0x31:P44=1;P54=0;break;
                          case 0x32:P04=1;P54=0;break;
                          case 0x33:P45=1;P53=0;break;
                          case 0x34:P53=1;P45=0;break;
                          case 0x35:P45=1;P53=1;break;
                          case 0x36:P45=0;P53=0;break;
                          case 0x37:
                                   if(Send_ed==1)
                                           {
                                           Send_ed=0;
                                           if(P16==1)
                                           {
                                              ref = 0xAA;
//                                                  SBUF=0xAA;
                          SBUF = ref;
//                        Send_ed=0;                                                                                    
                                           }
                                           else
                                           {
                                              SBUF = 0xBB;
                                                  while(TI==0);
                                                  TI=0;
                                           }
                                           }

                                           break;
                          case 0x38:
                                         if(Send_ed==1)
                                    {
                                                ref=0x01;
                                                SBUF=ref;
                                                Send_ed=0;
                                                }
                                        else
                                            {
                                             SBUF=0xDD;
                                                 }
                                    break;
                          case 0x39:
                                    if(Send_ed==1)
                                                {
                                                SBUF=0xAA;
                                    Send_ed=0;
                                                }
                                    break;
                          case 0x40:SBUF=0xAA;
                                    while(TI==0);
                                                TI=0;
                                    break;
                          default:P44=0;P04=0;break;
            }
//                if((TX1_Cnt != RX1_Cnt) && (!B_TX1_Busy))        //收到数据, 发送空闲
//                {
//                        SBUF = RX1_Buffer[TX1_Cnt];                //把收到的数据远样返回
//                        B_TX1_Busy = 1;
//                        if(++TX1_Cnt >= UART1_BUF_LENGTH)        TX1_Cnt = 0;
//                }
        }
}


//========================================================================
// 函数: void PrintString1(u8 *puts)
// 描述: 串口1发送字符串函数。
// 参数: puts:  字符串指针.
// 返回: none.
// 版本: VER1.0
// 日期: 2014-11-28
// 备注:
//========================================================================
//void PrintString1(u8 *puts)        //发送一个字符串
//{
//    for (; *puts != 0;        puts++)           //遇到停止符0结束
//        {
//                SBUF = *puts;
//                B_TX1_Busy = 1;
//                while(B_TX1_Busy);
//        }
//}

//========================================================================
// 函数: SetTimer2Baudraye(u16 dat)
// 描述: 设置Timer2做波特率发生器。
// 参数: dat: Timer2的重装值.
// 返回: none.
// 版本: VER1.0
// 日期: 2014-11-28
// 备注:
//========================================================================
void        SetTimer2Baudraye(u16 dat)        // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.
{
        AUXR &= ~(1<<4);        //Timer stop
        AUXR &= ~(1<<3);        //Timer2 set As Timer
        AUXR |=  (1<<2);        //Timer2 set as 1T mode
        TH2 = dat / 256;
        TL2 = dat % 256;
        IE2  &= ~(1<<2);        //禁止中断
        AUXR |=  (1<<4);        //Timer run enable
}

//========================================================================
// 函数: void        UART1_config(u8 brt)
// 描述: UART1初始化函数。
// 参数: brt: 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.
// 返回: none.
// 版本: VER1.0
// 日期: 2014-11-28
// 备注:
//========================================================================
void        UART1_config(u8 brt)        // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.
{
        /*********** 波特率使用定时器2 *****************/
        if(brt == 2)
        {
                AUXR |= 0x01;                //S1 BRT Use Timer2;
                SetTimer2Baudraye(65536UL - (MAIN_Fosc / 4) / Baudrate1);
        }

        /*********** 波特率使用定时器1 *****************/
        else
        {
                TR1 = 0;
                AUXR &= ~0x01;                //S1 BRT Use Timer1;
                AUXR |=  (1<<6);        //Timer1 set as 1T mode
                TMOD &= ~(1<<6);        //Timer1 set As Timer
                TMOD &= ~0x30;                //Timer1_16bitAutoReload;
                TH1 = (u8)((65536UL - (MAIN_Fosc / 4) / Baudrate1) / 256);
                TL1 = (u8)((65536UL - (MAIN_Fosc / 4) / Baudrate1) % 256);
                ET1 = 0;        //禁止中断
                INT_CLKO &= ~0x02;        //不输出时钟
                TR1  = 1;
        }
        /*************************************************/

        SCON = (SCON & 0x3f) | 0x40;        //UART1模式, 0x00: 同步移位输出, 0x40: 8位数据,可变波特率, 0x80: 9位数据,固定波特率, 0xc0: 9位数据,可变波特率
//        PS  = 1;        //高优先级中断
        ES  = 1;        //允许中断
        REN = 1;        //允许接收
        P_SW1 &= 0x3f;
        P_SW1 |= 0x00;                //UART1 switch to, 0x00: P3.0 P3.1, 0x40: P3.6 P3.7, 0x80: P1.6 P1.7 (必须使用内部时钟)
//        PCON2 |=  (1<<4);        //内部短路RXD与TXD, 做中继, ENABLE,DISABLE

        B_TX1_Busy = 0;
        TX1_Cnt = 0;
        RX1_Cnt = 0;
}


//========================================================================
// 函数: void UART1_int (void) interrupt UART1_VECTOR
// 描述: UART1中断函数。
// 参数: nine.
// 返回: none.
// 版本: VER1.0
// 日期: 2014-11-28
// 备注:
//========================================================================
void UART1_int (void) interrupt 4
{
        if(RI)
        {
                RI = 0;
                RX1_Buffer[RX1_Cnt] = SBUF;
                if(++RX1_Cnt >= UART1_BUF_LENGTH)        RX1_Cnt = 0;        //防溢出
        }

        if(TI)
        {
                TI =0;
                Send_ed =1 ;
                B_TX1_Busy = 0;
        }
}


0条回答

一周热门 更多>