基于51单片机的音乐盒设计——Proteus仿真原理图与PCB制作指南

功能:
0.本系统采用单片机STC89C52作为系统的主控芯片
1.采用LCD1602液晶显示当前时间/播放曲目信息
2.按键设置时间,切换/暂停曲目
3.内置8首歌曲,本设计采用了优质的外放喇叭,声音清晰、洪亮
4.采用DC002作为电源接口可直接输入5V给整个系统供电

原理图:

PCB :

主程序:

//
//51单片机音乐播放器程序
//STC89C52+11.0592MHz+无源蜂鸣器
//共三个按键:上一曲、一下曲、播放\暂停
//注意:按键必须按下约1S才有相应动作,播放完一首歌曲无按键按下时顺序播放

//
#include <REG52.H>
#include <stdio.h>
#include "delay.h"
#include "lcd1602.h"
#include "ds1302.h"

#define MUSIC_AMOUNT 8 //歌曲的数量
sbit SPEAKER = P3^2;     //无源蜂鸣器
sbit KEY_PAUSE = P3^5;
sbit KEY_SELECT = P3^3;
sbit KEY_PREVIOUS = P3^4;
sbit KEY_NEXT = P3^6;

//
unsigned char g_beatTime = 0;    //T0RH,T0RL为定时器T0的高低位初值,g_beatTime为对应的节拍时间
unsigned char g_musicNum = 1;              //g_musicNum为歌曲编号,g_noteCnt是查找歌曲数据表的地址
unsigned int g_noteCnt;
signed int g_fre;                      //对应频率数据表的地址
bit g_playEn = 1;                          //歌曲播放的使能标志位,用于播放\暂停
unsigned char g_keyValue;
unsigned char dispRow0[] = "  /  /  ";
unsigned char dispRow1[] = "  :  ";
bit g_switchSong = 1;
bit g_refreshTime = 1;
bit g_setTime = 0;
bit g_playFlag = 0;
unsigned char g_setLocation = 0;

void SetTime();
void DispTime();
void KeyScan();	                           //按键处理
void DelayOfBeat(unsigned char t);	   //延时子函数,控制发音的时间长度
void PlayOneNote(void);		           //演奏一个音符
void PlayMusic(void);	               //播放歌曲
void DispMusic(void);
void Timer0_Init();
void Timer1_Init();
//
//每三个数字,代表一个音符
//第一个数字是音符的数值1234567之一(第几个音),代表哆来咪发...
//第二个数字是0123之一,代表低音\中音\高音\超高音(第几个八度)
//第三个数字是时间长度,以半拍为单位,乐曲数据表的结尾是三个0
//



//《烟花易冷》
unsigned char code song1[]={
    5,2,1, 3,2,1, 2,2,2, 2,2,4, 3,2,1, 1,2,1, 2,2,1, 3,2,4, 
    5,2,1, 3,2,1, 2,2,2, 2,2,2, 5,1,1, 3,2,1, 4,2,1, 3,2,4, 
    3,2,1, 3,2,1, 7,2,1, 3,2,1, 2,2,2, 1,2,1, 7,1,1, 1,2,1, 
    2,2,1, 3,2,1, 6,2,3, 6,1,1, 1,2,1, 3,2,1, 2,2,1, 6,1,1, 
    1,2,1, 7,1,1, 5,1,1, 6,1,6, 5,2,1, 3,2,1, 2,2,2, 2,2,1, 
    2,2,1, 3,2,1, 1,2,1, 2,2,1, 3,2,4, 5,2,1, 3,2,1, 2,2,2, 
    2,2,1, 2,2,1, 5,1,1, 3,2,1, 4,2,1, 3,2,4, 3,2,1, 3,2,1, 
    7,2,3, 3,2,1, 2,2,2, 1,2,1, 7,1,1, 1,2,1, 2,2,1, 3,2,1, 
    6,2,3, 6,1,1, 1,2,1, 3,2,1, 2,2,1, 6,1,1, 1,2,1, 7,1,2, 
    5,1,2, 6,1,6, 0,0,0 };

//《发如雪》
unsigned char code song2[]={
    5,1,1, 2,2,1, 3,2,2, 2,2,1, 3,2,1, 5,2,1, 6,2,1, 5,2,3, 1,2,1, 2,2,1, 
    3,2,1, 6,2,1, 5,2,1, 3,2,1, 5,2,3, 5,2,1, 6,2,1, 1,3,2, 6,2,1, 5,2,1, 
    3,2,1, 5,2,1, 3,2,2, 1,2,1, 2,2,1, 3,2,1, 1,2,1, 6,1,1, 3,2,1, 2,2,1, 
    5,1,1, 2,2,1, 3,2,2, 2,2,1, 3,2,1, 5,2,1, 6,2,1, 5,2,3, 1,2,1, 2,2,1, 
    3,2,1, 6,2,1, 5,2,3, 5,2,1, 6,2,1, 1,3,2, 6,2,1, 
    5,2,1, 3,2,1, 5,2,1, 3,2,2, 1,2,1, 6,1,1, 3,2,1, 2,2,1, 1,2,1, 6,1,1, 
    1,2,1, 2,2,1, 2,2,1, 1,2,4, 0,0,0 };

//《简单爱》
unsigned char code song3[]={
    5,1,1, 1,2,1, 2,2,1, 3,2,1, 2,2,1, 3,2,1, 4,2,1, 5,2,1, 5,2,1, 5,2,1, 
    4,2,1, 3,2,1, 2,2,3, 5,1,1, 1,2,1, 2,2,1, 3,2,1, 4,2,1, 5,2,1, 5,2,1, 
    5,2,1, 6,2,1, 5,2,2, 2,2,1, 3,2,1, 1,2,2, 1,2,1, 6,1,1, 2,2,1, 2,2,1, 
    3,2,1, 3,2,1, 1,2,1, 5,2,1, 1,2,1, 5,2,1, 1,2,1, 7,1,1, 1,2,1, 1,2,1, 6,1,1, 
    2,2,1, 2,2,1, 3,2,1, 3,2,1, 5,2,1, 5,2,1, 
    5,2,1, 6,2,1, 5,2,2, 2,2,1, 3,2,1, 1,2,2,  
    5,1,1, 1,2,1, 2,1,2, 3,2,1, 2,2,1, 3,2,1, 4,2,1, 5,2,1, 5,2,1, 5,2,1, 
    4,2,1, 3,2,1, 2,2,3, 5,1,1, 1,2,1, 2,2,1, 3,2,1, 2,2,1, 3,2,1, 4,2,1, 
    5,2,1, 5,2,1, 5,2,2, 2,2,1, 3,2,1, 1,2,2, 1,2,1, 6,1,1, 2,2,1, 2,2,1, 
    3,2,1, 3,2,1, 1,2,2, 5,2,1, 1,2,1, 5,2,1, 5,2,1, 7,1,1, 1,2,1, 0,0,0 };

//《世上只有妈妈好》
unsigned char code song4[]={
    6,2,3, 5,2,1, 3,2,2, 5,2,2, 1,3,2, 6,2,1,
     //6,2,3代表6,中音,3个半拍;
     //5,2,1代表5,中音,1个半拍;
     //3,2,2代表3,中音,2个半拍;
     //5,2,2代表5,中音,2个半拍;
     //1,3,2代表1,高音,2个半拍;
     //...
    5,2,1,6,2,4, 3,2,2, 5,2,1, 6,2,1, 5,2,2, 3,2,2, 1,2,1,6,1,1, 5,2,1, 
    3,2,1, 2,2,4, 2,2,3, 3,2,1, 5,2,2, 5,2,1, 6,2,1, 3,2,2, 2,2,2, 
    1,2,4, 5,2,3, 3,2,1,2,2,1, 1,2,1, 6,1,1, 1,2,1, 5,1,6, 0,0,0};

//《当你孤单你会想起谁》
unsigned char code song5[]={
    3,2,2, 3,2,1, 4,2,1, 3,2,2, 2,2,1, 1,2,1,  2,2,2, 5,2,2, 2,2,2, 2,2,3,
    1,2,2, 1,2,1, 2,2,1, 1,2,2, 7,1,1, 6,1,1, 7,1,2, 3,2,2, 7,1,2, 7,1,3, 
    6,1,2, 2,2,1, 3,2,1, 2,2,1, 1,2,1, 6,1,2, 5,1,2, 2,2,1, 3,2,1, 2,2,1, 
    1,2,1, 6,1,2, 6,1,2, 2,2,1, 3,2,1, 2,2,1, 1,2,1, 6,1,1, 7,1,1, 1,2,6, 0,0,0 };

