51单片机蜂鸣器唱歌——天空之城

STC89C51RC单片机代码分享

我使用的单片机的频率是 12.000Mhz

型号是stc89c51rc

话不多说代码如下

主函数代码

#include <REGF51RC.H>
#include "Timer0.h"
#include "Delay.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

code 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};

code unsigned char Music[]={
  P,4,
  P,4,
	P,4,
	M6,2,
	M7,2,
	
	H1,4+2,
	M7,2,
	H1,4,
	H3,4,
	
	M7,4+4+4,
	M3,2,
	M3,2,
	
	M6,4+2,
	M5,2,
	M6,4,
	H1,4,
	
	M5,4+4+4,
	M3,4,
	
	M4,4+2,
	M3,2,
	M4,4,
	H1,4,
	
	M3,4+4,
	P,2,
	H1,2,
	H1,2,
	H1,2,
	
	M7,4+2,
	M4_,2,
	M4_,4,
	M7,4,
	
	M7,4+4,
	P,4,
	M6,2,
	M7,2,
	
	H1,2+4,
	M7,2,
	H1,4,
	H3,4,
	
	M7,4+4+4,
	M3,2,
	M3,2,
	
	M6,4+2,
	M5,2,
	M6,4,
	H1,4,
	
	M5,4+4+4,
	M2,2,
	M3,2,
	
	M4,4,
	H1,2,
	M7,4,
	H1,2+4,
	
	H2,2,
	H2,2,
	H3,2,
	H1,4+4,
	
	M5,4+4+4,
	M2,2,
	M3,2,
	
	M4,4,
	H1,2,
	H7,4,
	H1,2+4,
	
	H2,2,
	H2,2,
	H3,2,
	H1,4+4,
	
	H1,2,
	M7,4,
	M6,2,
	M6,2,
	M7,4,
	M5_,4,
	
	M6,4+4+4,
	H1,2,
	H2,2,
	
	H3,2+4,
	H2,2,
	H3,4,
	H5,4,
	
	H2,4+4+4,
	M5,2,
	M5,2,
	
	H1,2+4,
	M7,2,
	H1,4,
	H3,4,
	
	H3,4+4+4+4,
	
	M6,2,
	M7,2,
	H1,4,
	M7,4,
	H2,2,
	H2,2,
	
	H1,2+4,
	M5,2+4+4,
	
	H4,4,
	H3,4,
	H2,4,
	H1,4,
	
	H3,4+4+4,
	H3,4,
	
	H6,4+4,
	H5,4,
	H5,4,
	
	H3,2,
	H2,2,
	H1,4+4,
	P,2,
	H1,2,
	
	H2,4,
	H1,2,
	H2,2,
	H2,4,
	H5,4,
	
	H3,4+4+4,
	H3,4,
	
	H6,4+4,
	H5,4+4,
	
	H3,2,
	H2,2,
	H1,4+4,
	P,2,
	H1,2,
	
	H2,4,
	H1,2,
	H2,2+4,
	M7,4,
	
	M6,4+4+4,
	M6,2,
	M7,2,

	0xff

};
	

sbit buzzer=P2^0;
unsigned char FreqSelct=0,MusicSelct=0;
void  main()
{
  Timer0_init();
	while(1){
	FreqSelct=Music[MusicSelct];
		MusicSelct++;
		Delay(125*Music[MusicSelct]);
		MusicSelct++;
		TR0=0;
		Delay(5);
		TR0=1;
	}
}



void Timer0_isR() interrupt 1
{
  if(FreqSelct!=0xff){
		
	TL0 =FreqTable[FreqSelct]%256;
	TH0 =FreqTable[FreqSelct]/256;
	buzzer=~buzzer;
	}
	else{
	TR0=0;
	while(1);
	
	}

	
     

	
	}

添加其他部分

  1. Delay函数的头文件

以及其代码文件

