51单片机——蜂鸣器,播放音乐

一、蜂鸣器播放《起风了》

#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit voice=P1^1;   
uchar code sound[]={0xff,
                    0x2b,0x30,0x30,0x10,0x2b,0x30,0x30,0x10,0x2b,0x20,0x26,0x20,0x20,0x20,
                    0x26,0x20,0x2b,0x30,0x30,0x10,0x2b,0x30,0x30,0x10,0x2b,0x10,0x26,0x10,
                    0x2b,0x10,0x30,0x10,0x40,0x20,0x40,0x20,0x2b,0x30,0x30,0x10,0x2b,0x30,
                    0x30,0x10,0x2b,0x20,0x26,0x20,0x20,0x20,0x26,0x20,0x2b,0x30,0x26,0x10,
                    0x2b,0x20,0x30,0x20,0x2b,0x20,0x2b,0x20,0xff,0x20,0xff,0x20,0x2b,0x30,
					0x30,0x10,0x2b,0x30,0x30,0x10,0x2b,0x20,0x26,0x20,0x20,0x20,0x26,0x20,
					0x2b,0x30,0x26,0x10,0x2b,0x20,0x30,0x20,0x39,0x20,0x39,0x20,0x26,0x10,
					0x2b,0x10,0x30,0x10,0x2b,0x10,0x30,0x20,0x30,0x20,0x26,0x10,0x2b,0x10,
					0x30,0x10,0x2b,0x10,0x30,0x30,0x20,0x10,0x26,0x10,0x2b,0x10,0x30,0x10,
					0x2b,0x10,0x30,0x20,0x30,0x20,0x30,0x20,0x2b,0x20,0x26,0x20,0x30,0x20,0x1c,0x20,0x20,0x10,0x1c,0x10,
                    0x1c,0x30,0x30,0x10,0x19,0x20,0x1c,0x10,0x19,0x10,0x19,0x20,0x19,0x20,
                    0x1c,0x10,0x19,0x10,0x19,0x20,0x26,0x20,0x18,0x10,0x15,0x10,0x18,0x10,
                    0x19,0x10,0x1c,0x20,0x20,0x20,0x1c,0x20,0x20,0x10,0x1c,0x10,0x1c,0x10,
                    0x20,0x10,0x1c,0x10,0x20,0x10,0x1c,0x20,0x20,0x10,0x2b,0x10,0x2b,0x10,
                    0x20,0x20,0x20,0x10,0x26,0x20,0x26,0x20,0x30,0x20,0x2b,0x20,0x26,0x20,
                    0x30,0x20,0x1c,0x20,0x20,0x10,0x1c,0x10,0x1c,0x30,0x30,0x10,0x19,0x20,
                    0x1c,0x10,0x19,0x10,0x19,0x20,0x19,0x20,0x1c,0x10,0x19,0x10,0x19,0x20,
                    0x26,0x20,0x18,0x10,0x15,0x10,0x18,0x10,0x19,0x10,0x1c,0x20,0x20,0x20,
                    0x1c,0x20,0x13,0x10,0x13,0x10,0x13,0x20,0x20,0x20,0x1c,0x20,0x13,0x10,
                    0x13,0x10,0x13,0x10,0x20,0x20,0x1c,0x10,0x1c,0x20,0x1c,0x20,0x1c,0x20,
                    0x1c,0x20,0xff,0x20,0xff,0x20,  0x18,0x20,0x15,0x20,0x13,0x20,0x0e,0x10,0x10,0x10,0x10,0x20,0x0e,0x10,
                    0x10,0x10,0x10,0x20,0x0e,0x10,0x10,0x10,0x10,0x20,0x19,0x10,0x18,0x10,
                    0x18,0x20,0x0e,0x10,0x10,0x10,0x10,0x20,0x0e,0x10,0x10,0x10,0x10,0x20,
                    0x0e,0x10,0x10,0x10,0x10,0x10,0x13,0x30,0x15,0x20,0x18,0x10,0x1c,0x10,
                    0x1c,0x10,0x18,0x20,0x1c,0x10,0x15,0x10,0x18,0x10,0x1c,0x20,0x1c,0x10,
                    0x18,0x30,0x13,0x20,0x13,0x20,0x13,0x10,0x12,0x10,0x13,0x20,0x13,0x10,
                    0x15,0x20,0xff,0x20,0xff,0x20,0x18,0x20,0x15,0x20,0x13,0x20,0x0e,0x10,0x10,0x10,
                    0x10,0x20,0x0e,0x10,0x10,0x10,0x10,0x20,0x0e,0x10,0x10,0x10,0x10,0x20,
                    0x15,0x20,0x13,0x20,0x0e,0x10,0x10,0x10,0x10,0x20,0x0e,0x10,0x10,0x10,
                    0x10,0x20,0x0e,0x10,0x10,0x10,0x10,0x10,0x13,0x30,0x15,0x20,0x18,0x10,
                    0x1c,0x10,0x1c,0x10,0x13,0x30,0x15,0x20,0x18,0x10,0x1c,0x10,0x1c,0x10,
                    0x18,0x30,0x18,0x20,0x18,0x20,0xff,0x20,0xff,0x20,0x1c,0x10,0x13,0x10,
                    
                    0x15,0x20,0x18,0x10,0x1c,0x10,0x1c,0x10,0x13,0x30,0x15,0x20,0x18,0x10,
                    0x1c,0x10,0x1c,0x10,0x18,0x30,0x18,0x20,0x18,0x20,0x18,0x20,0x18,0x20,0x00,
                   };
