单片机小程序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
单片机最小系统播放欢乐颂及最炫民族风片段
学习单片机,制作单片机最小系统是每个单片机爱好者的都经历过的阶段,单片机最小系统制作好有什么用呢?为提高单片机爱好者学习制作单片机最小系统兴趣,这里介绍一个有趣的单片机小程序,就是用单片机最小系统播放欢乐颂及最炫民族风片段。
在按下图制作好单片机最小系统后,再增加三只元件:1个电阻、1个S9012三极管,1个8欧的小喇叭后,即可完成这个单片机最小系统播放欢乐颂及最炫民族风片段的小试验,单片机爱好者通过这个小试验后,一定会大大增强对单片机学习的兴趣。
这个单片机最小系统播放欢乐颂及最炫民族风片段的小试验,由于电路比较简单,所以这里就不画出原理图了,直接给出试验的PCB图,感兴趣的爱好者可以按图进行焊接制作,制作好后向单片机内烧写程序后安装上去就可以工作,感受一下单片机的无穷勉力。
焊好的单片机最小系统播放欢乐颂及最炫民族风片段试验板的实物照片
播放板的单片机最小系统PCB图
焊好的单片机最小系统播放试验板未装单片机的照片
焊好的单片机最小系统播放欢乐颂及最炫民族风片段试验板的实物反面照片
/****************************************************************************** /
/**程序名称:单片机播放欢乐颂与最炫民族风片段
/**其他:用于时钟周期为12MHz的STC89C52单片机
/**由电子乐屋整理制作,更详细的制作资料请到电子乐屋下载/mifklrxdjqbadoq
/****************************************************************************** /
#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++);
}。