2.定时器0的初始化头文件以及c代码

#取自江协科大51单片机系列第25到第26集#

单片机唱歌内附歌曲请环伺,爱情买卖等等 #include <REG51.H> #include <INTRINS.H> //本例采用89C51, 晶振为11.0592MHZ //关于如何编制音乐代码, 其实十分简单,各位可以看以下代码. //频率常数即音乐术语中的音调,而节拍常数即音乐术语中的多少拍; //所以拿出谱子, 试探编吧! sbit Beep = P1^5 ; unsigned char n=0; unsigned char code music_tab[] ={ 0x18, 0x30, 0x1C , 0x10, 0x20, 0x40, 0x1C , 0x10, 0x18, 0x10, 0x20 , 0x10, 0x1C, 0x10, 0x18 , 0x40, 0x1C, 0x20, 0x20 , 0x20, 0x1C, 0x20, 0x18 , 0x20, 0x20, 0x80, 0xFF , 0x20, 0x30, 0x1C, 0x10 , 0x18, 0x20, 0x15, 0x20 , 0x1C, 0x20, 0x20, 0x20 , 0x26, 0x40, 0x20, 0x20 , 0x2B, 0x20, 0x26, 0x20 , 0x20, 0x20, 0x30, 0x80 , 0xFF, 0x20, 0x20, 0x1C , 0x10, 0x18, 0x10, 0x20 , 0x20, 0x26, 0x20, 0x2B , 0x20, 0x30, 0x20, 0x2B , 0x40, 0x20, 0x20, 0x1C , 0x10, 0x18, 0x10, 0x20 , 0x20, 0x26, 0x20, 0x2B , 0x20, 0x30, 0x20, 0x2B , 0x40, 0x20, 0x30, 0x1C , 0x10, 0x18, 0x20, 0x15 , 0x20, 0x1C, 0x20, 0x20 , 0x20, 0x26, 0x40, 0x20 , 0x20, 0x2B, 0x20, 0x26 , 0x20, 0x20, 0x20, 0x30 , 0x80, 0x20, 0x30, 0x1C , 0x10, 0x20, 0x10, 0x1C , 0x10, 0x20, 0x20, 0x26 , 0x20, 0x2B, 0x20, 0x30 , 0x20, 0x2B, 0x40, 0x20 , 0x15, 0x1F, 0x05, 0x20 , 0x10, 0x1C, 0x10, 0x20 , 0x20, 0x26, 0x20, 0x2B , 0x20, 0x30, 0x20, 0x2B , 0x40, 0x20, 0x30, 0x1C , 0x10, 0x18, 0x20, 0x15 , 0x20, 0x1C, 0x20, 0x20 , 0x20, 0x26, 0x40, 0x20 , 0x20, 0x2B, 0x20, 0x26 , 0x20, 0x20, 0x20, 0x30 , 0x30, 0x20, 0x30, 0x1C , 0x10, 0x18, 0x40, 0x1C , 0x20, 0x20, 0x20, 0x26 , 0x40, 0x13, 0x60, 0x18 , 0x20, 0x15, 0x40, 0x13 , 0x40, 0x18, 0x80, 0x00 }; void int0() interrupt 1 { TH0=0xd8; TL0=0xef; n--; } void delay (unsigned char m) { unsigned i=3*m; while(--i); } void delayms(unsigned char a) { while(--a); } void main() { unsigned char p,m; unsigned char i=0; TMOD&=0x0f; TMOD|=0x01; TH0=0xd8;TL0=0xef; IE=0x82; play: while(1) { a: p=music_tab[i]; if(p==0x00) { i=0, delayms(1000); goto play;} else if(p==0xff) { i=i+1;delayms(100),TR0=0; goto a;} else {m=music_tab[i++], n=music_tab[i++];} TR0=1; //开定时器1 while(n!=0) Beep=~Beep,delay(m); TR0=0; //关定时器1 } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值