标题:新人求帮助解惑,谢谢
取消只看楼主
Lvlavandula
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-7-2
 问题点数:0 回复次数:0 
新人求帮助解惑,谢谢
大家好,因为有个程序很着急使用,但是却始终不太明白,想在这里请教一下。新人没有分数,还请见谅,如果可以请帮下吧~~
程序比较多~想麻烦大家帮忙解释一下,如果可以的话帮忙画一个程序流程图~~
程序是关于4*4键盘的电子琴模拟
两个相关文件:PlayMusic.c
#include <REG52.H>
#include "SoundPlay.h"
#define uchar unsigned char
#define uint unsigned int

uchar code table[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
uchar temp;
uint i,j;

void delay(uint x){
   uint i;
   for(i=0;i<x;i++);
}

void dou()
{
    uint x=150;
        while(x--){
            BeepIO=0;
            delay(120);
            BeepIO=1;
            delay(120);}
}
void re()
{
    uint x=150;
        while(x--){
            BeepIO=0;
            delay(115);
            BeepIO=1;
            delay(115);}   
}
void mi()
{
    uint x=150;
        while(x--){
            BeepIO=0;
            delay(110);
            BeepIO=1;
            delay(110);}
}
void fa()
{
    uint x=150;
        while(x--){
            BeepIO=0;
            delay(105);
            BeepIO=1;
            delay(105);}
}
void so()
{
    uint x=150;
        while(x--){
            BeepIO=0;
            delay(100);
            BeepIO=1;
            delay(100);}   
}
void la()
{
    uint x=150;
        while(x--){
            BeepIO=0;
            delay(95);
            BeepIO=1;
            delay(95);}
}
void si()
{
    uint x=150;
        while(x--){
            BeepIO=0;
            delay(90);
            BeepIO=1;
            delay(90);}
}
void doo()
{
    uint x=150;
        while(x--){
            BeepIO=0;
            delay(85);
            BeepIO=1;
            delay(85);}
}
void dou2()
{
    uint x=150;
        while(x--){
            BeepIO=0;
            delay(80);
            BeepIO=1;
            delay(80);}
}
void re2()
{
    uint x=150;
        while(x--){
            BeepIO=0;
            delay(75);
            BeepIO=1;
            delay(75);}   
}
void mi2()
{
    uint x=150;
        while(x--){
            BeepIO=0;
            delay(70);
            BeepIO=1;
            delay(70);}
}
void fa2()
{
    uint x=150;
        while(x--){
            BeepIO=0;
            delay(65);
            BeepIO=1;
            delay(65);}
}
void so2()
{
    uint x=150;
        while(x--){
            BeepIO=0;
            delay(60);
            BeepIO=1;
            delay(60);}   
}
void la2()
{
    uint x=150;
        while(x--){
            BeepIO=0;
            delay(55);
            BeepIO=1;
            delay(55);}
}
void si2()
{
    uint x=150;
        while(x--){
            BeepIO=0;
            delay(50);
            BeepIO=1;
            delay(50);}
}
void doo2()
{
    uint x=150;
        while(x--){
            BeepIO=0;
            delay(45);
            BeepIO=1;
            delay(45);}
}

void Delay1ms(unsigned int count)
{
    unsigned int i,j;
    for(i=0;i<count;i++)
    for(j=0;j<120;j++);
}

//*****************************Music******************************************************
//生日快乐
unsigned char code Music_Girl[]={ 0x19,0x67, 0x19,0x04, 0x1A,0x02, 0x19,0x02, 0x15,0x02,
                                  0x1B,0x01, 0x19,0x67, 0x19,0x04, 0x1A,0x02, 0x19,0x02,
                                  0x20,0x02, 0x1F,0x01, 0x19,0x67, 0x19,0x04, 0x23,0x02,
                                  0x21,0x02, 0x1F,0x02, 0x1B,0x0C, 0x1A,0x02, 0x18,0x67,
                                  0x18,0x04, 0x21,0x02, 0x1F,0x02, 0x20,0x02, 0x1F,0x01,
                                  0x00,0x00};
//一只哈巴狗
unsigned char code Music_Same[]={ 0x15,0x02, 0x15,0x02, 0x15,0x02, 0x16,0x02, 0x17,0x16,
                                  0x17,0x02, 0x17,0x02, 0x17,0x02, 0x18,0x02, 0x19,0x16,
                                  0x1A,0x02, 0x1A,0x02, 0x19,0x02, 0x18,0x02, 0x17,0x16,
                                  0x19,0x02, 0x19,0x02, 0x16,0x03, 0x17,0x03, 0x15,0x16,
                                  0x15,0x02, 0x15,0x02, 0x15,0x02, 0x16,0x02, 0x19,0x16,
                                  0x17,0x02, 0x17,0x02, 0x17,0x02, 0x18,0x02, 0x19,0x16,
                                  0x1A,0x02, 0x1A,0x02, 0x19,0x02, 0x18,0x02, 0x17,0x16,
                                  0x19,0x02, 0x19,0x02, 0x16,0x02, 0x17,0x02, 0x15,0x16,
                                  0x00,0x00 };
//两只蝴蝶                                 
unsigned char code Music_Two[] ={ 0x17,0x03, 0x16,0x03, 0x17,0x01, 0x16,0x03, 0x17,0x03,
                                  0x16,0x03, 0x15,0x01, 0x10,0x03, 0x15,0x03, 0x16,0x02,
                                  0x16,0x0D, 0x17,0x03, 0x16,0x03, 0x15,0x03, 0x10,0x03,
                                  0x10,0x0E, 0x15,0x04, 0x0F,0x01, 0x17,0x03, 0x16,0x03,
                                  0x17,0x01, 0x16,0x03, 0x17,0x03, 0x16,0x03, 0x15,0x01,
                                  0x10,0x03, 0x15,0x03, 0x16,0x02, 0x16,0x0D, 0x17,0x03,
                                  0x16,0x03, 0x15,0x03, 0x10,0x03, 0x15,0x03, 0x16,0x01,
                                  0x17,0x03, 0x16,0x03, 0x17,0x01, 0x16,0x03, 0x17,0x03,
                                  0x16,0x03, 0x15,0x01, 0x10,0x03, 0x15,0x03, 0x16,0x02,
                                  0x16,0x0D, 0x17,0x03, 0x16,0x03, 0x15,0x03, 0x10,0x03,
                                  0x10,0x0E, 0x15,0x04, 0x0F,0x01, 0x17,0x03, 0x19,0x03,
                                  0x19,0x01, 0x19,0x03, 0x1A,0x03, 0x19,0x03, 0x17,0x01,
                                  0x16,0x03, 0x16,0x03, 0x16,0x02, 0x16,0x0D, 0x17,0x03,
                                  0x16,0x03, 0x15,0x03, 0x10,0x03, 0x10,0x0D, 0x15,0x00,
                                  0x19,0x03, 0x19,0x03, 0x1A,0x03, 0x1F,0x03, 0x1B,0x03,
                                  0x1B,0x03, 0x1A,0x03, 0x17,0x0D, 0x16,0x03, 0x16,0x03,
                                  0x16,0x0D, 0x17,0x01, 0x17,0x03, 0x17,0x03, 0x19,0x03,
                                  0x1A,0x02, 0x1A,0x02, 0x10,0x03, 0x17,0x0D, 0x16,0x03,
                                  0x16,0x01, 0x17,0x03, 0x19,0x03, 0x19,0x03, 0x17,0x03,
                                  0x19,0x02, 0x1F,0x02, 0x1B,0x03, 0x1A,0x03, 0x1A,0x0E,
                                  0x1B,0x04, 0x17,0x02, 0x1A,0x03, 0x1A,0x03, 0x1A,0x0E,
                                  0x1B,0x04, 0x1A,0x03, 0x19,0x03, 0x17,0x03, 0x16,0x03,
                                  0x17,0x0D, 0x16,0x03, 0x17,0x03, 0x19,0x01, 0x19,0x03,
                                  0x19,0x03, 0x1A,0x03, 0x1F,0x03, 0x1B,0x03, 0x1B,0x03,
                                  0x1A,0x03, 0x17,0x0D, 0x16,0x03, 0x16,0x03, 0x16,0x03,
                                  0x17,0x01, 0x17,0x03, 0x17,0x03, 0x19,0x03, 0x1A,0x02,
                                  0x1A,0x02, 0x10,0x03, 0x17,0x0D, 0x16,0x03, 0x16,0x01,
                                  0x17,0x03, 0x19,0x03, 0x19,0x03, 0x17,0x03, 0x19,0x03,
                                  0x1F,0x02, 0x1B,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04,
                                  0x17,0x02, 0x1A,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04,
                                  0x17,0x16, 0x1A,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04,
                                  0x1A,0x03, 0x19,0x03, 0x17,0x03, 0x16,0x03, 0x0F,0x02,
                                  0x10,0x03, 0x15,0x00, 0x00,0x00 };
//***********************************************************************************
main()
{
    InitialSound();
    while(1)
    {
        if(state_song){
            Play(Music_Girl,0,3,360);
            Delay1ms(500);
            Play(Music_Same,0,3,360);
            Delay1ms(500);
            Play(Music_Two,0,3,360);
            Delay1ms(500);
            }
        else
        {
     
                P2=0xfe;
            temp=P2;
            temp=temp&0xf0;
            while(temp!=0xf0)
            {
                switch(temp)
                {
                    case 0xe0:dou();break;
                    case 0xd0:re();break;
                    case 0xb0:mi();break;
                    case 0x70:fa();break;
                }
                P2=0xfe;
                temp=P2;
                temp=temp&0xf0;
                }                       
             P2=0xfd;
             temp=P2;
             temp=temp&0xf0;
             while(temp!=0xf0)
             {
                 switch(temp)
                 {
                     case 0xe0:P0=table[4];so();break;
                     case 0xd0:P0=table[5];la();break;
                     case 0xb0:P0=table[6];si();break;
                     case 0x70:P0=table[7];doo();break;
                 }
                P2=0xfd;
                temp=P2;
                temp=temp&0xf0;
                }
            P2=0xfb;
            temp=P2;
            temp=temp&0xf0;
            while(temp!=0xf0)
            {
                switch(temp)
                {
                    case 0xe0:P0=table[8];dou2();break;
                    case 0xd0:P0=table[9];re2();break;
                    case 0xb0:P0=table[10];mi2();break;
                    case 0x70:P0=table[11];fa2();break;
                }
            P2=0xfe;
            temp=P2;
            temp=temp&0xf0;
        }
        P2=0xf7;
        temp=P2;
        temp=temp&0xf0;
        while(temp!=0xf0)
        {
            switch(temp)
            {
                case 0xe0:P0=table[12];so2();break;
                case 0xd0:P0=table[13];la2();break;
                case 0xb0:P0=table[14];si2();break;
                case 0x70:P0=table[15];doo2();break;
            }
            P2=0xfe;
            temp=P2;
            temp=temp&0xf0;
        }
                  
   
        }
    }
}
另一个相关文件:SoundPlay.h

/*说明**************************************************************************
 曲谱存贮格式 unsigned char code MusicName{音高,音长,音高,音长...., 0,0};    末尾:0,0 表示结束(Important)

 音高由三位数字组成:
         个位是表示 1~7 这七个音符
         十位是表示音符所在的音区:1-低音,2-中音,3-高音;
         百位表示这个音符是否要升半音: 0-不升,1-升半音。
 
 音长最多由三位数字组成:
         个位表示音符的时值,其对应关系是:
             |数值(n):  |0 |1 |2 |3 | 4 | 5 | 6
             |几分音符: |1 |2 |4 |8 |16 |32 |64         音符=2^n
         十位表示音符的演奏效果(0-2):  0-普通,1-连音,2-顿音
         百位是符点位: 0-无符点,1-有符点

 调用演奏子程序的格式
         Play(乐曲名,调号,升降八度,演奏速度);
    |乐曲名           : 要播放的乐曲指针,结尾以(0,0)结束;
    |调号(0-11)       :    是指乐曲升多少个半音演奏;
    |升降八度(1-3)      : 1:降八度, 2:不升不降, 3:升八度;
    |演奏速度(1-12000):    值越大速度越快;

***************************************************************************/
#ifndef __SOUNDPLAY_H_REVISION_FIRST__
#define __SOUNDPLAY_H_REVISION_FIRST__

//**************************************************************************

#define SYSTEM_OSC         11059200    //定义晶振频率12000000HZ
#define SOUND_SPACE     4/5         //定义普通音符演奏的长度分率,//每4分音符间隔
#define play 1
#define stop 0
sbit    BeepIO    =       P3^7;        //定义输出管脚
unsigned int  code FreTab[12]  = { 262,277,294,311,330,349,369,392,415,440,466,494 }; //原始频率表
unsigned char code SignTab[7]  = { 0,2,4,5,7,9,11 };                                   //1~7在频率表中的位置
unsigned char code LengthTab[7]= { 1,2,4,8,16,32,64 };                        
unsigned char Sound_Temp_TH0,Sound_Temp_TL0;    //音符定时器初值暂存
unsigned char Sound_Temp_TH1,Sound_Temp_TL1;    //音长定时器初值暂存
char state_song;
//**************************************************************************
void InitialSound(void)
{
    BeepIO = 0;
    Sound_Temp_TH1 = (65535-(1/1200)*SYSTEM_OSC)/256;    // 计算TL1应装入的初值     (10ms的初装值)
    Sound_Temp_TL1 = (65535-(1/1200)*SYSTEM_OSC)%256;    // 计算TH1应装入的初值
    TH1 = Sound_Temp_TH1;
    TL1 = Sound_Temp_TL1;
    TMOD  |= 0x11;
    ET0    = 1;
    ET1       = 0;
    EX0    = 1;
    EX1    = 1;
    TR0       = 0;
    TR1    = 0;
    EA     = 1;
    IT0    = 1;
    IT1    = 1;
}

void int0() interrupt 0
{
    state_song =stop;   
}

void BeepTimer0(void) interrupt 1    //音符发生中断
{
    BeepIO = !BeepIO;
    TH0    = Sound_Temp_TH0;
     TL0    = Sound_Temp_TL0;
}
void int1() interrupt 2
{
    state_song =play;   
}

void Play(unsigned char *Sound,unsigned char Signature,unsigned Octachord,unsigned int Speed)
{
    unsigned int NewFreTab[12];        //新的频率表
    unsigned char i,j;
    unsigned int Point,LDiv,LDiv0,LDiv1,LDiv2,LDiv4,CurrentFre,Temp_T,SoundLength;
    unsigned char Tone,Length,SL,SH,SM,SLen,XG,FD;
    for(i=0;i<12;i++)                 // 根据调号及升降八度来生成新的频率表
    {
        j = i + Signature;
        if(j > 11)
        {
            j = j-12;
            NewFreTab[i] = FreTab[j]*2;
        }
        else
            NewFreTab[i] = FreTab[j];

        if(Octachord == 1)
            NewFreTab[i]>>=2;
        else if(Octachord == 3)
            NewFreTab[i]<<=2;
    }                                    
   
    SoundLength = 0;
    while(Sound[SoundLength] != 0x00)    //计算歌曲长度
    {
        SoundLength+=2;
    }

    Point = 0;
    Tone   = Sound[Point];   
    Length = Sound[Point+1];             // 读出第一个音符和它时时值
   
    LDiv0 = 12000/Speed;                // 算出1分音符的长度(几个10ms)     
    LDiv4 = LDiv0/4;                     // 算出4分音符的长度
    LDiv4 = LDiv4-LDiv4*SOUND_SPACE;     // 普通音最长间隔标准
    TR0      = 0;
    TR1   = 1;
    while(Point < SoundLength&&state_song)
    {
        SL=Tone%10;                                 //计算出音符
        SM=Tone/10%10;                                 //计算出高低音
        SH=Tone/100;                                 //计算出是否升半
        CurrentFre = NewFreTab[SignTab[SL-1]+SH];     //查出对应音符的频率     
        if(SL!=0)
        {
            if (SM==1) CurrentFre >>= 2;         //低音
            if (SM==3) CurrentFre <<= 2;         //高音
            Temp_T = 65536-(50000/CurrentFre)*10/(12000000/SYSTEM_OSC);//计算计数器初值
            Sound_Temp_TH0 = Temp_T/256;
            Sound_Temp_TL0 = Temp_T%256;
            TH0 = Sound_Temp_TH0;  
            TL0 = Sound_Temp_TL0 + 12; //加12是对中断延时的补偿
        }
        SLen=LengthTab[Length%10];     //算出是几分音符
        XG=Length/10%10;             //算出音符类型(0普通1连音2顿音)
        FD=Length/100;
        LDiv=LDiv0/SLen;             //算出连音音符演奏的长度(多少个10ms)
        if (FD==1)
            LDiv=LDiv+LDiv/2;
        if(XG!=1)   
            if(XG==0)                 //算出普通音符的演奏长度
                if (SLen<=4)   
                    LDiv1=LDiv-LDiv4;
                else
                    LDiv1=LDiv*SOUND_SPACE;
            else
                LDiv1=LDiv/2;         //算出顿音的演奏长度
        else
            LDiv1=LDiv;
        if(SL==0) LDiv1=0;
            LDiv2=LDiv-LDiv1;         //算出不发音的长度
          if (SL!=0)
        {
            TR0=1;
            for(i=LDiv1;i>0;i--)     //发规定长度的音
            {
                while(TF1==0);
                TH1 = Sound_Temp_TH1;
                TL1 = Sound_Temp_TL1;
                TF1=0;
            }
        }
        if(LDiv2!=0)
        {
            TR0=0; BeepIO=0;
            for(i=LDiv2;i>0;i--)     //音符间的间隔
            {
                while(TF1==0);
                TH1 = Sound_Temp_TH1;
                TL1 = Sound_Temp_TL1;
                TF1=0;
            }
        }
        Point+=2;
        Tone=Sound[Point];
        Length=Sound[Point+1];
    }
    BeepIO = 0;
}
//**************************************************************************
#endif

再次感谢能看完的人们~~恳请给点帮助
搜索更多相关主题的帖子: include 电子琴 流程图 键盘 
2013-07-02 10:52



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-416497-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.053304 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved