51单片机双开关控制四LED灯 - do while语句实现
#include <reg51.h>
main() { char switch_status; char led_pattern;
do { switch_status = P1 & 0x03; // 读取P1口开关状态
// 根据开关状态确定LED亮灭模式
if (switch_status == 0x00) {
led_pattern = 0x0F; // 所有LED亮
} else if (switch_status == 0x01) {
led_pattern = 0x0C; // LED1和LED2亮
} else if (switch_status == 0x02) {
led_pattern = 0x03; // LED3和LED4亮
} else {
led_pattern = 0x00; // 所有LED灭
}
P2 = led_pattern; // 输出LED亮灭模式到P2口
} while (1); }
原文地址: https://www.cveoy.top/t/topic/pQ1 著作权归作者所有。请勿转载和采集!