发布网友 发布时间:2022-04-22 04:56
共2个回答
热心网友 时间:2023-06-30 08:52
程序代码如下:
#include<reg51.h>
sbit K1=P1^0;
sbit K2=P1^1;
void delay(int a)
{
int i;
while(a--)for(i=0;i<120;i++);
}
main()
{
unsigned char keyval=0,led=0xfe;
while(1)
{
if(K1==0)
{
delay(10);
if(K1==0)
{
keyval==1;
while(K1==0);
}
}
if(K2==0)
{
delay(10);
if(K2==0)
{
keyval==2;
while(K2==0);
}
}
if(keyval==1)P0=0xfe;
if(keyval==2)
{
P0=led;
led=_crol_(led,1);
delay(200);
}
}
}
热心网友 时间:2023-06-30 08:52
#include <reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit key1 =P2^0;
sbit key2 =P2^1;
sbit key3 =P2^2;
sbit key4 =P2^3;
uchar flag1=0,flag2=0,flag3=0,flag4=0;
void delay_ms(uint z)
{
uint i,j;
for(i=0;i<z;i++)
for(j=0;j<110;j++);
}
void O_to_E()
{
P0=0Xaa;
delay_ms(200);
P0=~0Xaa;
delay_ms(200);
}
void key_scan() //键盘扫描
{
if(!key1)
{
delay_ms(10); //延时消抖
if(!key1)
flag1=1;
}
if(!key2)
{
delay_ms(10); //延时消抖
if(!key2)
flag2=1;
}
if(!key3)
{
delay_ms(10); //延时消抖
if(!key3)
flag3=1;
}
if(!key4)
{
delay_ms(10);
if(!key4)
flag4=1; //延时消抖
}
}