『壹』 求: 用51单片机c语言操作使蜂鸣器奏出“祝你生日快乐”音乐的全部程序!
参考一下:
http://hi..com/%D7%F6%B6%F8%C2%DB%B5%C0/blog/item/88bfff323ec42ef21b4cff09.html
-------------------
;生日快乐歌的程序
ORG 0000H
SJMP START
ORG 000BH
LJMP TIM0
;-------------------------------------------
START:
MOV TMOD, #01H
MOV IE, #82H
;-------------------------------------------
START0:
MOV 30H, #0
;-------------------------------------------
GET_TONE:
MOV A, 30H
MOV DPTR, #TAB
MOVC A, @A+DPTR //假设取到82H
MOV R2, A //谱码82H存到R2
JZ END0 //是否取到结束码00H?
ANL A, #0FH //取(低四位)节拍码2
MOV R5, A //节拍码存于R5=2
MOV A, R2
SWAP A
ANL A, #0FH //取(高四位)音符码8
JNZ SING //取到的音符码是否为0
CLR TR0 //是,不发音
D1:
CALL DELAY
INC 30H
JMP GET_TONE
;-------------------------------------------
SING:
DEC A
MOV 22H, A //22H=07H
RL A //A=2*07H
MOV DPTR, #TAB1
MOVC A, @A+DPTR
MOV TH0, A
MOV 21H, A //21h=64898
MOV A, 22H
RL A
INC A
MOVC A, @A+DPTR
MOV TL0, A
MOV 20H, A //20h=64968
SETB TR0
SJMP D1
;-------------------------------------------
END0: CLR TR0
JMP START0
;-------------------------------------------
TIM0:
MOV TL0, 20H
MOV TH0, 21H
CPL P3.7 ;外接扬声器 发音
RETI
;-------------------------------------------
DELAY: MOV R7, #02
D2: MOV R4, #125
D3: MOV R3, #248
DJNZ R3, $
DJNZ R4, D3
DJNZ R7, D2
DJNZ R5, DELAY
RET
TAB1: DW 64260,64400,64524,64580,64684,64777,64820
DW 64898,64968,65030,65058,65110,65157,65178,65217
TAB:
DB 82H,01H,81H,94H,84H,0B4H,0A4H,04H
DB 82H,01H,81H,94H,84H,0C4H,0B4H,04H
DB 82H,01H,81H,0F4H,0D4H,0B4H,0A4H,94H
DB 0E2H,01H,0E1H,0D4H,0B4H,0C4H,0B4H,04H
DB 82H,01H,81H,94H,84H,0B4H,0A4H,04H
DB 82H,01H,81H,94H,84H,0C4H,0B4H,04H
DB 82H,01H,81H,0F4H,0D4H,0B4H,0A4H,94H
DB 0E2H,01H,0E1H,0D4H,0B4H,0C4H,0B4H,04H
DB 00H
END
『贰』 怎么把歌曲用单片机C语言程序进行播放,急急急急~~~~~~
#include <reg51.h> //包括一个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;
}
}
『叁』 单片机高手来,AT89S52的音乐发生器程序,这个是生日快乐歌的程序,会的帮我加下注释,一点过要详细
其中一个表格用来存储定时器初值 它能决定定时时间 也就能决定音乐的频率 另一个表格用来存储音符之间的的间隔时间
『肆』 求LCD1602显示生日快乐四个汉字的程序,单片机用AT89S52的,谢啦~
/*LCD 1602生成两行字,分别是:"Believe yourself!"和"Happybirthy to you!"并作向左循环移动!*/
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar code table1[]="Believe yourself!";
uchar code table2[]="Happybirthy to you!";
sbit RS=P1^0;
sbit RW=P1^1;
sbit E=P1^2;
void delay(uint ms)
{
uint n;
for(;ms>0;ms--)
for(n=124;n>0;n--);
}
/*写指令*/
void write_Instruction(uchar Instruction)
{
RS=0;
RW=0;
E=0;
P0=Instruction;
delay(5);
E=1;
delay(5);
E=0;
}
/*写数据*/
void write_Data(uchar Data)
{
RS=1;
RW=0;
E=0;
P0=Data;
delay(5);
E=1;
delay(5);
E=0;
}
/*初始化*/
void Init()
{
delay(15);
write_Instruction(0x38);
delay(5);
write_Instruction(0x38);
delay(5);
write_Instruction(0x38);
delay(5);
write_Instruction(0x38);
write_Instruction(0x38);
write_Instruction(0x08);
write_Instruction(0x01);
write_Instruction(0x06);
write_Instruction(0x0c);
}
void left_move()
{
uchar i;
for(i=0;i<16;i++)
{
write_Instruction(0x18);
delay(3000);
}
}
void main()
{
uchar i;
Init();
while(1)
{
write_Instruction(0x80);
for(i=0;i<16;i++)
{
write_Data(table1[i]);
delay(1);
}
write_Instruction(0x80+0x40);
for(i=0;i<19;i++)
{
write_Data(table2[i]);
delay(1);
}
left_move();
}
}