MCU / 5 /5.c
KeLe-open-code's picture
Publish reviewed course materials
37e076f verified
Raw History Blame Contribute Delete
679 Bytes
#include <REG51.H>
sbit c4=P3^0;
sbit c3=P3^1;
sbit c2=P3^2;
sbit c1=P3^3;
sbit r1=P3^6;
sbit r2=P3^7;
void delayMs(unsigned int i)
{ unsigned char t;
while (i--)
{ for(t=0;t<120;t++); }
}
unsigned char sk()
{
unsigned char k=0;
P3=255;
r1=0;
if(c1==0){k=1;}
else if(c2==0){k=2;}
else if(c3==0){k=3;}
else if(c4==0){k=4;}
r1=1;
r2=0;
if(k==0)
{
if(c1==0){k=5;}
else if(c2==0){k=6;}
else if(c3==0){k=7;}
else if(c4==0){k=8;}
}
r2=1;
return k;
}
void main()
{
unsigned char k=0;
P2=255;
while(1)
{
k=sk();
if(k!=0)
{
delayMs(50);
if(sk()==k)
{
P2=0x00;
P2=255U<<k;
}
}
}
}