找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1415|回复: 2
打印 上一主题 下一主题
收起左侧

哪位大神帮我改改程序,要求开关控制跑马灯并附加音乐

[复制链接]
跳转到指定楼层
楼主
ID:263267 发表于 2017-12-19 13:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
15黑币
  • #include "reg52.h"
  • sbit key1=P0^0;
  • sbit key2=P0^1;
  • sbit key3=P0^2;
  • sbit key4=P0^3;
  • sbit key5=P0^4;
  • sbit key6=P0^5;
  • sbit key7=P0^6;
  • sbit key8=P0^7;
  • #include <INTRINS.H>
  • sbit Beep =  P1^2;
  • unsigned char n=0;  //n为节拍常数变量
  • 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 sound()
  • { unsigned char p,m;   //m为频率常数变量
  •   unsigned char i=0;
  •   TMOD&=0x0f;
  •   TMOD|=0x01;
  •   TH0=0xd8;TL0=0xef;
  •   IE=0x82;
  • play:
  •    while(1)
  •     {
  •     a: p=music_tab;
  •        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
  •     }
  • }
  • int num=0;
  • int flag_hz=10;
  • void main()
  • {
  •         TMOD=0X01;
  •         TH0=(65536-50000)/256;
  •         TL0=(65536-50000)%256;
  •         EA=1;
  •         ET0=1;
  •         TR0=1;
  •         P2=0xff;
  •         while(1)
  •         {
  •                         sound();
  •                 if(key1==0)
  •                 {
  •                         flag_hz=5;
  •                 }
  •                 if(key2==0)
  •                 {
  •                         flag_hz=25;
  •                 }
  •                 if(key3==0)
  •                 {
  •                  flag_hz=45;
  •                 }
  •                 if(key4==0)
  •                 {
  •                 }
  •                 if(key5==0)
  •                 {
  •                         P3=0X03;
  •                         P2=0X03;
  •                 }
  •                 if(key6==0)
  •                 {
  •                         P3=0X33;
  •                         P2=0X33;
  •                 }
  •                 if(key7==0)
  •                 {
  •                         P3=0XAA;
  •                         P2=0XAA;
  •                 }
  •                 if(key8==0)
  •                 {
  •                         P3=0X99;
  •                         P2=0X99;
  •                 }
  •         }
  • }
  • void T0_time() interrupt 1                                 //50MS
  • {
  •         TH0=(65536-50000)/256;
  •         TL0=(65536-50000)%256;
  •         num++;
  •         if(num>=flag_hz)
  •         {
  •                 num=0;
  •                 P3=~P3;
  •                 P2=~P2;
  •         }
  • }

[color=rgb(51, 102, 153) !important]复制代码


警告:Build target 'Target 1'
assembling STARTUP.A51...
compiling shijian 7.c...
linking...
*** WARNING L5: CODE SPACE MEMORY OVERLAP
    FROM:    000BH
    TO:      000DH
