#include<reg52.h>
sbit S7 = P3^0;
sbit S6 = P3^1;
void Delay(void) //延时函数: 10ms@11.0592MHz
{
unsigned char i, j;
i = 108;
j = 145;
do
{
while (--j);
} while (--i);
}
void Select_HC138(unsigned char n)
{
switch(n)
{
case 4:
P2 = (P2 & 0x1f) | 0x80;
break;
case 5:
P2 = (P2 & 0x1f) | 0xa0;
break;
}
}
void Scan_keys()
{
if(S7 == 0)
{
Delay(); //去抖动
if(S7 == 0)
{
P0 = 0xfe; //S7为开关:点亮L0
}
while(S7 == 0); //防止闪烁
}
if(S6 == 0)
{
Delay();
if(S6 == 0)
{
P0 = (P0 >> 7) | (P0 << 1); //循环左移
}
while(S6 == 0);
}
}
void main()
{
Select_HC138(5);
P0 = 0x00; //关闭蜂鸣器和继电器
Select_HC138(4);
P0 = 0xff;
while(1)
{
Scan_keys();
}
}
蓝桥杯单片机CT107D_04_按键控制LED位移
最新推荐文章于 2023-02-02 12:16:22 发布