#include<reg51.h>//51单片机头文件
typedef unsigned char uchar;
typedef unsigned int uint;
sbit k1=P2^0;//第一首
sbit k2=P2^1;//第二首
sbit k3=P2^2;//第三首
sbit BEEP=P1^4;//蜂鸣器接口
char count2;//歌曲标志
uchar a,b,c,aa;
//---------------------------简谱---------------------------------------
//编程规则:字节高位是简谱,低位是持续时间,
//代表多少个十六分音符
//1-7代表中央C调,8-E代表高八度,0代表停顿
//最后的0是结束标志
uchar code table1[]={
0x12,0x22,0x34,0x84,0x74,0x54,0x38,0x42,0x32,0x22,0x42,0x34,0x84,0x72,0x82,0x94,0xA8,0x08,
0x32,0x31,0x21,0x32,0x52,0x32,0x31,0x21,0x32,0x62,
0x32,0x31,0x21,0x32,0x82,0x71,0x81,0x71,0x51,0x32,0x22,
0x32,0x31,0x21,0x32,0x52,0x32,0x31,0x21,0x32,0x62,
0x32,0x31,0x21,0x32,0x83,0x82,0x71,0x72,0x02,
0x63,0xA1,0xA2,0x62,0x92,0x82,0x52,
0x31,0x51,0x63,0x51,0x63,0x51,0x63,0x51,0x62,0x82,0x7C,0x02,
0x61,0x71,0x82,0x71,0x62,0xA2,0x71,0x76,
0x61,0x71,0x82,0x71,0x62,0x52,0x31,0x36,
0x61,0x71,0x82,0x71,0x62,0xA3,0x73,0x62,0x53,
0x42,0x63,0x83,0x83,0x91,0x91,
0x61,0x71,0x82,0x71,0x62,0x0A2,0x71,0x76,
0x61,0x71,0x82,0x71,0x62,0x52,0x31,0x36,
0x61,0x71,0x82,0x71,0x62,0xA3,0x73,0x62,0x53,
0x42,0x82,0x88,0x02,0x74,0x93,0x89,0xff };//千年之恋
uchar code table2[]={
0x12,0x12,0x22,0x32,0x31,0x22,0x21,0x22,
0x21,0x31,0x51,0x52,0x31,0x52,0x61,0x15,0x14,
0x51,0x52,0x31,0x52,0x62,0x13,0x11,0x13,0x32,0x28,0x08,0x28,
0x31,0x32,0x31,0x32,0x11,0x21,0x51,0x52,0x51,0x52,
0x51,0x51,0x31,0x32,0x31,0x32,0x81,0x72,0x63,
0x62,0x71,0x81,0x72,0x61,0x61,0x52,0x31,0x21,0x32,0x51,0x54,
0x22,0x12,0x11,0x12,0x11,0x12,0x12,0x14,0x26,0x32,0x26,
0x32,0x61,0x51,0x51,0x31,0x31,0x21,0x31,0x51,0x61,0x51,0x31,0x51,
0x02,0x32,0x81,0x81,0x81,0x81,0x62,0x52,0x34,
0x31,0x81,0x81,0x81,0x61,0x91,0x82,
0x51,0x51,0x51,0x51,0x31,0x61,0x53,
0x21,0x11,0x21,0x11,0x22,0x11,0x21,0x26,
0x32,0x61,0x51,0x51,0x31,0x31,0x21,0x31,0x51,0x61,0x51,0x31,0x51,0x52,
0x31,0x31,0x81,0x81,0x81,0x61,0x91,0x81,0x61,0x31,0x56,
0x32,0x32,0x81,0x81,0x81,0x81,0x91,0x81,0x61,0x81,0x61,0x51,0x31,0x51,0x34,
0x21,0x31,0x51,0x31,0x21,0x11,0x61,0x21,0x16,
0xff
}; //寂寞沙州
uchar code table3[]={
0xFD,0x82,0x68, 0xFD,0xC8,0x68, 0xFD,0x82,0x68, 0xFE,0x22,0x68, 0xFE,0x06,0x53,
0xFD,0x82,0x68, 0xFD,0xC8,0x68, 0xFd,0x82,0x68, 0xFe,0x56,0x68, 0xFE,0x22,0x53,
0xFd,0x82,0x68, 0xFe,0xC1,0x68, 0xFe,0x85,0x68, 0xFE,0x22,0x68, 0xFE,0x06,0x68, 0xFD,0xC8,0x68,
0xFE,0x9A,0x68, 0xFE,0x85,0x68, 0xFE,0x22,0x68, 0xFE,0x56,0x68, 0xFe,0x22,0x53,
0x00
};
//简谱音调对应的定时器初值
//适合12的晶振
uchar code cuzhi[]={
0xff,0xff,//占位
0xFC,0x8E,//中央C调1-7
0xFC,0xED,
0xFD,0x43,
0xFD,0x6A,
0xFD,0xB3,
0xFD,0xF3,
0xFE,0x2D,
0xFE,0x47, //高八度1-7
0xFE,0x76,
0xFE,0xA1,
0xFE,0xC7,
0xFE,0xD9,
0xFE,0xF9,
0xFF,0x16
};
uchar code yinyue[]={0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0x00,0x00};
//将音调转化为对应的LED样式
//延时165MS,即十六分音符子函数
void delay1(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=19000;y>0;y--);
}
//延时1MS子函数
void delay2(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
//按键扫描子函数
void key()
{
if(k1==0)
{
delay2(10);
if(k1==0)
{
while(k1==0);
count2=1;
c=0;//从头开始唱
TR0=1;//定时器T0开始计数
}
}
if(k2==0)
{
delay2(10);
if(k2==0)
{
while(k2==0);
count2=2;
c=0;//从头开始唱
TR0=1;//定时器T0开始计数
}
}
if(k3==0)
{
delay2(10);
if(k3==0)
{
while(k3==0);
count2=3;
c=0;//从头开始唱
TR0=1;//定时器T0开始计数
}
}
}
//播放音乐子函数
void song()
{
uint temp;
uchar aa;//aa是简谱
c=0;
while(1)
{
if(count2==0)
{
break;
}
if(count2==1) //选曲
temp=table1[c];
if(count2==2)
temp=table2[c];
if(count2==3)
temp=table3[~c];
if(temp==255)
break;
aa=temp/16; //取数的高4位
if(aa!=0)
{
a=cuzhi[aa*2];
b=cuzhi[aa*2+1];
}
else
{
TR0=0;
BEEP=0;//关蜂鸣器
}
delay1(temp%16); //取数的低4位
TR0=0; //唱完一个音停10MS
BEEP=0;
delay2(10);
TR0=1;
c++;
}
TR0=0; //停止定时器T0计数
BEEP=0;
}
//主函数
void main()
{
TMOD=0x11;//定时器0工作在方式1
TH0=0; //定时器0的高八位
TL0=0; //定时器0的低八位
TH1=0; //定时器1的高八位
TL1=0; //定时器1的低八位
ET0=1;//开定时器0中断,允许T0溢出中断
ET1=1; //允许T1溢出中断
TR1=1;//使定时器1开始中断
EA=1;//开总中断
count2=0;
BEEP=0;
while(1)//大循环
{
song();
}
}
//定时器0中断子函数
void t0() interrupt 1 //用于产生各种音调
{
TH0=a;
TL0=b;
BEEP=~BEEP;
}
//定时器1中断子函数
void t1() interrupt 3 //用于产生各种音调
{
key();
}
没有合适的资源?快使用搜索试试~ 我知道了~
41、数字音乐盒---卖.zip
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共18个文件
bak:5个
uvproj:1个
uvopt:1个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
需积分: 0 0 下载量 77 浏览量
2023-01-09
11:09:26
上传
评论
收藏 67KB ZIP 举报
温馨提示
41、数字音乐盒---卖.zip
资源推荐
资源详情
资源评论
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 18 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
m0_70532302
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)