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

📄 text1.c

📁 自己在keil51中用C实现了一个电子钢琴展览项目
💻 C
字号:
#include <absacc.h>
#include <reg51f.h>
#include <intrins.h>
#define uchar unsigned char
#define uint  unsigned int



unsigned char  receive_len,send_num,k,i,qunum ;
unsigned char  receive_check;
unsigned char  send_check;
unsigned char  receive_buf[8]	; //定义接收缓冲区
unsigned char  send_buf[8];  //定义发送缓冲区
unsigned char  qu[3]   ;
sbit     CO= P1^3;   //关机键		 
sbit     CO1=P1^2;
sbit     shutdown_s= P2^6; 	 //关大屏幕
sbit     shutdown_c= P2^2; 	 //	关计算机
sbit     shutdown_f= P2^7; 	  // 关烟雾器
sbit     shutdown= P2^0; 	 //关总电源
sbit     smog_hand=P1^1;	//烟雾手动
sbit     smog_open=P2^1;	//放烟雾
sbit     start=P1^0;//开机(遥控器)
sbit     play_1=P3^3;//连续播放即循环播放  
sbit     play_2=P3^2;//单次播放即手动检测激光信号
sbit     enable=P2^5;//激光琴总允许信号
sbit     IC4=P1^4;
sbit     ID4=P1^5;
sbit     IE4=P1^6;
sbit     IF4=P1^7;
sbit     SDA=P2^3;

bit    receive_status;
bit    send_status;
bit    auto_smog;
bit    sr_flag;
bit    CO_flag;
bit    start_flag;
bit    start_flag1;
bit    auto_flag;   
bit    sh_flag;
bit    play1_flag;
bit    play2_flag;
bit    pause;
bit    con;
bit    start_flag;
bit    KJOK;
bit     GJOK;
bit      pp;
bit    qmend;

uint	  time_50ms;
uint	  time_1s;
uint	  time_s;
uint	  time_s_gj;
uint	  time_s_kj; 
uint      time_s_3;



void  delay(void)
{ int t;
for(t=0;t<=800;t++)	{}
}
void  diao(void) interrupt  9  
{	  if(GJOK==0)
	  while(1) ;
}
void Timer(void) interrupt  1    using 1	    
{
TR0=0;
TH0=0x4c;            
TL0=0x00;
TR0=1; 
	
time_50ms++;
if(time_50ms==20)
{time_1s++;
time_s++;
time_s_3++;
time_s_kj++;
time_s_gj++;
time_50ms=0;
}

} 
void rs232(void) interrupt 4 	 using  2	  
{
	if(RI==1)
{
		if(receive_status==0)

	{  if (receive_len!=7)
		  {	 
			receive_buf[receive_len] = SBUF;
		 	receive_len = receive_len+1;
		  }
		else
		  {
		  	receive_buf[receive_len] = SBUF;
			receive_status=1;
			receive_len=0;
		  }
	}
	RI=0;
}
	else
	{
		if(send_status==0)
		{   SBUF =send_buf[send_num];
			send_num = send_num+1;
			if(send_num==8)
	   		{send_status = 1;
			send_num=1;} 
		}
	    
	TI=0;	
   	} 
    	
} 