uchar zdjs=0, jp;
del(yj);
void main(void)
  {
      uint dpjs=0;
      uchar yj;
      TMOD=0x01,IE=0x82;
      TH0=0xd8, TL0=0xef;
   TR0=1;
while(1)
   {  
      zdjs=0;
      dpjs++; yj=sound[dpjs]; 
     dpjs++; jp=sound[dpjs];
   while(zdjs!=jp)
       { 
      if(yj!=0xff)
   {
             if(yj!=0)
               {
             voice=!voice;
                 del(yj);
                }
                else
          {
          dpjs=0; 
                      break;
                     }
    }
    else
      {
        voice=0;
                 del(jp);
    }
      }
       }
   }
time0() interrupt 1  using 1
      {
         TH0=0xd8, TL0=0xef;
         zdjs++;
           }
del(yj)
    {
      uchar yj2=2;
       while(yj!=0)
         {      
               while(yj2!=0)
                  {
                     yj2--;
                     }
                  yj2=2;
             yj--;  
       }
   
     }

二、

1、蜂鸣器发声

#include <REG51.H>
sbit  beep=P1^0;
void delay(unsigned int i)
{
	unsigned int k;
	for(k=0;k<i;k++);
}
void main()
{
	while(1)
	{
		beep=0;
		delay(100);
		beep=1;
		delay(100);
	}
	
}

二、

2、方法二

#include <REG51.H>	  
sbit   beep=P1^1;	 

