基于51單片機的 點陣+音樂=祝你生日快樂
//-----------------------------------------------------
//12*12點陣+音樂產(chǎn)生>>
// MCU:89C51 晶振:12M
//編寫日期:2010。12。25
//編寫人:李春起
#include REG51.H>
#define uchar unsigned char
#define uint unsigned int
uint a=0,a1=0,a2=0;
uint miusfla,miust,mius=64536;
uint disfla1,sta=0;
char flag=0,mdflag=0;
sbit k1=P3^2;
sbit k2=P3^3;
sbit YY=P3^1;
uchar code tabfas[]={
//==============笑臉===================================
//P0口
0xFF,0xFF,0xFF,0xFF,0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F,
0xFF,0xFF,0xFF,0xFF,0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F,
//P1口
0x8E,0x4D,0xAB,0x57,0x5F,0x9F,0x9F,0x5F,0x5F,0xAF,0x4F,0x8F,
0x8E,0x4D,0xAB,0x97,0x9F,0x9F,0x9F,0x9F,0x9F,0xAF,0x4F,0x8F,
//P2口
0x87,0x88,0x50,0x20,0xA4,0x68,0x68,0xA4,0x20,0x50,0x88,0x87,
0x87,0x88,0x50,0x20,0xA4,0x68,0x68,0xA4,0x20,0x50,0x88,0x87,
//P0口
0xFF,0xFF,0xFF,0xFF,0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F,
0xFF,0xFF,0xFF,0xFF,0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F,
0xFF,0xFF,0xFF,0xFF,0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F,
0xFF,0xFF,0xFF,0xFF,0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F,
0xFF,0xFF,0xFF,0xFF,0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F,
0xFF,0xFF,0xFF,0xFF,0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F,
0xFF,0xFF,0xFF,0xFF,0xFE,0xFD,0xFB,0xF7,0xEF,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B,0x47,0x7F,0x4F,0xCF,0x4F,0x4F,0x4F,0x4F,0x0F, //年
0x0E,0x0D,0xEB,0x07,0x8F,0x8F,0xEF,0x8F,0x8F,0x8F,0x0F,0x0F, //快
0x0E,0x8D,0x4B,0x47,0x4F,0xAF,0x1F,0x0F,0x0F,0x0F,0x0F,0x0F, //樂
0xCE,0x2D,0x2B,0x27,0x4F,0x8F,0x8F,0x4F,0x2F,0x2F,0x2F,0xCF, //心形
0x0F,0x0F,0x2B,0x27,0xAF,0x6F,0xFF,0x6F,0xAF,0x2F,0x2F,0x0F, //李
0x0E,0x0D,0x2B,0xA7,0xAF,0xFF,0xAF,0xAF,0x2F,0x0F,0x0F,0x0F, //春
0x0E,0x0D,0x4B,0x47,0xEF,0x4F,0x4F,0x8F,0x8F,0x8F,0x0F,0x0F, //起
//0x0E,0xFD,0x5B,0x57,0x5F,0xFF,0x0F,0x4F,0x4F,0xCF,0x4F,0x0F, //題
0xCE,0x2D,0x2B,0x27,0x4F,0x8F,0x8F,0x4F,0x2F,0x2F,0x2F,0xCF, //心形
//P2口
0x08,0x04,0xFE,0x05,0x08,0x43,0x3E,0x02,0x3E,0x23,0x30,0x00, //祝
0x00,0x09,0x0A,0x7F,0x2A,0x08,0x0F,0x09,0x7F,0x09,0x0F,0x00, //鐘
0x00,0x02,0x01,0x7F,0x02,0x22,0x22,0x3F,0x22,0x22,0x02,0x00, //仕
0xc0,0x3f,0x88,0x6a,0x2a,0xff,0x2a,0xff,0x2a,0x6a,0xde,0x88,//廉
0x00,0x21,0x20,0x20,0x24,0x24,0x3F,0x24,0x24,0x20,0x20,0x00, //生
0x00,0x00,0x00,0x3F,0x22,0x22,0x22,0x22,0x3F,0x00,0x00,0x00, //日
//0x00,0x2A,0x5A,0xFF,0x1A,0x2A,0x40,0x3F,0x02,0x7E,0x02,0x00, //新
//0x08,0x09,0x0F,0x09,0x09,0x09,0xFF,0x09,0x09,0x09,0x08,0x00, //看
0x01,0x06,0x7F,0x22,0x14,0x0C,0x07,0x0C,0x14,0x27,0x24,0x00, //快
0x00,0x23,0x12,0x0E,0x22,0x7F,0x06,0x0A,0x13,0x22,0x00,0x00, //樂
0x03,0x04,0x08,0x10,0x20,0x60,0x20,0x10,0x08,0x04,0x02,0x01, //心形
0x00,0x00,0x11,0x11,0x12,0x52,0xFB,0x1A,0x16,0x11,0x11,0x00, //李
0x00,0x12,0x0A,0xFE,0xAB,0xAA,0xAA,0xFE,0x0A,0x12,0x00,0x00, //春
0x40,0x21,0x1D,0x09,0x1F,0x25,0x41,0x5D,0x54,0x53,0x58,0x00, //起
//0x84,0x45,0x35,0x3D,0x55,0x95,0x84,0xAF,0x9D,0x91,0xAF,0x80, //題
0x03,0x04,0x08,0x10,0x20,0x60,0x20,0x10,0x08,0x04,0x02,0x01, //心形
};
//----------------音符----------------------
uint code miusdata[]={
//祝你生日快樂
64260,64260,64399,64260,64579,64524,64260,64260,
64399,64260,64684,64580,64260,64260,64898,64777,
64580,64524,64399,64819,64819,64777,64580,64777,
64684,0,
64260,64260,64399,64260,64579,64524,64260,64260,
64399,64260,64684,64580,64260,64260,64898,64777,
64580,64524,64399,64819,64819,64777,64580,64684,
64580,0,
64580,64580,64684,64580,64819,64777,64580,64580,
64684,64580,64898,64819,64580,64580,65058,64968,
64819,64777,64684,65000,65000,64960,64819,64967,
64898,0,
64580,64580,64684,64580,64819,64777,64580,64580,
64684,64580,64898,64819,64580,64580,65058,64968,
64819,64777,64684,65000,65000,64960,64819,64898,
64819,0
};
//---------------節(jié)拍-----------------------
uchar code miustime[]={
40,40,80,80,80,160,40,40,80,80,80,160,40,40,
80,80,80,80,80,40,40,80,80,80,160,80,
40,40,80,80,80,160,40,40,80,80,80,160,40,40,
80,80,80,80,80,40,40,80,80,80,160,80,
40,40,80,80,80,160,40,40,80,80,80,160,40,40,
70,75,80,80,80,40,40,80,80,80,160,80,
40,40,80,80,80,160,40,40,80,80,80,160,40,40,
70,75,80,80,80,40,40,80,80,80,160,80,
};
void delay500();
//==========按鍵==========================================
void kk()
{
if(!k1){delay500();if(++flag>1)flag=0;else {a=72;a2=0;}sta=1;}
if(!k2){delay500();if(++mdflag>1)mdflag=0;sta=1;}
}
//=========延時===========================================
void delay(uint m)
{
uint i,j;
for(i=0;im;i++)
{
for(j=0;j300;j++)
{kk();if(sta){sta=0;goto abc;}}
}
abc:;
}
//------按鍵消拉動用----------------
void delay500()
{
uint i1;
P0=0XFF;
P1=0X0F;
P2=0X00;
for(i1=0;i132000;i1++);
}
//=========定時器T0用于頻率產(chǎn)生和顯示======================
void time0() interrupt 1
{
P0=0XFF;
P1=0X0F;
P2=0X00;
TH0=mius>>8;
TL0=mius0xff;
YY=~YY;
switch(flag)
{
case 0:{ //笑臉
P0=tabfas[a];
P1=tabfas[a+24];
P2=tabfas[a+48];
if(++disfla1700){if(++a>11)a=0;}
if(disfla1>=700){if(++a>23)a=11;}
if(disfla1>800){a=0;disfla1=0;}
}break;
case 1:{ //漢字
P0=tabfas[a];
P1=tabfas[a+192];
P2=tabfas[a+348];
if(++disfla1500){if(++a>(a2+83))a=(a2+72);}
else{if(++a>(a2+83))a=(a2+72);}
if(disfla1>1000){a2+=12;disfla1=0;}
if(a2>144){a2=0;}
}break;
default:break;
}
}
//========定時器T1只用于顯示================================
void time1() interrupt 3
{
P0=0XFF;
P1=0X0F;
P2=0X00;
TH1=0xf8;
TL1=0x30;
switch(flag)
{
case 0:{ //笑臉
P0=tabfas[a];
P1=tabfas[a+24];
P2=tabfas[a+48];
if(++disfla1700){if(++a>11)a=0;}
if(disfla1>=700){if(++a>23)a=11;}
if(disfla1>800){a=0;disfla1=0;}
}break;
case 1:{ //漢字
P0=tabfas[a];
P1=tabfas[a+192];
P2=tabfas[a+348];
if(++disfla1500){if(++a>(a2+83))a=(a2+72);}
else{if(++a>(a2+83))a=(a2+72);}
if(disfla1>1000){a2+=12;disfla1=0;}
if(a2>144){a2=0;}
}break;
default:break;
}
}
/////////////////////////////////////////////////////////////
//=========================================================//
//----------------主程序-----------------------------------//
//=========================================================//
/////////////////////////////////////////////////////////////
void main()
{
P0=0XFF;
P1=0X0F;
P2=0X00;
P3=0XFD;
TMOD=0X11;
TH0=mius>>8;
TL0=mius0xff;
TH1=0xf8;
TL1=0x30;
TR0=1;
ET0=1;
TR1=0;
ET1=1;
EA=1;
while(1)
{
switch(mdflag)
{
case 0:{//音樂暫停
TR0=0;TR1=1;kk();sta=0;
}break;
case 1:{ //音樂繼續(xù)
TR0=0;
delay(1);
TR0=1;
mius=miusdata[a1];
if(mius==0){TR0=0;TR1=1;}
else {TR1=0;TR0=1;}
delay(miustime[a1]);
if(++a1>=104)a1=0;
}break;
default:break;
}
}
}
評論