void pca(void) interrupt 6
{ 
if(i==3)
	 { i=0;
       qu[0]=0;
       qu[1]=0;
       qu[2]=0;
	   pp=1;
	  }

if(IC4==0&&ID4==0&&IE4==0&&IF4==0)
{ for(k=0;k<=20;k++)
      {delay(); 
	  }
if(IC4==0&&ID4==0&&IE4==0&&IF4==0)
{shutdown_c=~shutdown_c;
if(CCF1==1)
 { if(qu[0]==1||qu[1]==1||qu[2]==1)
   {
   i=0;
   qu[0]=0;
   qu[1]=0;
   qu[2]=0;
   
   }
   else
   {
   qu[i]=1;
   }
   CCF1=0;
 }
if(CCF2==1)
 { if(qu[0]==2||qu[1]==2||qu[2]==2)
   {
   i=0;
   qu[0]=0;
   qu[1]=0;
   qu[2]=0;
 
   }
   else
   {
   qu[i]=2;
   }
   CCF2=0;
 }
if(CCF3==1)
 { if(qu[0]==3||qu[1]==3||qu[2]==3)
   {
   i=0;
   qu[0]=0;
   qu[1]=0;
   qu[2]=0;
  
   }
   else
   {
   qu[i]=3;
   }
   CCF3=0;
 }
if(CCF4==1)
 { if(qu[0]==4||qu[1]==4||qu[2]==4)
   {
   i=0;
   qu[0]=0;
   qu[1]=0;
   qu[2]=0;
  
   }
   else
   {
   qu[i]=4;
   }
   CCF4=0;
 }
}
}
if(i==2)
{
switch(qu[0])
{case 1:   
   {//if(i>=1) 
	{switch(qu[1])
	  { case 3: { if(qu[2]==2)
			        { qunum=0;
			    //      play2_flag=0;
					   pp=0;
		         	}
		          if(qu[2]==4)
			        {qunum=1;
			    //      play2_flag=0;
					   pp=0;
		        	}
		 		 break;
		         }
	  case 4:	 { if(qu[2]==2)
			          {qunum=2; 
			//		   play2_flag=0;
					    pp=0;
		               }
		              if(qu[2]==3)
		               {qunum=3;
			    //        play2_flag=0;
						 pp=0;
			           }
					  break;
		          }
				
		        
	 default:  { i=0;
                 qu[0]=0;
                 qu[1]=0;
                 qu[2]=0;
				 pp=1;
				 break;
	           }
	
	  }
	}
	break;
   }
case 2:
  {
	//	if(i>=1) 
	{switch(qu[1])
	  { case 1: { if(qu[2]==3)
			        { qunum=4;
			    //      play2_flag=0;
					  pp=0;
		         	}
		          if(qu[2]==4)
			        {qunum=5;
			     //    play2_flag=0;
					   pp=0;
		        	}
		 		 break;
		         }
	     case 3:{ 
		            if(qu[2]==1)
			          {
					   qunum=6; 
					 //  play2_flag=0;
					   pp=0;
		               }
		              if(qu[2]==4)
		               {qunum=7;
			          //  play2_flag=0;
						pp=0;
					shutdown_f=~shutdown_f;
					//EC=0;
			           }
          
				 break;
		        }
	  case 4:	{ 
		            if(qu[2]==1)
			          {qunum=8; 
					   //play2_flag=0;
					    pp=0;
		               }
		              if(qu[2]==3)
		               {qunum=9;
			            //play2_flag=0;
						 pp=0;
			           }
		             
				 break;
		        }	 

	 default:  { i=0;
                 qu[0]=0;
                 qu[1]=0;
                 qu[2]=0;
				  pp=1;
                 break;
	           }
	
	  }
	}
   break;
  }
default:  {     i=0;
                 qu[0]=0;
                 qu[1]=0;
                 qu[2]=0;
				 pp=1;
               	 break;
	       }
}
}
if(qu[0]==1||qu[0]==2)
{i++;

}

}  

