基于stc15f2k60s2芯片单片机编程(超声波报警)

按键S7控制上下限,S6加S7减;(运行此程序时按键不好用)

#include 
#include 
void shumaguan();
void Timer0Init();
void Delay500ms();
void Delay2us();
void anjian();
unsigned char abile[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XBF,0XFF};
unsigned char saomiao[]={0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF};
unsigned int distance;
unsigned char high=30,low=10;
unsigned char a;
void Send_Wave();
sbit TX=P1^0;
sbit RX=P1^1;
sbit S7=P3^0;
sbit S6=P3^1;
sbit S5=P3^2;
unsigned int time=0;
void mdistance();
void main()
{	
//	P2=0XC0;P0=0X01;P2=0XFF;P0=0XFF;P2=0XA0;P0=0X00;
	Timer0Init();

while(1){
   
			mdistance();
// if(distance ==99)
//		 {
//			saomiao[0]=0x90;
//			 saomiao[1]=0x90;
//		 
//			 Delay500ms();
//		 }
//		 else{
//	
	if(a==0){saomiao[0]=abile[(distance%100)/10];saomiao[1]=abile[distance%10];Delay500ms();}
  if(a==1){saomiao[0]=abile[high/10];saomiao[1]=abile[high%10];Delay500ms();}
  if(a==2){saomiao[0]=abile[low/10];saomiao[1]=abile[low%10];Delay500ms();}
	anjian();
//}
}
}
void Delay500ms()		//@11.0592MHz
{
	unsigned char i, j, k;

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



void weixuan() interrupt 1
{
	unsigned char i;
	 P2=P2&0x1f|0xe0;
	P0=0xff;
	P2=0x1f;
  P2=P2&0x1f|0xc0;
	P0=1<<i;
	P2=0x1f;
  P2=P2&0x1f|0xe0;
	P0=saomiao[i];
	P2=0x1f;
	i++;
	if(i==8) i=0;
}
void Timer0Init()		//[email protected]
{
	AUXR |= 0x80;		//?????1T??
	TMOD &= 0xF0;		//???????
	TL0 = 0x9A;		//??????
	TH0 = 0xA9;		//??????
	TF0 = 0;		//??TF0??
	TR0 = 1;		//???0????	
	EA=1;
	ET0=1;
}


void Send_Wave()
{
		TX=1;
		_nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_();
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_();
		TX=0;
}
void mdistance()
{	
	unsigned int time=0;
	TMOD&=0X0F;//¶¨Ê±Æ÷1ģʽ
	TL1=0;
	TH1=0;//³õʼ»¯
	Send_Wave();  //·¢ËÍÉù²¨ÐźÅ
	TR1=1;//Æô¶¯¶¨Ê±Æ÷
	while((RX==1)&&(TF1==0)) ;//µÈ´ý³¬Éù²¨Ðźŷµ»Ø»òÕߵȵ½²âÁ¿³¬³ö·¶Î§
	TR1=0; //Í£Ö¹¶¨Ê±Æ÷
	
	if(TF1==0)
	{
		time=TH1;
		time=(time<<8)|TL1;  //½«¼ÆËãÖµ×°µ½16Ϊ¼ÆÊýÆ÷
		distance=((time/10)*17)/100+3;//²âÁ¿¾àÀë
	}
	else
	{   
	 	TF1=0;//½ûÖ¹ÖжÏÒç³ö
	 	distance=99;
	}
	if(distance>=high||distance<=low)
	{P2=0XA0;P0=0Xff;P2=0X1F;}
	else 
	{P2=0XA0;P0=0X00;P2=0X1F;}
		}
void Delay2us()		//@11.0592MHz
{
	unsigned char i;

	i = 3;
	while (--i);
}






		
void anjian()
	{

	   if(S7==0)
		 {
	     Delay2us();
           if(S7==0)
					 {
					 a++;
						 if(a==3)a=0;

					 }		
                while(!S7);					 
	 }
		   if(S6==0)
			 {	    
			Delay2us();
		 
           if(S6==0)
					 {
					if(a==1) high++;
				  if(a==2)low++;
					 }		
      while(!S6);					 		 
	 }
		   if(S5==0)
			 {
	   Delay2us();
		 
           if(S5==0)
					 {
					if(a==1) high--;
				  if(a==2) low--;
					 }		
                while(!S5);					 
		 }
	 }
	

你可能感兴趣的:(基于stc15f2k60s2芯片单片机编程(超声波报警))