单片机和喇叭播放音乐
时间:10-02
整理:3721RD
点击:
/******************************************************************************/
/**程序名称:单片机播放欢乐颂与最炫民族风片段
/**其 他:用于时钟周期为12MHz的STC89C52单片机
/******************************************************************************/
#include<reg52.h>
sbit BUZZER = P3^2;
#define uchar unsigned char
#define uint unsigned int
#define REST 36 //休止符
#define A 0 //低音1
#define AB 1 //低音1.5
#define B 2 //低音2
#define BC 3 //低音2.5
#define C 4 //低音3
#define D 5 //低音4
#define DE 6 //低音4.5
#define E 7 //低音5
#define EF 8 //低音5.5
#define F 9 //低音6
#define FG 10 //低音6.5
#define G 11 //低音7
#define L 12 //中音1
#define LM 13 //中音1.5
#define M 14 //中音2
#define MN 15 //中音2.5
#define N 16 //中音3
#define O 17 //中音4
#define OP 18 //中音4.5
#define P 19 //中音5
#define PQ 20 //中音5.5
#define Q 21 //中音6
#define QR 22 //中音6.5
#define R 23 //中音7
#define S 24 //高音1
#define SU 25 //高音1.5
#define U 26 //高音2
#define UV 27 //高音2.5
#define V 28 //高音3
#define W 29 //高音4
#define WX 30 //高音4.5
#define X 31 //高音5
#define XY 32 //高音5.5
#define Y 33 //高音6
#define YZ 34 //高音6.5
#define Z 35 //高音7
//音阶
uchar code m_note[][2] = {
{248,136},{248,243},{249,88},{249,184},{250,18},{250,103},{250,184},{251,3},{251,75},
{251,143},{251,206},{252,11},{252,67},{252,121},{252,172},{252,219},{253,9},{253,51},
{253,91},{253,129},{253,165},{253,199},{253,231},{254,5},{254,33},{254,60},{254,85},
{254,109},{254,132},{254,153},{254,173},{254,192},{254,210},{254,227},{254,243},{255,2},
{0,0},
};
//音符(1对应三十二分音符,32对应全音符)
uchar code display[][2] = {
//欢乐颂
{N,8},{N,8},{O,8},{P,8},{P,8},{O,8},{N,8},{M,8},{L,8},{L,8},{M,8},{N,8},{N,12},{M,4},{M,8},{0xff,8},
{N,8},{N,8},{O,8},{P,8},{P,8},{O,8},{N,8},{M,8},{L,8},{L,8},{M,8},{N,8},{M,12},{L,4},{L,8},{0xff,8},
{M,8},{M,8},{N,8},{L,8},{M,8},{N,4},{O,4},{N,8},{L,8},{M,8},{N,4},{O,4},{N,8},{M,8},{L,8},{M,8},{E,8},
{N,8},{N,8},{O,8},{P,8},{P,8},{O,8},{N,8},{M,8},{L,8},{L,8},{M,8},{N,8},{M,12},{L,4},{L,8},{0xff,8},
{M,8},{M,8},{N,8},{L,8},{M,8},{N,4},{O,4},{N,8},{L,8},{M,8},{N,4},{O,4},{N,8},{M,8},{L,8},{M,8},{E,8},
{N,8},{N,8},{O,8},{P,8},{P,8},{O,8},{N,8},{M,8},{L,8},{L,8},{M,8},{N,8},{M,12},{L,4},{L,8},{0xff,8},
//乐谱(最炫民族风)
{N,8},{F,4},{F,4},{L,8},{N,8},{M,4},{M,2},{N,2},{M,4},{L,4},
{M,4},{L,4},{F,8},{N,8},{F,4},{F,4},{L,8},{N,8},
{P,4},{M,2},{N,2},{M,4},{L,4},{M,4},{L,4},{G,4},{E,4},
{N,8},{F,4},{F,4},{L,8},{N,8},{M,4},{M,2},{N,2},{M,4},{L,4},
{M,4},{L,4},{F,4},{E,4},{N,8},{F,4},{F,4},{L,8},{N,8},
{P,4},{N,4},{0xff,24},//前奏
{F,8},{F,4},{E,4},{F,8},{F,4},{L,4},{L,8},{M,4},{L,4},{F,8},
{0xff,8},{L,8},{L,4},{E,4},{L,4},{M,4},{N,4},{P,4},{P,4},{N,4},
{M,8},{N,8},{0xff,8},{Q,4},{Q,4},{Q,4},{P,4},{N,4},{N,8},{L,4},
{F,4},{F,4},{F,4},{N,4},{M,8},{0xff,8},{N,4},{N,4},{P,4},{N,4},
{M,4},{N,4},{M,4},{L,4},{F,8},{E,8},{F,8},{0xff,8},
{F,8},{F,4},{E,4},{F,8},{F,4},{L,4},{L,8},{M,4},{L,4},{F,8},
{0xff,8},{L,8},{L,4},{E,4},{L,4},{M,4},{N,4},{P,4},{P,4},{N,4},
{M,8},{N,8},{0xff,8},{Q,4},{Q,4},{Q,4},{P,4},{N,4},{N,8},{L,4},
{F,4},{F,4},{F,4},{N,4},{M,8},{0xff,8},{N,4},{N,4},{P,4},{N,4},
{M,4},{N,4},{M,4},{L,4},{F,8},{E,8},{F,8},{0xff,8},
{N,4},{N,4},{P,4},{N,4},{N,4},{P,4},{P,4},{Q,4},{S,4},{Q,4},
{P,8},{Q,8},{0xff,8}, //此处有变调
{F,8},{F,4},{E,4},{F,8},{L,8},{M,4},{N,2},{M,2},{L,4},{M,4},
{N,8},{0xff,8},{F,4},{Q,4},{Q,4},{P,4},{M,4},{N,2},{M,2},
{L,4},{M,4},{N,8},{0xff,8},{0xff,16},{L,4},{F,4},{F,4},{L,4},
{M,8},{E,4},{E,4},{N,4},{P,4},{N,4},{M,4},{L,8},{0xff,8},
{F,4},{L,4},{M,4},{N,4},{M,4},{L,4},{E,4},{C,4},{F,8},{0xff,8},{0xff,16},
{F,8},{F,4},{E,4},{F,8},{L,8},{M,4},{N,2},{M,2},{L,4},{M,4},
{N,8},{0xff,8},{F,4},{Q,4},{Q,4},{P,4},{M,4},{N,2},{M,2},
{L,4},{M,4},{N,8},{0xff,8},{0xff,16},{L,4},{F,4},{F,4},{L,4},
{M,8},{E,4},{E,4},{N,4},{P,4},{N,4},{M,4},{L,12},{L,4},
{F,4},{L,4},{M,4},{N,4},{P,4},{N,4},{N,4},{P,4},{Q,8},{0xff,8},{0xff,8},
{Q,6}, {Q,2},{Q,4},{Q,4},{Q,6},{P,2},{N,4},{M,4},{M,6},{M,2},{N,6},{P,2},{N,6},{M,2},
{L,6},{F,2},{F,8},{F,4},{E,4},{F,8},{F,4},{L,4}, {M,4},{N,2},{M,2}, {L,4},{M,4},{N,8},{0xff,8},
{Q,4},{P,4},{N,4},{M,4},{P,4},{N,4},{M,4},{L,4},{L,8},{0xff,8},{F,2}, {A,2},{B,2},{C,2},{B,2},
{A,2}, {F,2},{A,2},
{N,4},{N,4},{P,4},{N,4},{N,4},{P,4},{P,4},{Q,4},{S,4},{Q,4},
{P,8},{Q,8},{0xff,8}, //此处有变调
{F,8},{F,4},{E,4},{F,8},{L,8},{M,4},{N,2},{M,2},{L,4},{M,4},
{N,8},{0xff,8},{F,4},{Q,4},{Q,4},{P,4},{M,4},{N,2},{M,2},
{L,4},{M,4},{N,8},{0xff,8},{0xff,16},{L,4},{F,4},{F,4},{L,4},
{M,8},{E,4},{E,4},{N,4},{P,4},{N,4},{M,4},{L,8},{0xff,8},
{F,4},{L,4},{M,4},{N,4},{M,4},{L,4},{E,4},{C,4},{F,8},{0xff,8},{0xff,16},
{F,8},{F,4},{E,4},{F,8},{L,8},{M,4},{N,2},{M,2},{L,4},{M,4},
{N,8},{0xff,8},{F,4},{Q,4},{Q,4},{P,4},{M,4},{N,2},{M,2},
{L,4},{M,4},{N,8},{0xff,8},{0xff,16},{L,4},{F,4},{F,4},{L,4},
{M,8},{E,4},{E,4},{N,4},{P,4},{N,4},{M,4},{L,12},{L,4},
{F,4},{L,4},{M,4},{N,4},{P,4},{N,4},{N,4},{P,4},{Q,8},{0xff,8},{0xff,8},
{0,0xff},
};
uchar time_h,time_l;
void Delay(uchar x);
void main()
{
uchar j;
uint i;
TMOD = 0x01;
ET0 = 1;
EA = 1;
TR0 = 1;
i = 0;
j = 0;
while(1)
{
while(display[i][1] != 0xff)
{
if(display[i][0] == 0xff)
{
TR0 = 0;
Delay(display[i][1]);
BUZZER = 1;
}
else
{
time_h = m_note[display[i][0]][0];
time_l = m_note[display[i][0]][1];
TH0 = time_h;
TL0 = time_l;
ET0 = 1;
TR0 = 1;
Delay(display[i][1]);
TR0 = 0;
ET0 = 0;
BUZZER = 1;
}
i++;
}
i = 0;
}
}
void Timer0() interrupt 1
{
TR0 = 0;
TH0 = time_h;
TL0 = time_l;
BUZZER = ~BUZZER;
TR0 = 1;
}
void Delay(uchar x)
{
uint i,j;
for(i = 0;i < x;i++)
for(j = 0;j < 12000;j++);
}
/**程序名称:单片机播放欢乐颂与最炫民族风片段
/**其 他:用于时钟周期为12MHz的STC89C52单片机
/******************************************************************************/
#include<reg52.h>
sbit BUZZER = P3^2;
#define uchar unsigned char
#define uint unsigned int
#define REST 36 //休止符
#define A 0 //低音1
#define AB 1 //低音1.5
#define B 2 //低音2
#define BC 3 //低音2.5
#define C 4 //低音3
#define D 5 //低音4
#define DE 6 //低音4.5
#define E 7 //低音5
#define EF 8 //低音5.5
#define F 9 //低音6
#define FG 10 //低音6.5
#define G 11 //低音7
#define L 12 //中音1
#define LM 13 //中音1.5
#define M 14 //中音2
#define MN 15 //中音2.5
#define N 16 //中音3
#define O 17 //中音4
#define OP 18 //中音4.5
#define P 19 //中音5
#define PQ 20 //中音5.5
#define Q 21 //中音6
#define QR 22 //中音6.5
#define R 23 //中音7
#define S 24 //高音1
#define SU 25 //高音1.5
#define U 26 //高音2
#define UV 27 //高音2.5
#define V 28 //高音3
#define W 29 //高音4
#define WX 30 //高音4.5
#define X 31 //高音5
#define XY 32 //高音5.5
#define Y 33 //高音6
#define YZ 34 //高音6.5
#define Z 35 //高音7
//音阶
uchar code m_note[][2] = {
{248,136},{248,243},{249,88},{249,184},{250,18},{250,103},{250,184},{251,3},{251,75},
{251,143},{251,206},{252,11},{252,67},{252,121},{252,172},{252,219},{253,9},{253,51},
{253,91},{253,129},{253,165},{253,199},{253,231},{254,5},{254,33},{254,60},{254,85},
{254,109},{254,132},{254,153},{254,173},{254,192},{254,210},{254,227},{254,243},{255,2},
{0,0},
};
//音符(1对应三十二分音符,32对应全音符)
uchar code display[][2] = {
//欢乐颂
{N,8},{N,8},{O,8},{P,8},{P,8},{O,8},{N,8},{M,8},{L,8},{L,8},{M,8},{N,8},{N,12},{M,4},{M,8},{0xff,8},
{N,8},{N,8},{O,8},{P,8},{P,8},{O,8},{N,8},{M,8},{L,8},{L,8},{M,8},{N,8},{M,12},{L,4},{L,8},{0xff,8},
{M,8},{M,8},{N,8},{L,8},{M,8},{N,4},{O,4},{N,8},{L,8},{M,8},{N,4},{O,4},{N,8},{M,8},{L,8},{M,8},{E,8},
{N,8},{N,8},{O,8},{P,8},{P,8},{O,8},{N,8},{M,8},{L,8},{L,8},{M,8},{N,8},{M,12},{L,4},{L,8},{0xff,8},
{M,8},{M,8},{N,8},{L,8},{M,8},{N,4},{O,4},{N,8},{L,8},{M,8},{N,4},{O,4},{N,8},{M,8},{L,8},{M,8},{E,8},
{N,8},{N,8},{O,8},{P,8},{P,8},{O,8},{N,8},{M,8},{L,8},{L,8},{M,8},{N,8},{M,12},{L,4},{L,8},{0xff,8},
//乐谱(最炫民族风)
{N,8},{F,4},{F,4},{L,8},{N,8},{M,4},{M,2},{N,2},{M,4},{L,4},
{M,4},{L,4},{F,8},{N,8},{F,4},{F,4},{L,8},{N,8},
{P,4},{M,2},{N,2},{M,4},{L,4},{M,4},{L,4},{G,4},{E,4},
{N,8},{F,4},{F,4},{L,8},{N,8},{M,4},{M,2},{N,2},{M,4},{L,4},
{M,4},{L,4},{F,4},{E,4},{N,8},{F,4},{F,4},{L,8},{N,8},
{P,4},{N,4},{0xff,24},//前奏
{F,8},{F,4},{E,4},{F,8},{F,4},{L,4},{L,8},{M,4},{L,4},{F,8},
{0xff,8},{L,8},{L,4},{E,4},{L,4},{M,4},{N,4},{P,4},{P,4},{N,4},
{M,8},{N,8},{0xff,8},{Q,4},{Q,4},{Q,4},{P,4},{N,4},{N,8},{L,4},
{F,4},{F,4},{F,4},{N,4},{M,8},{0xff,8},{N,4},{N,4},{P,4},{N,4},
{M,4},{N,4},{M,4},{L,4},{F,8},{E,8},{F,8},{0xff,8},
{F,8},{F,4},{E,4},{F,8},{F,4},{L,4},{L,8},{M,4},{L,4},{F,8},
{0xff,8},{L,8},{L,4},{E,4},{L,4},{M,4},{N,4},{P,4},{P,4},{N,4},
{M,8},{N,8},{0xff,8},{Q,4},{Q,4},{Q,4},{P,4},{N,4},{N,8},{L,4},
{F,4},{F,4},{F,4},{N,4},{M,8},{0xff,8},{N,4},{N,4},{P,4},{N,4},
{M,4},{N,4},{M,4},{L,4},{F,8},{E,8},{F,8},{0xff,8},
{N,4},{N,4},{P,4},{N,4},{N,4},{P,4},{P,4},{Q,4},{S,4},{Q,4},
{P,8},{Q,8},{0xff,8}, //此处有变调
{F,8},{F,4},{E,4},{F,8},{L,8},{M,4},{N,2},{M,2},{L,4},{M,4},
{N,8},{0xff,8},{F,4},{Q,4},{Q,4},{P,4},{M,4},{N,2},{M,2},
{L,4},{M,4},{N,8},{0xff,8},{0xff,16},{L,4},{F,4},{F,4},{L,4},
{M,8},{E,4},{E,4},{N,4},{P,4},{N,4},{M,4},{L,8},{0xff,8},
{F,4},{L,4},{M,4},{N,4},{M,4},{L,4},{E,4},{C,4},{F,8},{0xff,8},{0xff,16},
{F,8},{F,4},{E,4},{F,8},{L,8},{M,4},{N,2},{M,2},{L,4},{M,4},
{N,8},{0xff,8},{F,4},{Q,4},{Q,4},{P,4},{M,4},{N,2},{M,2},
{L,4},{M,4},{N,8},{0xff,8},{0xff,16},{L,4},{F,4},{F,4},{L,4},
{M,8},{E,4},{E,4},{N,4},{P,4},{N,4},{M,4},{L,12},{L,4},
{F,4},{L,4},{M,4},{N,4},{P,4},{N,4},{N,4},{P,4},{Q,8},{0xff,8},{0xff,8},
{Q,6}, {Q,2},{Q,4},{Q,4},{Q,6},{P,2},{N,4},{M,4},{M,6},{M,2},{N,6},{P,2},{N,6},{M,2},
{L,6},{F,2},{F,8},{F,4},{E,4},{F,8},{F,4},{L,4}, {M,4},{N,2},{M,2}, {L,4},{M,4},{N,8},{0xff,8},
{Q,4},{P,4},{N,4},{M,4},{P,4},{N,4},{M,4},{L,4},{L,8},{0xff,8},{F,2}, {A,2},{B,2},{C,2},{B,2},
{A,2}, {F,2},{A,2},
{N,4},{N,4},{P,4},{N,4},{N,4},{P,4},{P,4},{Q,4},{S,4},{Q,4},
{P,8},{Q,8},{0xff,8}, //此处有变调
{F,8},{F,4},{E,4},{F,8},{L,8},{M,4},{N,2},{M,2},{L,4},{M,4},
{N,8},{0xff,8},{F,4},{Q,4},{Q,4},{P,4},{M,4},{N,2},{M,2},
{L,4},{M,4},{N,8},{0xff,8},{0xff,16},{L,4},{F,4},{F,4},{L,4},
{M,8},{E,4},{E,4},{N,4},{P,4},{N,4},{M,4},{L,8},{0xff,8},
{F,4},{L,4},{M,4},{N,4},{M,4},{L,4},{E,4},{C,4},{F,8},{0xff,8},{0xff,16},
{F,8},{F,4},{E,4},{F,8},{L,8},{M,4},{N,2},{M,2},{L,4},{M,4},
{N,8},{0xff,8},{F,4},{Q,4},{Q,4},{P,4},{M,4},{N,2},{M,2},
{L,4},{M,4},{N,8},{0xff,8},{0xff,16},{L,4},{F,4},{F,4},{L,4},
{M,8},{E,4},{E,4},{N,4},{P,4},{N,4},{M,4},{L,12},{L,4},
{F,4},{L,4},{M,4},{N,4},{P,4},{N,4},{N,4},{P,4},{Q,8},{0xff,8},{0xff,8},
{0,0xff},
};
uchar time_h,time_l;
void Delay(uchar x);
void main()
{
uchar j;
uint i;
TMOD = 0x01;
ET0 = 1;
EA = 1;
TR0 = 1;
i = 0;
j = 0;
while(1)
{
while(display[i][1] != 0xff)
{
if(display[i][0] == 0xff)
{
TR0 = 0;
Delay(display[i][1]);
BUZZER = 1;
}
else
{
time_h = m_note[display[i][0]][0];
time_l = m_note[display[i][0]][1];
TH0 = time_h;
TL0 = time_l;
ET0 = 1;
TR0 = 1;
Delay(display[i][1]);
TR0 = 0;
ET0 = 0;
BUZZER = 1;
}
i++;
}
i = 0;
}
}
void Timer0() interrupt 1
{
TR0 = 0;
TH0 = time_h;
TL0 = time_l;
BUZZER = ~BUZZER;
TR0 = 1;
}
void Delay(uchar x)
{
uint i,j;
for(i = 0;i < x;i++)
for(j = 0;j < 12000;j++);
}
厉害
好资料 小编就不能打包分享吗
不错,程序复制后改了个输出口参数,就在本人的开发板上顺利运行了。只是,板上蜂鸣器音质太差了,声音也小,想搭个面包板用喇叭放下。
小编有改善音质的办法吗?