/
[文件名] C51音乐程序(八月桂花)
[功能] 通过单片机演奏音乐
注意:通过了74HC14控制ULN2003 驱动芯片驱动蜂鸣器
//
#include <REG52H>
#include <INTRINSH>
//本例采用89C52, 晶振为110592MHZ
//关于如何编制音乐代码, 其实十分简单,各位可以看以下代码
//频率常数即音乐术语中的音调,而节拍常数即音乐术语中的多少拍;
//所以拿出谱子, 试探编吧!
sbit Beep = P1^5 ;
unsigned char n=0; //n为节拍常数变量
unsigned char code music_tab[] ={
0x18, 0x30, 0x1C , 0x10, //格式为: 频率常数, 节拍常数, 频率常数, 节拍常数,
0x20, 0x40, 0x1C , 0x10,
0x18, 0x10, 0x20 , 0x10,
0x1C, 0x10, 0x18 , 0x40,
0x1C, 0x20, 0x20 , 0x20,
0x1C, 0x20, 0x18 , 0x20,
0x20, 0x80, 0xFF , 0x20,
0x30, 0x1C, 0x10 , 0x18,
0x20, 0x15, 0x20 , 0x1C,
0x20, 0x20, 0x20 , 0x26,
0x40, 0x20, 0x20 , 0x2B,
0x20, 0x26, 0x20 , 0x20,
0x20, 0x30, 0x80 , 0xFF,
0x20, 0x20, 0x1C , 0x10,
0x18, 0x10, 0x20 , 0x20,
0x26, 0x20, 0x2B , 0x20,
0x30, 0x20, 0x2B , 0x40,
0x20, 0x20, 0x1C , 0x10,
0x18, 0x10, 0x20 , 0x20,
0x26, 0x20, 0x2B , 0x20,
0x30, 0x20, 0x2B , 0x40,
0x20, 0x30, 0x1C , 0x10,
0x18, 0x20, 0x15 , 0x20,
0x1C, 0x20, 0x20 , 0x20,
0x26, 0x40, 0x20 , 0x20,
0x2B, 0x20, 0x26 , 0x20,
0x20, 0x20, 0x30 , 0x80,
0x20, 0x30, 0x1C , 0x10,
0x20, 0x10, 0x1C , 0x10,
0x20, 0x20, 0x26 , 0x20,
0x2B, 0x20, 0x30 , 0x20,
0x2B, 0x40, 0x20 , 0x15,
0x1F, 0x05, 0x20 , 0x10,
0x1C, 0x10, 0x20 , 0x20,
0x26, 0x20, 0x2B , 0x20,
0x30, 0x20, 0x2B , 0x40,
0x20, 0x30, 0x1C , 0x10,
0x18, 0x20, 0x15 , 0x20,
0x1C, 0x20, 0x20 , 0x20,
0x26, 0x40, 0x20 , 0x20,
0x2B, 0x20, 0x26 , 0x20,
0x20, 0x20, 0x30 , 0x30,
0x20, 0x30, 0x1C , 0x10,
0x18, 0x40, 0x1C , 0x20,
0x20, 0x20, 0x26 , 0x40,
0x13, 0x60, 0x18 , 0x20,
0x15, 0x40, 0x13 , 0x40,
0x18, 0x80, 0x00
};
void int0() interrupt 1 //采用中断0 控制节拍
{ TH0=0xd8;
TL0=0xef;
n--;
}
void delay (unsigned char m) //控制频率延时
{
unsigned i=3m;
while(--i);
}
void delayms(unsigned char a) //豪秒延时子程序
{
while(--a); //采用while(--a) 不要采用while(a--); 各位可编译一下看看汇编结果就知道了!
}
void main()
{ unsigned char p,m; //m为频率常数变量
unsigned char i=0;
TMOD&=0x0f;
TMOD|=0x01;
TH0=0xd8;TL0=0xef;
IE=0x82;
play:
while(1)
{
a: p=music_tab[i];
if(p==0x00) { i=0, delayms(1000); goto play;} //如果碰到结束符,延时1秒,回到开始再来一遍
else if(p==0xff) { i=i+1;delayms(100),TR0=0; goto a;} //若碰到休止符,延时100ms,继续取下一音符
else {m=music_tab[i++], n=music_tab[i++];} //取频率常数 和 节拍常数
TR0=1; //开定时器1
while(n!=0) Beep=~Beep,delay(m); //等待节拍完成, 通过P1口输出音频(可多声道哦!)
TR0=0; //关定时器1
}
}
#include <reg51h> //包括一个51标准内核的头文件
#define uchar unsigned char //定义一下方便使用
#define uint unsigned int
sbit BEEP=P3^6; //喇叭输出脚
uchar th0_f; //在中断中装载的T0的值高8位
uchar tl0_f; //在中断中装载的T0的值低8位
/ 延时 1ms /
void delay_1ms(unsigned int t)
{
unsigned char i,j;
for(t;t>0;t--)
{
for(i=34;i>0;i--)
for(j=13;j>0;j--);
}
}
//T0的值,及输出频率对照表
static code freq[36][2] = {
0xA9,0xEF, //00220HZ ,1 //0
0x93,0xF0, //00233HZ ,1# //1
0x73,0xF1, //00247HZ ,2 //2
0x49,0xF2, //00262HZ ,2# //3
0x07,0xF3, //00277HZ ,3 //4
0xC8,0xF3, //00294HZ ,4 //5
0x73,0xF4, //00311HZ ,4# //6
0x1E,0xF5, //00330HZ ,5 //7
0xB6,0xF5, //00349HZ ,5# //8
0x4C,0xF6, //00370HZ ,6 //9
0xD7,0xF6, //00392HZ ,6# //10
0x5A,0xF7, //00415HZ ,7 //11
0xD8,0xF7, //00440HZ 1 //12
0x4D,0xF8, //00466HZ 1# //13
0xBD,0xF8, //00494HZ 2 //14
0x24,0xF9, //00523HZ 2# //15
0x87,0xF9, //00554HZ 3 //16
0xE4,0xF9, //00587HZ 4 //17
0x3D,0xFA, //00622HZ 4# //18
0x90,0xFA, //00659HZ 5 //19
0xDE,0xFA, //00698HZ 5# //20
0x29,0xFB, //00740HZ 6 //21
0x6F,0xFB, //00784HZ 6# //22
0xB1,0xFB, //00831HZ 7 //23
0xEF,0xFB, //00880HZ `1 //24
0x2A,0xFC, //00932HZ `1# //25
0x62,0xFC, //00988HZ `2 //26
0x95,0xFC, //01046HZ `2# //27
0xC7,0xFC, //01109HZ `3 //28
0xF6,0xFC, //01175HZ `4 //29
0x22,0xFD, //01244HZ `4# //30
0x4B,0xFD, //01318HZ `5 //31
0x73,0xFD, //01397HZ `5# //32
0x98,0xFD, //01480HZ `6 //33
0xBB,0xFD, //01568HZ `6# //34
0xDC,0xFD, //01661HZ `7 //35
};
static code music1[464] = { // 《隐形的翅膀》
36,36,36,36,36,36,36,36,36,36,36,36,8,8,12,12, //每一
16,16,16,16,16,16,19,19,16,16,16,16,14,14,12,12, //次都在徘徊
12,12,12,12,12,12,9,9,8,8,8,36,9,9,12,12, //孤单中坚强 每一
16,16,16,16,16,16,19,19,19,19,19,19,21,21,19,19, //次就算很受伤
19,19,16,16,14,14,12,12,14,14,14,36,21,21,19,19, //也不闪泪光 我知
16,16,16,16,16,16,19,19,19,19,19,19,21,21,19,19, //道我一直有双
16,16,14,14,12,12,14,14,9,9,9,36,8,8,9,9, //隐形的翅膀 带我
12,12,12,12,12,12,16,16,14,14,14,14,16,14,14,14, //飞 飞过绝
12,12,12,12,12,12,12,12,12,12,12,36,8,8,12,12, //望 不去
16,16,16,16,16,16,19,19,16,16,16,16,14,14,12,12, //想他们拥有
12,12,12,12,12,12,9,9,8,8,8,36,8,8,12,12, //美丽的太阳 我看
16,16,16,16,16,16,19,19,19,19,19,19,21,21,19,19, //见每天的夕阳
19,19,16,16,14,14,12,12,14,14,14,36,21,21,19,19, //也会有变化 我知
16,16,16,16,16,16,19,19,19,19,19,19,21,21,19,19, //道我一直有双
16,16,14,14,12,12,14,14,9,9,9,36,8,8,9,9, //隐形的翅膀 带我
12,12,12,12,12,12,14,16,14,14,14,14,16,16,12,12, //飞 给我希
12,12,12,12,12,12,12,12,12,12,12,36,16,16,19,19, //望 我终
24,24,24,24,24,36,24,24,24,24,24,24,21,21,19,19, //于看到所有
21,21,24,24,16,16,14,14,12,12,12,36,12,12,12,12, //梦想都开花 追逐
12,12,24,24,24,24,21,21,19,19,16,16,14,14,12,12, //的年轻歌声多嘹
14,14,14,14,14,14,14,14,14,14,14,36,16,16,19,19, //亮 我终
24,24,24,24,24,36,24,24,24,24,24,24,21,21,19,19, //于翱翔用心
21,21,24,24,16,16,14,14,12,12,12,36,12,12,12,12, //凝望不害怕 哪里
12,12,24,24,24,24,21,21,19,19,16,16,14,14,12,12, //会有风就飞多远
12,12,12,12,12,12,12,12,12,12,36,36,16,16,19,19, //吧 隐形
24,24,24,24,24,36,24,24,23,23,23,23,21,21,19,19, //的翅膀让梦
21,21,24,24,16,16,14,14,12,12,12,36,12,12,12,12, //恒久比天长 留一
12,12,24,24,24,24,21,21,19,19,16,16,14,14,14,14, //个愿望让自己
14,14,14,14,12,12,12,12,12,12,12,12,12,12,12,36, //想象
};
static code music2[336] = { // 《北京欢迎你》
16,16,19,19,16,16,14,14,16,16,14,14,16,16,16,36, //迎接另一个晨曦
16,16,14,14,9,9,12,12,16,16,14,14,14,14,14,36, //带来全新空气
14,14,12,12,9,9,12,12,14,14,16,16,19,19,14,14, //气息改变情味不变
16,16,21,21,19,19,9,9,14,14,12,12,12,12,12,36, //茶香飘满情谊
16,16,19,19,16,16,14,14,16,16,14,14,16,16,16,36, //我家大门常打开
16,16,14,14,9,9,12,12,16,16,14,14,14,14,14,36, //开放怀抱等你
14,14,12,12,9,9,12,12,14,14,16,16,19,19,14,14, //拥抱过就有了默契
16,16,21,21,19,19,9,9,14,14,12,12,12,12,12,36, //你会爱上这里
14,14,12,12,9,9,12,12,14,14,16,16,19,19,14,14, //不管远近都是客人
16,16,21,21,19,19,19,19,16,16,16,16,16,16,16,36, //请不用客气
14,14,16,16,14,14,12,12,19,19,21,21,14,14,14,36, //相约好了在一起
9,9,16,16,14,14,14,14,12,12,12,12,12,12,12,36, //我们欢迎你
36,36,36,36,36,36,36,36,16,16,16,16,19,19,19,19, //北京
24,24,19,19,21,21,21,21,36,36,21,21,19,19,16,16, //欢迎你 为你开
16,16,19,19,19,19,19,19,19,19,19,36,16,16,19,19, //天辟地 流动
21,21,24,24,26,26,24,24,19,19,16,16,14,14,19,19, //中的魅力充满着朝
16,16,16,16,16,16,16,16,16,16,16,36,16,16,19,19, //气 北京
24,24,19,19,21,21,21,21,36,36,24,24,26,26,24,24, //欢迎你 在太阳
19,19,16,16,19,19,23,23,21,21,21,21,36,36,16,16, //下分享呼吸 在
14,14,16,16,19,19,28,28,26,26,26,26,26,26,23,23, //黄土地刷新成
24,24,24,24,24,24,24,24,24,24,24,24,24,24,36,36, //绩
};
static code music3[444] = { // 《仙剑奇侠传》
28,28,26,26,28,28,28,28,28,28,21,21,
26,26,26,26,24,24,23,23,21,21,21,23,21,21,19,19,
28,28,28,28,16,16,19,19,24,24,24,24,16,16,19,19,
26,26,26,26,24,24,23,23,21,21,21,21,21,21,21,21,
21,21,21,21,28,28,26,26,36,36,36,36,28,28,26,26,
21,21,21,21,24,24,26,26,28,28,28,28,28,28,28,28,
28,28,31,31,28,28,24,24,26,26,26,26,26,26,26,26,
21,21,21,21,24,24,28,28,26,26,26,26,24,24,24,24,
21,21,21,21,21,21,21,21,21,21,21,21,36,36,36,36,
28,28,26,26,28,28,28,28,28,28,21,21,
26,26,26,26,24,24,23,23,21,21,21,23,21,21,19,19,
28,28,28,28,16,16,19,19,24,24,24,24,16,16,19,19,
26,26,26,26,28,24,23,23,21,21,21,21,21,21,21,21,
21,21,21,21,28,28,26,26,36,36,36,36,28,28,26,26,
28,28,28,28,28,28,21,21,
26,26,26,26,24,24,23,23,21,21,21,23,21,21,19,19,
16,16,16,16,16,16,19,19,24,24,24,24,16,16,19,19,
26,26,26,26,28,24,23,23,21,21,21,21,21,21,21,21,
21,21,21,21,23,23,23,23,23,23,23,23,23,23,23,23,
21,21,21,21,24,24,26,26,28,28,28,28,28,28,28,28,
31,33,31,31,28,28,24,24,26,26,26,26,26,26,26,26,
21,21,21,21,24,24,28,28,26,26,26,26,24,24,24,24,
21,21,21,21,21,21,21,21,21,21,21,21,36,36,36,36,
28,28,26,26,28,28,28,28,28,28,21,21,
26,26,26,26,24,24,23,23,21,21,21,23,21,21,19,19,
28,28,28,28,16,16,19,19,24,24,24,24,16,16,19,19,
26,26,26,26,28,24,23,23,21,21,21,21,21,21,21,21,
23,23,23,23,23,23,23,23,23,23,23,23,21,21,19,19,
21,21,21,21,21,21,21,21,36,36,36,36,36,36,36,36,
};
static code music4[448] = { // 《笑傲江湖》
16,16,16,16,21,21,21,16,19,19,19,19,19,19,16,16,
14,14,16,16,19,19,21,21,16,16,16,16,16,16,16,36,
16,16,16,16,21,21,21,16,19,19,19,19,19,19,16,16,
14,14,16,16,19,19,21,19,16,16,16,16,36,16,16,14,
12,12,12,12,14,14,16,16,14,14,14,14,14,14,14,12,
9,9,9,12,14,14,16,16,19,19,19,21,19,19,16,14,
12,12,12,14,14,14,16,16,14,14,14,14,14,12,12,12,
9,9,9,12,14,16,16,19,21,21,21,21,21,21,21,36,
16,16,16,16,21,21,21,16,19,19,19,19,19,19,16,16,
14,14,16,16,19,19,21,19,16,16,16,16,16,16,16,36,
16,16,16,16,21,21,21,16,19,19,19,19,19,19,16,16,
14,14,14,16,19,19,21,19,16,16,16,16,36,16,16,14,
12,12,12,12,14,14,16,16,14,14,14,14,14,14,14,12,
9,9,9,12,14,14,16,16,19,19,19,21,19,19,16,14,
12,12,12,14,14,14,16,16,14,14,14,14,14,12,12,12,
9,9,9,12,14,16,16,19,21,21,21,21,21,21,21,36,
14,14,14,14,14,16,16,14,12,12,12,12,12,9,9,12,
14,14,14,14,14,16,16,14,12,12,9,9,36,12,9,12,
14,14,14,14,14,16,16,14,12,12,12,12,36,12,9,12,
14,14,14,14,14,19,19,16,16,16,16,16,16,16,16,36,
9,9,9,9,36,9,9,9,14,14,14,14,14,14,16,14,
12,12,12,12,36,12,12,14,9,9,9,9,9,9,7,7,
9,9,9,9,36,9,7,7,4,4,4,4,36,4,7,7,
2,2,2,2,36,2,2,2,4,4,4,4,4,4,4,36,
9,9,9,9,9,9,9,9,14,14,14,14,14,14,16,16,
12,12,12,12,12,12,14,14,9,9,9,9,12,9,
7,7,7,7,7,7,9,9,4,4,4,2,4,4,7,7,
9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
};
//定时中断0,用于产生唱歌频率
timer0() interrupt 1
{
TL0=tl0_f;TH0=th0_f; //调入预定时值
BEEP=~BEEP; //取反音乐输出IO
P2=~P2;
}
//音阶声音自动输出试验
void main(void) // 主程序
{
uint i;
TMOD = 0x01; //使用定时器0的16位工作模式
TR0 = 1;
ET0 = 1;
EA = 1;
while(1)
{
for(i=0;i<336;i++) //播放音乐
{
if(music2[i] == 36) //空音
{
TR0 = 0;
delay_1ms(190);
TR0 = 1;
}
else
{
tl0_f=freq[music2[i]][0]; //置一个音符的值
th0_f=freq[music2[i]][1];
delay_1ms(190);
}
}
TR0 = 0;
delay_1ms(3000);
TR0 = 1;
}
}
哈哈,网上找的,什么歌都可以,你去百度一下《青花瓷》的简谱就好了
#include <reg52h>
sbit speaker = P1^0;
unsigned char timer0h, timer0l, time;
//--------------------------------------
//单片机晶振采用110592MHz
// 频率-半周期数据表 高八位 本软件共保存了四个八度的28个频率数据
code unsigned char FREQH[] = {
0xF2, 0xF3, 0xF5, 0xF5, 0xF6, 0xF7, 0xF8, //低音1234567
0xF9, 0xF9, 0xFA, 0xFA, 0xFB, 0xFB, 0xFC, 0xFC,//1,2,3,4,5,6,7,i
0xFC, 0xFD, 0xFD, 0xFD, 0xFD, 0xFE, //高音 234567
0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF}; //超高音 1234567
// 频率-半周期数据表 低八位
code unsigned char FREQL[] = {
0x42, 0xC1, 0x17, 0xB6, 0xD0, 0xD1, 0xB6, //低音1234567
0x21, 0xE1, 0x8C, 0xD8, 0x68, 0xE9, 0x5B, 0x8F, //1,2,3,4,5,6,7,i
0xEE, 0x44, 0x6B, 0xB4, 0xF4, 0x2D, //高音 234567
0x47, 0x77, 0xA2, 0xB6, 0xDA, 0xFA, 0x16}; //超高音 1234567
//--------------------------------------
/code unsigned char sszymmh[] = {
3,2,3,5,2,1,6,2,2,5,2,1,6,2,
1,1,3,4,6,2,3,1,3,1,3,2,2,2,2,1,3,
2,1,5,2,4,5,2,3,3,2,1,6,2,2,3,2,1,2,
2,1,1,2,4,5,2,3,3,2,1,2,2,3,6,1,1,1,
2,4,1,2,4,1,3,3,6,2,1,5,2,2,6,2,1,6,
2,1,5,2,1,5,2,4,5,2,3,6,2,1,1,2,1,2,
2,1,5,2,1,3,2,4,2,2,3,3,2,1,5,2,2,1,
3,2,6,2,1,5,2,1,3,2,1,2,2,1,1,2,4,6,
2,3,1,3,1,5,2,1,3,2,1,2,2,2,6,1,1,1,
2,1,1,2,4,6,2,3,1,3,1,5,2,1,3,2,1,2,
2,2,1,2,4,6,2,3,1,3,1,5,2,1,3,2,1,2,
2,2,6,1,1,1,2,1,1,2,4,6,2,3,1,3,1,5,
2,1,3,2,1,2,2,2,6,1,1,1,2,1,1,2,4,
0, 0, 0};/
//code unsigned char sszymmh[] = {1,2,3,2,2,1,6,1,1,1,2,1,5,1,2,5,2,3,1,3,1,6,2,1,5,2,1,3,2,1,5,2,1,2,2,4,2,2,3,3,2,1,7,1,2,6,1,2,5,1,3,6,1,1,1,2,2,2,2,2,3,1,2,1,2,2,6,1,1,5,1,1,6,1,1,1,2,1,5,1,4,3,2,3,5,2,1,7,1,2,2,2,2,6,1,1,1,2,1,5,1,4,3,1,1,5,1,2,3,1,2,5,1,1,6,1,1,7,1,1,2,2,1,6,1,4,5,1,1,6,1,1,1,2,3,2,2,1,5,2,2,3,2,2,2,2,2,3,2,1,2,2,1,1,2,2,6,1,1,5,1,1,3,1,2,3,1,4,1,2,4,6,1,3,1,2,1,6,1,1,5,1,1,3,1,1,5,1,1,6,1,1,1,2,1,5,1,4,
//0, 0, 0};//梁祝
code unsigned char sszymmh[] = {5,1,1, 1,2,3, 1,2,1, 1,2,1, 1,2,1, 5,1,1, 6,1,1, 7,1,1, 1,2,2, 1,2,2,
3,2,1, 1,2,1, 2,2,1, 3,2,1, 5,2,2, 5,2,1, 5,2,2,
3,2,2, 3,2,1, 1,2,2, 3,2,1, 5,2,2, 3,2,1, 2,2,2, 2,2,4,
6,2,2,5,2,2,2,2,2,3,2,2,5,2,1,3,2,2,5,2,1,3,2,1,2,2,1,3,2,1,1,2,1,2,2,1,3,2,2,
5,2,2,6,1,1,1,2,1,1,2,1,3,2,2,3,2,1,5,2,2,5,2,1,2,2,1,2,2,1,2,2,1,6,1,1,6,1,1,2,2,4,
5,1,1,1,2,3,1,2,1,3,2,3,3,2,1,5,2,4,5,2,4,
1,2,2,3,2,1,5,2,1,5,1,1,6,2,2,5,2,2,
3,2,2,1,2,1,5,2,1,5,2,1,5,2,1,3,2,1,1,2,1,5,1,2,1,2,2,
3,1,1,1,1,1,5,2,1,5,2,1,5,2,1,3,2,1,1,2,1,5,1,2,1,2,2,5,1,2,1,2,2,5,1,2,1,2,2,1,2,2,
0, 0, 0};//国歌
/code unsigned char sszymmh[] = {6,2,3,5,2,1,3,2,2,5,2,2,6,2,1,6,2,2,5,2,1,3,2,4,6,1,3,1,2,1,2,2,2,5,2,1,3,2,1,2,2,4,
6,2,3,2,3,1,2,3,2,1,3,1,7,2,1,6,2,3,5,2,1,3,2,4,2,2,1,5,2,1,5,2,1,3,2,1,2,2,1,3,2,1,1,2,1,7,1,1,6,1,4,
1,2,3,6,1,1,6,2,1,1,2,2,2,2,2,5,
2,1,3,2,1,2,2,4,5,2,3,3,2,1,3,2,
2,5,2,2,6,2,4,2,3,3,2,3,1,1,3,2,
2,3,2,6,2,1,1,3,1,5,2,1,6,2,1,3,
2,4,2,2,1,5,2,1,5,2,1,3,2,1,2,2,
1,3,2,1,1,2,1,7,1,1,6,1,4,
0, 0, 0};//虞美人 /
//结束标记
//世上只有妈妈好数据表 要想演奏不同的乐曲, 只需要修改这个数据表
//code unsigned char sszymmh[] = {
//6, 2, 3, 5, 2, 1, 3, 2, 2, 5, 2, 2, 1, 3, 2, 6, 2, 1, 5, 2, 1,
//一个音符有三个数字。前为第几个音、中为第几个八度、后为时长(以半拍为单位)。
//6, 2, 3代表:6, 中音, 3个半拍;
//5, 2, 1代表:5, 中音, 1个半拍;
//3, 2, 2代表:3, 中音, 2个半拍;
//5, 2, 2代表:5, 中音, 2个半拍;
//1, 3, 2代表:1, 高音, 2个半拍;
//
//6, 2, 4, 3, 2, 2, 5, 2, 1, 6, 2, 1, 5, 2, 2, 3, 2, 2, 1, 2, 1,
//6, 1, 1, 5, 2, 1, 3, 2, 1, 2, 2, 4, 2, 2, 3, 3, 2, 1, 5, 2, 2,
//5, 2, 1, 6, 2, 1, 3, 2, 2, 2, 2, 2, 1, 2, 4, 5, 2, 3, 3, 2, 1,
//2, 2, 1, 1, 2, 1, 6, 1, 1, 1, 2, 1, 5, 1, 6, 0, 0, 0,,,,,,,,,,,,,,,,,,,,,6,2,3, 6,2,1, 1,3,2, 7,2,1, 6,2,1, 5,2,2, 3,2,1, 5,2,1, 6,2,4, 3,2,3,};
//--------------------------------------
void t0int() interrupt 1 //T0中断程序,控制发音的音调
{
TR0 = 0; //先关闭T0
speaker = !speaker; //输出方波, 发音
TH0 = timer0h; //下次的中断时间, 这个时间, 控制音调高低
TL0 = timer0l;
TR0 = 1; //启动T0
}
//--------------------------------------
void delay(unsigned char t) //延时程序,控制发音的时间长度
{
unsigned char t1;
unsigned long t2;
for(t1 = 0; t1 < t; t1++) //双重循环, 共延时t个半拍
for(t2 = 0; t2 < 8000; t2++); //延时期间, 可进入T0中断去发音
TR0 = 0; //关闭T0, 停止发音
}
//--------------------------------------
void song() //演奏一个音符
{
TH0 = timer0h; //控制音调
TL0 = timer0l;
TR0 = 1; //启动T0, 由T0输出方波去发音
delay(time); //控制时间长度
}
//--------------------------------------
void main(void)
{
unsigned char k, i;
TMOD = 1; //置T0定时工作方式1
ET0 = 1; //开T0中断
EA = 1; //开CPU中断
while(1) {
i = 0;
time = 1;
while(time) {
k = sszymmh[i] + 7 sszymmh[i + 1] - 1;
//第i个是音符, 第i+1个是第几个八度
timer0h = FREQH[k]; //从数据表中读出频率数值
timer0l = FREQL[k]; //实际上, 是定时的时间长度
time = sszymmh[i + 2]; //读出时间长度数值
i += 3;
song(); //发出一个音符
}
}
}
/======================================
应网友要求,下面再详细写一下乐谱和数据的转换关系。
以李叔同大师的《送别》的前二小节来说明转换的方法。
这部分的歌词是:"长 亭 外, 古 道 边,";
这部分的乐谱是:| 5 35 1 - | 6 16 5 - |。
(注意:乐谱中的1是高音,上边是带点的;还有些音符,应该有下划线,在这里都无法标出。感兴趣的网友应该去查看正规的乐谱。)
那么,据此就可以写出《送别》前二小节的数据表:
//--------------------------------------/
//code unsigned char sszymmh[] = {
//5, 2, 2, 3, 2, 1, 5, 2, 1, 1, 3, 4,
//嗦,中音,2个半拍; 咪,中音,1个半拍; 嗦,中音,1个半拍; 哆,高音,4个半拍
//6, 2, 2, 1, 3, 1, 6, 2, 1, 5, 2, 4,
//啦,中音,2个半拍; 哆,高音,1个半拍; 啦,中音,1个半拍; 嗦,中音,4个半拍
//0, 0, 0};
//结束标记
/--------------------------------------
记住:三个数字一组,代表一个音符。
第一个数字是1234567之一,代表音符哆来咪发;
第二个数字是0123之一,代表低音、中音、高音、超高音;
第三个数字是半拍的个数,代表时间长度。
当三个数字都是0,就代表乐曲数据表的结尾。
用这个数据表,替换掉程序中《世上只有妈妈好》的数据表,本程序就可以播放《送别》的前两小节。
/
以上就是关于51单片机控制喇叭的程序(c语言)全部的内容,包括:51单片机控制喇叭的程序(c语言)、怎么把歌曲用单片机C语言程序进行播放,急急急急~~~~~~、求c51单片机蜂鸣器演奏《青花瓷》c语言程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)