登录  | 立即注册

游客您好!登录后享受更多精彩

扫一扫,访问微社区

QQ登录

只需一步,快速开始

开启左侧

[寒假笔记] 51单片机新乐谱程序制作尝试

[复制链接]
发表于 2022-1-19 17:18:33 | 显示全部楼层 |阅读模式
学习笔记
学习科目: 51单片机技术
学习安排: 在一月十九日尝试抄写两只老虎蜂鸣器代码,并自主写出新乐谱代码
开始时间: 2022-01-19
结束时间: 2022-01-19
在一月十九日进行蜂鸣器新乐谱制作的尝试,乐谱代码如下,仅供参考。
#include <reg52.h>

sbit BUZZ=P1^3;

unsigned int code NoteFrequ[]={
        523,587,659,698,784,880,998,
        1047,1175,1319,1397,1568,1760,1976
};
unsigned int code NoteReload[]={
        65536-(32768000/12)/(523*2),
        65536-(32768000/12)/(587*2),
        65536-(32768000/12)/(659*2),
        65536-(32768000/12)/(698*2),
        65536-(32768000/12)/(784*2),
        65536-(32768000/12)/(880*2),
        65536-(32768000/12)/(998*2),
        65536-(32768000/12)/(1047*2),
        65536-(32768000/12)/(1175*2),
        65536-(32768000/12)/(1319*2),
        65536-(32768000/12)/(1397*2),
        65536-(32768000/12)/(1568*2),
        65536-(32768000/12)/(1760*2),
        65536-(32768000/12)/(1976*2),
};
bit enable=1;
bit tmrflag=0;
unsigned char T0RH=0xFF;
unsigned char T0RL=0x00;

void PlayUndertale();

void main()
{
        unsigned int i;
        EA=1;
        TMOD=0x01;
        TH0=T0RH;
        TL0=T0RL;
        ET0=1;
        TR0=1;
       
        while(1)
        {
                PlayUndertale();
                for(i=0;i<400000;i++);
        }
}
void PlayUndertale()
{
        unsigned char beat;
        unsigned char note;
        unsigned int time=0;
        unsigned int beatTime=0;
        unsigned int soundTime=0;
       
        unsigned char code UndertaleNote[]={
                1,5,4,1,3,3,4,  1,4,1,3,3,4,
                1,5,4,1,3,3,4,  1,4,6,5,4,5,
        };
        unsigned char code UndertaleBeat[]={
          4,4,4,4,4,4,4,  4,4,4,4,4,4,4,
          4,4,4,4,4,4,4,  4,4,4,4,4,4,4,
                };
        for(beat=0;beat<sizeof(UndertaleNote);)
                {
                        while(!tmrflag);
                        tmrflag=0;
                        if(time==0)
                        {
                                note=UndertaleNote[beat]-1;
                                T0RH=NoteReload[note]>>8;
                                T0RL=NoteReload[note];
                                beatTime=(UndertaleBeat[beat]*NoteFrequ[note])>>2;
                                soundTime=beatTime-(beatTime>>2);
                                enable=1;
                                time++;
                        }
                        else
                        {
                                if(time>=beatTime)
                                {
                                        time=0;
                                        beat++;
                                }
                                else
                                {
                                        time++;
                                        if(time==soundTime)
                                        {
                                                enable=0;
                                        }
                                }
                        }
                }       
}
void InterruptTimer0() interrupt 1
{
        TH0=T0RH;
        TL0=T0RL;
        tmrflag=1;
        if(enable)
                BUZZ=~BUZZ;
        else
                BUZZ=1;
}

好懒~~不想说~~~
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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