查看: 1521|回复: 0
打印 上一主题 下一主题

利用定时器产生乐曲

[复制链接]
跳转到指定楼层
沙发
发表于 2015-4-14 21:22:43 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include<reg52.h>                 //库文件
#define uchar unsigned char//宏定义无符号字符型
#define uint unsigned int  //宏定义无符号整型
/********************************************************************
                            初始定义
*********************************************************************/
uchar Count;
sbit _Speak =P3^7 ; //讯响器控制脚
/********************************************************************
                            歌曲字根码
*********************************************************************/
unsigned char code SONG[] ={ //祝你平安
0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80,0x26,0x20,0x30,0x20,
0x30,0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x20,
0x20,0x80,0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x20,
0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x20,
0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10,
0x2b,0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x20,
0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x20,
0x20,0x20,0x20,0x40,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20,
0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c,0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10,
0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26,0x10,0x26,0x10,0x2b,0x10,0x2b,0x80,
0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,0x10,0x2b,0x20,0x30,0x20,
0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30,0x10,0x30,0x20,
0x39,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,0x10,
0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x00,
//路边的野华不要采
0x30,0x1C,0x10,0x20,0x40,0x1C,0x10,0x18,0x10,0x20,0x10,0x1C,0x10,0x18,0x40,0x1C,
0x20,0x20,0x20,0x1C,0x20,0x18,0x20,0x20,0x80,0xFF,0x20,0x30,0x1C,0x10,0x18,0x20,
0x15,0x20,0x1C,0x20,0x20,0x20,0x26,0x40,0x20,0x20,0x2B,0x20,0x26,0x20,0x20,0x20,
0x30,0x80,0xFF,0x20,0x20,0x1C,0x10,0x18,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,
0x20,0x2B,0x40,0x20,0x20,0x1C,0x10,0x18,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,
0x20,0x2B,0x40,0x20,0x30,0x1C,0x10,0x18,0x20,0x15,0x20,0x1C,0x20,0x20,0x20,0x26,
0x40,0x20,0x20,0x2B,0x20,0x26,0x20,0x20,0x20,0x30,0x80,0x20,0x30,0x1C,0x10,0x20,
0x10,0x1C,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,0x20,0x2B,0x40,0x20,0x15,0x1F,
0x05,0x20,0x10,0x1C,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,0x20,0x2B,0x40,0x20,
0x30,0x1C,0x10,0x18,0x20,0x15,0x20,0x1C,0x20,0x20,0x20,0x26,0x40,0x20,0x20,0x2B,
0x20,0x26,0x20,0x20,0x20,0x30,0x30,0x20,0x30,0x1C,0x10,0x18,0x40,0x1C,0x20,0x20,
0x20,0x26,0x40,0x13,0x60,0x18,0x20,0x15,0x40,0x13,0x40,0x18,0x80,0x00,};
/********************************************************************
                            定时中断服务函数
*********************************************************************/
void Time0_Init()
{
        TMOD = 0x01;
        IE = 0x82;
        TH0 = 0xD8;
        TL0 = 0xEF; //12MZ晶振,10ms
}
/*********************************************************************/
void Time0_Int() interrupt 1
{
        TH0 = 0xD8;
        TL0 = 0xEF;                     
        Count++; //长度加1
}
/*********************************************************************/
void Delay_xMs(unsigned int x) //1MS 延时子程序
{
        unsigned int i,j;
        for( i =0;i < x;i++ )
        {
                for( j =0;j<3;j++ );
        }
}
/*********************************************************************/
void Play_Song(unsigned char i) //:歌曲播放子程序i 为播放哪一段曲目
{
        unsigned char Temp1,Temp2;
        unsigned int Addr;
        Count = 0; //中断计数器清0
        Addr = i * 217;
while(1)
        {
                Temp1 = SONG[Addr++];
                if ( Temp1 == 0xFF ) //休止符
                        {
                                TR0 = 0; Delay_xMs(100);
                        }
        else if ( Temp1 == 0x00 ) //歌曲结束符
                {
                        return;
                }
else
{
        Temp2 = SONG[Addr++];
        TR0 = 1;
        while(1)
        {
        _Speak = ~_Speak;
        Delay_xMs(Temp1);
        if ( Temp2 == Count )
                {
                        Count = 0;
                        break;
                }
        }
}
}
}
/********************************************************************
                            主函数
*********************************************************************/
void main() //主程序
{
        Time0_Init(); //定时器0 中断初始化
        while(1)
        {
                Play_Song(0);
        } //播放
}

/********************************************************************
                              结束
*********************************************************************/





本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?加入中科因仑

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入中科因仑

本版积分规则

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