新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > “屏幕式”聲光電子琴程序

“屏幕式”聲光電子琴程序

作者: 時(shí)間:2016-11-30 來源:網(wǎng)絡(luò) 收藏
/*編寫人:伍浩榮
8個按鍵發(fā)出8個基本音,
能播放內(nèi)置音樂,音樂跟隨燈光閃爍
版權(quán)所有,翻版必究
作者博客:http://hi.baidu.com/haorongwu/home*/
sfr P4=0xe8;//定義P4組I0口
#include
#define uint unsigned int
#define uchar unsigned char
sbit speaker=P1^4;//喇叭接30腳
sbit key1=P4^4;//流水燈按鍵(暫時(shí))
sbit key2=P4^6;//播放音樂按鍵(暫定)
uchar a,b,num1,s1num,n1,n2;
char num;//定義num為可以負(fù)數(shù)
uchar code yinfu[]={0xfb,0xe9, //Do
0xfc,0x5c, //Re
0xfc,0xc1, //Mi
0xfc,0xef, //Fa
0xfd,0x45, //So
0xfd,0x92, //La
0xfd,0xd0, //Si
0xfd,0xee, //Do#
0x00,0x00, //間隔
};
uchar code shengri_tone[]={ 1,0,1,2,1,4,3,0, //生日快樂音調(diào)
1,0,1,2,1,5,4,0,
1,0,1,8,6,4,3,2,0,
7,0,7,6,4,5,4,0 //0代表不發(fā)聲,即停頓;數(shù)字即為音調(diào)
};
uchar code shengri_beat[]={ 24,1,24,48,48,48,72,5,//節(jié)拍
24,1,24,48,48,48,72,5,
24,1,24,48,48,48,48,72,5,
24,1,24,48,48,48,72,5 //節(jié)拍,即tone表各音調(diào)的延時(shí)
};
uchar code ledtable[]={0x7f,0xbf,0xdf,0xef, //取反
0xf7,0xfb,0xfd,0xfe};//P0組口的發(fā)光二極管代碼(焊接過程可能會相反,具體更改)
uchar code ledtable2[]={0x7f,0xbf,0xdf,0xef, //取反
0xf7,0xfb,0xfd,0xfe};//P2組口發(fā)光二極管代碼,相反!
void check_key();//改為P3組為按鍵
void keyscan();
void turn();//順時(shí)針流動
void back();
void qianhou();
void dangshuang();
void delay(uint z);//延時(shí)函數(shù)聲明
void delay1(void);//聲明第二個延時(shí)函數(shù)
void play1(void);//播放生日快樂
void main()
{
s1num=0;//流水燈種類標(biāo)志
key1=1;
key2=1;
TMOD=0x01;
TH0=a;
TL0=b;
ET0=1;//打開定時(shí)器,但是未允許中斷
TR0=1;
while(1)
{
check_key();
keyscan();
}
}
void time0() interrupt 1
{
TH0=a;
TL0=b;
speaker=~speaker;
}
void check_key()
{
P3=0xff;//先賦給P2組口高電平
switch(P3)//按下一個鍵相應(yīng)4個燈亮
{
case 0xfe:P0=0xee;P2=0x77;a=0xfb;b=0xe9;EA=1;break;//P0,P2組為發(fā)光二極管組
case 0xfd:P0=0xdd;P2=0xbb;a=0xfc;b=0x5c;EA=1;break;//注意:EA不能改為TR0
case 0xfb:P0=0xbb;P2=0xdd;a=0xfc;b=0xc1;EA=1;break;
case 0xf7:P0=0x77;P2=0xee;a=0xfc;b=0xef;EA=1;break;
case 0xef:P0=0xee;P2=0x77;a=0xfd;b=0x45;EA=1;break;
case 0xdf:P0=0xdd;P2=0xbb;a=0xfd;b=0x92;EA=1;break;
case 0xbf:P0=0xbb;P2=0xdd;a=0xfd;b=0xd0;EA=1;break;
case 0x7f:P0=0x77;P2=0xee;a=0xfd;b=0xee;EA=1;break;
default:EA=0;speaker=0;//P0=0xff;P2=0xff ;
}
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void keyscan()
{
if(key1==0)
{
delay(5);
if(key1==0)
{
s1num++;
while(!key1);
if(s1num==1)
{
turn();
}
}
if(s1num==2)
{
back();
}
if(s1num==3)
{
qianhou();
}
if(s1num==4)
{
dangshuang();
}
if(s1num==5)
s1num=1;
}
if(key2==0)
{
delay(5);
while(~key2);
play1();
}
}
上一頁 1 2 下一頁

關(guān)鍵詞: 聲光電子琴流水

評論


技術(shù)專區(qū)

關(guān)閉