找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2245|回复: 3
收起左侧

求助大神,这个单片机驱动蜂鸣器唱歌程序为什么不能发声音?

[复制链接]
ID:420587 发表于 2018-11-4 18:23 | 显示全部楼层 |阅读模式
VL1W2HKIAL4(_VQHBVL6UEC.png


  1. #include <reg52.h>
  2. #define uchar unsigned char
  3. #define uint  unsigned int
  4. sbit key1=P3^2;//按key1可切换下一曲
  5. sbit key2=P3^3;//按key2可切换上一曲
  6. sbit speaker=P3^7;//扬声器输出口

  7. char count;//歌曲标志        
  8. uchar timeh,timel,i,k=0,counter=0;//用到的各个变量
  9. uchar code  graph[] =  //8*8点阵动态图数据
  10. {
  11.         0x00,0x82,0x82,0x86,0xA6,0xB7,0xFF,0xFF,
  12.         0x00,0x00,0x00,0x10,0x12,0x9B,0xBF,0xFF,
  13.         0x00,0x00,0x00,0x80,0xCA,0xEE,0xFF,0xFF,
  14.         0x42,0x42,0x52,0x52,0x73,0x7B,0xFB,0xFF,
  15.         0x42,0x42,0x66,0x76,0xFE,0xFF,0xFF,0xFF,
  16.         0x04,0x04,0x44,0x4E,0x4E,0x7F,0xFF,0xFF,
  17.         0x08,0x88,0x8A,0x8E,0xAE,0xFF,0xFF,0xFF,
  18.         0x10,0x10,0x12,0x1A,0x1A,0x9B,0xFF,0xFF,
  19. };
  20. //————————————寂寞沙洲冷————————————//
  21. uchar code jmszl[]={
  22.         0x12,0x12,0x22,0x32,0x31,0x22,0x21,0x22,
  23.         //自你走后心憔悴
  24.         0x21,0x31,0x51,0x52,0x31,0x52,0x61,0x15,0x14,
  25.         //白色油桐风中纷飞
  26.         0x51,0x52,0x31,0x52,0x62,0x13,0x11,0x13,0x32,0x28,0x08,0x28,
  27.         //落花似人有情 这个季节
  28.         0x31,0x32,0x31,0x32,0x11,0x21,0x51,0x52,0x51,0x52,
  29.         //河畔的风放肆拼命地吹
  30.         0x51,0x51,0x31,0x32,0x31,0x32,0x81,0x72,0x63,
  31.         //不断拨弄离人的眼泪
  32.         0x62,0x71,0x81,0x72,0x61,0x61,0x52,0x31,0x21,0x32,0x51,0x54,
  33.         //那样浓烈的爱再也无法给
  34.         0x22,0x12,0x11,0x12,0x11,0x12,0x12,0x14,0x26,0x32,0x26,
  35.         //伤感一夜一夜
  36.         0x32,0x61,0x51,0x51,0x31,0x31,0x21,0x31,0x51,0x61,0x51,0x31,0x51,
  37.         //当记忆的线缠绕过往支离破碎
  38.         0x02,0x32,0x81,0x81,0x81,0x81,0x62,0x52,0x34,
  39.         //是慌乱占据了心扉                                                                                                                                 
  40.         0x31,0x81,0x81,0x81,0x61,0x91,0x82,
  41.         //有花儿伴着蝴蝶
  42.         0x51,0x51,0x51,0x51,0x31,0x61,0x53,
  43.         //孤雁可以双飞        
  44.         0x21,0x11,0x21,0x11,0x22,0x11,0x21,0x26,
  45.         //夜深人静独徘徊
  46.         0x32,0x61,0x51,0x51,0x31,0x31,0x21,0x31,0x51,0x61,0x51,0x31,0x51,0x52,
  47.         //当幸福恋人寄来红色分享喜悦
  48.         0x31,0x31,0x81,0x81,0x81,0x61,0x91,0x81,0x61,0x31,0x56,
  49.         //闭上双眼难过头也不敢回
  50.         0x32,0x32,0x81,0x81,0x81,0x81,0x91,0x81,0x61,0x81,0x61,0x51,0x31,0x51,0x34,
  51.         //仍然捡尽寒枝不肯安歇微带着后悔
  52.         0x21,0x31,0x51,0x31,0x21,0x11,0x61,0x21,0x16,0xff
  53.         //寂寞沙洲我该思念谁
  54. };
  55. //—————————————————劳动最光荣———————————————————//
  56. uchar code ldzgr[]=
  57. {
  58.         0x52,0x82,0x82,0x52,0x62,0x62,0x54,0x32,0x52,0x12,0x32,0x28,
  59.         //太阳光,金亮亮,雄鸡唱三唱;
  60.         0x52,0x84,0x52,0x62,0x62,0x54,0x52,0x81,0xA1,0x92,0x52,0x88,
  61.         //花儿 醒来了,鸟儿忙梳妆
  62.         0x83,0x91,0x82,0x52,0x62,0x62,0x54,0x33,0x81,0x62,0x52,0x12,0x32,0x24,
  63.         //小喜鹊 造新房,小蜜蜂 采蜜糖
  64.         0x12,0x11,0x21,0x32,0x51,0x51,0x62,0x52,0x84,0x82,0x51,0x61,0x84,0xA4,0x92,
  65.         0x52,0x86,0x02,
  66.         //幸福的生活从哪里来?要靠劳动来创造
  67.         0x52,0x81,0x81,0x82,0x52,0x62,0x61,0x81,0x54,0x32,0x51,0x61,0x53,0x31,0x12,
  68.         0x32,0x24,
  69.         //青青的叶儿 红红的花,小蝴蝶 贪玩耍
  70.         0x52,0x82,0x82,0x52,0x62,0x62,0x54,0x51,0x61,0x81,0xA1,0x92,0x52,0x88,
  71.         //不爱劳动 不学习,我们大家不学它
  72.         0x83,0x91,0x82,0x52,0x62,0x62,0x54,0x33,0x81,0x62,0x52,0x12,0x32,0x24,
  73.         //要学喜鹊 造新房,要学蜜蜂 采蜜糖
  74.         0x12,0x11,0x21,0x32,0x52,0x82,0x52,0x68,0x52,0x61,0x61,0x84,0xA4,0x92,0x52,
  75.         0x84,0xff//歌曲结束符号
  76.         //劳动的快乐 说不尽,劳动的创造 最光荣
  77. };
  78. //—————————————————唯一——————————————————//
  79. uchar code wy[]=
  80. {         
  81.         0x04,0x43,0x43,0x43,0x33,0x23,0x13,0x33,0x54,0x83,0x83,0x93,0xA3,0x92,0x81,
  82.         0x63,0x62,0xA3,0x93,0x83,0x96,0x04,0x04,
  83.         //最近我无法呼吸,连自己的影子也都想逃避
  84.         0xA4,0x94,0x82,0x82,0x82,0x52,0x82,0x92,0xA4,
  85.         //Ba By 你就是我的唯一
  86.         0x72,0x72,0x72,0x52,0x72,0x82,0x94,0x04,0xA2,0xA2,0xB2,0xA2,0x82,0x92,
  87.         //两个世界都变心,回去谈何容易
  88.         0x04,0xA4,0x94,0x82,0x82,0x82,0x52,0x82,0x92,0xA4,
  89.         //确定 你就是我的唯一
  90.         0x72,0x72,0x72,0x52,0x72,0x82,0x92,0xA2,0x84,0x64,0x03,0x51,0xB2,0xA2,0x82,
  91.         0x61,0x51,
  92.         //独自对着电话说我爱你,我真的爱你
  93.         0x04,0xA4,0x94,0x82,0x82,0x82,0x82,0x72,0x82,0x52,0x32,0x68,
  94.         //BABY 我已不能多爱你一些
  95.         0x02,0xA4,0x94,0x82,0x82,0x82,0x82,0x72,0x82,0x54,0x42,0x66,0xff
  96.         //其实 早已超过了爱的极限
  97. };
  98. //————————————————同桌的你——————————————————//
  99. uchar code tzdn[]=
  100. {
  101.         0x52,0x52,0x52,0x52,0x32,0x42,0x56,0x86,0x62,0x62,0x62,0x62,0x42,0x62,0x56,
  102.         0x04,
  103.         //明天你是否会想起,昨天你写的日记
  104.         0x52,0x52,0x52,0x52,0x72,0x62,0x52,0x44,0x02,0x42,0x42,0x42,0x42,0x33,0x21,
  105.         0x16,0x02,
  106.         //明天你是否还惦记,曾经最爱哭的你
  107.         0x82,0x82,0x82,0x82,0x52,0x32,0x86,0x82,0xA4,0x92,0x92,0x92,0x93,0x81,0x72,
  108.         0x66,0x02,
  109.         //谁娶了多愁善感的你,谁看了你的日记
  110.         0x72,0x72,0x72,0x72,0x72,0x82,0x96,0x54,0x02,0x72,0x72,0x82,0x92,0x82,0x72,
  111.         0x16,0x02,
  112.         //谁把你的长发盘起,谁给你做的嫁衣
  113.         0x84,0x82,0x82,0x52,0x62,0x86,0xA6,0x94,0x92,0x92,0x82,0x72,0x66,0x74,0x72,
  114.         0x72,0x72,0x82,0x96,0x56,
  115.         //啦……,啦……,啦……
  116.         0x72,0x72,0x82,0x92,0x82,0x72,0x86,0x94,0x92,0x92,0x62,0x72,0x96,0xB6,0x94,
  117.         0x92,0x92,0x82,0x72,0x66,
  118.         //啦……,啦……,啦……
  119.         0x72,0x72,0x72,0x72,0x72,0x82,0x96,0x56,0x72,0x72,0x82,0x92,0x82,0x72,0x96,
  120.         0x86,0xff
  121.         //啦……,啦……,啦……,啦……
  122. };
  123. //———————————————小小葫芦娃—————————————————//
  124. uchar code hlw[]=
  125. {
  126.         0x12,0x12,0x35,0x11,0x12,0x31,0x02,
  127.         //葫芦娃,葫芦娃
  128.         0x62,0x62,0x61,0x51,0x62,0x51,0x12,0x31,0x02,
  129.         //一根藤上七朵花
  130.         0x81,0x61,0x61,0x51,0x65,0x51,0x12,0x21,0x02,
  131.         //风吹雨打都不怕
  132.         0x75,0x51,0x32,0x58,
  133.         //啦啦啦啦
  134.         0x81,0x01,0x61,0x61,0x51,0x51,0x62,0x61,0x01,
  135.         //叮当当冬冬当当
  136.         0x52,0x11,0x32,0x02,0x81,0x01,0x61,0x61,0x51,0x51,0x61,0x61,0x01,0x51,0x11,
  137.         0x22,0x02,
  138.         //葫芦娃,叮当当冬冬当当,七朵花
  139.         0x35,0x11,0x62,0x18,
  140.         //啦啦啦啦
  141.         0x31,0x52,0x61,0x31,0x52,0x61,
  142.         //葫芦娃,葫芦娃
  143.         0x85,0x01,0x71,0x52,0x65,0x02,0xff
  144.         //七朵花
  145. };
  146. //————————————————千年之恋        ————————————————//
  147. uchar code qnzl[]=
  148. {         
  149.         0x32,0x31,0x21,0x32,0x52,0x32,0x31,0x21,0x32,0x62,
  150.         //竹林的灯火 到过的沙漠
  151.          0x32,0x31,0x21,0x32,0x82,0x71,0x81,0x71,0x51,0x32,0x22,
  152.         //七色的国度 不断飘逸风中
  153.          0x32,0x31,0x21,0x32,0x52,0x32,0x31,0x21,0x32,0x62,
  154.         //有一种神秘 灰色的旋涡
  155.          0x32,0x31,0x21,0x32,0x83,0x82,0x71,0x72,0x02,
  156.         //将我卷入了迷雾中
  157.          0x63,0xA1,0xA2,0x62,0x92,0x82,0x52,
  158.         //看不清的双手
  159.          0x31,0x51,0x63,0x51,0x63,0x51,0x63,0x51,0x62,0x82,0x7C,0x02,
  160.         //一朵花传来谁经过的温柔
  161.          0x61,0x71,0x82,0x71,0x62,0xA2,0x71,0x76,
  162.         //穿越千年的伤痛
  163.          0x61,0x71,0x82,0x71,0x62,0x52,0x31,0x36,
  164.         //只为求一个结果
  165.          0x61,0x71,0x82,0x71,0x62,0xA3,0x73,0x62,0x53,
  166.         //你留下的轮廓 指引我
  167.          0x42,0x63,0x83,0x83,0x91,0x91,
  168.         //黑夜中不寂寞
  169.          0x61,0x71,0x82,0x71,0x62,0x0A2,0x71,0x76,
  170.         //穿越千年的哀愁
  171.          0x61,0x71,0x82,0x71,0x62,0x52,0x31,0x36,
  172.         //是你在尽头等我
  173.          0x61,0x71,0x82,0x71,0x62,0xA3,0x73,0x62,0x53,
  174.         //最美丽的感动 会值得
  175.          0x42,0x82,0x88,0x02,0x74,0x93,0x89,0xff//结束标志
  176.         //用一生守候
  177. };

  178. //简谱音调对应的定时器初值,适合11.0592M的晶振
  179. uchar code cuzhi[]={256,293,329,349,392,440,494,523,586,658,697,783,879,987};
  180. void delay(uint z)        //延时165MS,即十六分音符
  181. {        
  182.         uint x,y;
  183.         for(x=z;x>0;x--)
  184.                  for(y=13333;y>0;y--);
  185. }         
  186. void delay1(uint z)        //延时1MS
  187. {        
  188.         uint x,y;
  189.         for(x=z;x>0;x--)
  190.                  for(y=120;y>0;y--);
  191. }
  192. void timer0() interrupt 1         //定时器0,用于产生各种音调
  193. {
  194.         TH0=timeh;
  195.         TL0=timel;
  196.         speaker=~speaker;
  197. }
  198. void song()         //播放歌曲函数
  199. {
  200.         uint temp;
  201.         uchar jp;//jp是简谱   
  202.         i=0;
  203.         temp=tzdn[i];        //同桌的你        
  204.         jp=temp/16; //取数的高4位
  205.                 if(jp!=0)
  206.                 {
  207.                 timeh=(65536-1/cuzhi[jp])/256;
  208.                 timel=(65536-1/cuzhi[jp])%256;
  209.                 }
  210.                 else
  211.                 {
  212.                 TR0=0;
  213.                 speaker=1;//关扬声器
  214.                 }
  215.                 delay(temp%16); //取数的低4位
  216.                 TR0=0;         //唱完一个音停10MS
  217.                 speaker=1;
  218.                 delay1(10);
  219.                 TR0=1;
  220.                 i++;
  221. }

  222. void main()
  223. {        
  224.         count=0;//不唱歌
  225.         TMOD=0x11;//定时器工作方式
  226.         TH0=0;
  227.         TL0=0; //定时器0赋初值
  228.         ET0=1;        //定时器0溢出中断使能
  229.         TR1 = 1; //开始计时
  230.         ET1 = 1;  //定时器1溢出中断使能
  231.         while(1)
  232.         {
  233.                 song();        
  234.                 delay1(1000);
  235.         }                                 
  236. }
复制代码
回复

使用道具 举报

ID:413605 发表于 2018-11-4 23:19 | 显示全部楼层
看你是有源的,还是无源的,有源的只需要提供电压就能发声,无源的除了提供电压外还要一定频率的脉冲才行。
回复

使用道具 举报

ID:164602 发表于 2018-11-5 08:08 | 显示全部楼层
你知道蜂鸣器发声音需要多少电流吗?你知道单片机IO口的电流有多大吗?中学物理(怕你大学不学物理,所以只说中学物理)上讲过的功率你还记得吗?呵呵
给你一个电路看看,想想吧。单片机不仅仅只玩程序,还要玩硬件的,没有基本知识,没法玩哦。
1.jpg

三极管在这里就是为了放大电流,让蜂鸣器工作的。
回复

使用道具 举报

ID:319023 发表于 2018-11-5 09:47 | 显示全部楼层
是要看硬件的,软硬件联调才行。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表