本系统通过光敏电阻将光信号转化为电信号,再经由STC89C51单片机进行引脚的数据采集,获得信号后通过LM386放大音频信号,通过控制电路,控制扬声器发出相应频率声音,并将音符信息显示在1602液晶上。
硬件部分主要包含激光发射电路,激光接收电路,音频输出电路,以及液晶显示电路等,软件部分主要包括激光信号采集,液晶显示器的 *** 作,以及发声频率的控制。
51单片机制作的激光竖琴程序主要是:
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
//数码管 共阳
uchar code DSY_Table[]={
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,
0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xbf}
//音符对应的延时
uint code Tone_Delay_Table[]={
64021,64103,64260,64400,64524,64580,64684,64777,
64820,64898,64968,65030,65058,65110,65157,65178}
sbit BEEP=P3^2
uchar KeyNo
//生日快乐歌的音符频率表,不同的频率用延时值来表示
uchar code SONG_TONE[]={212,212,190,212,159,169,212,212,190,212,142,159,212,212,212,159,169,190,119,119,126,159,142,159,0}
uchar code SONG_LONG[]={9,3,12,12,12,24,9,3,12,12,12,24,9,3,12,12,12,12,9,3,12,12,12,24,0}
///////delay------
void DelayMS(uint x)
{
uchar i
while(x--)for(i=0i<120i++)
}
void PlayMusic()
{
uint i=0,j,k
while(SONG_LONG[i]!=0||SONG_TONE[i]!=0)
{
for(j=0j<SONG_LONG[i]*20j++)
{
BEEP=~BEEP
for(k=0k<SONG_TONE[i]/3k++)
}
DelayMS(80)//每个音符之间的时间间隔
i++
}
}
void Keys_SCAN()
{
uchar k,t,key_state
P1=0xff
//while(1)
//{
t=P1
if(t!=0xff)
{
DelayMS(10)
if(t!=P1)
{
switch(t){
case 0x87: BEEP=159 break//1mie do
case 0x47: BEEP=142 break//2mie re
case 0x27: BEEP=126 break//3mie mi
case 0x17: BEEP=119 break//4mie fa
case 0x0f: BEEP=212 break//5mie sol
default: BEEP=169
}// silu
}
//continue
// key_state=~t
// k=0
//while(key_state!=0)
// {
//k++
//key_state>>=1
// }
// KeyNo=k
//}
//return//return 语句的加法很重要
}
//return
//KeyNo=k
}
void play_Tone() interrupt 1
{
TH0=Tone_Delay_Table[KeyNo]/256
TL0=Tone_Delay_Table[KeyNo]%256
BEEP=~BEEP
}
//////MAIN/////////////////
void main()
{
//BEEP=0
//while(1)
//P0=0xF0
P0=0xbf
PlayMusic()
DelayMS(1000)
DelayMS(1000)
TMOD=0x01
IE=0x82
while(1)
{
P1=0xff
if(P1 != 0xff)
{
Keys_SCAN()
P0=DSY_Table[KeyNo]
TR0=1
}
else
{
TR0=0
}
DelayMS(2)
}
}
#include <reg51.h>#define uchar unsigned char
#define uint unsigned int
//数码管 共阳
uchar code DSY_Table[]={
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,
0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xbf}
//音符对应的延时
uint code Tone_Delay_Table[]={
64021,64103,64260,64400,64524,64580,64684,64777,
64820,64898,64968,65030,65058,65110,65157,65178}
sbit BEEP=P3^2
uchar KeyNo
//生日快乐歌的音符频率表,不同的频率用延时值来表示
uchar code SONG_TONE[]={212,212,190,212,159,169,212,212,190,212,142,159,212,212,212,159,169,190,119,119,126,159,142,159,0}
uchar code SONG_LONG[]={9,3,12,12,12,24,9,3,12,12,12,24,9,3,12,12,12,12,9,3,12,12,12,24,0}
///////delay------
void DelayMS(uint x)
{
uchar i
while(x--)for(i=0i<120i++)
}
void PlayMusic()
{
uint i=0,j,k
while(SONG_LONG[i]!=0||SONG_TONE[i]!=0)
{
for(j=0j<SONG_LONG[i]*20j++)
{
BEEP=~BEEP
for(k=0k<SONG_TONE[i]/3k++)
}
DelayMS(80)//每个音符之间的时间间隔
i++
}
}
void Keys_SCAN()
{
uchar k,t,key_state
P1=0xff
// while(1)
// {
t=P1
if(t!=0xff)
{
DelayMS(10)
if(t!=P1)
{
switch(t){
case 0x87:BEEP=159break//1mie do
case 0x47:BEEP=142 break//2mie re
case 0x27:BEEP=126break//3mie mi
case 0x17:BEEP=119break//4mie fa
case 0x0f:BEEP=212break//5mie sol
default: BEEP=169
} // silu
}
// continue
// key_state=~t
// k=0
// while(key_state!=0)
// {
// k++
// key_state>>=1
// }
// KeyNo=k
// }
//return //return 语句的加法很重要
}
//return
//KeyNo=k
}
void play_Tone() interrupt 1
{
TH0=Tone_Delay_Table[KeyNo]/256
TL0=Tone_Delay_Table[KeyNo]%256
BEEP=~BEEP
}
//////MAIN/////////////////
void main()
{
//BEEP=0
//while(1)
// P0=0xF0
P0=0xbf
PlayMusic()
DelayMS(1000)
DelayMS(1000)
TMOD=0x01
IE=0x82
while(1)
{
P1=0xff
if(P1 != 0xff)
{
Keys_SCAN()
P0=DSY_Table[KeyNo]
TR0=1
}
else
{
TR0=0
}
DelayMS(2)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)