//《乡间小路》
unsigned char code song6[]={
    3,1,2, 3,1,1, 3,1,1, 6,0,1, 6,0,1, 1,1,2,
    6,0,1, 5,0,1, 6,0,4, 6,0,2, 6,0,1 ,6,0,1, 6,0,2, 6,0,1, 1,1,1, 
    2,1,2, 2,1,1, 3,1,1, 2,1,4, 3,1,1, 3,1,1, 3,1,1, 2,1,1, 4,1,2,
    4,1,2, 3,1,2, 2,1,1, 1,1,1, 2,1,4, 7,0,1, 7,0,1, 7,0,1, 6,0,1, 
    5,0,1, 5,0,1, 6,0,1, 7,0,1, 7,0,1, 6,0,1, 5,0,1, 6,0,4, 6,1,2,
    3,1,1, 6,1,1, 7,1,1, 6,1,1, 5,1,1, 5,1,2, 5,1,1, 2,1,1, 5,1,1, 
    6,1,1, 5,1,1, 4,1,1, 4,1,2, 4,1,1, 3,1,1, 2,1,2, 1,1,1, 2,1,1, 
    3,1,1, 2,1,1, 1,1,1, 2,1,1, 3,1,4, 6,1,2, 3,1,1, 6,1,1, 7,1,1, 
    6,1,1, 5,1,1, 5,1,2, 2,1,1, 5,1,1, 6,1,1, 5,1,1, 4,1,1, 0,0,0};     

//《送别》
unsigned char code song7[]={
    5,1,2, 3,1,3, 5,1,1, 1,2,3, 6,1,2, 1,2,2, 
    5,1,4, 5,1,2, 1,1,1, 2,1,1, 3,1,2, 2,1,1, 1,1,1, 2,1,4, 5,1,2, 
    3,1,1, 5,1,1, 1,2,2, 7,1,1, 6,1,2, 1,2,2, 5,1,4, 5,1,2, 2,1,1, 
    3,1,1, 4,1,2, 7,0,1, 1,1,4, 6,1,2, 1,2,2, 1,2,4, 7,1,2, 6,1,1,
    7,1,1, 1,2,4, 6,1,1, 7,1,1, 1,2,1, 6,1,1, 6,1,1, 5,1,1, 3,1,1, 
    1,1,1, 2,1,8, 5,1,2, 3,1,1, 5,1,1, 1,2,2, 7,1,1, 6,1,2, 1,2,2, 
    5,1,4, 5,1,2, 2,1,1, 3,1,1, 4,1,2, 7,0,2, 1,1,4, 0,0,0};

//《最浪漫的事》
unsigned char code song8[]={
    5,1,1, 6,1,1, 1,2,1, 6,1,2, 6,1,1, 5,1,1, 6,1,1, 5,1,1, 3,1,1, 5,1,5, 
    5,1,1, 6,1,1, 1,2,1, 6,1,2, 6,1,1, 5,1,1, 6,1,1, 5,1,1, 6,1,1, 1,1,5, 
    1,1,1, 2,1,1, 3,1,1, 2,1,1, 2,1,1, 2,1,1, 1,1,1, 2,1,1, 1,1,1, 6,1,1, 
    3,1,2, 2,1,3, 5,1,1, 6,1,1, 1,2,1, 6,1,2, 6,1,1, 5,1,1, 6,1,1, 5,1,1, 
    6,1,1, 1,1,5, 1,1,1, 2,1,1, 3,1,1, 4,1,2, 4,1,1, 5,1,1, 6,1,1, 6,1,1, 
    5,1,1, 6,1,2, 1,2,1, 6,1,3, 1,2,1, 6,1,1, 5,1,1, 5,1,4, 1,1,1, 6,1,1, 
    5,1,5, 5,1,1, 6,1,1, 1,2,1, 3,1,1, 2,1,1, 3,1,1, 1,1,6, 0,0,0}; 

// 频率-半周期数据表 高八位  共保存了四个八度的28个频率数据
unsigned char code FREQH[]={
    0xF2, 0xF3, 0xF5, 0xF5, 0xF6, 0xF7, 0xF8,    //低音1234567
    0xF9, 0xF9, 0xFA, 0xFA, 0xFB, 0xFB, 0xFC, 0xFC,//1,2,3,4,5,6,7,i
    0xFC, 0xFD, 0xFD, 0xFD, 0xFD, 0xFE,            //高音 234567
    0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF};   //超高音 1234567

// 频率-半周期数据表 低八位
unsigned char code FREQL[]={
    0x42, 0xC1, 0x17, 0xB6, 0xD0, 0xD1, 0xB6,    //低音1234567
    0x21, 0xE1, 0x8C, 0xD8, 0x68, 0xE9, 0x5B, 0x8F, //1,2,3,4,5,6,7,i
    0xEE, 0x44, 0x6B, 0xB4, 0xF4, 0x2D,             //高音 234567
    0x47, 0x77, 0xA2, 0xB6, 0xDA, 0xFA, 0x16};   //超高音 1234567

void main(void)
{

    Timer0_Init();
    Timer1_Init();
    DS1302_Init();
    // DS1302_Write_Time();
    LcdReset();
    DelayMs(10);
    LcdClear();
    DispStr(0, 0, "    MusicBox    ");
    DelaySecond(2);
    LcdClear();

    while (1)
    {

        if (g_setTime == 1)
        {
            SetTime();
        }
        else
        {
            DS1302_Read_Time();
            DispMusic();
        }

        if (g_playFlag == 1)
        {
            PlayMusic(); //根据当前状态播放相应歌曲的某个音符
        }



        if (g_refreshTime == 1) //每200ms刷新一次屏幕
        {
            DispTime();
            g_refreshTime = 0;
        }

        KeyScan();
    }
}

void DelayOfBeat(unsigned char t) //延时子函数,控制发音的时间长度,每个节拍0.4S
{
    unsigned char t1;
    unsigned long t2;
    for (t1 = 0; t1 < t; t1++) //嵌套循环, 共延时t个半拍
    {
        for (t2 = 0; t2 < 8000; t2++) //延时期间, 可进入T0中断去发音
        {
            ;
        }
    }
    TR0 = 0; //关闭T0, 停止发音
}

void PlayOneNote(void) //演奏一个音符
{
    TH0 = FREQH[g_fre];      //下次的中断时间,这个时间控制音调高低
    TL0 = FREQL[g_fre];
    TR0 = 1;     //启动T0, 由T0输出方波发音
    DelayOfBeat(g_beatTime); //每个音符的演奏时间
}

void PlayMusic(void) //播放相应歌曲的某个音符
{

    if ((g_musicNum == 1) && (g_playEn == 1))
    {
        g_fre = song1[g_noteCnt] + 7 * song1[g_noteCnt + 1] - 1; //第i个是音符,第i+1个是第几个八度
        g_beatTime = song1[g_noteCnt + 2]; //读出时间长度数值
        g_noteCnt += 3;
        if (g_fre < 0) //判断歌曲的结束位,结束后转到下一首
        {
            g_noteCnt = 0; //下一首从头播放
            g_musicNum = 2;
        }
        PlayOneNote(); //发出一个音符
        
    }
    
    if ((g_musicNum == 2) && (g_playEn == 1))
    {
        g_fre = song2[g_noteCnt] + 7 * song2[g_noteCnt + 1] - 1;
        g_beatTime = song2[g_noteCnt + 2];
        g_noteCnt += 3;
        if (g_fre < 0)
        {
            g_noteCnt = 0;
            g_musicNum = 3;
        }
        PlayOneNote();
    }
    
    if ((g_musicNum == 3) && (g_playEn == 1))
    {
        g_fre = song3[g_noteCnt] + 7 * song3[g_noteCnt + 1] - 1;
        g_beatTime = song3[g_noteCnt + 2];
        g_noteCnt += 3;
        if (g_fre < 0)
        {
            g_noteCnt = 0;
            g_musicNum = 4;
        }
        PlayOneNote();
    }
    
    if ((g_musicNum == 4) && (g_playEn == 1))
    {
        g_fre = song4[g_noteCnt] + 7 * song4[g_noteCnt + 1] - 1;
        g_beatTime = song4[g_noteCnt + 2];
        g_noteCnt += 3;
        if (g_fre < 0)
        {
            g_noteCnt = 0;
            g_musicNum = 5;
        }
        PlayOneNote();
    }
    
    if ((g_musicNum == 5) && (g_playEn == 1))
    {
        g_fre = song5[g_noteCnt] + 7 * song5[g_noteCnt + 1] - 1;
        g_beatTime = song5[g_noteCnt + 2];
        g_noteCnt += 3;
        if (g_fre < 0)
        {
            g_noteCnt = 0;
            g_musicNum = 6;
        }
        PlayOneNote();
    }
    
    if ((g_musicNum == 6) && (g_playEn == 1))
    {
        g_fre = song6[g_noteCnt] + 7 * song6[g_noteCnt + 1] - 1;
        g_beatTime = song6[g_noteCnt + 2];
        g_noteCnt += 3;
        if (g_fre < 0)
        {
            g_noteCnt = 0;
            g_musicNum = 7;
        }
        PlayOneNote();
    }
    
    if ((g_musicNum == 7) && (g_playEn == 1))
    {
        g_fre = song7[g_noteCnt] + 7 * song7[g_noteCnt + 1] - 1;
        g_beatTime = song7[g_noteCnt + 2];
        g_noteCnt += 3;
        if (g_fre < 0)
        {
            g_noteCnt = 0;
            g_musicNum = 8;
        }
        PlayOneNote();
    }
    
    if ((g_musicNum == 8) && (g_playEn == 1))
    {
        g_fre = song8[g_noteCnt] + 7 * song8[g_noteCnt + 1] - 1;
        g_beatTime = song8[g_noteCnt + 2];
        g_noteCnt += 3;
        if (g_fre < 0)
        {
            g_noteCnt = 0;
            g_musicNum = 1;
        }
        PlayOneNote();
    }
    
}

void KeyScan() //按键程序
{
    /*********************歌曲播放*********************/
    if (!KEY_SELECT)
    {
        DelayMs(10); //按键去抖动
        if (!KEY_SELECT)
        {
            g_playFlag = 0;
            g_setTime = 1;
            g_setLocation++;
        }

        while (!KEY_SELECT)
            ;
    }

    if (!KEY_PAUSE)
    {
        DelayMs(5);
        if (!KEY_PAUSE)
        {
            g_playFlag = ~g_playFlag;
        }
        while (!KEY_PAUSE)
            ;
    }

    if (!KEY_PREVIOUS)
    {
        DelayMs(5);
        if (!KEY_PREVIOUS)
        {
            if (g_setTime == 1)
            {
                switch (g_setLocation)
                {
                case 1:
                {
                    if (timeBufDec[1] == 0)
                    {
                        timeBufDec[1] = 100;
                    }
                    timeBufDec[1]--;
                    break;
                }
                case 2:
                {
                    timeBufDec[2]--;
                    if (timeBufDec[2] < 1)
                    {
                        timeBufDec[2] = 12;
                    }
                    break;
                }
                case 3:
                {
                    timeBufDec[3]--;
                    if (timeBufDec[3] < 1)
                    {
                        timeBufDec[3] = YDay(timeBufDec[1], timeBufDec[2]);
                    }
                    break;
                }
                case 4:
                {
                    if (timeBufDec[4] == 0)
                    {
                        timeBufDec[4] = 24;
                    }
                    timeBufDec[4]--;
                    break;
                }
                case 5:
                {
                    if (timeBufDec[5] == 0)
                    {
                        timeBufDec[5] = 60;
                    }
                    timeBufDec[5]--;
                    break;
                }
                case 6:
                {
                    timeBufDec[7]--;
                    if (timeBufDec[7] < 1)
                    {
                        timeBufDec[7] = 7;
                    }
                    break;
                }
                default:;
                }
            }
            else
            {
                g_musicNum--;
                g_noteCnt = 0;
                if (g_musicNum < 1)
                {
                    g_musicNum = MUSIC_AMOUNT;
                    TR0 = 0;
                }
            }
            while (!KEY_PREVIOUS)
                ;
        }
    }

仿真演示视频:
https://www.bilibili.com/video/BV1he4y1u7Ux/

实物演示视频:
https://www.bilibili.com/video/BV1cR4y1C7Sf/

物联沃分享整理
物联沃-IOTWORD物联网 » 基于51单片机的音乐盒设计——Proteus仿真原理图与PCB制作指南

发表评论