【【51单片机蜂鸣器实现起风了】】

哀伤如同风,消失无影踪。

前面的有两个非常关键的点希望兄弟们明白
我一开始也失算了
【【51单片机蜂鸣器实现起风了】】_第1张图片
【【51单片机蜂鸣器实现起风了】】_第2张图片
这里兄弟们注意务必改成int 不然会超出
就会出现播放一半从头开始的情况

下面是两份起风了代码直接附上main.c
因为另外的其实和我之前说的模板都一样复制粘贴就行

为什么是2份因为什么ABCD大调我不懂啊 我看乐谱看不懂555
第一份speed为300

main.c

#include 
#include "Delay.h"
#include"Timer0.h"

sbit Buzzer=P2^5;

//音符与索引对应表,P:休止符,L:低音,M:中音,H:高音,下划线:升半音符号#
#define P	0
#define L1	1
#define L1_	2
#define L2	3
#define L2_	4
#define L3	5
#define L4	6
#define L4_	7
#define L5	8
#define L5_	9
#define L6	10
#define L6_	11
#define L7	12
#define M1	13
#define M1_	14
#define M2	15
#define M2_	16
#define M3	17
#define M4	18
#define M4_	19
#define M5	20
#define M5_	21
#define M6	22
#define M6_	23
#define M7	24
#define H1	25
#define H1_	26
#define H2	27
#define H2_	28
#define H3	29
#define H4	30
#define H4_	31
#define H5	32
#define H5_	33
#define H6	34
#define H6_	35
#define H7	36


#define SPEED 300  //我们以4分音符为基准
unsigned int  FreqTable[]={
	0,
	63628,63731,63835,63928,64021,64103,64185,64260,64331,64400,64463,64524,
	64580,64633,64684,64732,64777,64820,64860,64898,64934,64968,65000,65030,
	65058,65085,65110,65134,65157,65178,65198,65217,65235,65252,65268,65283
};

unsigned int code  Music[]={
//音符,时值,
	
    0,4,
18,2,
19,2,
21,2,
23,2,
11,4,
26,2,
23,6,
0,2,
18,2,
19,2,
21,2,
23,2,
    9,4,
    26,2,
    23,2,
    21,2,
    23,2,
    19,2,
    21,2,
    18,2,
    19,2,
14,2,
0,4,
18,2,
19,2,
21,2,
23,2,
11,4,
26,2,
23,6,
0,2,
18,2,
19,2,
21,2,
23,2,
9,4,
26,2,
23,2,
21,2,
23,2,
19,2,
21,2,
18,2,
19,2,
14,2,
0,4,
9,6,
7,2,
9,6,
7,2,
9,4,
11,4,
14,4,
11,4,
9,6,
7,2,
9,6,
7,2,
9,2,
11,2,
9,2,
7,2,
1,4,
0,4,
9,6,
7,2,
9,6,
7,2,
9,4,
11,4,
14,4,
11,4,
9,6,
11,2,
9,4,
7,4,
9,8,
10,8,
9,6,
7,2,
9,6,
7,2,
9,4,
11,4,
14,4,
11,4,
9,6,
11,2,
9,4,
7,4,
3,4,
0,4,
11,2,
9,2,
7,2,
9,2,
7,4,
0,4,
11,2,
9,2,
7,2,
9,2,
7,4,
0,4,
11,2,
9,2,
7,2,
9,2,
7,8,
0,8,
0,4,
7,4,
9,4,
11,4,
7,4,
16,4,
14,2,
16,6,
0,2,
7,2,
18,4,
16,2,
18,6,
0,4,
18,4,
16,2,
18,6,
11,4,
19,2,
21,2,
19,2,
18,2,
16,4,
14,4,
16,4,
14,2,
16,4,
14,2,
16,2,
14,2,
16,4,
14,2,
9,4,
14,4,
11,8,
0,8,
7,4,
9,4,
11,4,
7,4,
16,4,
14,2,
16,6,
0,2,
7,2,
18,4,
16,2,
18,6,
0,4,
18,4,
16,2,
18,6,
11,4,
19,2,
21,2,
19,2,
18,2,
16,4,
14,4,
16,4,
23,2,
23,6,
14,4,
16,4,
23,2,
23,4,
14,4,
16,2,
16,12,
0,4,
19,4,
21,4,
23,4,
28,2,
26,6,
28,2,
26,6,
28,2,
26,6,
21,4,
23,6,
28,2,
26,6,
28,2,
26,6,
28,2,
26,4,
23,8,
21,4,
19,2,
16,4,
19,4,
19,2,
21,4,
19,2,
16,4,
19,4,
23,8,
23,4,
21,6,
0,4,
19,4,
21,4,
23,4,
28,2,
26,6,
28,2,
26,6,
28,2,
26,6,
0,2,
21,2,
23,4,
28,2,
26,6,
28,2,
26,6,
28,2,
26,6,
23,6,
21,4,
19,2,
16,4,
23,4,
21,4,
19,2,
16,4,
16,2,
19,2,
19,8,
0,4,
16,2,
23,6,
21,4,
19,2,
16,4,
23,4,
21,4,
19,2,
16,4,
16,2,
19,6,
19,8,

	0xFF
};

	unsigned int FreqSelect,MusicSelect;
	
