刚学完单片机的朋友可以看一看。
主要功能:
1.播放音乐
2.切歌
3.显示歌曲数
仿真:
代码:
#include<REG52.H>
//#define sound_amount 5
sbit play_up=P1^0;
sbit play_down=P1^1;
sbit pause=P1^2;
sbit speaker=P3^1;
unsigned char timer0h,timer0l,time,n;
unsigned char music_num;
unsigned int num;
signed int fre;
bit play_enable;
void delay(unsigned char t);
void delayms(unsigned int t);
void song(void);
void music_play(void);
unsigned code dm[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f};
unsigned char code FREQH[]={
0xF2, 0xF3, 0xF5, 0xF5, 0xF6, 0xF7, 0xF8,
0xF9, 0xF9, 0xFA, 0xFA, 0xFB, 0xFB, 0xFC, 0xFC,
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,
0xEE, 0x44, 0x6B, 0xB4, 0xF4, 0x2D,
0x47, 0x77, 0xA2, 0xB6, 0xDA, 0xFA, 0x16};
void KEY()
{
if(!pause)
{
delayms(5);
if(!pause)
{
TR0=0;
speaker=1;
if(music_num==0)
{
music_num=1;
num=0;
play_enable=1;
}
else
{
play_enable=~play_enable;
speaker=1;
}
while(!pause)
{
if(play_enable==0){}
}
}
}
if((!play_up)&&(music_num!=0))
{
delayms(5);
if((!play_up)&&(music_num!=0))
{
TR0=0;
speaker=1;
music_num-=1;
if(music_num<=0)
music_num=3;
num=0;
if(music_num==(sound_amount+1))
music_num=1;
delayms(500);
}
}
if((!play_down)&&(music_num!=0))
{
delayms(5);
if((!play_down)&&(music_num!=0))
{
TR0=0;
speaker=1;
music_num+=1;
if(music_num>=4)
music_num=1;
num=0;
delayms(500);
}
}
}
void main(void)
{
TMOD=0x11;
ET0=1;
EA=1;
while(1)
{
music_play();
KEY();
}
}
void delayms(unsigned int t)
{
unsigned int i,j;
for(i=0;i<t;i++)
{
for(j=0;j<123;j++)
;
}
}
void delay(unsigned char t)
{
unsigned char t1;
unsigned long t2;
for(t1=0;t1<t;t1++)
{
for(t2=0;t2<6500;t2++)
{
KEY();
}
}
TR0=0;
}
void timer0(void) interrupt 1
{
speaker=!speaker;
TH0=timer0h;
TL0=timer0l;
}文章来源:https://www.toymoban.com/news/detail-512417.html
void song(void)
{
TH0=timer0h;
TL0=timer0l;
TR0=1;
delay(time);
}
void music_play(void)
{
int i=0;
P2=0x00;
if(play_enable==1){
P2=dm[music_num];
}
else if(play_enable==0){
P2=dm[0];
delayms(5);
}
if((music_num==1)&&(play_enable==1))
{
fre=song1[num]+7*song1[num+1]-1;
timer0h=FREQH[fre];
timer0l=FREQL[fre];
time=song1[num+2];
num+=3;
if(fre<0)
{
num=0;
music_num=2;
}
song();
}
if((music_num==2)&&(play_enable==1))
{
fre=song2[num]+7*song2[num+1]-1;
timer0h=FREQH[fre];
timer0l=FREQL[fre];
time=song2[num+2];
num+=3;
if(fre<0)
{
num=0;
music_num=3;
}
song();
}
if((music_num==3)&&(play_enable==1))
{
fre=song3[num]+7*song3[num+1]-1;
timer0h=FREQH[fre];
timer0l=FREQL[fre];
time=song3[num+2];
num+=3;
if(fre<0)
{
num=0;
music_num=4;
}
song();
}
} 文章来源地址https://www.toymoban.com/news/detail-512417.html
到了这里,关于【无标题】单片机小项目———音乐播放器(keil+protues)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!