單片機(jī)C語(yǔ)言程序設(shè)計(jì):K1-K4按鍵狀態(tài)顯示
/* 名稱:K1-K4 按鍵狀態(tài)顯示
說(shuō)明:K1、K2 按下時(shí) LED 點(diǎn)亮,松開時(shí)熄滅,
*/
K3、K4 按下并釋放時(shí) LED 點(diǎn)亮,再次按下并釋放時(shí)熄滅;
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit LED1=P0^0;
sbit LED2=P0^1;
sbit LED3=P0^2;
sbit LED4=P0^3;
sbit K1=P1^0;
sbit K2=P1^1;
sbit K3=P1^2;
sbit K4=P1^3;
//延時(shí)
void DelayMS(uint x)
{
uchar i;
while(x--) for(i=0;i<120;i++);
}
//主程序
void main()
{
P0=0xff;
P1=0xff;
while(1)
{
LED1=K1;
LED2=K2;
if(K3==0)
{
while(K3==0);
LED3=~LED3;
}
if(K4==0)
{
while(K4==0);
LED4=~LED4;
}
DelayMS(10);
}
}
編輯:admin 最后修改時(shí)間:2018-05-19