这个东西是09年刚学单片机时做的,基于51单片机。点阵屏用的是一个集成好的模块,就是四个595两两级联分别控制行和列,屏可能有点老,所以不是很亮。字摸直接用网上的字摸提取软件就能提取。另外用定时计数器产生方波做了一个音乐播放器(用了一个运放),放的是天空之城的音乐,同时用一个两位数码管显示当前的音乐音阶(第一位数码管表示高中低音)。感觉里面用了不少51的资源,挺好的程序,现在完全开源的给大家分享出来
作品图片:
- #include<reg51.h>
- sbit RCK=P2^2;
- sbit SRCK_1=P2^3;
- sbit SRCK_2=P2^4;
- sbit SER_IN_1=P2^5;
- sbit SER_IN_2=P2^6;
- sbit DIG1=P2^0;
- sbit DIG2=P2^1;
- sbit buz=P2^7;
- unsigned char num=13;--字幕有多少个字
- unsigned int speed=85;--每状态持续时间,数字越小,字幕速度越快
- unsigned int song_num=136;--音节个数
- unsigned char code song[]=--中音前缀为1,低音为0,高音为2+后位为歌谱
- {
- 16,17,21,17,21,23,17,13,13,16,15,16,21,15,10,13,13,14,13,14,21,
- 10,13,01,21,21,21,17,14,14,17,17,10,16,17,21,17,21,23,17,10,13,13,16,15,16,21,
- 15,10,13,14,21,17,17,21,22,22,23,21,10,21,17,16,16,17,15,16,10,21,22,23,22,23,
- 25,22,10,15,15,21,17,21,23,23,10,10,16,17,21,17,22,22,21,15,15,10,24,23,22,21,
- 23,23,10,23,26,25,25,23,22,21,10,21,22,21,22,22,25,23,10,23,26,25,23,22,21,10,
- 21,22,21,22,22,17,16,10,16,17,16
- };
-
-
- unsigned char code song_speed[]=--节拍,数目越大声音持续时间越长,与各曲谱位置对应
- {
- 40,60,120,80,100,100,300,40,60,120,80,100,100,200,100,50,50,120,80,80,120,100,200,25,25,
- 25,25,120,80,105,95,200,100,50,50,120,80,100,100,200,100,50,50,120,80,100,100,300,50,50,
- 100,40,50,110,100,33,33,33,100,100,120,80,50,50,100,100,200,100,50,50,120,80,100,100,
- 200,100,50,50,110,90,100,100,200,100,100,45,55,100,100,50,50,110,80,110,100,100,100,100,100,
- 400,200,100,100,200,100,100,50,50,100,50,50,100,45,55,100,100,200,100,100,200,200,120,80,
- 200,50,50,100,60,40,50,100,200,100,50,50,400
- };
-
- unsigned char code hanzi[]=--字模提取BADC,右旋90度
- {
- 0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0xFF,0xFF,0x80,0x40,0x80,0x40,0xA0,0x23,--长
- 0x90,0x04,0x88,0x08,0x84,0x10,0x86,0x20,0x80,0x60,0xC0,0x20,0x80,0x00,0x00,0x00,
-
- 0x20,0x04,0x22,0x04,0x2A,0x02,0x2A,0x01,0xAA,0xFF,0x6A,0x49,0x3A,0x49,0x2F,0x49,--春
- 0x2A,0x49,0x6A,0x49,0xAA,0xFF,0x2A,0x01,0x2A,0x02,0x22,0x06,0x20,0x02,0x00,0x00,
-
- 0x44,0x10,0x44,0x30,0xFC,0x1F,0x46,0x08,0x44,0x48,0x00,0x48,0xFE,0x44,0x92,0x44,--理
- 0x92,0x44,0xFE,0x7F,0x92,0x44,0x92,0x46,0xFF,0x44,0x02,0x60,0x00,0x40,0x00,0x00,
-
- 0x00,0x20,0x04,0x20,0x04,0x20,0x04,0x20,0x04,0x20,0x04,0x20,0x04,0x20,0xFC,0x3F,--工
- 0x04,0x20,0x04,0x20,0x04,0x20,0x04,0x20,0x06,0x20,0x04,0x30,0x00,0x20,0x00,0x00,
-
- 0x80,0x00,0x80,0x40,0x40,0x40,0x20,0x4A,0x50,0x52,0x48,0x62,0x44,0x42,0xC3,0x7F,--姓 0x44,0x42,0x68,0x62,0x50,0x52,0x20,0x4A,0x40,0x60,0xC0,0x40,0x40,0x00,0x00,0x00,
-
- 0x10,0x00,0x10,0x80,0x10,0x40,0x10,0x30,0x10,0x0C,0x90,0x23,0x7F,0x20,0x10,0x10,--名0xF0,0x3F,0x12,0x44,0x14,0x42,0x10,0x41,0xD0,0x40,0x18,0x40,0x10,0x78,0x00,0x00,
-
- 0x80,0x00,0x40,0x00,0x20,0x00,0xF8,0xFF,0x87,0x00,0x40,0x00,0x30,0x00,0x0F,0x00,--作
- 0xF8,0xFF,0x88,0x08,0x88,0x08,0xC8,0x08,0x88,0x0C,0x0C,0x08,0x08,0x00,0x00,0x00,
- 0x00,0x00,0x00,0xFF,0x00,0x41,0x7E,0x41,0x22,0x41,0x22,0x41,0xA2,0xFF,0x22,0x01,--品
- 0x22,0xFF,0x22,0x41,0x22,0x41,0x7F,0x41,0x02,0x41,0x80,0xFF,0x00,0x01,0x00,0x00,
-
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x7C,0x3B,0xFE,--!
- 0x3B,0xFE,0x10,0x7C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
-
- 0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,-图
- 0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,
-
- 0x00,0x01,0x40,0x00,0x10,0x00,0x04,0x00,0x01,0x00,0x04,0x00,0x10,0x00,0x40,0x00,
- 0x00,0x01,0x00,0x04,0x00,0x10,0x00,0x04,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,
-
- 0x00,0x01,0x40,0x00,0x10,0x00,0x04,0x00,0x01,0x00,0x04,0x00,0x10,0x00,0x40,0x00,-案
- 0x00,0x01,0x00,0x04,0x00,0x10,0x00,0x04,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,
-
- 0xF0,0x00,0x08,0x01,0x14,0x02,0x52,0x04,0x12,0x08,0x12,0x10,0x24,0x22,0x58,0x55,
- 0x88,0x68,0x04,0x20,0x12,0x10,0x12,0x08,0x52,0x04,0x14,0x02,0x08,0x01,0xF0,0x00
- };
-
- unsigned char code saomiao[]=--字幕扫描数组
- {
- 0x00,0x01,0x00,0x02,0x00,0x04,0x00,0x08,0x00,0x10,0x00,0x20,0x00,0x40,0x00,0x80,
- 0x01,0x00,0x02,0x00,0x04,0x00,0x08,0x00,0x10,0x00,0x20,0x00,0x40,0x00,0x80,0x00
- };
- unsigned char tone_l_h[]={0xf0,0xf9,0xf9,0xfa,0xfa,0xfb,0xfb,0xfc};--底音高8位频率
- unsigned char tone_l_l[]={0xf0,0x21,0xe0,0x8b,0xd7,0x68,0xe8,0x5b};--底音低8位频率
- unsigned char tone_m_h[]={0xf0,0xfc,0xfc,0xfd,0xfd,0xfd,0xfd,0xfe};--中音高8位频率
- unsigned char tone_m_l[]={0xf0,0x8f,0xef,0x45,0x6c,0xb5,0xf5,0x2e};--中音低8位频率
- unsigned char tone_h_h[]={0xf0,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xff};--高音高8位频率
- unsigned char tone_h_l[]={0xf0,0x47,0x77,0xa2,0xb6,0xda,0xfa,0x16};--高音低8位频率
- unsigned char code number[]={0xc0,0xf9,0xa4,0xb0,0X99,0X92,0X82,0Xf8,0X80,0X90};--数码管显示0~9
- unsigned char tone_yin[]={0xf7,0xbf,0xfe};--低中高音节数码管显示
- unsigned char *s=saomiao,*h=hanzi,*w,*t,flog1=0,flog2=0,x,m,count,TH_M0,TL_M0,n;
- unsigned int cx;
- void delay(unsigned int num)--普通延时函数,用于调整音调
- {
- unsigned int z;
- for(z=0;z<num;z++)
- {;}
- }
-
- delay_1()--倒计时延时函数,可控制执行次数,用于控制字幕滚动速度
- {
- cx--;
- if(cx!=0) return(1);
- if(cx==0) return(0);
- }
- void write_1(unsigned char byte)--字模寄存器写入(根据点阵特点,调整输出次序)
- {
- unsigned char i;
- for(i=0;i<8;i++)
- {
- SRCK_1=0;
- if((byte&0x01)==0)
- SER_IN_1=0;
- else SER_IN_1=1;
- SRCK_1=1;
- byte=byte>>1;
- }
- SRCK_1=0;
- }
-
-
- void write_2(unsigned char byte)--扫描行寄存器写入
- {
- unsigned char i;
- for(i=0;i<8;i++)
- {
- SRCK_2=0;
- if((byte&0x80)==0)
- SER_IN_2=0;
- else SER_IN_2=1;
- SRCK_2=1;
- byte=byte<<1;
- }
- SRCK_2=0;
- }
- void scan()--执行一次,扫描一行
- {
- unsigned char j;
- flog1++;
- if(flog1==17)
- {
- flog1=1;
- s=saomiao;
- }
- for(j=0;j<2;j++)
- {
- write_2(*(s++));
- }
-
- }
- void gundong(unsigned char n)--每次赋值是输入2个值,n从1开始,产生滚动
- {
- unsigned char j;
- if(flog2==0)
- {
- t=&hanzi[2*(n-1)];
- if(n<=((num-1)*16+1))
- {
- w=&hanzi[2*(n-1)+32];
- }
- else
- {
- w=&hanzi[2*(n-1)-2*(num-1)*16];--每次只显示16位,32个字节,w是尾地址
- }
- h=t;
- flog2++;
- }
- if(h==w)--他和下面的IF不可调换次序
- {
- h=t;
- }
- if(h==(hanzi+num*32))
- {
- h=hanzi;
- }
- for(j=0;j<2;j++)
- {
- write_1(*(h++));
- }
- }
- main()
- {
- unsigned char i;
- cx=speed;
- SRCK_1=0;
- SRCK_2=0;
- RCK=0;--清零很重要???
- n=0;
- EA=1;--打开中断
- ET0=1;
- ET1=1;--易错点,定时器打开,中断也得开
- TMOD=0x11;--打开定时器
- TR0=1;
- TR1=1;
- PT0=1;--优先级设定
- while(1)
- {
- for(i=0;i<song_num;i++)
- {
- x=song[i];
- m=song[i]/10;
- switch(m)--分中底高位赋值
- {
- case 0:{TH_M0=tone_l_h[x];TL_M0=tone_l_l[x];break;}
- case 1:{TH_M0=tone_m_h[x];TL_M0=tone_m_l[x];break;}
- case 2:{TH_M0=tone_h_h[x];TL_M0=tone_h_l[x];break;}
- }
- TH0=TH_M0;
- TL0=TL_M0;
- delay(song_speed[i]*20);--节奏控制
- }
- }
- }
-
- void play_music(void) interrupt 1 --输出音乐函数+数码输出函数
- {
- TH0=TH_M0;
- TL0=TL_M0;
- if(x!=0) buz=!buz;
- if(x==0) buz=0;
- count++;
- if(count==1)
- {
- DIG1=0;
- DIG2=1;
- P0=tone_yin[m];
- }
- if(count==2)
- {
- count=0;
- DIG1=1;
- DIG2=0;
- P0=number[x];
- }
- }
- void play(void) interrupt 3
-
- {
- if(n==(num*16))
- {
- n=0;
- }
- n++;
- while(delay_1())
- {
- gundong(n);
- scan();
- RCK=0;
- RCK=1;
- }
- flog1=0;
- flog2=0;
- cx=speed;
- s=saomiao;
- TH1=0xEF;--放后面,防止不准
- TL1=0xEF;
- TF1=0;
- }
复制代码
|