找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 747|回复: 2
收起左侧

为什么单片机串口二没有通信还是串口一通信

[复制链接]
ID:1012516 发表于 2022-5-24 16:41 | 显示全部楼层 |阅读模式
#include "stc12c5a60s2.h"
#include <intrins.h>

typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsigned long uint32;
typedef signed char int8;
typedef signed int int16;
typedef signed long int32;

sbit Red=P2^7;
sbit Green=P2^6;
sbit Yellow=P2^5;
sbit Sound=P2^4;
sbit RS485_DIR = P1^5;  
sbit WDI=P0^7;
sbit receiveok=P2^0;
sbit sendok=P2^1;
// sbit RS485_RXD=P1^
// sbit RS485_TXD=P1^

uint8 M=0,R=0,Y=0,G=0,RY=0,RG=0,YG=0,RGY=0;S=0;
uint8 a=0,b=0,c=0,d=0,e=0;
uint16 t=0;
uint16 t1=0;
uint16 t2=0;
uint32 t3=0;
uint8 light;
uint8 flaglight;
uint8 flagboma;

bit flagframe = 0;      
bit flagTxd = 0;   
uint8 flag=0;
bit flag1=0;
uint8 T0RH;     
uint8 T0RL;

uint8 code tab1[1] = {0x22};   
uint8 code tab2[2] = {0x88,0x99};
uint8 res=0;
uint8 cntRxd = 0;   
uint8 bufRxd[64];
uint8 pdata buf[40];
#define S2RI 0x01  
#define S2TI 0x02

void UartRxdMonitor(uint8 ms);
void ConfigTimer0(uint16 ms);
void ConfigUart(uint16 baud);
//void UartDriver(
void Delays(uint16 xms);
void UartWrite(uint8 *ptr, uint8 len);
void ConfigUart2();

uint8 GetLightState()
{

        if(t>=2000)
        {
                if(S>=1)   
                {
                                flaglight=0x05;
                                t=0;
                }
                else if(R>=1||RY>=1||RG>=1||RGY>=1)
                {
                        flaglight =0x01;
                        t=0;
                }
                else if(((Y>=1)||(YG>=1))&&(R==0)&&(RY==0)&&(RG==0)&&(RGY==0))
                {
                        flaglight=0x02;
                        t=0;
                }
                else if((G>=1)&&(R==0)&&(RY==0)&&(RG==0)&&(RGY==0)&&(Y==0)&&(YG==0))
                {
                        flaglight=0x03;
                        t=0;
                }        
                else if((M>=1)&&(G==0)&&(R==0)&&(RY==0)&&(RG==0)&&(RGY==0)&&(Y==0)&&(YG==0))
                {
                        flaglight=0x04;
                        t=0;
                }        
               
                R=0; Y=0;        G=0; M=0; RY=0; RG=0;        YG=0; RGY=0;S=0;
  }
        return flaglight;
}

void watchdog()
{
   WDI=~WDI;
}

// void Delay1000ms()              
//
//         unsigned char i, j,

//         i = 4
//         j =
//         k = 20
//         
//        
//                 
//               
//                         while (--k
//                 } while (--j
//         } while (--i
//

void Delay1000ms()               
{
        unsigned char i, j, k;

        i = 22;
        j = 3;
        k = 227;
        do
        {
                do
                {
                        while (--k);
                } while (--j);
        } while (--i);
}

void main()
{
        EA = 1;   
//         IPH=0x1
//         IP=0x0
        PCON=0x00;
//         IPH=0x0
//         IP=0x0
//     IP2H=0x01;
//         IP2=0x0
        ConfigTimer0(1);  
//         ConfigUart(9600);
  ConfigUart2();
        watchdog();
        
        while(1)
        {                           
//        WDT_CONTR = 0X3
          flagboma=(~P0)&0x0f;

//                  UartWrite(&flagboma,1
                watchdog();               
                flag=GetLightState();
                if(flag1==1)
                {
                        flag1=0;
                        UartWrite(tab1,1);
                        UartWrite(&flag,1);
                        UartWrite(&flagboma,1);
                        UartWrite(tab2,2);
                  watchdog();
                        sendok=0;
                        Delay1000ms();
                        sendok=1;
    }
//                 if(t2>210
                if(t2>500)        
                {
                        t2=0;
                        
                        if(flag==0x01)  
                        {
                                if(a<1)
                                {
                                        a++;
                                        UartWrite(tab1,1);
                                        UartWrite(&flag,1);
                                        UartWrite(&flagboma,1);
                                        UartWrite(tab2,2);
                                        watchdog();
                                        sendok=0;
                                        Delay1000ms();
                                        sendok=1;
                                        b=0;
                                        c=0;
                                        d=0;
                                         e=0;
                                        t3=0;
                                }
                        }
                        if(flag==0x02)   
                        {
                                if(b<1)
                                {
                                        b++;
                                        UartWrite(tab1,1);
                                        UartWrite(&flag,1);
                                        UartWrite(&flagboma,1);
                                        UartWrite(tab2,2);
                                        watchdog();
                                        sendok=0;
                                        Delay1000ms();
                                        sendok=1;
                                        a=0;
                                        c=0;
                                        d=0;
                                        e=0;
                                        t3=0;
                                }
                        }
                        if(flag==0x03)   
                        {
                                if(c<1)
                                {
                                        c++;
                                        UartWrite(tab1,1);
                                        UartWrite(&flag,1);
                                        UartWrite(&flagboma,1);
                                        UartWrite(tab2,2);
                                        watchdog();
                                        sendok=0;
                                        Delay1000ms();
                                        sendok=1;
                                        a=0;
                                        b=0;
                                        d=0;
                                         e=0;
                                        t3=0;
                                }
                        }
                        if(flag==0x04)   
                        {
                                if(d<1)
                                {
                                        d++;
                                        UartWrite(tab1,1);
                                        UartWrite(&flag,1);
                                        UartWrite(&flagboma,1);
                                        UartWrite(tab2,2);
                                        watchdog();
                                        sendok=0;
                                        Delay1000ms();
                                        sendok=1;
                                        a=0;
                                        b=0;
                                        c=0;
                                         e=0;
                                        t3=0;
                                }
                        }
                         if(flag==0x05)   
                        {
                                if(e<1)
                                {
                                        e++;
                                        UartWrite(tab1,1);
                                        UartWrite(&flag,1);
                                        UartWrite(&flagboma,1);
                                        UartWrite(tab2,2);
                                  watchdog();
                                 sendok=0;
         Delay1000ms();
         sendok=1;
                                        a=0;
                                        b=0;
                                        c=0;
                                        d=0;
                                        t3=0;
                                }
                        }
                }
        }
}
        
        
        


