资源描述
单片机c语言一闪一闪亮晶晶的乐谱程序
用使用定时器T0的中断控制播放音乐《一闪一闪亮晶晶》 1.
C音调与频率的对应关系表
音调 低1(“dao”) 低2 低3 低4 低5 低6 低7 频率 262 294 330 349 392 440 494
1(中音“dao”) 音调 2 3 4 5 6 7 频率 523 587 659 698 784 880 988 音调 高1 高2 高3 高4 高5 高6 高7 频率 1064 1175 1318 1397 1586 1760 1967 2.定时常数C计算公式
610us4608302f C= ,1.085usf
T0工作方式0.最大计数为8192,在已知定是常数为C的条件下,13位计算器的高8位和低5的初值可由以下公式设定
TH0=(8192-C)/32;
TL0=(8192-C)%32;
简单乐谱 3.
|1 1 5 5| 6 6 5 -| 4 4 3 3| 2 2 1 -| 5 5 4 4 | 3 3 2 -| 5 5 4 4 |3 3 2 -| 1 1 5 5 | 6 6 5 -| 4 4 3
3|2 2 1 -|
4..节拍控制
本例简谱的节拍为每分钟72拍,则每个节拍需时间
(1000*60ms)/72=833ms
? “1 “为1拍,需延时833ms
? “2”为1/2拍,需延时833/2ms
? “3“为1/4,需延时833/4ms
5.程序
/*----------------------------------------------
使用定时器T0的中断控制播放音乐《一闪一闪亮晶晶》,T0工作方式0,
图表列出了C音调与频率的对应关系。*
---------------------------------------------------- */
#include<reg52.h>
sbit sound=P3^7; //定义声音为引脚P3.7
unsigned int C; //存储定时器的定时常熟
//以下是C调中音的音频定义
#define dao 523 //讲“dao”宏定义为中音“1”的频率523HZ
#define re 587
#define mi 659
#define fa 698
#define sao 784
located in the Tomb, Dong Shen Jia bang, defer the next day focused on the assassination. Linping, Zhejiang, 1 of which liquor wine masters (Wuzhen said information is Carpenter), who got A few bayonets, due to missed fatal, when night came
#define la 880
#define xi 987
/*-------------------------------------------
延时子程序 200ms
-------------------------------------------*/
void delay()
{unsigned char i,j;
for(i=0;i<200;i++)
for(j=0;j<110;j++);
}
/*---------------------------------
主函数
----------------------------------*/ void main()
{ unsigned char i,j;
unsigned int code f[]={dao,dao,sao,sao,
la,la,sao,
fa,fa,mi,mi,
re,re,dao,
sao,sao,fa,fa,
mi,mi,re,
sao,sao,fa,fa,
mi,mi,re,
dao,dao,sao,sao,
la,la,sao,
fa,fa,mi,mi,
re,re,dao,
0xff}; //以0xff作为音调的结束标志
//一下是简谱中的每个音调的节拍,4对应4个延时单位,8对应8个延时单位
unsigned char code JP[]={4,4,4,4, //每行对应一小节音频的节拍
4,4,8,
4,4,4,4,
4,4,8,
4,4,4,4,
4,4,8,
4,4,4,4,
4,4,8,
4,4,4,4,
4,4,8 ,
4,4,4,4,
4,4,8,
};
EA=1; //开总中断
ET0=1; //定时器T0中断允许
located in the Tomb, Dong Shen Jia bang, defer the next day focused on the assassination. Linping, Zhejiang, 1 of which liquor wine masters (Wuzhen said information is Carpenter), who got A few bayonets, due to missed fatal, when night came
TMOD=0x00; //使用定时器T0的方式0(13位计数器)
while(1)
{ i=0;//从第一个音调f[0]开始播放
while(f[i]!=0xff) //只要没有读到结束标志就继续播放
{ C=460830/f[i];
TH0=(8192-C)/32;
TL0=(8192-C)%32;
TR0=1;
for(j=0;j<JP[i];j++)
delay();
TR0=0;
i++;
}
}
}
/*----------------------------------------
定时器T0的中断服务子程序,使P3.7引脚输出音频的方波
------------------------------------------------*/
void Time0(void) interrupt 1 using 1
{ sound=!sound;
TH0=(8192-C)/32;
TL0=(8192-C)%32;
}
located in the Tomb, Dong Shen Jia bang, defer the next day focused on the assassination. Linping, Zhejiang, 1 of which liquor wine masters (Wuzhen said information is Carpenter), who got A few bayonets, due to missed fatal, when night came
展开阅读全文