两个人体红外传感器计数,判断屋里的人数的单片机程序

转载至http://www.51hei.com/bbs/dpj-146402-1.html
51黑本人账号
很简单的一个程序,大一的时候写的,
今天有人加我问这个问题,就把代码翻出来了,刚好分享出来
就是用外部中断检测判断状态的先后,来判断人的进出,在main里加减

单片机程序源码如下:

#include 
#include 
#define LCD1602_DB  P0
sbit red   = P2^0;  //红外1
sbit red1 = P2^1;   //红外2
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
unsigned char flag1s = 0;
unsigned char flag2s = 0;
unsigned char code LedChar[] = { //数码管显示字符转换表
0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};
unsigned char LedBuff[6] = { //数码管显示缓冲区,初值 0xFF 确保启动时都不亮
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};
unsigned char i = 0;
shu(unsigned char sec);
void delay(unsigned int i)
{
        while (i--);
}
shu(unsigned char sec)
{
LedBuff[0] = LedChar[sec%10];
LedBuff[1] = LedChar[sec/10%10];
LedBuff[2] = LedChar[sec/100%10];
LedBuff[3] = LedChar[sec/1000%10];
LedBuff[4] = LedChar[sec/10000%10];
LedBuff[5] = LedChar[sec/100000%10];
}
void main()
{
        unsigned int sec = 0,j = 0;    



red = 1;
    red1 = 1;
    ENLED = 0; 
ADDR3 = 1;
    TMOD = 0x01;
    TH0 = 0xFC;
    TL0 = 0x67;
    TR0 = 1;
    EA = 1;
    ET0 = 1;
    sec = 0;
    shu(sec);
    while(1)
    {         
       shu(sec);        
            if(flag1s == 1 )                  
            {
                    flag1s = 0;                        
                    sec++;
            
            }
            if(flag2s == 1 )                  
            {
                flag2s = 0;        
                    if(sec > 0)
                     sec--;
            } 
            shu(sec); 
    /*for(;sec<20;sec++)
               {itoa(sec, s);
            delay(100009);
            LcdShowStr(8, 0, s); } */
                                    


       }                
}
void InterruptTimer0() interrupt 1
{
    TH0 = 0xFC; //重新加载初值
    TL0 = 0x67;
    if (red == 0&&red1==1)  
    {
        delay(99);
                if(red1==0)
        flag1s = 1;
    }
    if (red1 == 0&&red == 1)  
    {
        delay(99);
                if(red == 0)
       flag2s = 1;
    }
        switch (i)
{
case 0: ADDR2=0; ADDR1=0; ADDR0=0; i++; P0=LedBuff[0]; break;
case 1: ADDR2=0; ADDR1=0; ADDR0=1; i++; P0=LedBuff[1]; break;
case 2: ADDR2=0; ADDR1=1; ADDR0=0; i++; P0=LedBuff[2]; break;
case 3: ADDR2=0; ADDR1=1; ADDR0=1; i++; P0=LedBuff[3]; break;
case 4: ADDR2=1; ADDR1=0; ADDR0=0; i++; P0=LedBuff[4]; break;
case 5: ADDR2=1; ADDR1=0; ADDR0=1; i=0; P0=LedBuff[5]; break; 
default: break;
}
}

很简单的代码,只能同时一人通过

你可能感兴趣的:(51)