【51单片机】用蜂鸣器播放《小半》

180it 2022-04-06 PM 732℃ 0条

note是音符,beat是节拍

#include "reg52.h"

sbit bell = P1 ^ 5;
unsigned char note, beat;
unsigned char code sheet[49][2] = { //0~48,49个音符各自的时间对应的初值
    {0, 0},
    {0xF8, 0x8B},
    {0xF8, 0xF2},
    {0xF9, 0x5B},
    {0xF9, 0xB7},
    {0xFA, 0x14},
    {0xFA, 0x66},
    {0xFA, 0xB9},
    {0xFB, 0x03},
    {0xFB, 0x4A},
    {0xFB, 0x8F},
    {0xFB, 0xCF},
    {0xFC, 0x0B},
    {0xFC, 0x43},
    {0xFC, 0x78},
    {0xFC, 0xAB},
    {0xFC, 0xDB},
    {0xFD, 0x08},
    {0xFD, 0x33},
    {0xFD, 0x5B},
    {0xFD, 0x81},
    {0xFD, 0xA5},
    {0xFD, 0xC7},
    {0xFD, 0xE7},
    {0xFE, 0x05},
    {0xFE, 0x21},
    {0xFE, 0x3C},
    {0xFE, 0x55},
    {0xFE, 0x6D},
    {0xFE, 0x84},
    {0xFE, 0x99},
    {0xFE, 0xAD},
    {0xFE, 0xC0},
    {0xFE, 0x02},
    {0xFE, 0xE3},
    {0xFE, 0xF3},
    {0xFF, 0x02},
    {0xFF, 0x10},
    {0xFF, 0x1D},
    {0xFF, 0x2A},
    {0xFF, 0x36},
    {0xFF, 0x42},
    {0xFF, 0x4C},
    {0xFF, 0x56},
    {0xFF, 0x60},
    {0xFF, 0x69},
    {0xFF, 0x71},
    {0xFF, 0x79},
    {0xFF, 0x81}};
unsigned char code music[][2] = {
    {0, 4},
    {20, 4},
    {15, 4},
    {17, 4},
    {20, 12},
    {17, 4},
    {24, 2},
    {25, 2},
    {24, 2},
    {25, 2},
    {24, 4},
    {20, 4},
    {15, 4},
    {20, 4},
    {15, 4},
    {17, 4},
    {20, 12},
    {17, 4},
    {24, 2},
    {25, 2},
    {24, 2},
    {25, 2},
    {24, 4},
    {20, 4},
    {15, 4},
    {17, 4},
    {10, 4},
    {13, 4},
    {22, 12},
    {17, 4},
    {22, 4},
    {22, 4},
    {22, 4},
    {20, 4},
    {17, 4},
    {20, 8},
    {17, 4},
    {17, 4},
    {15, 4},
    {13, 4},
    {15, 12},
    {0, 12},
    {20, 4},
    {15, 4},
    {17, 4},
    {20, 12},
    {17, 4},
    {24, 2},
    {25, 2},
    {24, 2},
    {25, 2},
    {24, 4},
    {20, 4},
    {15, 4},
    {20, 4},
    {15, 4},
    {17, 4},
    {20, 12},
    {17, 4},
    {24, 2},
    {25, 2},
    {24, 2},
    {25, 2},
    {24, 4},
    {20, 4},
    {15, 4},
    {17, 4},
    {10, 4},
    {13, 4},
    {22, 12},
    {17, 4},
    {22, 4},
    {22, 4},
    {22, 4},
    {20, 4},
    {17, 4},
    {20, 8},
    {17, 4},
    {17, 4},
    {15, 4},
    {13, 4},
    {15, 12},
    {0, 8},
    {13, 4},
    {17, 4},
    {15, 4},
    {17, 4},
    {17, 4},
    {15, 4},
    {17, 4},
    {17, 8},
    {22, 4},
    {22, 4},
    {17, 4},
    {17, 4},
    {20, 4},
    {22, 4},
    {22, 2},
    {22, 2},
    {22, 4},
    {20, 4},
    {17, 4},
    {18, 12},
    {0, 8},
    {17, 4},
    {20, 4},
    {20, 4},
    {20, 4},
    {20, 4},
    {25, 4},
    {22, 4},
    {20, 4},
    {20, 6},
    {20, 2},
    {20, 4},
    {25, 4},
    {27, 4},
    {24, 4},
    {24, 8},
    {24, 12},
    {0, 12},
    {20, 4},
    {22, 4},
    {20, 4},
    {25, 4},
    {27, 4},
    {24, 4},
    {25, 4},
    {27, 4},
    {24, 4},
    {25, 4},
    {27, 4},
    {24, 4},
    {25, 4},
    {27, 4},
    {24, 4},
    {25, 4},
    {25, 4},
    {25, 4},
    {25, 4},
    {20, 4},
    {22, 4},
    {22, 12},
    {22, 4},
    {25, 4},
    {22, 4},
    {29, 4},
    {27, 4},
    {29, 4},
    {29, 4},
    {27, 4},
    {29, 4},
    {29, 4},
    {27, 4},
    {29, 4},
    {29, 4},
    {34, 4},
    {25, 4},
    {24, 4},
    {24, 4},
    {24, 4},
    {24, 4},
    {22, 4},
    {20, 4},
    {20, 12},
    {17, 4},
    {18, 4},
    {20, 4},
    {25, 4},
    {27, 4},
    {24, 4},
    {25, 4},
    {27, 4},
    {24, 4},
    {25, 4},
    {27, 4},
    {24, 4},
    {25, 4},
    {27, 4},
    {24, 4},
    {25, 4},
    {25, 4},
    {25, 4},
    {25, 4},
    {20, 4},
    {22, 4},
    {22, 12},
    {22, 4},
    {25, 4},
    {22, 4},
    {29, 4},
    {27, 4},
    {29, 4},
    {29, 4},
    {27, 4},
    {29, 4},
    {29, 4},
    {27, 4},
    {29, 4},
    {29, 4},
    {22, 4},
    {25, 4},
    {24, 4},
    {24, 4},
    {24, 4},
    {24, 4},
    {22, 4},
    {20, 4},
    {20, 12},
    {20, 8},
    {17, 4},
    {17, 12},
    {17, 12},
    {0, 12},
    {0, 12},
    {20, 12},
    {20, 12},
    {20, 12},
    {34, 4},
    {30, 2},
    {34, 4},
    {0xFF, 0xFF}};

void delay(unsigned char p)
{
  unsigned char i, j;
  for (; p > 0; p--)
    for (i = 181; i > 0; i--)
      for (j = 181; j > 0; j--)
        ;
}

void main()
{
  unsigned char i = 0;
  TMOD = 0x01; //timer0 - mode1
  EA = 1;
  ET0 = 1;
  while (1)
  {

    note = music[i][0]; //音符
    beat = music[i][1]; //节拍
    if (note == 0x00)
    {
      TR0 = 0;
      delay(beat);
      i++;
    }
    else if (note == 0xFF)
    {
      TR0 = 0;
      delay(30);
      i = 0;
    }
    else if (note == music[i + 1][0])
    {
      TR0 = 1;
      delay(beat);
      TR0 = 0;
      delay(1);
      i++;
    }
    else
    {
      TR0 = 1;
      delay(beat);
      i++;
    }
  }
}

void timer0_interrupt() interrupt 1
{
  bell = !bell;
  TH0 = sheet[note][0];
  TL0 = sheet[note][1];
}

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

【51单片机】用蜂鸣器播放《小半》