⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 suidao1.c

📁 隧道灯控制 隧道灯控制 隧道灯控制
💻 C
字号:
 #include <reg51.h>

 #define uchar unsigned char
 #define uint  unsigned int

 #define start_byte 0xca
 #define end_byte 0xcc
 #define DimmChangeTime 30
 #define LEDAFACE  11
 #define WithOutComConst 30
/*
P03		P02		P01		P00
A绿 	A红		B绿		B红
LED1	LED2	LED3	LED4
*/
// #define ADDRESS 0x31

//FUN: from 0x30 to 0x38
//CHK: = (0xca+add+FUN+DATA0+DATA1+0xcc)%0xff
  //======= Var Define =======
 uchar data us100_cnt,ms_cnt,ms100_cnt; //system timer
 uchar data scount;
 uchar data address;
 uint data bright;
 uint data I1,I2;
 uint data TI1,TI2;
 uchar data cnt11,cnt22,cnt33,cnt44;


uchar data countad=0;
 uchar idata rbuf[14];
 uchar idata tbuf[14];
 uchar data comm_gap,i,j,tran_len,rececnt;
 uchar idata *rece_p;
 uchar idata *tran_p;
 uchar  data ad_buf[8];
// uchar  idata  Status[4];
 uchar data adchannel,BeepCnt,Dimm1,Dimm2,Dimm1Cnt,Dimm2Cnt;
 uchar data dimmcnt1,dimmcnt2,Dimm1Changed,Dimm2Changed;
 uchar data instate;
//bit Status=1;
 uchar bdata OutStatus;

sbit Led1 = OutStatus^3;
sbit Led2 = OutStatus^2;
sbit Led3 = OutStatus^1;
sbit Led4 = OutStatus^0;
//uchar bdata Outerr;
//sbit outstate1R = Outerr^0;
//sbit outstate1G = Outerr^1;
//sbit outstate2R = Outerr^2;
//sbit outstate2G = Outerr^3;
bit outstate1R ;
bit outstate1G  ;
bit outstate2R ;
bit outstate2G ;


   //======= Bit Define =======
 bit rfin,rsta,t10ms,t100ms,dimmauto,conv1,conv2,tsta,adready,G_LED1,G_LED2;

 //uint code DimmerYZ[2] = {18,120};
 //uchar code DimTab[3] = {50,25,15};
                           //   1     2     3     4
 uchar code LIMIT[3][4][3] ={{{3,50,105},{3,50,135},{3,50,105},{3,50,135}},  //high dimmer
                             {{3,40,55},{3,52,70},{3,40,55},{3,52,70}},  //mid dimmer
                             {{3,23,35},{3,32,45},{3,23,35},{3,32,45}}}; //low dimmer

// uchar code BITMASK[8] = {1,2,4,8,0x10,0x20,0x40,0x80};
 uchar code ADMASK[8] = {0,0x80,0x40,0xc0,0x20,0xa0,0x60,0xe0};

  //======= I/O Define ========
//sbit CS_5045=P3^5;
//sbit STB_LED=P1^6;

//sbit ERRLED = P3^7;
//sbit TRE485 = P3^3;

sbit SWLED1 = P2^0;
sbit SWLED2 = P2^1;
sbit SWLED3 = P2^2;

sbit CON_AD1=P0^6;
sbit CON_AD2=P2^6;

//sbit ConvertSta = P2^0;
//sbit ADIN = P3^2;
sbit LED1R = P0^0;
sbit LED1G = P0^1;
sbit LED2R = P0^2;
sbit LED2G = P0^3;
sbit state2G = P3^4;
sbit state2R = P3^5;
sbit state1G = P3^6;
sbit state1R = P3^7;

bit flag1 = 1;
//bit flag2 = 1;
uchar data count1 = 0;
//uchar data count2 = 0;
uchar data i;

