51单片机 播放青花瓷(源码)

  1. 蜂鸣器没有特定的引脚,随便定义一个就OK。
  2. 单片机的内部RAM容量比较小,所以常数数组一般放在程序存储区处,以减少RAM数据区的使用。
  3. 音律和谱曲数组是copy大佬的。
  4. 用的T0中断请求
#include 
#define uchar unsigned char
sbit beepIO=P1^5; //定义蜂鸣器端口为p1^5,根据单片机实际蜂鸣器实际接口改变
uchar m,n; //定义4个八度 每八度12分音律 共48音律
uchar code T[49][2]= {{0,0}, //定义音律49个二维数组
    {0xF9,0x1F},{0xF9,0x82},{0xF9,0xDF},{0xFA,0x37},{0xFA,0x8A},{0xFA,0xD8},{0xFB,0x23},{0xFB,0x68},{0xFB,0xAA},{0xFB,0xE9},{0xFC,0x24},{0xFC,0x5B},
    {0xFC,0x8F},{0xFC,0xC1},{0xFC,0xEF},{0xFD,0x1B},{0xFD,0x45},{0xFD,0x6C},{0xFD,0x91},{0xFD,0xB4},{0xFD,0xD5},{0xFD,0xF4},{0xFE,0x12},{0xFE,0x2D},
    {0xFE,0x48},{0xFE,0x60},{0xFE,0x78},{0xFE,0x86},{0xFE,0xA3},{0xFE,0xB6},{0xFE,0xC9},{0xFE,0xDA},{0xFF,0xEB},{0xFE,0xFA},{0xFF,0x09},{0xFF,0x17},
    {0xFF,0x24},{0xFF,0x30},{0xFF,0x3C},{0xFF,0x47},{0xFF,0x51},{0xFF,0x5B},{0xFF,0x64},{0xFF,0x6D},{0xFF,0x75},{0xFF,0x7D},{0xFF,0x84},{0xFF,0x8B}
};
uchar code music[][2]= {{0,4}, //定义曲谱数组,前数为音律,后数为音符节拍 ,要换歌改变简谱对应的音律号即可
    {0,1},{1,1},{3,1},{5,1},{6,1},{8,1},{10,1},{12,1},{13,1},//演示超低音八度 123457671
    {0,1},{13,1},{15,1},{17,1},{18,1},{20,1},{22,1},{24,1},{25,1},//演示低音八度12345671
    {0,1},{25,1},{27,1},{29,1},{30,1},{32,1},{34,1},{36,1},{37,1},//演示中音八度12345671
    {0,1},{37,1},{39,1},{41,1},{42,1},{44,1},{46,1},{48,2},//演示高音八度 1234567
    {0,4},{24,4},{24,4},{21,4},{19,4},{21,4},{14,8},{19,4},{21,4},{24,4},{21,4},{19,16},//记录菊花台简谱歌词:0553236 23532 天青色等烟雨 而我在等你
    {0,4},{24,4},{24,4},{21,4},{19,4},{21,4},{12,8},{19,4},{21,4},{24,4},{19,4},{17,16},//简谱歌词:0553235 23521 炊烟袅袅升起 隔江千万里
    {0,4},{17,4},{19,4},{21,4},{24,4},{26,4},{24,4},{22,4},{24,4},{21,4},{21,4},{19,4},{19,16},//简谱歌词:01235654 53322 在平地书刻你房间上的飘影
    {0,4},{17,4},{19,4},{17,4},{17,4},{19,4},{17,4},{19,4},{19,4},{21,8},{24,4},{21,4},{21,12},//简谱歌词:就当我为遇见你伏笔
    {0,4},{24,4},{24,4},{21,4},{19,4},{21,4},{14,8},{19,4},{21,4},{24,4},{21,4},{19,16}, //简谱歌词:0553236 23532 天青色等烟雨 而我在等你
    {0,4},{24,4},{24,4},{21,4},{19,4},{21,4},{12,8},{19,4},{21,4},{24,4},{19,4},{17,16}, //简谱歌词:0553235 23521 月色被打捞起 掩盖了结局
    {0,4},{17,4},{19,4},{21,4},{24,4},{26,4},{24,4},{22,4},{24,4},{21,4},{21,4},{19,4},{19,12},//简谱歌词:0123 5654 5332 25 322 11 如传世的青花瓷在独自美丽
    {12,4},{21,8},{19,8},{19,4},{17,20}, //简谱歌词:你眼带笑意
    {0xFF,0xFF}
}; //歌曲结尾标识


void delay(uchar p) //延时函数 无符号字符型变量
{
    uchar i,j; //定义无符号字符型变量J和I
    for(; p>0; p--) // 此处P值即主函数的n值节拍个数
        for(i=181; i>0; i--) //延时181*181个机器周期约35毫秒,即一个1/16节拍
            for(j=181; j>0; j--);
}




void T0_int() interrupt 1
{
    beepIO=!beepIO; //蜂鸣器翻转发声
    TH0=T[m][0];
    TL0=T[m][1];
//音律延时周期次数码表赋给定时寄存器作为计数初始值,每TH0TL0个机器周期触发蜂鸣器端口翻转,演奏出不同音符
}


void main()
{
    uchar i=0; //定义无符号字符型变量i,初始值为0
    TMOD=0x01;
    EA=1;
    ET0=1; //开启T0定时16位方式,总中断开启,开启T0外部中断请求
    while(1) //开始曲谱演奏,循环无限重复
    {
        m=music[i][0]; //将音律号赋值给m
        n=music[i][1]; //将节拍号赋值给n
        if(m==0x00) //如果音律号为0,
        {
            TR0=0;
            delay(n);
            i++;
        } //关闭计时器,延迟n拍,将循环数I加1 ,准备读下一个音符
        else if(m==0xFF) //否则如果音律数为FF
        {
            TR0=0;
            delay(30);
            i=0;
        } //开启节拍延时30个1/16节拍,歌曲停顿2秒,将循环数I置0
        else if(m==music[i+1][0]) // 否则如果把下一个音律号数给变量m
        {
            TR0=1;
            delay(n);
            TR0=0;
            i++;
        } //定时器0打开延迟n拍,关闭定时器T0,读下一个音符,循环数加1读下一个音律
        else //音符若不为零
        {
            TR0=1;
            delay(n);
            i++;
        }//打开定时器,延时n个1/16拍,循环数I加1 ,准备演奏下一个音符
    }
}

你可能感兴趣的:(MCU,串口通信,c语言)