|
20黑币
学校实训,要求51实现电子琴,要求如题。
数码管为共阴数码管,但多了595芯片
程序借鉴了一部分网上内容,目前可以实现1-7的发音和数码管显示,但按8按键却蜂鸣器长鸣,不知道什么地方出错
希望各位大神帮忙看下
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit BeepIO =P1^7;
sbit DI1 =P1^0;
sbit DI2 =P1^1;
sbit DI3 =P1^2;
sbit DI4 =P1^3;
sbit DIO =P1^4;
sbit RCK =P1^5;
sbit SCLK =P1^6;
uint tune[]={0xfc44,0xfcac,0xfd09,0xfd34,0xfd82,0xfdc8,0xfe07};
uchar code num[]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,
0x00,0xff
};
//void KeyProc(uchar dat);
uchar SoundTH;
uchar SoundTL;
unsigned char Time;
unsigned char High,Low; //定时器预装值的高8位和低8位
// 音阶频率表 高八位
unsigned char code FREQH[]={
0xF2,0xF3,0xF5,0xF5,0xF6,0xF7,0xF8,
0xF9,0xF9,0xFA,0xFA,0xFB,0xFB,0xFC,0xFC, //1,2,3,4,5,6,7,8,i
0xFC,0xFD,0xFD,0xFD,0xFD,0xFE,
0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF,
} ;
// 音阶频率表 低八位
unsigned char code FREQL[]={
0x42,0xC1,0x17,0xB6,0xD0,0xD1,0xB6,
0x21,0xE1,0x8C,0xD8,0x68,0xE9,0x5B,0x8F, //1,2,3,4,5,6,7,8,i
0xEE,0x44, 0x6B,0xB4,0xF4,0x2D,
0x47,0x77,0xA2,0xB6,0xDA,0xFA,0x16,
};
//世上只有妈妈好数据表
unsigned char code MUSIC[]={ 6,2,3, 5,2,1, 3,2,2, 5,2,2, 1,3,2, 6,2,1, 5,2,1,
6,2,4, 3,2,2, 5,2,1, 6,2,1, 5,2,2, 3,2,2, 1,2,1,
6,1,1, 5,2,1, 3,2,1, 2,2,4, 2,2,3, 3,2,1, 5,2,2,
5,2,1, 6,2,1, 3,2,2, 2,2,2, 1,2,4, 5,2,3, 3,2,1,
2,2,1, 1,2,1, 6,1,1, 1,2,1, 5,1,6, 0,0,0
};
void KeyProc(uchar dat);
void delayTips(unsigned char t);
void PlaySong();
void Song()
{
uchar i;
uchar k;
i=0;
while(i<100)
{
k=MUSIC[i]+7*MUSIC[i+1]-1;//去音符振荡频率所需数据
High=FREQH[k];
Low=FREQL[k];
Time=MUSIC[i+2]; //节拍时长
i=i+3;
PlaySong();
// delayTips(Time); //延时所需要的节拍
if(P0==0xef)
{
while(P0==0xef);
break;
}
}
// PlaySong();
TR0=0;
}
//延时函数大约约2*z+5us
void delay2xus(unsigned char z)
{
while(z--);
}
// 延时函数大约约1ms
void delayms(unsigned char x)
{
while(x--)
{
delay2xus(245);
delay2xus(245);
}
}
//节拍延时函数
void delayTips(unsigned char t)
{
unsigned char i;
for(i=0;i<t;i++)
{
delayms(250);
}
TR0=0;
}
//播放音乐的函数
void PlaySong()
{
TH0=High;//赋值定时器时间,决定频率
TL0=Low;
TR0=1; //打开定时器
delayTips(Time); //延时所需要的节拍
}
/*
//void KeyProc(uchar dat);
void Song()
{
uchar i;
uchar k;
i=0;
while(i<100)
{
k=MUSIC[i]+7*MUSIC[i+1]-1;//去音符振荡频率所需数据
High=FREQH[k];
Low=FREQL[k];
Time=MUSIC[i+2]; //节拍时长
i=i+3;
}
PlaySong();
}
*/
void LEDIO(uchar x)
{
uchar i;
for(i=0;i<8;i++)
{
if(x&0x80)
DIO = 1;
else
DIO = 0;
SCLK = 0;
SCLK = 1;
x <<= 1;
}
}
//数据送入74HC595的并口输出寄存器
void LEDRCK()
{
RCK = 0;
RCK = 1;
}
//延时50*T时间函数
void delay_50ms(uint t)
{
uint j;
for(;t>0;t--)
for(j=6254;j>0;j--)
;
}
/*void LEDdisplay()
{
uchar i;
for(i=0;i<18;)
{
LEDIO(num[i]);
LEDRCK();
delay_50ms(20);
i++;
if(i==18)
i=0;
}
}
*/
void main()
{
uchar dat;
TMOD = 0X01;
ET0 = 1;
TR0 = 0;
EA = 1;
DI1 =1;
DI2 =1;
DI3 =1;
DI4 =1;
while(1)
{ dat=~P0;
KeyProc(dat);
}
}
void KeyProc(uchar dat)
{
uchar i;
if(dat==0x00)
{
TR0=0;BeepIO=1;
return;
}
switch(dat)
{
case 0x01: i=0;
break;
case 0x02: i=1;
break;
case 0x04: i=2;
break;
case 0x08: i=3;
break;
case 0x10: i=4;
break;
case 0x20: i=5;
break;
case 0x40: i=6;
break;
case 0x80: i=7;
break;
default:break;
}
if(i<7)
{
LEDIO(num[i+1]);
LEDRCK();
SoundTH =tune[i]/256;
SoundTL =tune[i]%256;
TR0=1;}
else
{Song();
}
}
void BeepTimer()interrupt 1
{
BeepIO=!BeepIO;
TH0= SoundTH;
TL0= SoundTL;
}
|
|