通过独立键盘分别控制四个led灯的亮灭(在一个主函数中实现)。
1、在指定路径下创建工程并添加新文件
2、编写程序
通过不停的按下按键,使得灯以二进制的方式,不断往上加一,达到用灯来表示按键次数
#include <STC89C5xRC.H>
void Delay(unsigned int xms) //@12.000MHz
{
unsigned char i, j;
while(xms)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
xms--;
}
}
void main()
{
unsigned char LEDNum=0; // char max num is 255
while(1)
{
if(P31==0)
{
Delay(20);
while(P31==0);
Delay(20);
LEDNum++;
P2=~LEDNum;
}
}
}
#include <reg52.h>
sbit LED1 = P1 ^ 0; // LED1 连接到 P1.0 口
sbit LED2 = P1 ^ 1; // LED2 连接到 P1.1 口
sbit LED3 = P1 ^ 2; // LED3 连接到 P1.2 口
sbit LED4 = P1 ^ 3; // LED4 连接到 P1.3 口
void main(void) {
LED1 = 0; // 点亮 LED1
LED2 = 1; // 熄灭 LED2
LED3 = 0; // 点亮 LED3
LED4 = 1; // 熄灭 LED4
while (1) {
// 循环控制 LED 灯的亮灭
}
}
仅供参考,望采纳,谢谢。