刚看了一份关于光立方的视频里片面提到“呼吸灯”一词,忙草草写了一个带数码管显示亮度变化的呼吸灯程序,其中整型frequency为调节呼吸频率的参数,附图为临时做的gif文件,需等会儿才见效果/*12Mhz??§????*/
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
uint f=100; /*????????f????????????1~10khz*/
uint H=6;/*????????·§????????0~10000/f????H>L*/
uint L=0;/*????????·§????????0~10000/f????H>L*/
uchar code wela[]={0xfe,0xfd,0xfb,0xf7};
uchar code dula[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xD8,0x80,0x90};
uint m=0,t;
uchar amp=0,frequency=100;
sbit pwm=P1^7;
sbit pwm_=P1^6;
sbit hkey=P3^5;
sbit lkey=P3^3;
void diskey();
void delay(uint z);
void display();
void xunhuan();
void main()
{
EA=1;
ET0=1;
TMOD=0x02;
TH0=146;
t=10000/f;
TR0=1;
while(1)
{
if(hkey==0)diskey();
if(lkey==0)diskey();
display();
xunhuan();
}
}
void TRT0() interrupt 1
{
m++;
if(m>=t)m=0;
if(m==L){pwm=0;}
if(m==H){pwm=1;pwm_=0;}
}
void diskey()
{
display();
if(hkey==0&&H<100)H++;
delay(10);
if(lkey==0&&H>0)H--;
display();
}
void xunhuan()
{
if(amp==1)H++;
if(H>99)amp=0;
if(amp==0)H--;
if(H<1)amp=1;
delay(frequency);
}
void delay(uint z)
{
uint x,y;
for(y=z;y>0;y--)
for(x=12;x>0;x--);
}
void display()
{
delay(2);
P2=dula[H/1000];
P0=wela[0];
delay(5);
P0=0xff;
P2=dula[H%1000/100];
P0=wela[1];
delay(5);
P0=0xff;
P2=dula[H%100/10];
P0=wela[2];
delay(5);
P0=0xff;
P2=dula[H%10];
P0=wela[3];
delay(5);
P0=0xff;
}