1.用4个LED灯发光二极管实现至少四种灯光效果
2.可以通过输入按键在几种灯光之间切换
3.可以通过暂停按键停止彩色灯光效果,使小灯全亮,再次按下按键后继续之前的效果
4.增加自动在几种效果间切换功能
原理图很简单,正常的最小系统,加上你需要的LED灯和按键,分别定义他们的引脚就可以了,最后把对应引脚改进程序就搞定。
#include"reg52.h"
#include"intrins.h"
#define uchar unsigned char
#define uint unsigned int
#define LED P0
sbit KEY0=P2^0;
sbit KEY1=P2^1;
sbit KEY2=P2^2;
unsigned char code led_1[4]={0xfe,0xfd,0xfb,0xf7};//,0xef,0xdf,0xbf,0x7f};//定义第一种流水灯状态
unsigned char code led_2[4]={0xf7,0xfb,0xfd,0xfe};//0x7f,0xbf,0xdf,0xef};//,0xf7,0xfb,0xfd,0xfe};//定义第二种流水灯状态
unsigned char code led_3[4]={0xfe,0xf7,0xfd,0xfb};//,0xff};//定义第三种流水灯状态
unsigned char code led_4[4]={0xfd,0xfe,0xfb,0xf7};//,0xff};//定义第四种流水灯状态
unsigned char code led_5[8]={0xfe,0xfd,0xfb,0xf7,0xf7,0xfb,0xfd,0xfe};//,0xff};//定义第五种流水灯状态
unsigned char code led_6[16]={0xfe,0xfd,0xfb,0xf7,0xf7,0xfb,0xfd,0xfe,0xfe,0xf7,0xfd,0xfb,0xfd,0xfe,0xfb,0xf7};
unsigned char code led_8[4]={0xff,0xff,0xff,0xff};
unsigned int k,z,d; //定义四个无符号整型变量
void delayms(uint ms) //延时
{
uint i,j;
for(i=ms ; i>0 ; i--)
for(j=110 ; j>0 ; j--);
}
void key0()
{
if(KEY0 == 0)
{
delayms(10);
if(KEY0== 0)
{
k++;
delayms(10);
if(k>5)
k = 1;
}
}
}
void key1()
{
if(KEY1 == 0)
{
delayms(10);
if(KEY1 == 0)
{
d++;
delayms(10);
if(d>2)
d = 1;
}
}
}
void main()
{
uchar mode=0;
uchar key_flag=0;
while(!key_flag)
{
if(!KEY0)
{
_nop_();
if(!KEY0)
{
key_flag=1;
mode=1;
key0();
while(!KEY0);
}
}
if(!KEY1)
{
_nop_();//单周期指令,对于一个12M的晶体振荡器,延迟时间是1uS
if(!KEY1)
{
key_flag=1;
mode=2;
key1();
while(!KEY1);
}
}
if(!KEY2)
{
_nop_();
if(!KEY2)
{
key_flag=1;
mode=3;
while(!KEY2);
}
}
}
if (mode == 1)//按键1控制流水灯
{
k = 1; //初始化为开机第一种流水灯状态
while(1)
{
switch(k)
{
case 1: //第一种流水灯状态
for(z=0 ; z<4 ; z++)
{
LED = led_1[z];
delayms(500);
key0();
if(KEY0 == 0)
break;
}
delayms(500);
break;
case 2: //第二种流水灯状态
for(z=0 ; z<4 ; z++)
{
LED = led_2[z];
delayms(500);
key0();
if(KEY0 == 0)
break;
}
delayms(500);
break;
case 3: //第三种流水灯状态
for(z=0 ; z<4 ; z++)
{
LED = led_3[z];
delayms(500);
key0();
if(KEY0 == 0)
break;
}
delayms(500);
break;
case 4: //第四种流水灯状态
for(z=0 ; z<4 ; z++)
{
LED = led_4[z];
delayms(500);
key0();
if(KEY0 == 0)
break;
}
delayms(500);
break;
case 5: //第四种流水灯状态
for(z=0 ; z<8 ; z++)
{
LED = led_5[z];
delayms(500);
key0();
if(KEY0 == 0)
break;
}
delayms(500);
break;
}
}
}
else if (mode == 2)//按键2控制流水灯
{
d = 1;
while(1)
{
switch(d)
{
case 1:
for(z=0 ; z<4 ; z++)
{
LED = 0xf0;
delayms(500);
key1();
if(KEY1 == 0)
break;
}
break;
case 2:
for(z=0 ;z <4 ; z++)
{
LED = led_1[z];
delayms(500);
key1();
if(KEY1 == 0)
break;
}
delayms(500);
break;
}
}
}
else if (mode == 3)//按键3控制流水灯
{
while(1)
{
for(z=0 ; z<16 ; z++)
{
LED = led_6[z];
delayms(500);
if(KEY2 == 0)
break;
}
}
}
}
|