void main()
{
	
	Timer0Init();
	while(1)
	{
		if(Music[MusicSelect]!=0xFF)
		{
		FreqSelect=Music[MusicSelect];
		MusicSelect++;
		Delay(SPEED/4*Music[MusicSelect]);
		MusicSelect++;
	   TR0=0;
		Delay(5);
		TR0=1;	
			
		}
		else
		{
			TR0=0;
			while(1);
			
		}
		
	}	
	
}


void Timer0_Routine() interrupt 1
{
	if(FreqTable[FreqSelect])	//如果不是休止符
	{
		/*取对应频率值的重装载值到定时器*/
		TL0 = FreqTable[FreqSelect]%256;		//设置定时初值
		TH0 = FreqTable[FreqSelect]/256;		//设置定时初值
		Buzzer=!Buzzer;	//翻转蜂鸣器IO口
	}
}

兄弟们担待,开头有个好像不对调,我太懒了
第二份main.c
第二份speed 600

main.c

#include 
#include "Delay.h"
#include"Timer0.h"

sbit Buzzer=P2^5;

//音符与索引对应表,P:休止符,L:低音,M:中音,H:高音,下划线:升半音符号#
#define P	0
#define L1	1
#define L1_	2
#define L2	3
#define L2_	4
#define L3	5
#define L4	6
#define L4_	7
#define L5	8
#define L5_	9
#define L6	10
#define L6_	11
#define L7	12
#define M1	13
#define M1_	14
#define M2	15
#define M2_	16
#define M3	17
#define M4	18
#define M4_	19
#define M5	20
#define M5_	21
#define M6	22
#define M6_	23
#define M7	24
#define H1	25
#define H1_	26
#define H2	27
#define H2_	28
#define H3	29
#define H4	30
#define H4_	31
#define H5	32
#define H5_	33
#define H6	34
#define H6_	35
#define H7	36


#define SPEED 600  //我们以4分音符为基准
unsigned int  FreqTable[]={
	0,
	63628,63731,63835,63928,64021,64103,64185,64260,64331,64400,64463,64524,
	64580,64633,64684,64732,64777,64820,64860,64898,64934,64968,65000,65030,
	65058,65085,65110,65134,65157,65178,65198,65217,65235,65252,65268,65283
};

