求51单片机一个点动开关控制三个led程序
发布网友
发布时间:2023-07-12 11:47
我来回答
共1个回答
热心网友
时间:2024-12-03 07:34
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar j,k;
sbit key1=P3^0;
sbit key2=P3^1;
sbit key3=P3^2;
void delay(int ms)
{
uchar i;
while(ms--)
for(i=0;i<123;i++);
}
uchar code tab[]={
0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,
0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,
0xfe,0xfb,0xef,0x7f,0xfd,0xf7,0xdf,0x7f,
};
void keyscan()
{
while(!key1)
{
delay(10);
if(key1)
k=1;
}
while(!key2)
{
delay(10);
if(key2)
k=2;
}
while(!key3)
{
delay(10);
if(key3)
k=3;
}
}
void main()
{
while(1)
{
keyscan();
switch(k)
{
case 1:
{
for(j=0;j<8;j++)
{
keyscan();
if(k!=1)
break;
P1=tab[j];
delay(200);
}
};break;
case 2:
{
for(j=0;j<8;j++)
{
keyscan();
if(k!=2)
break;
P1=tab[j+8];
delay(200);
}
};break;
case 3:
{
for(j=0;j<8;j++)
{
keyscan();
if(k!=3)
break;
P1=tab[j+16];
delay(200);
}
};break;
default:
break;
}
}
}