#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 ;
}
}
}
}
}
|