找回密码
 注册

QQ登录

只需一步,快速开始

搜索

51单片机 生日快乐歌 代码

[复制链接]
eng 发表于 2021-7-6 17:12:25 | 显示全部楼层 |阅读模式
1.jpg
  1. #include <REG51.H>
  2. #define uchar unsigned char
  3. sbit beepIO=P3^3;
  4. uchar m,n;
  5. uchar code T[49][2]={{0,0},
  6. {0xF8,0x8B},{0xF8,0xF2},{0xF9,0x5B},{0xF9,0xB7},{0xFA,0x14},{0xFA,0x66},{0xFA,0xB9},{0xFB,0x03},{0xFB,0x4A},{0xFB,0x8F},{0xFB,0xCF},{0xFC,0x0B},
  7. {0xFC,0x43},{0xFC,0x78},{0xFC,0xAB},{0xFC,0xDB},{0xFD,0x08},{0xFD,0x33},{0xFD,0x5B},{0xFD,0x81},{0xFD,0xA5},{0xFD,0xC7},{0xFD,0xE7},{0xFE,0x05},
  8. {0xFE,0x21},{0xFE,0x3C},{0xFE,0x55},{0xFE,0x6D},{0xFE,0x84},{0xFE,0x99},{0xFE,0xAD},{0xFE,0xC0},{0xFE,0x02},{0xFE,0xE3},{0xFE,0xF3},{0xFF,0x02},
  9. {0xFF,0x10},{0xFF,0x1D},{0xFF,0x2A},{0xFF,0x36},{0xFF,0x42},{0xFF,0x4C},{0xFF,0x56},{0xFF,0x60},{0xFF,0x69},{0xFF,0x71},{0xFF,0x79},{0xFF,0x81}
  10. };
  11. uchar code music[][2]={{0,4},
  12. {17,6},{17,2},{19,8},{17,8},{22,8},{21,16},
  13. {17,6},{17,2},{19,8},{17,8},{24,8},{22,16},{17,6},{17,2},{29,8},{26,8},{22,8},{21,8},{19,8},{27,6},{27,2},{26,8},{22,8},{24,8},{22,16},
  14. {0xFF,0xFF}};

  15. void delay(uchar p)
  16. {
  17.     uchar i,j;
  18.     for(;p>0;p--)
  19.     for(i=181;i>0;i--)
  20.     for(j=181;j>0;j--);
  21. }

  22. void pause()
  23. {
  24.     uchar i,j;
  25.     for(i=150;i>0;i--)
  26.     for(j=150;j>0;j--);
  27. }

  28. void T0_int() interrupt 1
  29. {
  30. beepIO=!beepIO;
  31. TH0=T[m][0]; TL0=T[m][1];
  32. }

  33. void main()
  34. {
  35. uchar i=0;
  36. TMOD=0x01;  EA=1; ET0=1;
  37. while(1)
  38.       {
  39.         m=music[i][0];n=music[i][1];
  40.            if(m==0x00)
  41.              {TR0=0;delay(n);i++;}
  42.            else if(m==0xFF)
  43.              {TR0=0;delay(30);i=0;}
  44.            else if(m==music[i+1][0])
  45.              {TR0=1;delay(n);TR0=0;pause();i++;}
  46.            else
  47.              {TR0=1;delay(n);i++;}
  48.       }
复制代码


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

本版积分规则

QQ|手机版|小黑屋|ELEOK |网站地图

GMT+8, 2024-11-21 20:08

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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