//======= Function Define ========
// void ClrLed(void);
 //void delay(uint a);
 extern uchar read_ad();
 //========= Main Programm =========
 MAIN ()
{
//uchar data ii;
    //TRE485 = 0;

    OutStatus = 0xff;
    TMOD=0x12;
    TH0=256-100;
    TL0=256-100;

//    PCON |= 0x80;
//    SCON = 0x50;

//    RCAP2H=0xff;
//    RCAP2L=0xdc;  // b8;  //9600   dc:19200
//    T2CON=0x30;

    us100_cnt = 10;
    adready = 0;
 //   rececnt = WithOutComConst;

    LED1R = 1;
    LED2R = 1;
    LED1G = 1;
    LED2G = 1;

    ET0=1;
    TR0=1;
//    TR2 = 1;
//    ES=1;
    EA=1;
    count1 = 0;



while (1)
 {

/*   if (rfin)
    {
      address = ((~P1)>>2)&0xf;
//      if (rbuf[1]!=address) {rfin = 0; continue;};
      j=start_byte;
      rece_p=&rbuf+1;
      for (i=0;i<12;i++)
       {j+=(*rece_p);
        if (*rece_p++==end_byte)
          if (*rece_p==j) {i=0; rfin = 0; break;}
       }
      rececnt = WithOutComConst;
      if (i==0)
       { // check right ,process.
         if (rbuf[2]==0x80) {
             TRE485 = 1;
             if (rbuf[3]==0xaa) Led1 = 0;
             else Led1 = 1;
             if (rbuf[4]==0xaa) Led3 = 0;
             else Led3 = 1;
             if (rbuf[5]==0xaa) Led2 = 0;
             else Led2 = 1;
             if (rbuf[6]==0xaa) Led4 = 0;
             else Led4 = 1;
             tbuf[0] = 0xca;

             tbuf[1] = ad_buf[4];
             tbuf[2] = ad_buf[1];
             tbuf[3] = ad_buf[2];
             tbuf[4] = ad_buf[3];

             tbuf[5] = ad_buf[6];
             tbuf[6] = ad_buf[5];

             tbuf[7] = Status[0];
             tbuf[8] = Status[1];
             tbuf[9] = Status[2];
             tbuf[10] = Status[3];

             tbuf[11] = 0xcc;
             tran_p=tbuf;
             tran_len=11;
             tsta=1;
             comm_gap=10;
             SBUF=*tran_p;
         }
         rfin = 0;
       }//end of check right &
    }//end of rfin
*/
  if (t10ms)
     {
      // if (--comm_gap==0)
      //    rsta=0;
        t10ms=0;
       ad_buf[adchannel] = read_ad();


       adchannel++;
       adchannel &= 7;
//       if (adchannel>5) adchannel = 0;
       P2 &= 0x1f;
       P2 |= ADMASK[adchannel];//4051ABC状态


     } // end of commgap
  if(ad_buf[5]>50)
  {
    count1++;
    if(count1>=3) OutStatus = 0xf0;
  }
  else
  {
  count1 = 0;
  if (t100ms) {

//      CS_5045 = 0;
//      CS_5045 = 1;
      t100ms = 0;
	  /*if(flag2 ==0 )
		{
			count1++;
			if( count1 >=7 )
			flag1 = 0;
			else
			flag1 = 1;
			if (count1 >10)
			count1 = 0;
			flag2 = 1;
		 }
          */
          instate = ~(P2|0Xf8);
	  switch (instate)
	  {
	  	case 0x00:
		{
			Led1 = 1;Led2 = 1;Led3 = 1;Led4 = 1;
                        flag1 = 1;
			break;
		}
		case 0x04:
		{
			Led1 = 0;Led2 = 1;Led3 = 1;Led4 = 1;
                        flag1 =1;
			break;
		}
		case 0x02:
		{
			Led1 = 1;Led2 = 0;Led3 = 1;Led4 = 1;
                        flag1 = 1;
			break;
		}
		case 0x06:
		{
			Led1 = 1;Led2 = 1;Led3 = 0;Led4 = 1;
                        flag1 = 1;
			break;
		}
		case 0x01:
		{
			Led1 = 1;Led2 = 1;Led3 = 1;Led4 = 0;
                        flag1 = 1;
			break;
		}
		case 0x05:
		{
			Led1 = 0;Led2 = 1;Led3 = 1;Led4 = 0;
                        flag1 = 0;
			break;

		}
		case 0x03:
		{
			Led1 = 1;Led2 = 0;Led3 = 1;Led4 = 0;
                        flag1 = 0;
			break;
		}
                case 0x07:
		{
			Led1 = 1;Led2 = 0;Led3 = 0;Led4 = 1;
                        flag1 = 0;
			break;
		}
     }



  //convert start
//      if (adchannel == 0) {

      //StrTab[][10] = {{"关闭"},{"开路"},{"多个损坏"},{"正常"},{"电流偏大"}};


                 if(( Led1 == 0 )&&(flag1 == 1 ))
                {
                outstate1R = 1;
                outstate2R = 1;
                outstate2G = 1;
                  if(ad_buf[1]>LIMIT[0][3][1])

		 outstate1G = 0;
              else

                  outstate1G = 1;}
                else if(( Led2 == 0 )&&(flag1 == 1 ))
                { outstate1G = 1;
                outstate2R = 1;
                outstate2G = 1;
                if (ad_buf[4]>LIMIT[0][0][1])

                  outstate1R = 0;
              else
                outstate1R = 1;

                }

                else if(( Led4 == 0 )&&(flag1 == 1 ))
                {outstate1R = 1;
                outstate1G = 1;
                outstate2G = 1;
                if (ad_buf[2]>LIMIT[0][2][1])


                 outstate2R = 0;
                 else

                outstate2R = 1;

                }
               else if(( Led3 == 0 )&&(flag1 == 1 ))
                {outstate1R = 1;
                outstate1G = 1;
                outstate2R = 1;
                if (ad_buf[3]>LIMIT[0][1][1])
                 outstate2G = 0;
                 else

                outstate2G = 1;

                }
                 else if(( Led4 == 0 )&&(flag1 == 0 )&&(Led1 == 0))
                {outstate1R = 1;
                outstate2G = 1;
                if (ad_buf[2]>LIMIT[0][2][1])


                 outstate2R = 0;
                 else

                outstate2R = 1;
                 if(ad_buf[1]>LIMIT[0][3][1])

		 outstate1G = 0;
                 else

                  outstate1G = 1;
                }
                else if(( Led4 == 0 )&&(flag1 == 0 )&&(Led2 == 0))
                {
                outstate1G = 1;
                outstate2G = 1;
                if (ad_buf[2]>LIMIT[0][2][1])

                 outstate2R = 0;
                 else

                outstate2R = 1;
                 if (ad_buf[4]>LIMIT[0][0][1])

                  outstate1R = 0;
                 else

                outstate1R = 1;
                }
                else if(( Led2 == 0 )&&(flag1 == 0 )&&( Led3 == 0 ))
                { outstate1G = 1;
                outstate2R = 1;

                if (ad_buf[4]>LIMIT[0][0][1])

                  outstate1R = 0;
                 else

                outstate1R = 1;
                 if (ad_buf[3]>LIMIT[0][1][1])

                 outstate2G = 0;
                 else

                outstate2G = 1;
                }
                 else
                 {
                  outstate1G = 1;
                  outstate1R = 1;
                outstate2R = 1;
                outstate2G = 1;
                }
                state1G = outstate1G;
                state1R = outstate1R;
                state2G = outstate2G;
                state2R = outstate2R;



          /*if (ad_buf[6] < DimmerYZ[0])
		  {
          	if (Dimm1Changed!=2)
			{
          	    Dimm1Changed=2;
          	    Dimm1Cnt = DimmChangeTime;
          	   }
          	else {
          	    if (Dimm1Cnt) Dimm1Cnt--;
          	    else Dimm1 = 2;
          	}
          }
          else if (ad_buf[6] > DimmerYZ[1])
		  {
          	 if (Dimm1Changed!=0)
			 {
          	    Dimm1Changed=0;
          	    Dimm1Cnt = DimmChangeTime;
          	   }
          	 else
			 {
          	    if (Dimm1Cnt) Dimm1Cnt--;
          	    else Dimm1 = 0;
          	 }
               }
               else {
          	 if (Dimm1Changed!=1) {
          	    Dimm1Changed=1;
          	    Dimm1Cnt = DimmChangeTime;
          	   }
          	 else {
          	    if (Dimm1Cnt) Dimm1Cnt--;
          	    else Dimm1 = 1;
          	 }
               }
//--------------------------------------------------------
          if (ad_buf[5] < DimmerYZ[0])
		  {
          	if (Dimm2Changed!=2) {
          	    Dimm2Changed=2;
          	    Dimm2Cnt = DimmChangeTime;
          	   }
          	else {
          	    if (Dimm2Cnt) Dimm2Cnt--;
          	    else Dimm2 = 2;
          	}
          }
          else if (ad_buf[5] > DimmerYZ[1]) {
          	 if (Dimm2Changed!=0) {
          	    Dimm2Changed=0;
          	    Dimm2Cnt = DimmChangeTime;
          	   }
          	 else {
          	    if (Dimm2Cnt) Dimm2Cnt--;
          	    else Dimm2 = 0;
          	 }
               }
               else {
          	 if (Dimm2Changed!=1) {
          	    Dimm2Changed=1;
          	    Dimm2Cnt = DimmChangeTime;
          	   }
          	 else {
          	    if (Dimm2Cnt) Dimm2Cnt--;
          	    else Dimm2 = 1;
          	 }
               }*/

//          }//end of adchannel = 0
//      adready = 1;
  }//END OF T100MS

 }}//end of while (1)
}//end of main()

// ======== Function ========
/*void delay(uint a)
{ uint data ii;
  uchar data jj;
	for (ii=0;ii<a;ii++) {
//                CS_5045 = 0;
//                CS_5045 = 1;
		for (jj=0;jj<28;jj++) {jj = jj;}
                }
}
*/

//============ INT =============
/*void x0(void) interrupt 0
{

}*/

void t0(void) interrupt 1
{
  /*if (Dimm1)
  {
    if (dimmcnt1)
    dimmcnt1--;
    else
	{
      G_LED1 = ~G_LED1;
      if (G_LED1)
	  {
          dimmcnt1 = 50-DimTab[Dimm1];
          LED1R = 1;
          LED1G = 1;
//          P0 |= LEDAFACE;
      }//end of led off
      else
	  {
          dimmcnt1 = DimTab[Dimm1];
    LED1R = Led2;
//    LED2R = Led2;
    LED1G = Led1;
//    LED2G = Led4;
      }//end of led on
    }
  }
  else {
    LED1R = Led2;
//    LED2R = Led2;
    LED1G = Led1;
//    LED2G = Led4;
  }

    if (Dimm2) {
    if (dimmcnt2) dimmcnt2--;
    else {
      G_LED2 = ~G_LED2;
      if (G_LED2) {
          dimmcnt2 = 50-DimTab[Dimm2];
          LED2R = 1;
          LED2G = 1;
      }//end of led off
      els11111111111111111111111111111111e {
          dimmcnt2 = DimTab[Dimm2];
//    LED1R = Led1;
    LED2R = Led4;
//    LED1G = Led3;
    LED2G = Led3;
      }//end of led on
    }
  }
  else {
//    LED1R = Led1;
    LED2R = Led4;
//    LED1G = Led3;
    LED2G = Led3;
  }
*/
LED1R = Led2;
//    LED2R = Led2;
    LED1G = Led1;
//    LED2G = Led4;
    LED2R = Led4;
//    LED1G = Led3;
    LED2G = Led3;

  if ((--us100_cnt)==0)
   {
//----- Display prog --------
   us100_cnt=10;
 //--------------------------
   if (++ms_cnt>=100) {
       ms_cnt=0;
       t100ms=1;
       };
   if (ms_cnt%10==0) t10ms=1;
   }//end of 1ms
 }

/* void Comm(void) interrupt 4
 {
  ES=0;
  if (TI)
   {
    TI=0;
    if (tsta)
     {// send continue
       if (--tran_len==0) tsta=0;
       comm_gap=10;
       SBUF = *(++tran_p);
     }
    else
     {// end of send
      TRE485=0;
     }
   }
  if (RI)
   {
     RI=0;
     B=SBUF;
     if (!rfin)
      {
      if (rsta)
       {
        if (*rece_p==end_byte)
           {rfin=1;rsta=0;};
        *(++rece_p)=B;
        if (rece_p==(&rbuf+12)) {rsta=0;}
        comm_gap=10;
       }//end of rsta
      else
       {
         if (B==start_byte)
          {   rsta=1;
              rece_p=rbuf;
              comm_gap=10;
          }
//         else rtmp=B;
       }//end of normal receive
      }//end of rfin
   }//end of ri
  ES=1;
 }
 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -