找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 4876|回復(fù): 10
打印 上一主題 下一主題
收起左側(cè)

單片機(jī)音樂(lè)與燈一起閃的程式

  [復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:21069 發(fā)表于 2018-12-18 02:56 | 只看該作者 |只看大圖 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式


#include <REG52.H>
#include "SoundPlay.h"

unsigned char RunMode;


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

unsigned char code LEDDisplayCode[] = { 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,        //0~7
                                                          0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xFF};

void Display(unsigned char Value)
{
        P3 = LEDDisplayCode[Value];
}

void LEDFlash(unsigned char Count)
{
        unsigned char i;
        bit Flag;
        for(i = 0; i<Count;i++)
        {
                Flag = !Flag;
                if(Flag)
                        Display(RunMode);
                else
                        Display(0x10);
                Delay1ms(100);
        }
        Display(RunMode);
}

unsigned char GetKey(void)
{
        unsigned char KeyTemp,CheckValue,Key = 0x00;
        CheckValue = P2&0x32;
        if(CheckValue==0x32)
                return 0x00;
        
        Delay1ms(10);
        KeyTemp = P2&0x32;
        if(KeyTemp==CheckValue)
                return 0x00;

        if(!(CheckValue&0x02))
                Key|=0x01;
        if(!(CheckValue&0x10))
                Key|=0x02;
        if(!(CheckValue&0x20))
                Key|=0x04;
        return Key;
}

unsigned int Timer0Count,SystemSpeed,SystemSpeedIndex;
void InitialTimer2(void)
{
        T2CON  = 0x00;                        //16 Bit Auto-Reload Mode
         TH2 = RCAP2H = 0xFC;          //重裝值,初始值        TL2 = RCAP2L = 0x18;
        ET2=1;                                        //定時(shí)器 2 中斷允許
        TR2 = 1;                                //定時(shí)器 2 啟動(dòng)
        EA=1;
}

unsigned int code SpeedCode[]={   1,   2,   3,   5,   8,  10,  14,  17,  20,  30,
                                                             40,  50,  60,  70,  80,  90, 100, 120, 140, 160,
                                                                180, 200, 300, 400, 500, 600, 700, 800, 900,1000};//30
void SetSpeed(unsigned char Speed)
{
        SystemSpeed =SpeedCode[Speed];
}

void LEDShow(unsigned int LEDStatus)
{
        P1 = ~(LEDStatus&0x00FF);
        P0 = ~((LEDStatus>>8)&0x00FF);
}

void InitialCPU(void)
{
        RunMode = 0x00;
        Timer0Count = 0;
        SystemSpeedIndex = 9;

        P1 = 0x00;
        P0 = 0x00;
        P2 = 0xFF;
        P3 = 0x00;
        Delay1ms(500);
        P1 = 0xFF;
        P0 = 0xFF;
        P2 = 0xFF;
        P3 = 0xFF;
        SetSpeed(SystemSpeedIndex);
        Display(RunMode);
}

//Mode 0
unsigned int LEDIndex = 0;
bit LEDDirection = 1,LEDFlag = 1;
void Mode_0(void)
{
        LEDShow(0x0001<<LEDIndex);
        LEDIndex = (LEDIndex+1)%16;
}
//Mode 1
void Mode_1(void)
{
        LEDShow(0x8000>>LEDIndex);
        LEDIndex = (LEDIndex+1)%16;
}
//Mode 2
void Mode_2(void)
{
        if(LEDDirection)
                LEDShow(0x0001<<LEDIndex);
        else
                LEDShow(0x8000>>LEDIndex);
        if(LEDIndex==15)
                LEDDirection = !LEDDirection;
   LEDIndex = (LEDIndex+1)%16;
}
//Mode 3
void Mode_3(void)
{
        if(LEDDirection)
                LEDShow(~(0x0001<<LEDIndex));
        else
                LEDShow(~(0x8000>>LEDIndex));
        if(LEDIndex==15)
                LEDDirection = !LEDDirection;
   LEDIndex = (LEDIndex+1)%16;
}

//Mode 4
void Mode_4(void)
{
        if(LEDDirection)
        {
                if(LEDFlag)
                        LEDShow(0xFFFE<<LEDIndex);
                   else
                        LEDShow(~(0x7FFF>>LEDIndex));
        }
        else
        {
                if(LEDFlag)
                        LEDShow(0x7FFF>>LEDIndex);
                else
                        LEDShow(~(0xFFFE<<LEDIndex));
        }
        if(LEDIndex==15)
        {
                LEDDirection = !LEDDirection;
                if(LEDDirection)        LEDFlag = !LEDFlag;
        }
           LEDIndex = (LEDIndex+1)%16;
}

//Mode 5
void Mode_5(void)
{
        if(LEDDirection)
                LEDShow(0x000F<<LEDIndex);
        else
                LEDShow(0xF000>>LEDIndex);
        if(LEDIndex==15)
                LEDDirection = !LEDDirection;
    LEDIndex = (LEDIndex+1)%16;
}

//Mode 6
void Mode_6(void)
{
        if(LEDDirection)
                LEDShow(~(0x000F<<LEDIndex));
        else
                LEDShow(~(0xF000>>LEDIndex));
        if(LEDIndex==15)
                LEDDirection = !LEDDirection;
           LEDIndex = (LEDIndex+1)%16;
}

//Mode 7
void Mode_7(void)
{
        if(LEDDirection)
                LEDShow(0x003F<<LEDIndex);
        else
                LEDShow(0xFC00>>LEDIndex);
        if(LEDIndex==9)
                LEDDirection = !LEDDirection;
    LEDIndex = (LEDIndex+1)%10;
}

//Mode 8
void Mode_8(void)
{
        LEDShow(++LEDIndex);
}

void Timer0EventRun(void)
{
        if(RunMode==0x00)
        {
                Mode_0();        
        }
        else if(RunMode ==0x01)
        {
                Mode_1();
        }
        else if(RunMode ==0x02)
        {
                Mode_2();
        }
        else if(RunMode ==0x03)
        {
                Mode_3();
        }
        else if(RunMode ==0x04)
        {
                Mode_4();
        }
        else if(RunMode ==0x05)
        {
                Mode_5();
        }
        else if(RunMode ==0x06)
        {
                Mode_6();
        }
        else if(RunMode ==0x07)
        {
                Mode_7();
        }
        else if(RunMode ==0x08)
        {
                Mode_8();
        }
}

void Timer2(void) interrupt 5 using 3
{
        TF2 = 0;         //中斷標(biāo)志清除( Timer2 必須軟件清標(biāo)志!)
        if(++Timer0Count>=SystemSpeed)
        {
                Timer0Count = 0;
                Timer0EventRun();
           }
}
unsigned char MusicIndex = 0;
void KeyDispose(unsigned char Key)
{
        if(Key&0x01)
        {
                LEDDirection = 1;
                LEDIndex = 0;
                LEDFlag = 1;
                RunMode = (RunMode+1)%10;
                Display(RunMode);
                if(RunMode==0x09)
                        TR2 = 0;
                else
                        TR2 = 1;
        }
        if(Key&0x02)
        {
                if(RunMode==0x09)
                {
                        MusicIndex =(MusicIndex+MUSICNUMBER-1)%MUSICNUMBER;
                }
                else
                {
                        if(SystemSpeedIndex>0)
                        {
                                --SystemSpeedIndex;
                                SetSpeed(SystemSpeedIndex);
                        }
                        else
                        {
                                LEDFlash(6);
                        }
                 }
        }
        if(Key&0x04)
        {
                if(RunMode==0x09)
                {
                        MusicIndex =(MusicIndex+1)%MUSICNUMBER;
                }
                else
                {
                        if(SystemSpeedIndex<28)
                        {
                                ++SystemSpeedIndex;
                                SetSpeed(SystemSpeedIndex);
                        }
                        else
                        {
                                LEDFlash(6);
                        }
                   }
        }        
}
//*****************************Music******************************************************
//揮著翅膀的女孩
unsigned char code Music_Girl[]={ 0x17,0x02, 0x17,0x03, 0x18,0x03, 0x19,0x02, 0x15,0x03,
                                  0x16,0x03, 0x17,0x03, 0x17,0x03, 0x17,0x03, 0x18,0x03,
                                  0x19,0x02, 0x16,0x03, 0x17,0x03, 0x18,0x02, 0x18,0x03,
                                  0x17,0x03, 0x15,0x02, 0x18,0x03, 0x17,0x03, 0x18,0x02,
                                  0x10,0x03, 0x15,0x03, 0x16,0x02, 0x15,0x03, 0x16,0x03,
                                  0x17,0x02, 0x17,0x03, 0x18,0x03, 0x19,0x02, 0x1A,0x03,
                                  0x1B,0x03, 0x1F,0x03, 0x1F,0x03, 0x17,0x03, 0x18,0x03,
                                  0x19,0x02, 0x16,0x03, 0x17,0x03, 0x18,0x03, 0x17,0x03,
                                  0x18,0x03, 0x1F,0x03, 0x1F,0x02, 0x16,0x03, 0x17,0x03,
                                  0x18,0x03, 0x17,0x03, 0x18,0x03, 0x20,0x03, 0x20,0x02,
                                  0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x20,0x03, 0x21,0x03,
                                  0x20,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x1F,0x03,
                                  0x1B,0x03, 0x19,0x03, 0x19,0x03, 0x15,0x03, 0x1A,0x66,
                                  0x1A,0x03, 0x19,0x03, 0x15,0x03, 0x15,0x03, 0x17,0x03,
                                  0x16,0x66, 0x17,0x04, 0x18,0x04, 0x18,0x03, 0x19,0x03,
                                  0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x20,0x03, 0x21,0x03,
                                  0x20,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x1F,0x03,
                                  0x1B,0x03, 0x19,0x03, 0x19,0x03, 0x15,0x03, 0x1A,0x66,
                                  0x1A,0x03, 0x19,0x03, 0x19,0x03, 0x1F,0x03, 0x1B,0x03,
                                  0x1F,0x00, 0x1A,0x03, 0x1A,0x03, 0x1A,0x03, 0x1B,0x03,
                                  0x1B,0x03, 0x1A,0x03, 0x19,0x03, 0x19,0x02, 0x17,0x03,
                                  0x15,0x17, 0x15,0x03, 0x16,0x03, 0x17,0x03, 0x18,0x03,
                                  0x17,0x04, 0x18,0x0E, 0x18,0x03, 0x17,0x04, 0x18,0x0E,
                                  0x18,0x66, 0x17,0x03, 0x18,0x03, 0x17,0x03, 0x18,0x03,
                                  0x20,0x03, 0x20,0x02, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66,
                                  0x20,0x03, 0x21,0x03, 0x20,0x03, 0x1F,0x03, 0x1B,0x03,
                                  0x1F,0x66, 0x1F,0x04, 0x1B,0x0E, 0x1B,0x03, 0x19,0x03,
                                  0x19,0x03, 0x15,0x03, 0x1A,0x66, 0x1A,0x03, 0x19,0x03,
                                  0x15,0x03, 0x15,0x03, 0x17,0x03, 0x16,0x66, 0x17,0x04,
                                  0x18,0x04, 0x18,0x03, 0x19,0x03, 0x1F,0x03, 0x1B,0x03,
                                  0x1F,0x66, 0x20,0x03, 0x21,0x03, 0x20,0x03, 0x1F,0x03,
                                  0x1B,0x03, 0x1F,0x66, 0x1F,0x03, 0x1B,0x03, 0x19,0x03,
                                  0x19,0x03, 0x15,0x03, 0x1A,0x66, 0x1A,0x03, 0x19,0x03,
                                  0x19,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x00, 0x18,0x02,
                                  0x18,0x03, 0x1A,0x03, 0x19,0x0D, 0x15,0x03, 0x15,0x02,
                                  0x18,0x66, 0x16,0x02, 0x17,0x02, 0x15,0x00, 0x00,0x00};
//同一首歌
unsigned char code Music_Same[]={ 0x0F,0x01, 0x15,0x02, 0x16,0x02, 0x17,0x66, 0x18,0x03,
                                  0x17,0x02, 0x15,0x02, 0x16,0x01, 0x15,0x02, 0x10,0x02,
                                  0x15,0x00, 0x0F,0x01, 0x15,0x02, 0x16,0x02, 0x17,0x02,
                                  0x17,0x03, 0x18,0x03, 0x19,0x02, 0x15,0x02, 0x18,0x66,
                                  0x17,0x03, 0x19,0x02, 0x16,0x03, 0x17,0x03, 0x16,0x00,
                                  0x17,0x01, 0x19,0x02, 0x1B,0x02, 0x1B,0x70, 0x1A,0x03,
                                  0x1A,0x01, 0x19,0x02, 0x19,0x03, 0x1A,0x03, 0x1B,0x02,
                                  0x1A,0x0D, 0x19,0x03, 0x17,0x00, 0x18,0x66, 0x18,0x03,
                                  0x19,0x02, 0x1A,0x02, 0x19,0x0C, 0x18,0x0D, 0x17,0x03,
                                  0x16,0x01, 0x11,0x02, 0x11,0x03, 0x10,0x03, 0x0F,0x0C,
                                  0x10,0x02, 0x15,0x00, 0x1F,0x01, 0x1A,0x01, 0x18,0x66,
                                  0x19,0x03, 0x1A,0x01, 0x1B,0x02, 0x1B,0x03, 0x1B,0x03,
                                  0x1B,0x0C, 0x1A,0x0D, 0x19,0x03, 0x17,0x00, 0x1F,0x01,
                                  0x1A,0x01, 0x18,0x66, 0x19,0x03, 0x1A,0x01, 0x10,0x02,
                                  0x10,0x03, 0x10,0x03, 0x1A,0x0C, 0x18,0x0D, 0x17,0x03,
                                  0x16,0x00, 0x0F,0x01, 0x15,0x02, 0x16,0x02, 0x17,0x70,
                                  0x18,0x03, 0x17,0x02, 0x15,0x03, 0x15,0x03, 0x16,0x66,
                                  0x16,0x03, 0x16,0x02, 0x16,0x03, 0x15,0x03, 0x10,0x02,
                                  0x10,0x01, 0x11,0x01, 0x11,0x66, 0x10,0x03, 0x0F,0x0C,
                                  0x1A,0x02, 0x19,0x02, 0x16,0x03, 0x16,0x03, 0x18,0x66,
                                  0x18,0x03, 0x18,0x02, 0x17,0x03, 0x16,0x03, 0x19,0x00,
                                  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 };
//***********************************************************************************
unsigned char * SelectMusic(unsigned char SoundIndex)
{
        unsigned char *MusicAddress = 0;
        switch (SoundIndex)
        {        
                case 0x00:
                        MusicAddress = &Music_Girl[0];        //揮著翅膀的女孩               
                        break;
                case 0x01:
                        MusicAddress = &Music_Same[0];        //同一首歌               
                        break;
                case 0x02:
                        MusicAddress = &Music_Two[0];        //兩只蝴蝶  
                        break;
                case 0x03:
                        break;
                case 0x04:
                        break;
                case 0x05:
                        break;
                case 0x06:
                        break;
                case 0x07:
                        break;
                case 0x08:
                        break;
                case 0x09:
                        break;
                default:break;
        }
        return MusicAddress;
}

void PlayMusic(void)
{        
        Delay1ms(200);
        Play(SelectMusic(MusicIndex),0,3,360);
}
//***********************************************************************************
main()
{
        unsigned char Key;
        InitialCPU();
        InitialSound();
        InitialTimer2();

        while(1)
        {
                Key = GetKey();
                if(RunMode==0x09)
                {
                        PlayMusic();
                }
                if(Key!=0x00)
                {
                        KeyDispose(Key);
                }
        }
}


評(píng)分

參與人數(shù) 1黑幣 +15 收起 理由
shuisheng + 15 贊一個(gè)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏3 分享淘帖 頂1 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:378182 發(fā)表于 2018-12-18 09:11 | 只看該作者
"SoundPlay.h"在哪里可找到?
回復(fù)

使用道具 舉報(bào)

板凳
ID:21069 發(fā)表于 2018-12-18 12:23 | 只看該作者
漏傳了,在這里咯

attach]162328[/attach]

音樂(lè)與燈一起閃的程式.c及SoundPlay.h.zip

5.21 KB, 下載次數(shù): 43, 下載積分: 黑幣 -5

評(píng)分

參與人數(shù) 1黑幣 +60 收起 理由
admin + 60 回帖助人的獎(jiǎng)勵(lì)!

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

地板
ID:453330 發(fā)表于 2018-12-24 13:03 | 只看該作者
多謝樓主分享
回復(fù)

使用道具 舉報(bào)

5#
ID:472242 發(fā)表于 2019-2-3 21:14 | 只看該作者
不錯(cuò)不錯(cuò),找了好久了

評(píng)分

參與人數(shù) 2黑幣 +47 收起 理由
jinsheng7533967 + 35
站著說(shuō)話不腰疼 + 12

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

6#
ID:472325 發(fā)表于 2019-2-7 00:13 | 只看該作者
好樣的,分享偉大

評(píng)分

參與人數(shù) 2黑幣 +50 收起 理由
Sawardeakar + 35
#include51.h + 15

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

7#
ID:475975 發(fā)表于 2019-2-12 18:29 | 只看該作者
多謝樓主分享
回復(fù)

使用道具 舉報(bào)

8#
ID:471375 發(fā)表于 2019-2-12 22:01 來(lái)自觸屏版 | 只看該作者
可以可以,不錯(cuò)
回復(fù)

使用道具 舉報(bào)

9#
ID:847670 發(fā)表于 2020-11-23 10:28 | 只看該作者
有師兄能幫忙解釋一下嗎
回復(fù)

使用道具 舉報(bào)

10#
ID:847670 發(fā)表于 2020-11-23 10:30 | 只看該作者
有同學(xué)能解釋一下這個(gè)程序嗎,或者能分享一下SoundPlay.h嗎,新人黑幣不夠
回復(fù)

使用道具 舉報(bào)

11#
ID:847670 發(fā)表于 2020-11-23 18:34 | 只看該作者
這個(gè)程序仿真出來(lái)了嗎
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表