main()
{  
AUXR= 0x0; 
shutdown_s=0;
shutdown_c=0;
shutdown_f=0; 
shutdown=1;	
smog_open=0;
enable=1;
CO1=1;

SDA=1;
IC4=1;
ID4=1;
IE4=1;
IF4=1;
TMOD = 0x01;  //定时器1,2工作于方式1
TH0=0x4c;             
TL0=0x00;
TR0=1;
RCAP2H=0xff;     //	   定时器二做波特率发生器  9600
RCAP2L=0xdC;	
T2CON=0x34;	//
PCON = 0x00;//SMOD=0;
SCON = 0x50;//串行口于模式1,8位UART
IE = 0x92;//turn on EA,ES,ET0,EC
EBO=1;
IP=0x10;
CMOD=0x00;//PCA允许中断							    
CCAPM0=0x00;
CCAPM1=0x00;
CCAPM2=0x00;
CCAPM3=0x00;
CCAPM4=0x00;
CCF1=0;
CCF2=0;
CCF3=0;
CCF4=0;
   
receive_status =0;//一串数据接收成功标志 1-成功
send_status =0;
send_num=1;
play_1=1;
play_2=1;
smog_hand=1;
CO=1;
start=1;
sr_flag=0;
play1_flag=0;
play2_flag=0;
KJOK=1;
CO_flag=1;
start_flag=1;
auto_flag=1;
start_flag=1;
start_flag1=0;
sh_flag=0;
pause=0;
con=0;
receive_len=0;
GJOK=1;
pp=1;
qmend=0;
i=0;
qu[0]=0;
qu[1]=0;
qu[2]=0;


while(1)
{  
 SDA=1;
 SDA=0;
 
//关机
if((CO==0&&sr_flag==0&&play_1==1&&play_2==1&&KJOK==0)||CO1==0)
{  delay();
   delay();
   delay();
if((CO==0&&sr_flag==0&&play_1==1&&play_2==1)||CO1==0)
 { sr_flag=1;
   send_buf[0] =0x71;
   send_buf[1] ='C';
   send_buf[2] ='O';
   send_buf[3] =',';
   send_buf[4] =0;
   send_buf[5] =0x23;
   send_check=0xa5^((send_buf[0]+send_buf[1]+send_buf[2]+send_buf[3]+send_buf[4]+send_buf[5])%0x100);	 
   send_buf[6] =send_check;
   send_buf[7] =0x0d;
   send_status =0;
   SBUF=send_buf[0];
   time_s_gj=0;
   CO_flag=0; 
   GJOK=0;
   EC=0;
   start_flag=1;
   enable=0;
  }
}
if(CO==1||play_1==0||play_2==0)
{sr_flag=0;}

//延时10分钟关机 

if(time_s_gj>=60&&CO_flag==0)
 { shutdown_s=0;
   shutdown_c=0;
   shutdown_f=0; 		   
   shutdown=0; 
   CO_flag=1;
    } 

//开机
 if(start==0&&start_flag1==0)
   { CO_flag=1;
     shutdown_c=1;
     time_s_kj=0;
     start_flag1=1; 
     start_flag=0;
   }

if(time_s_kj>=60&&start_flag==0)
  { shutdown_s=1;
    start_flag=1;
    KJOK=0;
   }
if(start==1)
  {start_flag1=0;
   }


if(KJOK==0&&GJOK==1)
{
//自动烟雾
if(time_s>=1200)
  { shutdown_f=1;
 
     if(time_s>=1800)
       { 
	   	smog_open=1;
		 if(time_s>=1815)
           {  smog_open=0;
              shutdown_f=0;
              time_s=0;
           }
       }
  }
 //烟雾手动
if(smog_hand==0&&sh_flag==0)	
 {delay();
 if(smog_hand==0&&sh_flag==0)
 { shutdown_f=1;
   time_s_3=0;
   sh_flag=1;
   auto_flag=0;
 }
 }
if(time_s_3>=600&&auto_flag==0)
 {	smog_open=1;   //放烟雾
    if(time_s_3>=615)
      { smog_open=0;//停止放烟
        shutdown_f=0;//关烟雾器
        auto_flag=1;
		sh_flag=0;
      }
 }

//连续播放
if(play_1==0&&play1_flag==0&&CO==1)
   {  delay();
      if(play_1==0&&play1_flag==0&&CO==1)
        { play1_flag=1;
          send_buf[0] =0x71;   
          send_buf[1] ='P';
          send_buf[2] ='L';
          send_buf[3] =',';
          send_buf[4]=0x0a;
          send_buf[5] =0x23;
          send_check=0xa5^((send_buf[0]+send_buf[1]+send_buf[2]+send_buf[3]+send_buf[4]+send_buf[5])%0x100);
          send_buf[6] =send_check;
          send_buf[7] =0x0d;
          send_status =0;
          SBUF=send_buf[0]; 
        }
	}
if(play_1==1||CO==0)
   {play1_flag=0;}

//单次播放
if(play_2==0&&play2_flag==0&&CO==1)
  {delay();
   if(play_2==0&&play2_flag==0&&CO==1)
     {CCAPM1=0x11;
      CCAPM2=0x11;
      CCAPM3=0x11;
      CCAPM4=0x11;
	  EC=1;
	  play2_flag=1;
	 }
} 
if(play_2==1||CO==0)
{play2_flag=0;
CCAPM1=0x00;
CCAPM2=0x00;
CCAPM3=0x00;
CCAPM4=0x00;
EC=0;}

if(pp==0&&qmend==0&&play2_flag==1)
        { CCAPM1=0x00;
          CCAPM2=0x00;
          CCAPM3=0x00;
          CCAPM4=0x00;
		  EC=0;
		  pp=1;
		 
          qmend=1;
          send_buf[0]=0x71;   
          send_buf[1]='P';
          send_buf[2]='L';
          send_buf[3]=',';
          send_buf[4]=qunum;	//播放曲目
          send_buf[5]=0x23;
          send_check=0xa5^((send_buf[0]+send_buf[1]+send_buf[2]+send_buf[3]+send_buf[4]+send_buf[5])%0x100);
          send_buf[6]=send_check;
          send_buf[7]=0x0d;
          send_status=0;
          SBUF=send_buf[0];
         }
      	
     

 

//暂停播放
if(CO==0&&play_1==0&&pause==0)
   {delay();
    if(CO==0&&play_1==0&&pause==0)
      {pause=1;
       send_buf[0] =0x71;   
       send_buf[1] ='S';
       send_buf[2] ='T';
       send_buf[3] =',';
       send_buf[4]=0;
       send_buf[5] =0x23;
       send_check=0xa5^((send_buf[0]+send_buf[1]+send_buf[2]+send_buf[3]+send_buf[4]+send_buf[5]) %0x100);
       send_buf[6] =send_check;
       send_buf[7] =0x0d;
       send_status =0;
       SBUF=send_buf[0];
      }
   }
if(CO!=0||play_1!=0)
{pause=0;}

//继续播放
if(CO==0&&play_2==0&&con==0)
   {delay();
    if(CO==0&&play_2==0&&con==0)
      { con=1;
        send_buf[0] =0x71;
        send_buf[1] ='P';
        send_buf[2] ='L';
        send_buf[3] =',';
        send_buf[4]=0;
        send_buf[5] =0x23;
        send_check=0xa5^((send_buf[0]+send_buf[1]+send_buf[2]+send_buf[3]+send_buf[4]+send_buf[5])%0x100);
        send_buf[6] =send_check;
        send_buf[7] =0x0d;
        send_status =0;
        SBUF=send_buf[0];
      }  
    }
if(CO!=0||play_2!=0)
     {con=0;}


if(receive_status==1)
{
 receive_check=0xa5^( receive_buf[0]+receive_buf[1]+receive_buf[2]+receive_buf[3]+receive_buf[4]+receive_buf[5] ) %0x100 ;
if (receive_buf[0]==0x73&&receive_buf[1]==0x44&&receive_buf[2]==0x55&&receive_buf[3]==0x2c&&receive_buf[4]==0&&receive_buf[5]==0x23&&receive_buf[6]==receive_check&&receive_buf[7]==0x0d)
// if() 
  {	qmend=0;
     CCAPM1=0x11;
     CCAPM2=0x11;
     CCAPM3=0x11;
     CCAPM4=0x11;
     EC=1; 
     send_buf[0] =0x71;  
     send_buf[1] ='D';
     send_buf[2] ='U';
     send_buf[3] ='O';
     send_buf[4]='K';
     send_buf[5] =0x23;
     send_check=0xa5^((send_buf[0]+send_buf[1]+send_buf[2]+send_buf[3]+send_buf[4]+send_buf[5]) %0x100);
     send_buf[6] =send_check;
     send_buf[7] =0x0d;
	 send_status =0;
	 SBUF=send_buf[0];
	   	 
  }
 if(receive_buf[0]==0x73&&(receive_buf[1]!=0x44||receive_buf[2]!=0x55||receive_buf[3]!=0x2c||receive_buf[4]!=0||receive_buf[5]!=0x23||receive_buf[6]!=receive_check||receive_buf[7]!=0x0d))
  {  	qmend=1;

    send_buf[0] =0x71;   
    send_buf[1] ='D';
    send_buf[2] ='U';
    send_buf[3] ='E';
    send_buf[4]='R';
    send_buf[5] =0x23;
    send_check=0xa5^((send_buf[0]+send_buf[1]+send_buf[2]+send_buf[3]+send_buf[4]+send_buf[5])%0x100);
    send_buf[6] =send_check;
    send_buf[7] =0x0d;
	send_status =0;
    SBUF=send_buf[0];
    }
if (receive_buf[0]==0x7a&&receive_buf[1]=='C'&&receive_buf[2]=='O'&&receive_buf[3]=='O'&&receive_buf[4]=='K'&&receive_buf[5]=='#'&&receive_buf[6]==receive_check&&receive_buf[7]==0x0d)
	{ time_s_gj=0;  
	  CO_flag=0; 
      GJOK=0;
      EC=0;
      start_flag=1;
      enable=0;}	
/*
 //     	 //关机信息发送不成功,再发送一遍
 if (receive_buf[0]==0x7a&&receive_buf[1]=='S'&&receive_buf[2]=='T'&&receive_buf[3]=='O'&&receive_buf[4]=='K'&&receive_buf[5]=='#'&&receive_buf[6]==receive_check&&receive_buf[7]==0x0d)
    {play1_flag=1;receive_status=0; }	    
 //if (receive_buf[0]==0x7a&&receive_buf[1]=='S'&&receive_buf[2]=='T'&&receive_buf[3]=='E'&&receive_buf[4]=='R'&&receive_buf[5]=='#'&&receive_buf[6]==receive_check&&receive_buf[7]==0x0d)
    //  {play1_flag=0; }	//暂停信息发送不成功,再发送一次
 if(receive_buf[0]==0x7a&&receive_buf[1]=='P'&&receive_buf[2]=='L'&&receive_buf[3]=='O'&&receive_buf[4]=='K'&&receive_buf[5]=='#'&&receive_buf[6]==receive_check&&receive_buf[7]==0x0d)
  {receive_status=0;}	//	单次信息发送成功,	 `
 if (receive_buf[0]==0x7a&&receive_buf[1]=='P'&&receive_buf[2]=='L'&&receive_buf[3]=='E'&&receive_buf[4]=='R'&&receive_buf[5]=='#'&&receive_buf[6]==receive_check&&receive_buf[7]==0x0d)
     {play2_flag=0;pp=0;qmend=0;con=0;pause=0;} //单次、继续、连续信息发送不成功,再发送一次   */

receive_status=0;  
} 		 

}

}

}

⌨️ 快捷键说明

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