单片机编程题:开关控制四盏灯用开关K1,K2控制红黄兰绿四盏灯的亮灭.四盏灯接在P2口,K1,K2接在P1口.是没有按时候先亮第一盏,按K1亮第二盏而第一盏就灭了,再按K2就亮第三盏前面两盏就灭了,
来源:学生作业帮助网 编辑:作业帮 时间:2024/07/03 12:54:51
![单片机编程题:开关控制四盏灯用开关K1,K2控制红黄兰绿四盏灯的亮灭.四盏灯接在P2口,K1,K2接在P1口.是没有按时候先亮第一盏,按K1亮第二盏而第一盏就灭了,再按K2就亮第三盏前面两盏就灭了,](/uploads/image/z/7260587-35-7.jpg?t=%E5%8D%95%E7%89%87%E6%9C%BA%E7%BC%96%E7%A8%8B%E9%A2%98%EF%BC%9A%E5%BC%80%E5%85%B3%E6%8E%A7%E5%88%B6%E5%9B%9B%E7%9B%8F%E7%81%AF%E7%94%A8%E5%BC%80%E5%85%B3K1%2CK2%E6%8E%A7%E5%88%B6%E7%BA%A2%E9%BB%84%E5%85%B0%E7%BB%BF%E5%9B%9B%E7%9B%8F%E7%81%AF%E7%9A%84%E4%BA%AE%E7%81%AD.%E5%9B%9B%E7%9B%8F%E7%81%AF%E6%8E%A5%E5%9C%A8P2%E5%8F%A3%2CK1%2CK2%E6%8E%A5%E5%9C%A8P1%E5%8F%A3.%E6%98%AF%E6%B2%A1%E6%9C%89%E6%8C%89%E6%97%B6%E5%80%99%E5%85%88%E4%BA%AE%E7%AC%AC%E4%B8%80%E7%9B%8F%2C%E6%8C%89K1%E4%BA%AE%E7%AC%AC%E4%BA%8C%E7%9B%8F%E8%80%8C%E7%AC%AC%E4%B8%80%E7%9B%8F%E5%B0%B1%E7%81%AD%E4%BA%86%2C%E5%86%8D%E6%8C%89K2%E5%B0%B1%E4%BA%AE%E7%AC%AC%E4%B8%89%E7%9B%8F%E5%89%8D%E9%9D%A2%E4%B8%A4%E7%9B%8F%E5%B0%B1%E7%81%AD%E4%BA%86%2C)
单片机编程题:开关控制四盏灯用开关K1,K2控制红黄兰绿四盏灯的亮灭.四盏灯接在P2口,K1,K2接在P1口.是没有按时候先亮第一盏,按K1亮第二盏而第一盏就灭了,再按K2就亮第三盏前面两盏就灭了,
单片机编程题:开关控制四盏灯
用开关K1,K2控制红黄兰绿四盏灯的亮灭.四盏灯接在P2口,K1,K2接在P1口.
是没有按时候先亮第一盏,按K1亮第二盏而第一盏就灭了,再按K2就亮第三盏前面两盏就灭了,再把K1打开就亮第四盏前三盏暗,最后再把K2打开就回到第一盏灯亮其他暗.就是这样的,很麻烦.还要用C语言来编.应该是C51的东西吧.
单片机编程题:开关控制四盏灯用开关K1,K2控制红黄兰绿四盏灯的亮灭.四盏灯接在P2口,K1,K2接在P1口.是没有按时候先亮第一盏,按K1亮第二盏而第一盏就灭了,再按K2就亮第三盏前面两盏就灭了,
昨天就看到有人问了,不会还是你吧,你要说清楚啊,想怎么控制,一起亮,一起灭?一个按键开,一个按键关?说具体点,要不怎么帮你
今天晚上上线晚了,明天上班抽时间给你写吧,一会儿就搞定了,要不要留个邮箱啊,电路图也给你吧
还有,一定要按这个顺序吗?如果我乱按会出现什么情况,直接无效?
算了,直接贴上来吧
#include "reg52.h"
#define uint unsigned int
#define uchar unsigned char
sbit LED_R = P2^0;
sbit LED_Y = P2^1;
sbit LED_B = P2^2;
sbit LED_G = P2^3;
sbit Key1 = P1^0;
sbit Key2 = P1^1;
uchar Key1_Times = 0, Key2_Times = 0, Key_Flag = 0;
void Delay(uint x)
{
uint a,b;
for(a = x; a > 0; a--)
for(b = 110; b > 0; b--);
}
void KeyScan()
{
if(Key1 == 0)
{
Delay(10);
if(Key1 == 0)
{
Key_Flag = 1;
Key1_Times++;
if(Key1_Times == 3)
Key1_Times = 2;
}
while(!Key1);
}
if(Key2 == 0)
{
Delay(10);
if(Key2 == 0)
{
Key_Flag = 1;
Key2_Times++;
if(Key2_Times == 3)
Key2_Times = 2;
}
while(!Key2);
}
}
void main()
{
P2 = 0xff;
LED_R = 0;
while(1)
{
KeyScan();
if(Key_Flag == 1)
{
Key_Flag = 0;
if((Key1_Times == 1) && (Key2_Times == 0))
{
LED_R = 1;
LED_Y = 0;
}
else if((Key1_Times == 1) && (Key2_Times == 1))
{
LED_R = 1;
LED_Y = 1;
LED_B = 0;
}
else if((Key1_Times == 1) && (Key2_Times == 2))
{
Key2_Times--;
}
else if((Key1_Times == 2) && (Key2_Times == 0))
{
Key1_Times--;
}
else if((Key1_Times == 2) && (Key2_Times == 1))
{
LED_R = 1;
LED_Y = 1;
LED_B = 1;
LED_G = 0;
}
else if((Key1_Times == 2) && (Key2_Times == 2))
{
LED_Y = 1;
LED_B = 1;
LED_G = 1;
LED_R = 0;
Key1_Times = 0;
Key2_Times = 0;
}
}
}
}