标题:
STC15单片机按键控制流水灯方向左移右移源程序
[打印本页]
作者:
gaolixinca
时间:
2022-11-30 13:48
标题:
STC15单片机按键控制流水灯方向左移右移源程序
STC15单片机按键控制流水灯方向,按键1,流水灯左移;按键2,流水灯右移。
51hei图片20221130134553.png
(107.59 KB, 下载次数: 31)
下载附件
2022-11-30 13:47 上传
单片机源程序如下:
#include <reg51.h>
unsigned char ucLed = 1;
unsigned char ucKey_State = 0;
// 延时函数(最小约1ms@12MHz)
void Delay(unsigned int uiNum)
{
unsigned int i;
while(uiNum--)
for(i=0; i<628; i++);
}
// 按键处理:SW1-0x10, SW2-0x20, SW3-0x40, SW4-0x80
void Key_Proc(void)
{
unsigned char ucKey_Value = 0;
P0 |= 0xf0;
if((P0 & 0xf0) != 0xf0) // 按键按下
{
Delay(10); // 延时消抖
if((P0 & 0xf0) != 0xf0) // 按键按下
ucKey_Value = ~P0 & 0xf0; // 读取键值
}
if(ucKey_Value != ucKey_State)// 键值改变
ucKey_State = ucKey_Value; // 保存键值
else
ucKey_Value = 0; // 清除键值
switch(ucKey_Value)
{
case 0x10: // SW1按下
ucLed <<= 1; // 左移
if(ucLed == 0x10) // LED4亮后
ucLed = 1; // LED1亮
break;
case 0x20: // SW2按下
ucLed >>= 1; // 右移
if(ucLed == 0) // LED1亮后
ucLed = 8; // LED4亮
}
P0 = ~ucLed;
}
// 主函数
void main(void)
{
while(1)
{
Key_Proc();
}
}
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1