51单片机蜂鸣器演奏晴天源码

#include <REGX52.H>
#include"Delay.h"
#include"Timer0.h"

#define SPEED 	500

//音符与索引对应表,P:休止符,L:低音,M:中音,H:高音,下划线:升半音符号#
#define P0   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


sbit Buzzer=P2^5;

void Delay(unsigned int xms)		//@12.000MHz
{
	unsigned char i, j;
	while(xms--)
	{
			i = 2;
		j = 239;
		do
		{
			while (--j);
		} while (--i);

	}
	
}


void Timer0_Init(void)		//1毫秒@12.000MHz
{
	TMOD &= 0xF0;		//设置定时器模式
	TMOD |= 0x01;		//设置定时器模式
	TL0 = 0x18;		//设置定时初值
	TH0 = 0xFC;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
	ET0=1;
	EA=1;
}


unsigned int FreqTable[]={
	0,
	63777,63872,63969,64054,64140,64216,64291,64360,64426,64489,64547,64607,
	64655,64704,64751,64795,64837,64876,64913,64948,64981,65012,65042,65070,
	65095,65120,65144,65166,65186,65206,65225,65242,65259,65274,65289,65303,
	
};

unsigned int code Music[]={
	L6,4,
	M1,4,
	M5,4,
	M1,4,
	L4,4,
	L5,2,
	L6,2,
	M5,4,
	M1,4,
	L1,4,
	L5,4,
	M5,4,
	M1,4,
	L1,4,
	M5,4,
	L7,4,
	M5,4,
	L6,4,
	M1,4,
	M5,4,
	L6,4,
	L4,4,
	L5,2,
	L6,2,
	M5,4,
	M1,4,
	L1,4,
	L5,4,
	M5,4,
	M1,4,
	L1,4,
	M5,4,
	L7,2,
	M1,2,
	M5,4,//前奏
	
	M5,4,
	M5,4,
	M1,4,
	M1,8,
	M2,4,
	M3,4,//故事的小黄花
	
	M5,4,
	M5,4,
	M1,4,
	M2,2,
	M3,2,
	M2,2,
	M1,2,
	L5,4,//从出生那年就飘着
	
	M5,4,
	M5,4,
	M1,4,
	M1,8,
	M2,4,
	M3,4,//童年的荡秋千
	
	
	M3,4, 
	M2,2, 
	M3,2, 
	M4,2, 
	M3,2, 
	M2,2,
	M4,2, 
	M3,2, 
	M2,2, 
	M1,4,//随记忆一直晃到现在
	
		
	L5,4,  
	M1,4,  
	M1,4, 
	M3,4, 
	M4,4, 
	M3,4,  
	M2,4,  
	M1,2, 
	M2,4, 
	M3,4,  
	M3,4, 
	M3,4,  
	M3,4,  
	M2,2,  
	M3,2,  
	M2,4, 
	M1,8, //Re So So Si Do Si La  So  La Si Si Si La Si La So 

	L5,4,  
	M1,4,  
	M2,4, 
	M3,4, 
	M4,4, 
	M3,4, 
	M2,4,  
	M1,2,//吹着前奏望着天空 

	M2,2,  
	M3,4,  
	M3,4, 
	M3,4,  
	M3,4, 
	M2,2, 
	M3,2,	
	M2,4,
	M1,2,//我想起花瓣试着掉落

	L7,2,  
	M1,2,  
	M1,2,  
	M1,2,  
	M1,2,  
	L7,2, 
	M1,4, 
	M1,2, //为你翘课的那一天

	M1,2, 
	M1,2, 
	M1,2, 
	M1,2,
	L7,2,
	M1,4, 
	M1,2, //花落得那一天

	M1,2, 
	M1,2, 
	M1,2, 
	M1,2, 
	L7,2, 
	M1,4, 
	M1,2, //教室的那一间

	M1,2, 
	M1,2, 
	M1,2, 
	M1,2, 
	M5,2,
	M5,4, 
	M5,2,//我怎么看不见

	M5,2, 
	M5,2, 
	M5,2,
	M5,2, 
	M5,4,
	M5,2, //消失的下雨天

	M5,2,
	M5,2, 
	M5,2, 
	M5,2,
	M4,2, 
	M3,2, 
	M3,2, //我好想再淋一遍

	M1,2,
	M1,2, 
	M1,2, 
	M1,2, 
	L6,4,
	L7,4, 
	M1,4, 
	M5,4, 
	M4,4,
	M3,4,
	M1,4, 
	M1,4,//没想到失去的勇气我还留着

	M1,2, 
	M1,2, 
	M1,2, 
	M1,2, 
	M3,4, 
	M1,4, //好想再问一遍

	L6,4, 
	L7,4, 
	M1,4, 
	M5,4, 
	M4,4, 
	M3,4, 
	M1,4, 
	M2,4, 
	M2,10,//你会等待还是离开

	M3,4, 
	M2,4, 
	M4,4,
	M3,8,
	M1,4, 
	M5,4, 
	M7,4, 
	H1,4, 
	M7,4, 
	M1,4, 
	M1,8,// 刮风这天我试过握着你手

	M1,4,
	M6,4,
	M6,4, 
	M6,4, 
	M5,4, 
	M5,8, 
	M5,4, 
	M4,4,
	M3,4,
	M2,4,
	M3,4,
	M3,4,
	M3,10,//但偏偏雨渐渐大到我看你不见

	M3,4,
	M4,4,
	M5,4, 
	M3,8,
	M4,4,
	M5,4, 
	M7,4, 
	H2,4,
	M7,4,
	H1,4, 
	H1,8,//还要多久我才能在你身边

	H1,4, 
	H1,4, 
	M5,4, 
	M5,4, 
	M6,4, 
	M5,2, 
	M4,2, 
	M4,4, 
	M2,4,
	M3,4, 
	M4,4,
	M5,4, 
	M6,4, 
	M1,4,
	M6,6,
	M7,2, 
	M7,4, //等到放晴的那天也许我会比较好一点

	M3,4,
	M2,4,
	M4,4,
	M3,8, 
	M1,4,
	M5,4,
	M7,4,
	H1,4, 
	M7,4, 
	M1,4, 
	M1,8,//从前从前有个人爱你很久

	M1,4,
	M6,4, 
	M6,4, 
	M6,4, 
	M5,4,
	M5,8, 
	M5,4, 
	M4,4, 
	M3,4,
	M2,4, 
	M3,4,
	M4,4, 
	M3,4, 
	M3,10,//但偏偏风渐渐把距离吹的好远

	M3,4, 
	M4,4, 
	M5,4,
	M3,8,
	M4,4, 
	M5,4,
	M7,4, 
	H2,4, 
	M7,4, 
	H1,4, 
	H1,4,// 好不容易又能再多爱一天

	H1,4, 
	H1,4,
	M5,4,
	M5,4,
	M6,4, 
	M5,4,
	M4,4,
	L6,4,
	L7,4,
	M1,4, 
	M2,4, 
	M3,4,
	M2,4,
	M3,4, 
	M1,4, 
	M1,10, 
	//但故事的最后你好像还是说了拜拜

	
	0xFF
	//终止标志

	
};

