#include "AT89X52.H"
#include "math.h"
sbit NTL = P0^0;//继电器控制,输出
sbit NTR = P0^1;
sbit NTU = P0^2;
sbit NTD = P0^3;
sbit INL = P0^4;//云台运动状况的检测电,输入
sbit INR = P0^5;
sbit INU = P0^6;
sbit IND = P0^7;
sbit LEFC = P2^0;//继电器的通断检测,
sbit LEGC = P2^1;
sbit UPPC = P2^2;
sbit DOWC = P2^3;
sbit SOUND = P2^7;
int counter=0,TimerCnt1Ms=0,LastTime1Ms;
char b,c,d= 0;
char Flag = 0,sc[3];
void Delay(int DelayTimeInMs);
void sendchar();
void checkpinc(char c);
void checkpind(char d);
void show(void);
void judge(char Flag);
void main (void)
{
SCON = 0x52; /* 设置串行口控制寄存器SCON*/
TMOD = 0x21; /* */
TCON = 0x69; /* TCON */
TH1 = 0xf3; /* TH1 */
ET1=1;
EA=1;
b = 0x01;
while( b<0x10 )
{ P0=0xFF;
switch(b)
{case 0x01 :
P0|=0x0F;
NTL=0;
break;
case 0x02 :
P0|=0x0F;
NTR=0;
break;
case 0x04 :
P0|=0x0F;
NTU=0;
break;
case 0x08 :
P0|=0x0F;
NTD=0;
break;
default:
P0=0x0F;
break;
}
Delay(900);
b <<=1;
}
SOUND=1;//蜂鸣器响
P0|=0x0F;
for(;;)
{
show();
judge (Flag);
sendchar();
if (Flag!=0xff)
{
int DelayNum=0;
do
{
SOUND=~SOUND;
DelayNum++;
Delay(1);
}
while( DelayNum<3000);
}
else
Delay(500);
}
}
void show(void)
{
b = 0x01;
c = 0x80;
d = 0x10;
Flag=0x00;
while( b<0x10 )
{ switch(b)
{case 0x01 :
P0|=0x0F;
NTL=0;
break;
case 0x02 :
P0|=0x0F;
NTR=0;
break;
case 0x04 :
P0|=0x0F;
NTU=0;
break;
case 0x08 :
P0|=0x0F;
NTD=0;
break;
}
checkpinc(c);
if (counter>10)
checkpind(d);
b <<=1;
c >>=1;
d <<=1;
counter=0;
}
SOUND=1;
NTL=1;
NTR=1;
NTU=1;
NTD=1;
}
void Delay(int DelayTimeInMs) // Note:DelayTimeInMs<1000
{
int LastTime1Ms = TimerCnt1Ms;
while(abs(TimerCnt1Ms-LastTime1Ms) TimerCnt1Ms=0; } // Timer0 overflow IntNum=10 void timer1_ovf_isr(void) interrupt 3 { unsigned char Second,Minute,Hour = 0; TimerCnt1Ms++; if( TimerCnt1Ms==1000 )//1秒 { TimerCnt1Ms = 0; Second++; if( Second==60 ) { Second = 0; Minute++; if( Minute==60 ) { Minute = 0; Hour++; } } } } void sendchar() { char i; sc[2]=0x0d; if ((Flag&0x0f)>9) sc[0]=65+(Flag&0x0f)-0x0a; else sc[0]=48+(Flag&0x0f); if((Flag&0xf0)>0x90) sc[1]=65+((Flag>>4)&0x0f)-0x0a; else sc[1]=48+((Flag&0xf0)>>4); for (i=0;i<=2;i++) { SBUF=sc[i]; // while (!(USR & 0x40)) { } // USR|=0x40; } } void checkpinc(char c) //C端口检测 { int DelayNum=0; Delay(30); counter=0; do { Delay(2); DelayNum++; // if (PINC&c==c) counter++; } while( DelayNum<15); if (counter>10) Flag |=c; } void checkpind(char d) //D端口检测 { int DelayNum=0; counter=0; do { Delay(2); DelayNum++; // if (d!=(PIND&d)) counter++; } while( DelayNum<30); if (counter>5) Flag |=(d>>4); } void judge (char Flag) { int j,m,n; for (j=0;j<=2;j++) { if (Flag!=0xff) { m=((Flag||0x0f)>>4)||0xf0; n=Flag||0xf0; if ((m-n)<=0) {} else { if ((((~n)||0xf3)==0xff)|((n||0xf3)==0xff)) { } else { P0=((~n)||0xf3); Delay(200); P0=0x8f; } if ((((~n)||0xfc)==0xff)|((n||0xfc)==0xff)) { } else { P0=((~n)||0xfc); Delay(200); P0=0x8f; } } Delay(5); show(); } else j=3; P0=0x8f; } } 电原理图 控制板