Program Size: data=14.0 xdata=0 code=606
creating hex file from "shijian  7"...
"shijian  7" - 0 Error(s), 1 Warning(s).

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:258566 发表于 2017-12-19 18:22 | 只看该作者
看看文中的注释 //
有二处/改成// 源码能通过编译,其他未看
  1. #include "reg52.h"
  2. sbit key1 = P0 ^ 0;
  3. sbit key2 = P0 ^ 1;
  4. sbit key3 = P0 ^ 2;
  5. sbit key4 = P0 ^ 3;
  6. sbit key5 = P0 ^ 4;
  7. sbit key6 = P0 ^ 5;
  8. sbit key7 = P0 ^ 6;
  9. sbit key8 = P0 ^ 7;
  10. #include <INTRINS.H>
  11. int num = 0;
  12. int flag_hz = 10;
  13. sbit Beep = P1 ^ 2;
  14. unsigned char n = 0;        //n???????
  15. unsigned char code music_tab[] = {
  16.         0x18, 0x30, 0x1C, 0x10,        //???: ????, ????, ????, ????,
  17.         0x20, 0x40, 0x1C, 0x10,
  18.         0x18, 0x10, 0x20, 0x10,
  19.         0x1C, 0x10, 0x18, 0x40,
  20.         0x1C, 0x20, 0x20, 0x20,
  21.         0x1C, 0x20, 0x18, 0x20,
  22.         0x20, 0x80, 0xFF, 0x20,
  23.         0x30, 0x1C, 0x10, 0x18,
  24.         0x20, 0x15, 0x20, 0x1C,
  25.         0x20, 0x20, 0x20, 0x26,
  26.         0x40, 0x20, 0x20, 0x2B,
  27.         0x20, 0x26, 0x20, 0x20,
  28.         0x20, 0x30, 0x80, 0xFF,
  29.         0x20, 0x20, 0x1C, 0x10,
  30.         0x18, 0x10, 0x20, 0x20,
  31.         0x26, 0x20, 0x2B, 0x20,
  32.         0x30, 0x20, 0x2B, 0x40,
  33.         0x20, 0x20, 0x1C, 0x10,
  34.         0x18, 0x10, 0x20, 0x20,
  35.         0x26, 0x20, 0x2B, 0x20,
  36.         0x30, 0x20, 0x2B, 0x40,
  37.         0x20, 0x30, 0x1C, 0x10,
  38.         0x18, 0x20, 0x15, 0x20,
  39.         0x1C, 0x20, 0x20, 0x20,
  40.         0x26, 0x40, 0x20, 0x20,
  41.         0x2B, 0x20, 0x26, 0x20,
  42.         0x20, 0x20, 0x30, 0x80,
  43.         0x20, 0x30, 0x1C, 0x10,
  44.         0x20, 0x10, 0x1C, 0x10,
  45.         0x20, 0x20, 0x26, 0x20,
  46.         0x2B, 0x20, 0x30, 0x20,
  47.         0x2B, 0x40, 0x20, 0x15,
  48.         0x1F, 0x05, 0x20, 0x10,
  49.         0x1C, 0x10, 0x20, 0x20,
  50.         0x26, 0x20, 0x2B, 0x20,
  51.         0x30, 0x20, 0x2B, 0x40,
  52.         0x20, 0x30, 0x1C, 0x10,
  53.         0x18, 0x20, 0x15, 0x20,
  54.         0x1C, 0x20, 0x20, 0x20,
  55.         0x26, 0x40, 0x20, 0x20,
  56.         0x2B, 0x20, 0x26, 0x20,
  57.         0x20, 0x20, 0x30, 0x30,
  58.         0x20, 0x30, 0x1C, 0x10,
  59.         0x18, 0x40, 0x1C, 0x20,
  60.         0x20, 0x20, 0x26, 0x40,
  61.         0x13, 0x60, 0x18, 0x20,
  62.         0x15, 0x40, 0x13, 0x40,
  63.         0x18, 0x80, 0x00
  64. };
  65. void int0() interrupt 1
  66. {
  67.         TH0 = 0xd8;
  68.         TL0 = 0xef;
  69.         n--;
  70. }
  71. void delay(unsigned char m)        //??????
  72. {
  73.         unsigned i = 3 * m;
  74.         while (--i) ;
  75. }
  76. void delayms(unsigned char a)        //???????
  77. {
  78.         while (--a) ;
  79. }
  80. void sound()
  81. {
  82.         unsigned char p, m;        //m???????
  83.         unsigned char i = 0;
  84.         TMOD &= 0x0f;
  85.         TMOD |= 0x01;
  86.         TH0 = 0xd8;
  87.         TL0 = 0xef;
  88.         IE = 0x82;
  89.   play:
  90.         while (1)
  91.         {
  92.           a:p = music_tab;
  93.                 if (p == 0x00)
  94.                 {
  95.                         i = 0, delayms(1000);
  96.                         goto play;
  97.                 }        //
  98.                 else if (p == 0xff)
  99.                 {
  100.                         i = i + 1;
  101.                         delayms(100), TR0 = 0;
  102.                         goto a;
  103.                 }
  104.                 else
  105.                 {
  106.                         m = music_tab[i++], n = music_tab[i++];
  107.                 }        //
  108.                 TR0 = 1;        //????1
  109.                 while (n != 0)
  110.                         Beep = ~Beep, delay(m);        //
  111.                 TR0 = 0;        //????1
  112.         }
  113. }

  114. void main()
  115. {
  116.         TMOD = 0X01;
  117.         TH0 = (65536 - 50000) / 256;
  118.         TL0 = (65536 - 50000) % 256;
  119.         EA = 1;
  120.         ET0 = 1;
  121.         TR0 = 1;
  122.         P2 = 0xff;
  123.         while (1)
  124.         {
  125.                 sound();
  126.                 if (key1 == 0)
  127.                 {
  128.                         flag_hz = 5;
  129.                 }
  130.                 if (key2 == 0)
  131.                 {
  132.                         flag_hz = 25;
  133.                 }
  134.                 if (key3 == 0)
  135.                 {
  136.                         flag_hz = 45;
  137.                 }
  138.                 if (key4 == 0)
  139.                 {
  140.                 }
  141.                 if (key5 == 0)
  142.                 {
  143.                         P3 = 0X03;
  144.                         P2 = 0X03;
  145.                 }
  146.                 if (key6 == 0)
  147.                 {
  148.                         P3 = 0X33;
  149.                         P2 = 0X33;
  150.                 }
  151.                 if (key7 == 0)
  152.                 {
  153.                         P3 = 0XAA;
  154.                         P2 = 0XAA;
  155.                 }
  156.                 if (key8 == 0)
  157.                 {
  158.                         P3 = 0X99;
  159.                         P2 = 0X99;
  160.                 }
  161.         }
  162. }
  163. void T0_time() interrupt 1        //50MS
  164. {
  165.         TH0 = (65536 - 50000) / 256;
  166.         TL0 = (65536 - 50000) % 256;
  167.         num++;
  168.         if (num >= flag_hz)
  169.         {
  170.                 num = 0;
  171.                 P3 = ~P3;
  172.                 P2 = ~P2;
  173.         }
  174. }
复制代码


回复

使用道具 举报

板凳
ID:263267 发表于 2017-12-19 19:57 | 只看该作者

需要改到开关控制跑马灯并附加音乐
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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