实现一个键控制5种花样流水灯,一个键控制1-7个音阶,并录入,和播放,还有自动播放自带歌曲并且RGB LED随着音乐变化颜 {MOD}。。。。目前烧入到
单片机放,在自己焊接的
PCB板上,运行很慢很卡。。求各位大神帮忙,非常感谢!!!!!!!!!!!!!!!!!!附:
电路图!!!!非常感谢,
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uint code a[14]={63625,63833,64019,64104,64260,64400,64524,
65058,65110,65157,65178,65217,65252,65283};//各音符对应定时初值
char b[100]={8,9,10,11,12,13,14,1,1,5,5,6,6,5,15,16};//歌曲编码,该数组前七个元素对应1-7音阶(1)
char i=0,j=7,l=0,num7=1,num9=5,h=0,k;
sbit k0=P0^7;
sbit k1=P2^1;
sbit k2=P2^2;
sbit k3=P2^3;
sbit k4=P2^4;
sbit k5=P2^5;
sbit k6=P2^6;//1-7音阶对应按键
sbit k7=P2^7;//播放录入
sbit k9=P2^0;//流水灯
sbit speak=P3^7;//蜂鸣器
sbit RGBred=P3^3;
sbit RGBgreen=P3^5;
sbit RGBblue=P3^6;
uchar shunxu[3][3]={ //定义RGB彩灯亮的顺序
{0,0,1},
{0,1,0},
{1,0,0}
} ;
void chushideng() //RGB彩灯点亮的函数
{
RGBblue=shunxu[h][0];
RGBred=shunxu[h][1];
RGBgreen=shunxu[h][2];
h++;
if(h==3)
h=0;
}
void init()
{
TMOD=0x11;//设置定时器的工作方式;
TH1=(65536-45872)/256;
TL1=(65536-45872)%256;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;//给计数器赋初值;
EA=1;//开总中断
ET0=1;
ET1=1;
PT0=1;
TR1=0;//打开0,1中断
TR0=1;
}
void delayms(int x)
{
int m,n;
for(m=0;m<x;m++)
for(n=0;n<110;n++);
}
void song()
{
i=7;//(1)
while(b[i]!=16&&num7==0)//16表示乐曲结束
{
if(b[i]==15)//15表示乐曲中间的休止符
{
delayms(250);
}
if(b[i]!=15)//(0-13表示正常的音符)
{
P1=0xff;
chushideng();
TR1=1;
delayms(250);
TR1=0;
delayms(125);
}
i++;
}
RGBblue=1;
RGBred=1;
RGBgreen=1;
num7=1;
}
void keyscan2()
{
if(k9==0)//五种流水灯
{
delayms(10);
if(k9==0)
{
while(!k9);
num9++;
if(num9>5)
num9=0;
}
}
switch(num9)
{
case 0:
P1=0xfe;
delayms(100);
for(k=0;k<7;k++)
{
P1<<=1;
P1|=0x01;
delayms(100);
};
P1=0xdf;
delayms(200);
for(k=7;k>0;k--)
{
P1>>=1;
P1|=0x80;
delayms(100);
};
break;
case 1:
P1=0xfe;
delayms(100);
for(k=0;k<7;k++)
{
P1<<=1;
P1|=0x01;
delayms(100);
};
break;
case 2:
P1=0xfc;
delayms(100);
for(k=0;k<4;k++)
{
P1<<=2;
P1|=0x03;
delayms(100);
};
break;
case 3:
P1=~P1;
delayms(100);
break;
case 4:
P1=0x7e;
delayms(100);
P1=0xbd;
delayms(100);
P1=0xdb;
delayms(100);
P1=0xe7;
delayms(100);
P1=0xdb;
delayms(100);
P1=0xbd;
delayms(100);
P1=0x7e;
delayms(100);
break;
case 5:
P1=0xff;
break;
}
if(k0==0)
{
delayms(5);
if(k0==0)
{
while(!k0);
i=0;
TR1=1;
delayms(125);
TR1=0;
b[j++]=1;
b[j]=16;
}
}
if(k1==0)
{
delayms(10);
if(k1==0)
{
while(!k1);
i=1;
TR1=1;
delayms(125);
TR1=0;
b[j++]=2;
b[j]=16;
}
}
if(k2==0)
{
delayms(10);
if(k2==0)
{
while(!k2);
i=2;
TR1=1;
delayms(125);
TR1=0;
b[j++]=3;
b[j]=16;
}
}
if(k3==0)
{
delayms(10);
if(k3==0)
{
while(!k3);
i=3;
TR1=1;
delayms(125);
TR1=0;
b[j++]=4;
b[j]=16;
}
}
if(k4==0)
{
delayms(10);
if(k4==0)
{
while(!k4);
i=4;
TR1=1;
delayms(125);
TR1=0;
b[j++]=5;
b[j]=16;
}
}
if(k5==0)
{
delayms(10);
if(k5==0)
{
while(!k5);
i=5;
TR1=1;
delayms(125);
TR1=0;
b[j++]=5;
b[j]=16;
}
}
if(k6==0)
{
delayms(10);
if(k4==0)
{
while(!k6);
i=6;
TR1=1;
delayms(125);
TR1=0;
b[j++]=6;
b[j]=16;
}
}
song();
}
void main()
{
init();
while(1)
keyscan2();
}
void T1
time() interrupt 3 //定时器T0的中断函数
{
TH1=a[b[i]-1]/256;
TL1=a[b[i]-1]%256;
speak=~speak;
if(num7==0)
{
RGBblue=~RGBblue;
RGBgreen=~RGBgreen;
RGBred=~RGBred;
}
}
void T0time() interrupt 1
{
TH0=(65536-200)/256;
TL0=(65536-200)%256;
TR0=0;
if(k7==0)
{
delayms(5);
while(!k7);
if(k7==0)
num7=0;
}
TR0=1;
}
-
电路图
把延时缩短点,加大外部晶振
一周热门 更多>