我的彩灯设计P3口四个按键
时间:10-02
整理:3721RD
点击:
主题:模拟彩灯,开发板从左至右4个按键分别为S1-S4,从上至下8个灯为L1-L8,按下S1键8只LED小灯以1HZ频率闪烁,按下S2键8只灯奇偶交替点亮,间隔0.3S,按下S3键,L1-L4与L5-L8灯交替点亮,间隔0.2s,按下S4键熄灭所有灯。
include <reg51.h> //包含库函数文件
#define S1 0x0e
#define S2 0x0d
#define S3 0x0b
#define S4 0x07
void delay(unsigned int t)
{
unsigned int i,j;
for(i=0;i<t;i++) //延时t个1ms
for(j=0;j<112;j++); //延时约1ms
}
//4个简单按键扫描子程序
unsigned char ScanKey(void)
{
unsigned char key;
if((P3&0x0f)==0x0f) return(0xff);
delay(30);
if((P3&0x0f)==0x0f) return(0xff);
key=P3&0x0f;
while((P3&0x0f)!=0x0f);//等待弹出
//beep();
return(key);
}
void main()
{
unsigned char KeyValue,mode=4;
while(1)
{
P3=0xff;
KeyValue=ScanKey();//读入键值
switch(KeyValue)
{
case S1: mode=1; break;
case S2: mode=2; break;
case S3: mode=3; break;
case S4: mode=4; break;
default: break;
}
switch(mode)
{
case 1: P1=0x00;delay(500);P1=0xff;delay(500); break;
case 2: P1=0x55;delay(300);P1=0xaa;delay(300); break;
case 3: P1=0xf0;delay(200);P1=0x0f;delay(200); break;
case 4: P1=0xff; break;
default: break;
}
}
}
include <reg51.h> //包含库函数文件
#define S1 0x0e
#define S2 0x0d
#define S3 0x0b
#define S4 0x07
void delay(unsigned int t)
{
unsigned int i,j;
for(i=0;i<t;i++) //延时t个1ms
for(j=0;j<112;j++); //延时约1ms
}
//4个简单按键扫描子程序
unsigned char ScanKey(void)
{
unsigned char key;
if((P3&0x0f)==0x0f) return(0xff);
delay(30);
if((P3&0x0f)==0x0f) return(0xff);
key=P3&0x0f;
while((P3&0x0f)!=0x0f);//等待弹出
//beep();
return(key);
}
void main()
{
unsigned char KeyValue,mode=4;
while(1)
{
P3=0xff;
KeyValue=ScanKey();//读入键值
switch(KeyValue)
{
case S1: mode=1; break;
case S2: mode=2; break;
case S3: mode=3; break;
case S4: mode=4; break;
default: break;
}
switch(mode)
{
case 1: P1=0x00;delay(500);P1=0xff;delay(500); break;
case 2: P1=0x55;delay(300);P1=0xaa;delay(300); break;
case 3: P1=0xf0;delay(200);P1=0x0f;delay(200); break;
case 4: P1=0xff; break;
default: break;
}
}
}
跑了一遍,不错是设计效果。程序是否还可优化不懂了(第一感觉:按键电路程序应加几重防抖动),学习学习.....
彩灯 相关文章: