通过两个红外,统计进出教室的人数。

 #include 
 #include 
 #define uint unsigned int
 #define uchar unsigned char
 sbit RED1=P3^0;
 sbit RED2=P3^1;
 uchar i;
 uchar table[]={0xff,0x7f,0x3f,0x1f,0x0f,0x07,0x03,0x01,0x00};
 void delay(uint z)
 {
  uint x,y;
for(x=z;x>0;x--)
for(y=114;y>0;y--);

 }

 int main()
 {

//红外默认状态 1
     int flag1=0;
 int flag2=0;
 int num=0;
 P1=0xff; 
 while(1)
 {                                                                                                            
 if(flag1==0&&flag2==0&&RED1==0&&RED2!=0)
 {
   flag1=1;
   flag2=0;
  }
   if(RED2==0&&RED1!=0&&flag1==1&&flag2==0)
 {                                                                                                                            
    delay(500);
num++;
if(num>=9)
 {
  num=8;
 }                                                                                                 
flag1=0;
 }
 if(flag1==0&&flag2==0&&RED2==0&&RED1!=0)
 {
   flag2=1;
   flag1=0;
   
 }
   if(RED1==0&&RED2!=0&&flag2==1&&flag1==0)
 {

    delay(500);
num--;                                                                             
if(num<=0)                                                                        
num=0;                                                                                                                                   
flag2=0;                                                                                                                                                        
 }
  P1=table[num];
  }
    
  return ;
 }


 

你可能感兴趣的:(通过两个红外,统计进出教室的人数。)