Tuesday, March 5, 2013

Code đếm người vào ra + cảm biến ánh sáng

#include <REGX51.H>
sbit Led1=P3^6;
sbit Led2=P3^7;
sbit cb=P3^3;
sbit out1=P0^1;
sbit out2=P0^4;
sbit out3=P0^7;

unsigned char ma[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
char k=0,m=0;l=0;
unsigned char kiemtrahongngoai(void)
{
if(Led1==1&&m!=1)
{
while(Led1==1&&m!=1)
{
;
}
k++;
if(k==2) k=0;
}
//
if(Led2==1&&k!=1)
{
while(Led2==1&&k!=1)
{
;
}
m++;
if(m==2) m=0;
}
if(k==1&&Led2==1)
{
while(k==1&&Led2==1)
{;}
l++;
k=0;
m=0;
if(l==16) {l=15;}
}
//
if(m==1&&Led1==1)
{
while(m==1&&Led1==1)
{;}
l--;
k=0;
m=0;
if(l==-1)    {l=0;}
}
return l;
}

void hienthi(unsigned char i)
 {
if(i<10)    {P1=0xc0; P2=ma[i];}
if(i>9&&i<16)    {P1=0xf9; P2=ma[i-10];}
if(cb==0)
    {
    if(i>0&&i<6)    {out1=0;out2=out3=1;}
    if(i>5&&i<11)    {out1=out2=0; out3=1;}
    if(i>10&&i<16)    {out1=out2=out3=0;}
    }
else   
    {   
        out1=out2=out3=1;
    }
}
void main(void)
{
while(1)
{
kiemtrahongngoai();
hienthi(l);
}
}

1 comment: