51单片机播放10首歌
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include
#include
#include
#include
#define uchar unsigned char
#define uint unsigned int
uchar code * song;
uchar code * song_name;
uchar code * song_lrc;
uchar table[10]={'0','1','2','3','4','5','6','7','8','9'};
uchar code tab[]={0xf8,0x94,0xf9,0x5c,0xfa,0x1a,0xfa,0x6a,0xfb,0x00,0xfb,0x8c,0xfc,0x0e,
0xfc,0x4a,0xfc,0xae,0xfd,0x08,0xfd,0x30,0xfd,0x80,0xfd,0xc6,0xfe,0x02,
0xfe,0x2a,0xfe,0x5c,0xfe,0x84,0xfe,0x98,0xfe,0xc0,0xfe,0xe8,0xfe,0x06,0xff};
//两只老虎
uchar code song1[]={0x44,0x54,0x64,0x44,0x44,0x54,0x64,0x44,0x64,0x74,0x88,0x64,
0x74,0x88,0x82,0x92,0x82,0x72,0x64,0x44,0x82,0x92,0x82,0x72,
0x64,0x44,0x54,0x14,0x48,0x54,0x14,0x48,0xff};
//新年快乐
uchar code song2[]={0x42,0x42,0x44,0x14,0x02,0x62,0x62,0x64,0x44,0x04,0x42,0x62,
0x84,0x84,0x02,0x72,0x62,0x54,0x04,0x52,0x62,0x74,0x74,0x02,
0x62,0x52,0x64,0x44,0x04,0x42,0x62,0x54,0x14,0x02,0x32,0x52,
0x44,0x04,0xff};
//生日快乐
uchar code song3[]={0xC4,0x12,0x32,0x44,0x42,0x52,0x52,0x44,0x32,0x32,0x12,0x14,
0x12,0x12,0x32,0x42,0x54,0x44,0x4F,0xC4,0x12,0x32,0x44,0x42,
0x72,0x58,0x42,0x32,0x34,0x14,0x12,0x32,0x34,0x12,0x1F,0xff};
//星星索
uchar code song4[]={0x82,0x01,0x81,0x94,0x84,0xB4,0x04,0x04,0x82,0x01,0x81,0x94,
0x84,0xC4,0xB4,0x04,0x82,0x01,0x81,0xF4,0xD4,0xB4,0xA4,0x94,
0x04,0xE2,0x01,0xE1,0xD4,0xB4,0xC4,0xB4,0x04,0xff};
//梁祝
uchar code song5[]={0xA2,0xB2,0xD2,0xB2,0xA4,0xB2,0xD2,0xB2,0xA2,0x82,0x72,0x88,
0x72,0x82,0xA2,0x82,0x72,0x62,0x42,0x62,0x38,0x38,0xA4,0x02,
0xB2,0xA4,0x84,0x72,0x82,0x72,0x62,0x72,0x84,0x72,0x64,0x62,
0x12,0x34,0x02,0x42,0x38,0x38,0x44,0x02,0x32,0x44,0x64,0x72,
0x82,0x72,0x62,0x72,0x84,0xA2,0x72,0xff };
//同一首歌
uchar code song6[]={0x42,0x82,0x82,0x82,0x86,0x72,0x63,0x71,0x62,0x52,0x48,0xB2,
0xB2,0xB2,0xB2,0xB6,0xA2,0x83,0xA1,0xA2,0x92,0x88,0x82,0xB2,
0xB2,0xA2,0x86,0x72,0x63,0x71,0x62,0x52,0x44,0x12,0x02,0x12,
0x62,0x62,0x52,0x46,0x82,0x73,0x61,0x52,0x32,0x48,0x00,0xff};
//记事本
uchar code song7[]={0x84,0x82,0x82,0x82,0xB2,0xB2,0x82,0xC2,0xC3,0x82,0x84,0xB2,
0xA2,0xA2,0x93,0x04,0x94,0x92,0x82,0x82,0x63,0x62,0x62,0x52,
0x52,0x42,0x44,0x04,0x62,0x52,0x52,0x42,0x84,0x04,0x92,0x82,
0x94,0x82,0x63,0x04,0x84,0x82,0x82,0x82,0xB2,0xB2,0x82,0xC2,
0xC3,0x83,0x04,0x84,0xB2,0xA2,0xA2,0x93,0x94,0x82,0x92,0x82,
0x63,0x04,0x52,0x62,0x52,0x62,0x44,0x04,0xff};
//绿岛小夜曲
uchar code song8[]={0xA2,0xB2,0xD2,0xB2,0xA4,0xB2,0xD2,0xB2,0xA2,0x82,0x72,0x88,
0x72,0x82,0xA2,
0x82,0x72,0x62,0x42,0x62,0x38,0x38,0xA4,0x02,
0xB2,0xA4,0x84,0x72,0x82,0x72,0x62,0x72,0x84,0x72,0x64,0x62,
0x12,0x34,0x02,0x42,0x38,0x38,0x44,0x02,0x32,0x44,0x64,0x72,
0x82,0x72,0x62,0x72,0x84,0xA2,0x72,0xff};
//让我们荡起双桨
uchar code song9[]={0x06,0x08,0x08,0x08,0x09,0x08,0x0A,0x18,0x0C,0x08,0x0A,0x08,0x08,0x08,0x09,
0x10,0x06,0x20,0x00,0x08,0x08,0x08,0x09,0x08,0x0A,0x08,0x0C,0x18,0x00,0x01,0x0C,0x08,0x0D,0x10,0x09,0x10,0x0A,0x30,0x00,0x01,0x0A,0x08,0x0C,0x08,0x0D,0x20,0x0C,0x18,0x0D,0x08,0x0F,0x08,0x0E,0x04,0x0D,0x04,0x0C,0x08,0x0D,0x08,0x0A,0x10,0x00,0x04,0x08,0x08,0x09,0x08,0x0A,0x18,0x0C,0x08,0x08,0x10,0x06,0x10,0x08,0x08,0x09,0x08,0x0A,0x08,0x0D,0x08,0x0C,0x30,0x00,0x10,0x0A,0x20,0x0D,0x18,0x00,0x01,0x0D,0x08,0x0C,0x08,0x0B,0x08,0x0A,0x10,0x00,0x01,0x09,0x20,0x0A,0x18,0x0C,0x08,0x06,0x08,0x08,0x08,0x09,0x10,0x00,0x10,0x08,0x08,0x09,0x08,0x0A,0x10,0x0C,0x0B,0x00,0x01,0x0C,0x04,0x0D,0x10,0x0F,0x10,0x0E,0x08,0x0D,0x08,0x0C,0x08,0x0A,0x08,0x0D,0x40 ,0x00,0x01,0xff};
uchar code song_name10[18]={0xc7,0xfa,0xc4,0xbf,0xca,0xae,0xa1,0xc3,0xc0,0xbc,0xbb,0xa8,0xb2,0xdd,0xff};//兰花草
uchar code song10[]={0x42,0x82,0x82,0x82,0x86,0x72,0x63,0x71,0x62,0x52,0x48,0xB2,
0xB2,0xB2,0xB2,0xB6,0xA2,0x83,0xA1,0xA2,0x92,0x88,0x82,0xB2,
0xB2,0xA2,0x86,0x72,0x63,0x71,0x62,0x52,0x44,0x12,0x02,0x12,
0x62,0x62,0x52,0x46,0x82,0x73,0x61,0x52,0x32,0x48,0x00,0xff};
unsigned char code init[]={0xB0,0xB4,0xBC,0xFC,0xD1,0xA1,0xD4,0xF1,0xB8,0xE8,0xC7,
0xFA,0x20,0x20,0x20,0x20,0xff};
//unsigned char code qingling[]={0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0xff};
uchar data flag;
uchar data stl0;
uchar data sth0;
uchar data l;
uchar data k;
uchar data key;
uchar data t;
uchar data n;
uchar Key_Value=0xff;
unsigned char bdata key;
unsigned char bdata status;
uchar COUNT_1=0,count=0,yimiao=0,j,i;
sbit Sing=P2^7; /*P3口接蜂鸣器,输出方波 */
sbit E=P2^2;
sbit RW=P2^1;
sbit RS=P2^0;
void delay_song(); /* 延时128ms */
void delay(int i); /* 延时 */
void KeyScan(); /* 键盘扫描函数 */
uchar zz;
void delay_song(void) /*延时128ms*/
{
uint count111=140;
unsigned int i,j;
for(i=0;i
}
void delay(int i) /*延时*/
{
int j,k;
for(j=0;j{
k=0x0f;
while(k--);
}
}
void Delay_Ms(uint x)//1ms
{
uint g,k;
for(g=0;g
}
void Delay_xMs(uint x)//1ms
{
unsigned int i,j;
for( i =0;i < x;i++ )
{
for( j =0;j<3;j++ );
}
}
void delay_5us()
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
bit Busy(void)
{
bit busy_flag = 0;
RS = 0;
RW = 1;
E = 1;
delay_5us();
busy_flag = (bit)(P0 & 0x80);
E = 0;
return busy_flag;
}
void wcmd(uchar del)
{
while(Busy());
RS = 0;
RW = 0;
E = 0;
delay_5us();
P0 = del;
delay_5us();
E = 1;
delay_5us();
E = 0;
}
void wdata(uchar del)
{
while(Busy());
RS = 1;
RW = 0;
E = 0;
delay_5us();
P0 = del;
delay_5us();
E = 1;
delay_5us();
E = 0;
}
void L1602_Init(void)
{
wcmd(0x38);
wcmd(0x0c);
wcmd(0x06);
wcmd(0x01);
}
void L1602_char(uchar hang,uchar lie,char sign)
{
uchar a;
if(hang == 1) a = 0x80;
if(hang == 2) a = 0xc0;
a = a + lie - 1;
wcmd(a);
wdata(sign);
}
void Time1_Init()
{
// TMOD=0x11;
IE=0x8B;
TH1=0x4C;// 50ms
TL1=0x00;
}
void timer0(void) interrupt 1 using 1 /*定时器0中断服务程序 */
{
TL0=stl0; /* 装入音频初值 */
TH0=sth0;
Sing=!Sing; /* 输出方波 */
}
void int1_serve(void) interrupt 0 using 3 /* 外部中断0服务程序 */
{
EA=0; /*关闭所有中断 */
key=1;
Sing=1;
n=0;
t=0;
}
void Time1_Int() interrupt 3
{
TH1=0x4C;// 1ms
TL1=0x00;
count++;
if(count==20)
{
yimiao++;
count=0;
L1602_char(2,1,table[yimiao/10]);
L1602_char(2,2,table[yimiao%10]);
}
}
uchar Keyscan(void)
{
uchar i,j, temp, Buffer[4] = {0xfe, 0xfd, 0xfb, 0xf7};
for(j=0; j<4; j++)
{
P1 = Buffer[j];
temp = 0x10;
for(i=0; i<4; i++)
{
if(!(P1 & temp))
{
return (i+j*4);
}
temp <<= 1;
}
}
return 0;
}
uchar Saomiao()
{
uchar eee=0,tizhi;
P1 = 0xf0;
if(P1 != 0xf0)
{
Delay_Ms(10); //按键消抖
if(P1 != 0xf0)
{
Key_Value = Keyscan();
count=0;
}
}
if(Key_Value==1)//播放 1
{
zz=1;
TR1=0;yimiao=0;
Key_Value=0;
L1602_char(2,14,table[0]);
L1602_char(2,15,table[zz]);
TR1=1;
song=song1;TR1=0;eee=0xff; ;
}
else if(Key_Value==2)//播放 1
{
zz=2;
TR1=0;yimiao=0;
Key_Value=0;
L1602_char(2,14,table[0]);
L1602_char(2,15,table[zz]);
TR1=1;
song=song2;TR1=0;eee=0xff; ;
}
else if(Key_Value==3)//播放 1
{
zz=3;
TR1=0;yimiao=0;
Key_Value=0;
L1602_char(2,14,table[0]);
L1602_char(2,15,table[zz]);
TR1=1;
song=song3;TR1=0;eee=0xff;
}
else if(Key_Value==4)//播放 1
{
zz=4;
TR1=0;
yimiao=0;
Key_Value=0;
L1602_char(2,14,table[0]);
L1602_char(2,15,table[zz]);
TR1=1;
song=song4; TR1=0;eee=0xff;
}
else if(Key_Value==5)//播放 1
{
zz=5;
TR1=0;
yimiao=0;
Key_Value=0;
L1602_char(2,14,table[0]);
L1602_char(2,15,table[zz]);
TR1=1;
song=song5;TR1=0;eee=0xff;
}else if(Key_Value==6)//播放 1
{
zz=6;
TR1=0;
yimiao=0;
Key_Value=0;
L1602_char(2,14,table[0]);
L1602_char(2,15,table[zz]);
TR1=1;
song=song6;TR1=0;eee=0xff;
}else if(Key_Value==7)//播放 1
{
zz=7;
TR1=0;
yimiao=0;
Key_Value=0;
L1602_char(2,14,table[0]);
L1602_char(2,15,table[zz]);
TR1=1;
song=song7;TR1=0;eee=0xff;
}
else if(Key_Value==8)//播放 1
{
zz=8;
TR1=0;
yimiao=0;
Key_Value=0;
L1602_char(2,14,table[0]);
L1602_char(2,15,table[zz]);
TR1=1;
song=song8;TR1=0;eee=0xff;
}
else if(Key_Value==9)//播放 1
{
zz=9;
TR1=0;
yimiao=0;
Key_Value=0;
L1602_char(2,14,table[0]);
L1602_char(2,15,table[zz]);
TR1=1;
song=song9;TR1=0;eee=0xff;
}
else if(Key_Value==10)//播放 1
{
zz=10;
TR1=0;
yimiao=0;
Key_Value=0;
L1602_char(1,14,table[zz/10]);
L1602_char(1,15,table[zz%10]);
TR1=1;
song=song10;TR1=0;eee=0xff;zz=10;
}
else if(Key_Value==11)//上一首
{
Delay_Ms(100);
if(Key_Value==11)
{
zz--;
TR1=0;
yimiao=0;
Key_Value=0;
Sing=0;
Delay_Ms(10);
eee=0xff;
L1602_char(1,14,table[zz/10]);
L1602_char(1,15,table[zz%10]);
if(zz==1)
{
song=song1;
}else if(zz==2)
{
song=song2;
}else if(zz==3)
{
song=song3;
}else if(zz==4)
{
song=song4;
}else if(zz==5)
{
song=song5;
}else if(zz==6)
{
song=song6;
}else if(zz==7)
{
song=song7;
}else if(zz==8)
{
song=song8;
}else if(zz==9)
{
song=song9;
}else if(zz==10)
{
song=song10;
}
}
}
else if(Key_Value==12)//下一首
{
Delay_Ms(100);
if(Key_Value==12)
{
zz++;
TR1=0;
yimiao=0;
Key_Value=0;
Sing=0;
Delay_Ms(10);
eee=0xff;
L1602_char(1,14,table[zz/10]);
L1602_char(1,15,table[zz%10]);
if(zz==1)
{
song=song1;
}else if(zz==2)
{
song=song2;
}else if(zz==3)
{
song=song3;
}else if(zz==4)
{
song=song4;
}else if(zz==5)
{
song=song5;
}else if(zz==6)
{
song=song6;
}else if(zz==7)
{
song=song7;
}else if(zz==8)
{
song=song8;
}else if(zz==9)
{
song=song9;
}else if(zz==10)
{
song=song10;
}
}
}
else if(Key_Value==13)//暂停
{
TR1=0;
Delay_Ms(100);
if(Key_Value==13)
{
Key_Value=0;
EA=0;
Sing=0;
TR1=0;
TR0=0;
tizhi=yimiao;
L1602_char(2,1,table[tizhi/10]);
L1602_char(2,2,table[tizhi%10]);
while(1)
{
P1 = 0xf0;
if(P1 != 0xf0)
{
Delay_Ms(100); //按键消抖
if(P1 != 0xf0)
{
Key_Value = Keyscan();
}
}
if(Key_Value==13)//暂停
{
Key_Value=0;
EA=1;
Sing=1;
TR1=1;
TR0=1;
break;
}
}
}
}
return eee;
}
main()
{
uint j,temp;;
TMOD=0x11;
L1602_Init();
// Delay_Ms(1);
Time1_Init();
L1602_char(1,1,' ');
L1602_char(1,5,'W');
L1602_char(1,6,'e');
L1602_char(1,7,'l');
L1602_char(1,8,'c');
L1602_char(1,9,'o');
L1602_char(1,10,'m');
L1602_char(1,11,'e');
Delay_Ms(1000);
L1602_Init();
// Saomiao(); /*定时器0工作在方式1 */
do
{
// IE=0x83;/*开外部中断0,定时器0,以及单片机总中断*/
// TL1=-50000%256;
// TH1=-50000/256;
flag=0; /* 控制节拍的标志数 */
key=0;
n=0;
t=0;
j=0;
//KeyScan(); /*扫描按键,扫描到便传送相应的歌曲数组到全局指针*/
// Saomiao();
// IE=0x83;
while(1)
{ Saomiao();
while(Key_Value!=0xff)
{
j=0;count=0;
while(*(song+j)!=0xff)
{// if(key)break;
Key_Value=0xff;
Saomiao();
if(temp!=song)
{
temp=song;
j=0;
}
// if(Key_Value!=0xff)
// {Key_Value=0xff;Sing=0;break ;}
k=*(song+j)&0x0f;
l=*(song+j)>>4;
sth0=tab[2*l];
stl0=tab[2*l+1];
TH0=sth0;
TL0=stl0;
TR0=1;
TR1=1;
do
{ if(Saomiao()==0xff)break ;
delay_song();
flag++;
}while(flag
//if(j%10==0)
j++;
}
}
}
}while(1);
}