unsigned int code  Music[]={
//音符,时值,
	
M2,3,
    M1,1,
    M2,3,
    M1,1,
    M2,2,
    M3,2,
    M5,2,
    M3,2,
    
    M2,3,
    M1,1,
    M2,3,
    M1,1,
    M2,1,
    M3,1,
    M2,1,
    M1,1,
    L5,2,
    P,2,
    
    M2,3,
    M1,1,
    M2,3,
    M1,1,
    M2,2,
    M3,2,
    M5,2,
    M3,2,
    
    M2,3,
    M3,1,
    M2,2,
    M1,1,
    M2,5,
    P,4,
    
    M2,3,
    M1,1,
    M2,3,
    M1,1,
    M2,2,
    M3,2,
    M5,2,
    M3,2,
    
    M2,3,
    M3,1,
    M2,2,
    M1,1,
    L6,3,
    P,2,
    M3,1,
    M2,1,
    M1,1,
    M2,1,
    
    M1,2,
    P,2,
    M3,1,
    M2,1,
    M1,1,
    M2,1,
    M1,2,
    P,2,
    M3,1,
    M2,1,
    M1,1,
    M2,1,
    
    M1,4,
    P,6,
    M1,2,
    M2,2,
    M3,2,
    
    M1,2,
    M6,2,
    M5,1,
    M6,3,
    P,1,
    M1,1,
    M7,2,
    M6,1,
    M7,3,
    
    P,2,
    M7,2,
    M6,1,
    M7,3,
    M3,2,
    H1,1,
    H2,1,
    H1,1,
    M7,1,
    M6,2,
    
    M5,2,
    M6,2,
    M5,1,
    M6,2,
    M5,1,
    M6,1,
    M5,1,
    M6,2,
    M5,1,
    M2,2,
    M5,1,
    
    M5,1,
    M3,5,
    P,4,
    M1,2,
    M2,2,
    M3,2,
    
    M1,2,
    M6,2,
    M5,1,
    M6,3,
    P,1,
    M1,1,
    M7,2,
    M6,1,
    M7,3,
    
    P,2,
    M7,2,
    M6,1,
    M7,3,
    M3,2,
    H1,1,
    H2,1,
    H1,1,
    M7,1,
    M6,2,
    
    M5,2,
    M6,2,
    H3,4,
    M5,2,
    M6,2,
    H3,3,
    M5,1,
    
    M5,1,
    M6,11,
    P,2,
    H1,2,
    
    H2,2,
    H3,2,
    H6,1,
    H5,3,
    H6,1,
    H5,3,
    H6,1,
    H5,3,
    
    H2,1,
    H3,3,
    H6,1,
    H5,3,
    H6,1,
    H5,3,
    H6,1,
    H5,2,
    H3,1,
    
    H3,3,
    H2,2,
    H1,1,
    M6,2,
    H1,3,
    H2,2,
    H1,1,
    M6,2,
    
    H1,2,
    H3,6,
    H2,1,
    H3,1,
    H2,2,
    P,2,
    H1,2,
    
    
    
    H2,2,
    H3,2,
    H6,1,
    H5,3,
    H6,1,
    H5,3,
    H6,1,
    H5,2,
    P,1,
    
    H2,1,
    H3,2,
    H6,1,
    H5,3,
    H6,1,
    H5,3,
    H6,1,
    H5,2,
    H3,2,
    
    H3,1,
    H2,2,
    H1,1,
    M6,2,
    H3,2,
    H2,2,
    H1,1,
    M6,3,
    H1,2,
    
    H1,8,
    M6,1,
    H3,3,
    H2,2,
    H1,1,
    M6,1,
    
    M6,1,
    H3,2,
    H2,2,
    H1,1,
    M6,3,
    H1,7,
    
    H1,8,
    P,8,

	0xFF
};

	unsigned int FreqSelect,MusicSelect;
	
void main()
{
	
	Timer0Init();
	while(1)
	{
		if(Music[MusicSelect]!=0xFF)
		{
		FreqSelect=Music[MusicSelect];
		MusicSelect++;
		Delay(SPEED/4*Music[MusicSelect]);
		MusicSelect++;
	   TR0=0;
		Delay(5);
		TR0=1;	
			
		}
		else
		{
			TR0=0;
			while(1);
			
		}
		
	}	
	
}


void Timer0_Routine() interrupt 1
{
	if(FreqTable[FreqSelect])	//如果不是休止符
	{
		/*取对应频率值的重装载值到定时器*/
		TL0 = FreqTable[FreqSelect]%256;		//设置定时初值
		TH0 = FreqTable[FreqSelect]/256;		//设置定时初值
		Buzzer=!Buzzer;	//翻转蜂鸣器IO口
	}
}

over兄弟们 两段任君采撷
speed越小速度越快 可以做变奏的
以后再更新一点别的 兄弟用那个软件做起来也很方便

你可能感兴趣的:(51单片机微型设计,51单片机,嵌入式硬件,单片机)