unsigned int FreqSelect,MusicSelect;

void main()
{
	Timer0_Init();
	
	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;
	}
		
		
}

我的单片机蜂鸣器接口是P2^5,不一样的朋友自己改一下哈

根据您的问题,我了解到您想要了解如何使用51单片机蜂鸣器演奏晴天》这首歌曲。以下是一个简单的示例代码,可以帮助您实现这个目标: ```c #include <reg51.h> // 定义音符频率 #define C4 262 #define D4 294 #define E4 330 #define F4 349 #define G4 392 #define A4 440 #define B4 494 #define C5 523 // 定义音符时长 #define Q 500 // 四分音符 #define H 1000 // 二分音符 #define W 2000 // 全音符 // 延时函数 void delay(unsigned int ms) { unsigned int i, j; for (i = 0; i < ms; i++) for (j = 0; j < 110; j++); } // 发声函数 void beep(unsigned int freq, unsigned int duration) { unsigned int i; unsigned int period = 1000000 / freq; // 计算周期 for (i = 0; i < duration / period; i++) { P1 = 0x01; // 输出高电平 delay(period / 2); P1 = 0x00; // 输出低电平 delay(period / 2); } } void main() { // 晴天歌曲音符数组 unsigned int notes[] = {E4, E4, F4, G4, G4, F4, E4, D4, C4, C4, D4, E4, E4, D4, D4, E4, E4, F4, G4, G4, F4, E4, D4, C4, C4, D4, E4, D4, C4}; // 晴天歌曲时长数组 unsigned int durations[] = {Q, Q, H, Q, Q, H, Q, Q, H, Q, Q, H, Q, Q, H, Q, Q, H, Q, Q, H, Q, Q, H, Q, Q, H, W}; unsigned int i; while (1) { for (i = 0; i < sizeof(notes) / sizeof(notes[0]); i++) { beep(notes[i], durations[i]); delay(50); // 音符之间的间隔 } } } ``` 这段代码使用了51单片机的GPIO口来控制蜂鸣器发声。通过定义音符频率和时长的数组,可以按照《晴天》的音符和节奏来演奏。在主循环中,通过调用`beep`函数来发出相应的音符,并通过`delay`函数来控制音符之间的间隔。 请注意,这只是一个简单的示例代码,实际上演奏一首完整的歌曲可能需要更复杂的算法和数据结构。您可以根据自己的需求进行修改和扩展。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值