void   delay(unsigned int i)
{
   while(i--);        
}
void   main()		  
{
   while(1)          
   {   	 
   	 beep=1;           	      
         delay(100);	  
	 beep=0;           
	 delay(100);	  
   } 
}
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit voice=P1^1;   
uchar code sound[]={0xff,
                    0x2b,0x30,0x30,0x10,0x2b,0x30,0x30,0x10,0x2b,0x20,0x26,0x20,0x20,0x20,
                    0x26,0x20,0x2b,0x30,0x30,0x10,0x2b,0x30,0x30,0x10,0x2b,0x10,0x26,0x10,
                    0x2b,0x10,0x30,0x10,0x40,0x20,0x40,0x20,0x2b,0x30,0x30,0x10,0x2b,0x30,
                    0x30,0x10,0x2b,0x20,0x26,0x20,0x20,0x20,0x26,0x20,0x2b,0x30,0x26,0x10,
                    0x2b,0x20,0x30,0x20,0x2b,0x20,0x2b,0x20,0xff,0x20,0xff,0x20,0x2b,0x30,
					0x30,0x10,0x2b,0x30,0x30,0x10,0x2b,0x20,0x26,0x20,0x20,0x20,0x26,0x20,
					0x2b,0x30,0x26,0x10,0x2b,0x20,0x30,0x20,0x39,0x20,0x39,0x20,0x26,0x10,
					0x2b,0x10,0x30,0x10,0x2b,0x10,0x30,0x20,0x30,0x20,0x26,0x10,0x2b,0x10,
					0x30,0x10,0x2b,0x10,0x30,0x30,0x20,0x10,0x26,0x10,0x2b,0x10,0x30,0x10,
					0x2b,0x10,0x30,0x20,0x30,0x20,0x30,0x20,0x2b,0x20,0x26,0x20,0x30,0x20,0x1c,0x20,0x20,0x10,0x1c,0x10,
                    0x1c,0x30,0x30,0x10,0x19,0x20,0x1c,0x10,0x19,0x10,0x19,0x20,0x19,0x20,
                    0x1c,0x10,0x19,0x10,0x19,0x20,0x26,0x20,0x18,0x10,0x15,0x10,0x18,0x10,
                    0x19,0x10,0x1c,0x20,0x20,0x20,0x1c,0x20,0x20,0x10,0x1c,0x10,0x1c,0x10,
                    0x20,0x10,0x1c,0x10,0x20,0x10,0x1c,0x20,0x20,0x10,0x2b,0x10,0x2b,0x10,
                    0x20,0x20,0x20,0x10,0x26,0x20,0x26,0x20,0x30,0x20,0x2b,0x20,0x26,0x20,
                    0x30,0x20,0x1c,0x20,0x20,0x10,0x1c,0x10,0x1c,0x30,0x30,0x10,0x19,0x20,
                    0x1c,0x10,0x19,0x10,0x19,0x20,0x19,0x20,0x1c,0x10,0x19,0x10,0x19,0x20,
                    0x26,0x20,0x18,0x10,0x15,0x10,0x18,0x10,0x19,0x10,0x1c,0x20,0x20,0x20,
                    0x1c,0x20,0x13,0x10,0x13,0x10,0x13,0x20,0x20,0x20,0x1c,0x20,0x13,0x10,
                    0x13,0x10,0x13,0x10,0x20,0x20,0x1c,0x10,0x1c,0x20,0x1c,0x20,0x1c,0x20,
                    0x1c,0x20,0xff,0x20,0xff,0x20,  0x18,0x20,0x15,0x20,0x13,0x20,0x0e,0x10,0x10,0x10,0x10,0x20,0x0e,0x10,
                    0x10,0x10,0x10,0x20,0x0e,0x10,0x10,0x10,0x10,0x20,0x19,0x10,0x18,0x10,
                    0x18,0x20,0x0e,0x10,0x10,0x10,0x10,0x20,0x0e,0x10,0x10,0x10,0x10,0x20,
                    0x0e,0x10,0x10,0x10,0x10,0x10,0x13,0x30,0x15,0x20,0x18,0x10,0x1c,0x10,
                    0x1c,0x10,0x18,0x20,0x1c,0x10,0x15,0x10,0x18,0x10,0x1c,0x20,0x1c,0x10,
                    0x18,0x30,0x13,0x20,0x13,0x20,0x13,0x10,0x12,0x10,0x13,0x20,0x13,0x10,
                    0x15,0x20,0xff,0x20,0xff,0x20,0x18,0x20,0x15,0x20,0x13,0x20,0x0e,0x10,0x10,0x10,
                    0x10,0x20,0x0e,0x10,0x10,0x10,0x10,0x20,0x0e,0x10,0x10,0x10,0x10,0x20,
                    0x15,0x20,0x13,0x20,0x0e,0x10,0x10,0x10,0x10,0x20,0x0e,0x10,0x10,0x10,
                    0x10,0x20,0x0e,0x10,0x10,0x10,0x10,0x10,0x13,0x30,0x15,0x20,0x18,0x10,
                    0x1c,0x10,0x1c,0x10,0x13,0x30,0x15,0x20,0x18,0x10,0x1c,0x10,0x1c,0x10,
                    0x18,0x30,0x18,0x20,0x18,0x20,0xff,0x20,0xff,0x20,0x1c,0x10,0x13,0x10,
                    
                    0x15,0x20,0x18,0x10,0x1c,0x10,0x1c,0x10,0x13,0x30,0x15,0x20,0x18,0x10,
                    0x1c,0x10,0x1c,0x10,0x18,0x30,0x18,0x20,0x18,0x20,0x18,0x20,0x18,0x20,0x00,
                   };
uchar zdjs=0, jp;
del(yj);
void main(void)
  {
      uint dpjs=0;
      uchar yj;
      TMOD=0x01,IE=0x82;
      TH0=0xd8, TL0=0xef;
   TR0=1;
while(1)
   {  
      zdjs=0;
      dpjs++; yj=sound[dpjs]; 
     dpjs++; jp=sound[dpjs];
   while(zdjs!=jp)
       { 
      if(yj!=0xff)
   {
             if(yj!=0)
               {
             voice=!voice;
                 del(yj);
                }
                else
          {
          dpjs=0; 
                      break;
                     }
    }
    else
      {
        voice=0;
                 del(jp);
    }
      }
       }
   }
time0() interrupt 1  using 1
      {
         TH0=0xd8, TL0=0xef;
         zdjs++;
           }
del(yj)
    {
      uchar yj2=2;
       while(yj!=0)
         {      
               while(yj2!=0)
                  {
                     yj2--;
                     }
                  yj2=2;
             yj--;  
       }
   
     }

仿真图百度网盘自取 

链接:https://pan.baidu.com/s/1wV6i-nDFsJw0882sDD-ung 
提取码:2222

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值