void Delay10us(uint8 t)
        do{
                _nop_();
                _nop_();
                _nop_();
                _nop_();
                _nop_();
                _nop_();
                _nop_();
                _nop_();
        }while(t--);
}


void UartWrite(uint8 *ptr, uint8 len)   
{
//        uint8 cntTxd;
        RS485_DIR = 1;   
//        cntTxd = 1
        while(len--)
        {
                flagTxd = 0;
                S2BUF = *ptr++;
                while(!flagTxd);                        
  }
        Delay10us(5);  
        RS485_DIR = 0;

}




void ConfigTimer0(uint16 ms)
{
        uint32 tmp;
        tmp = 11059200 / 12;   
        tmp = (tmp*ms) / 1000;  
        tmp = 65536 - tmp + 32;   
        T0RH = (uint8) (tmp >> 8);   
        T0RL = (uint8) tmp;
        TMOD &= 0XF0;   
        TMOD |= 0X01;   
        TH0 = T0RH;
        TL0 = T0RL;
        ET0 = 1;         
        TR0 = 1;   
}


void UartRxdMonitor(uint8 ms)
{
        static uint8 idletmr = 0;
        static uint8 cntbkp = 0;
        if(cntRxd>0)
        {
                if(cntbkp != cntRxd)
                {
                        cntbkp = cntRxd;
                        idletmr = 0;
                }
                else            
                {
                        if(idletmr < 30)  
                        {
                                idletmr += ms;
                                if(idletmr >= 30)  
                                {
                                        flagframe=1;
                                }
                        }
                }
        }
        else
        {
                cntbkp = 0;
        }
}

void Timer0() interrupt 1
{
        uint8 state;
        
        TH0 = T0RH;
        TL0 = T0RL;
        UartRxdMonitor(1);
        t++;
        t1++;
        t2++;
        t3++;
        if(t1>200)   
        {
                state=P2&0xF0;
                switch(state)
                {
                         case 0xF0: M++; break;   
                         case 0x70: R++; break;   
                         case 0xC0: Y++; break;   
                         case 0xA0: G++; break;   
                        case 0x20: RG++; break;   
                        case 0x40: RY++; break;   
                        case 0x80: YG++; break;   
                        case 0x00: RGY++; break;  
                        case 0x60: S++; break;
                        default: break;
                }
                t1=0;
        }
        if(t3>1000)   
        {
                t3=0;
                flag1=1;
        }        
}


void ConfigUart2(void)
{
   RS485_DIR = 0;            
          AUXR&=0xf7;
          S2CON = 0x50;   
          AUXR |=0x04;        
         AUXR|=0X10;         
         BRT=0xdc;
        IE2 |= 0x01;                                         

}


void UART2() interrupt 8
{
        
        if(S2CON&S2RI)
        {
//                 S2CON&=0XF
                 S2CON&=~S2RI;
                if(cntRxd<sizeof(bufRxd))
                {
                        bufRxd[cntRxd++] = S2BUF;
                }

  }
        if(S2CON&S2TI)
        {
//           S2CON&=0XF
                S2CON&=~S2TI;
                flagTxd=1;
        }
        
}
回复

使用道具 举报

ID:428114 发表于 2022-5-24 17:49 | 显示全部楼层
先把语文学好啊,实在不明白你想说什么。
回复

使用道具 举报

ID:390416 发表于 2022-5-25 10:51 | 显示全部楼层
单片机串口二波特率250000接收,转串口-9600到电脑串口助手 http://www.51hei.com/bbs/dpj-220335-1.html
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表