发布网友 发布时间:2023-08-04 02:55
共2个回答
热心网友 时间:2024-12-02 07:02
#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^0;
uchar KeyNo;
//生日快乐歌的音符频率表,不同的频率用延时值来表示
uchar code SONG_TONE[]={212,212,190,212,159,169,212,212,190,212,142,159,212,212,106,126,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,12,9,3,12,12,12,24,0};
///////delay------
void DelayMS(uint x)
{
uchar i;
while(x--)for(i=0;i<120;i++);
}
void PlayMusic()
{
uint i=0,j,k;
while(SONG_LONG[i]!=0||SONG_TONE[i]!=0)
{
for(j=0;j<SONG_LONG[i]*20;j++)
{
BEEP=~BEEP;
for(k=0;k<SONG_TONE[i]/3;k++);
}
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) 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()
{
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);
}
}
热心网友 时间:2024-12-02 07:03
你是做毕业设计嘛?成功没?