#include <REG52.H>
#include <INTRINS.H>
int y=0,zhan,s;
/*
程序默認IO連接方式:
*/
sbit sce = P0^0; //片選
sbit res = P0^1; //復位,0復位
sbit dc = P0^2; //1寫數據,0寫指令
sbit sdin = P1^4; //數據
sbit sclk = P1^7; //時鐘
sbit Beep = P1^5 ;
sbit anjian0=P3^2;
sbit anjian1=P3^3;
sbit deng=P2^7;
unsigned char n=0,receive,o; //n為節拍常數變量
//八月桂花
unsigned char code music_tab0[] ={
0x18, 0x30, 0x1C , 0x10, //格式為: 頻率常數, 節拍常數, 頻率常數, 節拍常數, 48
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
};
//天空之城
unsigned char code music_tab1[] ={
0x1c, 0x30, 0x19, 0x30, 0x18, 0x40, 0x19, 0x30,
0x18, 0x30, 0x13, 0x30, 0x19, 0x18, 0x26, 0x30,
0x26, 0x30, 0x1c, 0x40, 0x20, 0x30, 0x1c, 0x30,
0x18, 0x30, 0x20, 0x60, 0x26, 0x30, 0x26, 0x30,
0x24, 0x40, 0x26, 0x30, 0x24, 0x30, 0x18, 0x40,
0x26, 0x60, 0x18, 0x20, 0x18, 0x20, 0x18, 0x20,
0x19, 0x40, 0x24, 0x30, 0x24, 0x30, 0x19, 0x30,
0x19, 0x60, 0x1c, 0x30, 0x19, 0x30, 0x18, 0x40,
0x19, 0x30, 0x18, 0x30, 0x13, 0x30, 0x19, 0x60,
0x26, 0x30, 0x26, 0x30, 0x1c, 0x40, 0x20, 0x30,
0x1c, 0x30, 0x18, 0x30, 0x20, 0x80, 0x26, 0x30,
0x24, 0x30, 0x24, 0x30, 0x18, 0x30, 0x19, 0x20,
0x19, 0x30, 0x18, 0x30, 0x15, 0x20, 0x15, 0x20,
0x13, 0x20, 0x18, 0x40, 0x18, 0x30, 0x19, 0x30,
0x1c, 0x20, 0x1c, 0x20, 0x19, 0x30, 0x20, 0x30,
0x1c, 0x60, 0x18, 0x30, 0x15, 0x30, 0x13, 0x40,
0x15, 0x30, 0x13, 0x30, 0x10, 0x30, 0x15, 0x60,
0x20, 0x30, 0x20, 0x30, 0x18, 0x30, 0x19, 0x30,
0x18, 0x30, 0x13, 0x30, 0x13, 0x60, 0x1c, 0x20,
0x19, 0x20, 0x18, 0x30, 0x19, 0x30, 0x15, 0x20,
0x15, 0x20, 0x18, 0x40, 0x20, 0x30, 0x20, 0x30,
0x12, 0x30, 0x13, 0x30, 0x15, 0x30, 0x18, 0x30,
0x13, 0xc0, 0x13, 0x80, 0x13, 0x30, 0x0e, 0x80,
0x10, 0x30, 0x10, 0x30, 0x13, 0x20, 0x15, 0x20,
0x18, 0x40, 0x18, 0x30, 0x15, 0x30, 0x18, 0x20,
0x15, 0x20, 0x15, 0x30, 0x10, 0x30, 0x13, 0x60,
0x13, 0x30, 0x0e, 0x60, 0x10, 0x60, 0x13, 0x20,
0x15, 0x20, 0x18, 0x40, 0x18, 0x30, 0x15, 0x30,
0x15, 0x20, 0x18, 0x20, 0x15, 0x30, 0x19, 0x30,
0x1c, 0x60, 0x1c, 0x30, 0x19, 0x30, 0x1c, 0xc0,
0x00,
};
//吉他
unsigned char code music_tab2[] ={
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, 0x00,
};
//6*16字符
unsigned char code shuzi[]={
/*-- 文字: G --*/
/*-- 宋體9; 此字體下對應的點陣為:寬x高=6x12 --*/
/*-- 高度不是8的倍數,現調整為:寬度x高度=6x16 --*/
0xF0,0x08,0x04,0x44,0xCC,0x40,0x00,0x01,0x02,0x02,0x01,0x00
};
unsigned char code yi[][24]=
{
//八
0x00,0x00,0x00,0xFE,0x00,0x00,0x00,0x7F,0x80,0x00,0x00,0x00,0x08,0x04,0x03,0x00,
0x00,0x00,0x00,0x00,0x01,0x06,0x08,0x00,
//月
0x00,0x00,0xFE,0x92,0x92,0x92,0x92,0x92,0x92,0xFE,0x00,0x00,0x08,0x06,0x01,0x00,
0x00,0x00,0x00,0x08,0x08,0x0F,0x00,0x00,
//桂
0x88,0x68,0xFF,0x48,0x20,0x24,0x24,0xBF,0x24,0x24,0x20,0x00,0x00,0x00,0x0F,0x00,
0x08,0x09,0x09,0x0F,0x09,0x09,0x08,0x00,
//花
0x82,0x42,0xE2,0x17,0x02,0x02,0xF2,0x87,0x42,0x22,0x02,0x00,0x00,0x00,0x0F,0x00,
0x04,0x02,0x07,0x08,0x08,0x08,0x0E,0x00,
};
/*-- 文字: 單 --*/
/*-- 宋體9; 此字體下對應的點陣為:寬x高=12x12 --*/
/*-- 高度不是8的倍數,現調整為:寬度x高度=12x16 --*/
unsigned char code er[][24]=
{
//天
0x20,0x22,0x22,0x22,0xA2,0x7E,0xA2,0x22,0x22,0x22,0x20,0x00,0x08,0x08,0x04,0x02,
0x01,0x00,0x01,0x02,0x04,0x08,0x08,0x00,
//空
0x0C,0xA4,0x94,0x8C,0x85,0x86,0x84,0x8C,0x94,0xA4,0x0C,0x00,0x08,0x08,0x08,0x08,
0x08,0x0F,0x08,0x08,0x08,0x08,0x08,0x00,
//之
0x00,0x08,0x08,0x08,0x09,0x8A,0x48,0x28,0x18,0x08,0x00,0x00,0x00,0x08,0x04,0x02,
0x05,0x08,0x08,0x08,0x08,0x08,0x08,0x00,
//城
0x08,0xFF,0x08,0x00,0xFC,0x24,0xE4,0x04,0xFF,0x84,0x65,0x00,0x02,0x03,0x09,0x06,
0x01,0x02,0x0B,0x04,0x03,0x04,0x0E,0x00,
};
unsigned char code shan[][24]=
{
//吉
0x04,0xA4,0xA4,0xA4,0xA4,0xBF,0xA4,0xA4,0xA4,0xA4,0x04,0x00,0x00,0x0F,0x04,0x04,
0x04,0x04,0x04,0x04,0x04,0x0F,0x00,0x00,
//它
0x1C,0x04,0xF4,0x84,0x85,0x46,0x44,0x24,0x24,0x04,0x1C,0x00,0x00,0x00,0x07,0x08,
0x08,0x08,0x08,0x08,0x08,0x0E,0x00,0x00,
//掃
0x88,0x88,0xFF,0x48,0x48,0x02,0x22,0x22,0x22,0x22,0xFE,0x00,0x00,0x08,0x0F,0x00,
0x00,0x04,0x04,0x04,0x04,0x04,0x0F,0x00,
//弦
0xF2,0x92,0x9E,0x00,0x44,0x64,0x55,0xCE,0x44,0x24,0x04,0x00,0x08,0x08,0x07,0x00,
0x04,0x06,0x05,0x04,0x04,0x06,0x0C,0x00,
};
void delay1ms(unsigned int ii) //1ms延時函數
{
unsigned int i,x;
for (x=0;x<ii;x++)
{
for (i=0;i<100;i++);
}
}
/*--------------------------------------------
LCD_write_byte: 使用SPI接口寫數據到LCD
輸入參數:dt:寫入的數據;
command :寫數據/命令選擇;
----------------------------------------------*/
void LCD_write_byte(unsigned char dt, unsigned char command)
{
unsigned char i;
sce=0;
dc=command;
for(i=0;i<8;i++)
{
if(dt&0x80)
sdin=1;
else
sdin=0;
dt=dt<<1;
sclk=0;
sclk=1;
}
dc=1;
sce=1;
sdin=1;
}
/*---------------------------------------
LCD_init: 3310LCD初始化
----------------------------------------- */
void LCD_init(void)
{
res=0;
//delay1ms(10);
res=1;
LCD_write_byte(0x21,0); //初始化Lcd,功能設定使用擴充指令
LCD_write_byte(0xC6,0); //設定液晶偏置電壓
LCD_write_byte(0x06,0); //溫度校正
LCD_write_byte(0x13,0); //1:48
LCD_write_byte(0x20,0); //使用基本指令
LCD_write_byte(0x0C,0); //設定顯示模式,正常顯示
}
/*-------------------------------------------
LCD_set_XY: 設置LCD坐標函數
輸入參數:X:0-83 Y:0-5
---------------------------------------------*/
void LCD_set_XY(unsigned char X, unsigned char Y)
{
LCD_write_byte(0x40 | Y, 0); // column
LCD_write_byte(0x80 | X, 0); // row
}
/*------------------------------------------
LCD_clear: LCD清屏函數
--------------------------------------------*/
void LCD_clear(void)
{
unsigned char t;
unsigned char k;
LCD_set_XY(0,0);
for(t=0;t<6;t++)
{
for(k=0;k<84;k++)
{
LCD_write_byte(0x00,1);
}
}
}
/*---------------------------------------------
LCD_write_shu: 顯示6(寬)*16(高)點陣列數字字母符號等半角類
輸入參數:c:顯示的字符;
-----------------------------------------------*/
void LCD_write_shu(unsigned char row, unsigned char page,unsigned char c) //row:列 page:頁 dd:字符
{
unsigned char i;
LCD_set_XY(row*6, page); // 列,頁
for(i=0; i<6;i++)
{
LCD_write_byte(shuzi[c*12+i],1);
}
LCD_set_XY(row*6, page+1); // 列,頁
for(i=6; i<12;i++)
{
LCD_write_byte(shuzi[c*12+i],1);
}
}
/**************************************************************/
/*-------------------------------------------------
LCD_write_chinese_string: 在LCD上顯示漢字
輸入參數:X、Y :顯示漢字的起始X、Y坐標;
ch_with :漢字點陣的寬度
num :顯示漢字的個數;
line :漢字點陣數組中的起始行數
row :漢字顯示的行間距
--------------------------------------------------*/
void LCD_write_chinese_string(unsigned char X, unsigned char Y,unsigned char ch_with,unsigned char num, unsigned char line,unsigned char row)
{
unsigned char i,n;
LCD_set_XY(X,Y); //設置初始位置
for (i=0;i<num;)
{
for (n=0; n<ch_with*2; n++) //寫一個漢字
{
if (n==ch_with) //寫漢字的下半部分
{
if (i==0) LCD_set_XY(X,Y+1);
else
{
LCD_set_XY((X+(ch_with+row)*i),Y+1);
}
}
LCD_write_byte(yi[line+i][n],1);
}
i++;
LCD_set_XY((X+(ch_with+row)*i),Y);
}
}
void LCD_write_chinese_string1(unsigned char X, unsigned char Y,unsigned char ch_with,unsigned char num, unsigned char line,unsigned char row)
{
unsigned char i,n;
LCD_set_XY(X,Y); //設置初始位置
for (i=0;i<num;)
{
for (n=0; n<ch_with*2; n++) //寫一個漢字
{
if (n==ch_with) //寫漢字的下半部分
{
if (i==0) LCD_set_XY(X,Y+1);
else
{
LCD_set_XY((X+(ch_with+row)*i),Y+1);
}
}
LCD_write_byte(er[line+i][n],1);
}
i++;
LCD_set_XY((X+(ch_with+row)*i),Y);
}
}
void LCD_write_chinese_string2(unsigned char X, unsigned char Y,unsigned char ch_with,unsigned char num, unsigned char line,unsigned char row)
{
unsigned char i,n;
LCD_set_XY(X,Y); //設置初始位置
for (i=0;i<num;)
{
for (n=0; n<ch_with*2; n++) //寫一個漢字
{
if (n==ch_with) //寫漢字的下半部分
{
if (i==0) LCD_set_XY(X,Y+1);
else
{
LCD_set_XY((X+(ch_with+row)*i),Y+1);
}
}
LCD_write_byte(shan[line+i][n],1);
}
i++;
LCD_set_XY((X+(ch_with+row)*i),Y);
}
}
void int0() interrupt 1 //采用定時器中斷0 控制節拍 10.851MS
{ TH0=0xd8;
TL0=0xef;
n--;
}
void delay (unsigned char m) //控制頻率延時
{
unsigned i=3*m;
while(--i);
}
void delayms(unsigned char a) //豪秒延時子程序
{
while(--a); //采用while(--a) 不要采用while(a--); 各位可編譯一下看看匯編結果就知道了!
}
//外部中斷0
void int0_isr(void) interrupt 0 using 1
{
delayms(200);
EX0=0;//外部中斷0關
if(anjian0)
{
//LCD_write_chinese_string(1,0,12,4,0,0);
y++;//向上切歌
if(y==3)
{
y=0;
}
}
EX0=1;//外部中斷0開
}
//外部中斷1
void int1_isr(void) interrupt 2 using 2
{
delayms(200);
EX1=0;//外部中斷1關
if(!anjian1)
{
if(y==0)
{y=2;}
}
if(y>=1)
{
y--; //向下切歌
}
EX1=1;
}
//藍牙選歌
void com_int(void) interrupt 4
{
EA=0;//總中斷關
deng=SBUF;
if(RI==1)
{
o=SBUF;
RI=0;
switch(o)
{
case 0: y=0,LCD_clear(),LCD_write_chinese_string(1,0,12,4,0,0);break;//第一首歌
case 1: y=1,LCD_clear(),LCD_write_chinese_string1(1,0,12,4,0,0);break;//第二首歌
case 2: y=2,LCD_clear(),LCD_write_chinese_string2(1,0,12,4,0,0);break;//第三首歌
case 4: zhan=1;break;//暫停
case 5: zhan=0;break;//繼續
default:break;
}
}
EA=1;
}
//主函數
void main()
{
unsigned char i=0,p,m; //m為頻率常數變量
TMOD=0x21;//定時器方式1 16位 T1定時器 方式2
TH0=0xd8;
TL0=0xef; //55535
IE=0x82;
EA=1;//總中斷
EX0=1;//外部中斷0
EX1=1;
IT0=1;//跳沿觸發
IT1=1;
PS=1;//串口中斷最高
ES=1;
SCON=0xd0;//設置串口方式3
PCON=0x00;
TH1=0xfd; //9600
TL1=0xfd;
TR1=1; //接受定時器
sce=0;
res=0;
res=1;
LCD_init(); //初始化LCD模塊
LCD_clear(); //清屏幕
LCD_write_chinese_string(1,0,12,4,0,0);
play:
while(1)
{
//LCD_write_chinese_string(1,0,12,4,0,0);
while(zhan);//暫停
a: {switch(y)
{
case 0:
{
P2=0X01;
p=music_tab0[i];
if(p==0x00) { i=0, delayms(1000); goto play;} //如果碰到結束符,延時1秒,回到開始再來一遍
else if(p==0xff) { i=i+1;delayms(100),TR0=0; goto a;} //若碰到休止符,延時100ms,繼續取下一音符
else {m=music_tab0[i++], n=music_tab0[i++];}
TR0=1; //開定時器1
while(n!=0) Beep=~Beep,delay(m); //等待節拍完成, 通過P1口輸出音頻
TR0=0; //關定時器1 ;
}
break; //取頻率常數 和 節拍常數
case 1:
{
P2=0X02;
p=music_tab1[i];
if(p==0x00) { i=0, delayms(1000); goto play;} //如果碰到結束符,延時1秒,回到開始再來一遍
else if(p==0xff) { i=i+1;delayms(100),TR0=0; goto a;} //若碰到休止符,延時100ms,繼續取下一音符
else {m=music_tab1[i++], n=music_tab1[i++];}
TR0=1; //開定時器1
while(n!=0) Beep=~Beep,delay(m); //等待節拍完成, 通過P1口輸出音頻
TR0=0; //關定時器1 ;
}
break; //取頻率常數 和 節拍常數
case 2:
{
P2=0X04;
p=music_tab2[i];
if(p==0x00) { i=0, delayms(1000); goto play;} //如果碰到結束符,延時1秒,回到開始再來一遍
else if(p==0xff) { i=i+1;delayms(100),TR0=0; goto a;} //若碰到休止符,延時100ms,繼續取下一音符
else {m=music_tab2[i++], n=music_tab2[i++];}
TR0=1; //開定時器1
while(n!=0) Beep=~Beep,delay(m); //等待節拍完成, 通過P1口輸出音頻
TR0=0; //關定時器1 ;
}
}
}
}
}
|