仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
共有6个模式,可以显示速率和模式。
单片机源程序如下:
- #include <reg51.h>
- #define uchar unsigned cha
- #define uint unsigned int
- //定义特殊功能寄存器的位变量
- sbit key1=P3^2;//按key1可自动//外部中断
- sbit key2=P3^3;//按key2可切换//外部中断
- sbit fm=P3^7;//蜂鸣器连续的IO口
- sbit key3=P3^4;
- sbit key4=P3^5;
- sbit wei=P3^0;
- uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //共阴极数码管显示码制0~F
- uchar code tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //共阴极数码管显示码制0~F
- //速度模式共有5种
- unsigned char code SpeedCode[]={100,60,30,14,5};
- uchar count2=1,RunMode=1;//音乐、流水灯标志
- uchar zd=1; //自动播放标志
- uchar timeh,timel,i;
- uint LEDIndex = 0;//LED位置,声明8位变量LEDIndex,用来指示8个LED哪个灯被点亮
- bit LEDDirection = 1,LEDFlag = 1;//LED方向,LED亮灭
- uint Timer0Count,SystemSpeed;//跑马灯速度,系统速度
- char SystemSpeedIndex=1;//速度模式参数
- //---------------------------简谱---------------------------------------
- //编程规则:字节高位是简谱,低位是持续时间,
- //代表多少个十六分音符
- //1-7代表中央C调,8-E代表高八度,0代表停顿
- //最后的0是结束标志
- uchar code qnzl[]={ //千年之恋
- 0x12,0x22,0x34,0x84,0x74,0x54,0x38,0x42,0x32,0x22,0x42,0x34,0x84,0x72,0x82,0x94,0xA8,0x08,
- //前奏
- 0x32,0x31,0x21,0x32,0x52,0x32,0x31,0x21,0x32,0x62,
- //竹林的灯火 到过的沙漠
- 0x32,0x31,0x21,0x32,0x82,0x71,0x81,0x71,0x51,0x32,0x22,
- //七色的国度 不断飘逸风中
- 0x32,0x31,0x21,0x32,0x52,0x32,0x31,0x21,0x32,0x62,
- //有一种神秘 灰色的旋涡
- 0x32,0x31,0x21,0x32,0x82,0x83,0x71,0x72,0x02,
- //将我卷入了迷雾中
- 0x63,0xA1,0xA2,0x62,0x92,0x82,0x52,
- //看不清的双手
- 0x31,0x51,0x63,0x51,0x63,0x51,0x63,0x51,0x62,0x82,0x7C,0x02,
- //一朵花传来谁经过的温柔
- 0x61,0x71,0x82,0x71,0x62,0xA2,0x71,0x76,
- //穿越千年的伤痛
- 0x61,0x71,0x82,0x71,0x62,0x52,0x31,0x36,
- //只为求一个结果
- 0x61,0x71,0x82,0x71,0x62,0xA3,0x73,0x62,0x53,
- //你留下的轮廓 指引我
- 0x42,0x61,0x62,0x83,0x83,0x91,0x91,
- //黑夜中不寂寞
- 0x61,0x71,0x82,0x71,0x62,0x0A2,0x71,0x76,
- //穿越千年的哀愁
- 0x61,0x71,0x82,0x71,0x62,0x52,0x31,0x36,
- //是你在尽头等我
- 0x61,0x71,0x82,0x71,0x62,0xA3,0x73,0x62,0x53,
- //最美丽的感动 会值得
- 0x42,0x82,0x88,0x02,0x74,0x94,0x89,0xff//结束标志
- //用一生守候
- } ;
- uchar code jmszl[]={ //寂寞沙洲冷
- 0x12,0x12,0x22,0x32,0x31,0x22,0x21,0x22,
- //自你走后心憔悴
- 0x21,0x31,0x51,0x52,0x31,0x52,0x61,0x15,0x14,
- //白色油桐风中纷飞
- 0x51,0x52,0x31,0x52,0x62,0x13,0x11,0x13,0x32,0x28,0x08,0x28,
- //落花似人有情 这个季节
- 0x31,0x32,0x31,0x32,0x11,0x21,0x51,0x52,0x51,0x52,
- //河畔的风放肆拼命地吹
- 0x51,0x51,0x31,0x32,0x31,0x32,0x81,0x72,0x63,
- //不断拨弄离人的眼泪
- 0x62,0x71,0x81,0x72,0x61,0x61,0x52,0x31,0x21,0x32,0x51,0x54,
- //那样浓烈的爱再也无法给
- 0x22,0x12,0x11,0x12,0x11,0x12,0x12,0x14,0x26,0x32,0x26,
- //伤感一夜一夜
- 0x32,0x61,0x51,0x51,0x31,0x31,0x21,0x31,0x51,0x61,0x51,0x31,0x51,
- //当记忆的线缠绕过往支离破碎
- 0x02,0x32,0x81,0x81,0x81,0x81,0x62,0x52,0x34,
- //是慌乱占据了心扉
- 0x31,0x81,0x81,0x81,0x61,0x91,0x82,
- //有花儿伴着蝴蝶
- 0x51,0x51,0x51,0x51,0x31,0x61,0x53,
- //孤雁可以双飞
- 0x21,0x11,0x21,0x11,0x22,0x11,0x21,0x26,
- //夜深人静独徘徊
- 0x32,0x61,0x51,0x51,0x31,0x31,0x21,0x31,0x51,0x61,0x51,0x31,0x51,0x52,
- //当幸福恋人寄来红色分享喜悦
- 0x31,0x31,0x81,0x81,0x81,0x61,0x91,0x81,0x61,0x31,0x56,
- //闭上双眼难过头也不敢回
- 0x32,0x32,0x81,0x81,0x81,0x81,0x91,0x81,0x61,0x81,0x61,0x51,0x31,0x51,0x34,
- //仍然捡尽寒枝不肯安歇微带着后悔
- 0x21,0x31,0x51,0x31,0x21,0x11,0x61,0x21,0x16,
- //寂寞沙洲我该思念谁
- 0xff};
-
- uchar code srkl[]={0x52,0x52,0x64,0x54,0x84,0x74,//生日快乐
- 0x52,0x52,0x64,0x54,0x94,0x84,
- 0x52,0x52,0xc4,0xA4,0x84,0x74,0x64,
- 0xB2,0xB2,0xA4,0x84,0x94,0x84,
- 0x52,0x52,0x64,0x54,0x84,0x74,
- 0x52,0x52,0x64,0x54,0x94,0x84,
- 0x52,0x52,0xc4,0xA4,0x84,0x74,0x64,
- 0xB2,0xB2,0xA4,0x84,0x94,0x84,0xff};
-
- uchar code goon[]={0x64,0x84,0x92,0x91,0x81,0x93,0x61, //康定情歌
- 0x54,0x62,0x82,0x92,0x91,0x81,0x92,
- 0x64,0x62,0x82,0x92,0x91,0x81,0x93,
- 0x61,0x54,0x82,0x62,0x51,0x61,0x51,
- 0x41,0x52,0x24,0x92,0x54,0x82,0x64,
- 0x52,0x42,0x94,0x82,0x62,0x51,0x61,
- 0x51,0x41,0x52,0x24,0xff};
-
- //----------------------------简谱音调对应的定时器初值---------------------------
- //适合11.0592M的晶振
- uchar code chuzhi[]={
- 0xff,0xff,//占位
- 0xFC,0x8E,//中央C调1-7
- 0xFC,0xED,
- 0xFD,0x43,
- 0xFD,0x6A,
- 0xFD,0xB3,
- 0xFD,0xF3,
- 0xFE,0x2D,
- 0xFE,0x47, //高八度1-7
- 0xFE,0x76,
- 0xFE,0xA1,
- 0xFE,0xC7,
- 0xFE,0xD9,
- 0xFE,0xF9,
- 0xFF,0x16
- };
-
- void LEDShow(unsigned int LEDStatus)
- {
- P1 = ~(LEDStatus&0x00FF); //P1输出低八位
-
- }
-
- //速度选择函数
- void SetSpeed(unsigned char Speed)
- {
- SystemSpeed =SpeedCode[Speed];//速度模式
- }
-
- void delay(uint z) //延时165MS,即十六分音符
- { uint x,y;
- for(x=z;x>0;x--)
- for(y=19000;y>0;y--);
- }
-
- void delay1(uint z) //延时1MS
- { uint x,y;
- for(x=z;x>0;x--)
- for(y=112;y>0;y--);
- }
- //模式1(一个灯右移)
- void Mode_1(void)
- {
- LEDShow(0x80>>LEDIndex);
- LEDIndex = (LEDIndex+1)%8;//LEDIndex+1对8取余,保证LEDIndex在0~7之间
- }
-
- //Mode 2
- void Mode_2(void)
- {
- if(LEDDirection) //选择LED方向
- LEDShow(~(0x01<<LEDIndex)); //LED一个灭的灯左移
- else
- LEDShow(~(0x80>>LEDIndex));//LED一个灭的灯右移
- if(LEDIndex==7)
- LEDDirection = !LEDDirection;//改变方向
- LEDIndex = (LEDIndex+1)%8;//循环
- }
-
- //Mode 3
- void Mode_3(void)
- {
- if(LEDDirection)
- {
- if(LEDFlag) //LED灭
- LEDShow(0xAA<<LEDIndex); //四灭的灯左移
- else
- LEDShow(~(0x55>>LEDIndex));//四个亮的灯右移
- }
- else
- {
- if(LEDFlag)
- LEDShow(0x7F>>LEDIndex); //一个灭的灯右移
- else
- LEDShow(~(0xFE<<LEDIndex));//一个亮的灯左移
- }
- if(LEDIndex==7)
- {
- LEDDirection = !LEDDirection;
- if(LEDDirection) LEDFlag = !LEDFlag;
- }
- LEDIndex = (LEDIndex+1)%8;
- }
-
- //Mode 4
- void Mode_4(void)
- {
- if(LEDDirection)
- LEDShow(0x0F<<LEDIndex); //四个右灯左移
- else
- LEDShow(0xF0>>LEDIndex); //四个左灯右移
- if(LEDIndex==7)
- LEDDirection = !LEDDirection;
- LEDIndex = (LEDIndex+1)%8;
- }
-
- //Mode 5
- void Mode_5(void)
- {
- if(LEDDirection)
- LEDShow(~(0x0F<<LEDIndex));//左四个灯左移
- else
- LEDShow(~(0xF0>>LEDIndex));//四个灯右移
- if(LEDIndex==7)
- LEDDirection = !LEDDirection;
- LEDIndex = (LEDIndex+1)%8;
- }
- //Mode 6
- void Mode_6(void)
- {
- if(LEDDirection)
- LEDShow(~(0x1F<<LEDIndex));//左五个灯左移
- else
- LEDShow(~(0x30>>LEDIndex));//两个灯右移
- if(LEDIndex==7)
- LEDDirection = !LEDDirection;
- LEDIndex = (LEDIndex+1)%8;
- }
- void Timer0EventRun(void)
- {
- switch(RunMode)
- {
- case 1:Mode_1();break;
- case 2:Mode_2();break;
- case 3:Mode_3();break;
- case 4:Mode_4();break;
- case 5:Mode_5();break;
- case 6:Mode_6();break;
- default:break;
- }
- }
-
- void song()
- {
- uint temp;
- uchar jp;//jp是简谱
- i=0;
- while(1)
- {
- if(key3==0)
- {
- delay1(7);
- if(key3==0)
- {
-
- P2=tab[SystemSpeedIndex];
- SystemSpeedIndex++; //选择速度
- if(SystemSpeedIndex>5)
- SystemSpeedIndex=1;
- SetSpeed(SystemSpeedIndex);
- }
- while(!key3);
- }
- if(key4==0)
- {
- delay1(7);
- if(key4==0)
- {
-
- P2=tab[SystemSpeedIndex];
- SystemSpeedIndex--;
- if(SystemSpeedIndex<1)
- SystemSpeedIndex=5;
- SetSpeed(SystemSpeedIndex);
- }
- while(!key4);
- }
- P0=table[RunMode]; //共阴极数码管显示码制
- P2=tab[SystemSpeedIndex];//速度显示
- if(count2==0)
- {
- break;
- }
- if(count2==1) //千年之恋
- temp=qnzl[i];
- if(count2==2) //寂寞沙洲冷
- temp=jmszl[i];
- if(count2==3) //生日快乐
- temp=srkl[i];
- if(count2==4) //康定情歌
- temp=goon[i];
- if(temp==0xff)
- break;
- jp=temp/16; //取数的高4位
-
- if(jp!=0)
- {
- timeh = chuzhi[jp*2];
- timel = chuzhi[jp*2+1];
- TR0=1;//定时器T0的开定时器位,TR0=1表示开定时器
- }
- else
- {
- TR0=0;//定时器T0的开定时器位,TR0=0表示关定时器
- fm=0;//关蜂鸣器
- }
- delay(temp%16); //取数的低4位
- TR0=0; //唱完一个音停15MS,//定时器T0的开定时器位,TR0=0表示关定时器
- fm=0;
- i++;
- }
- TR0=0;//定时器T0的开定时器位,TR0=0表示关定时器
- fm=0;
- }
-
- main()
- {
- EX1=1;//开外部中断1
- IT1=1;//外部中断1下降沿触发方式
- EX0=1;//开外部中断0
- IT0=1;//外部中断0下降沿触发方式
- TMOD=0x11;//定时器0,1工作在方式1
- TH0=0;TL0=0;
- TH1=0xFC;//定时器1 的高8位里写入十六进制值 0xFC
- TL1=0xFB;
- ET0=1;//定时器0的中断允许开关
- ET1=1;
- TR0=0;//定时器T0的开定时器位,TR0=0表示关定时器
- wei=1;
- P0=table[1];//共阴(阳)极数码管显示码制0~F
- P2=tab[1];
- SetSpeed(0);
- EA=1;//开总中断
- TR1=1;//启动定时器T
- while(1)
- {
- song();
- delay1(50);
- if(zd)//自动播放标志zd=1
- {
- count2++;
- if(count2>4)
- count2=1;
- RunMode++;
- if(RunMode>6)
- RunMode=1;
- SystemSpeedIndex++;
- if(SystemSpeedIndex>5)
- SystemSpeedIndex=1;
- if(SystemSpeedIndex<1)
- SystemSpeedIndex=5;
- }
- }
- }
-
- void int1() interrupt 2 //自动播放歌曲和跑马灯,外部中断1
- {
- EA=0;//关总中断
- if(key2==0)
- {
- delay1(7);
- if(key2==0)
- {
- zd = 0;
- TR0=1; //定时器T0的开定时器位,TR0=1表示开定时器
- i=0;//从头开始唱
- delay1(5);
- count2++;
- if(count2>4)//音乐标志
- count2=1;
- RunMode++;
- if(RunMode>6)//流水灯标志
- RunMode=1;
- }
- while(!key2);
- }
- EA=1;//开总中断
- }
- void int0() interrupt 0
- {
- EA=0;
- delay1(7);
- if(key1==0)
- {
- delay1(5);
- if(key1==0)
- {
- zd=1;
- }
- while(!key1);
- }
- EA=1;
- }
- void timer0() interrupt 1
- {
- EA=0;
- TR0=0;
- TH0=timeh;
- TL0=timel;
- fm=~fm;
- TR0=1;
- EA=1;
- }
- void Timer1() interrupt 3
- {
- EA=0;
- TR1=0;
- TH1=0XFC;
- TL1=0xfB;
- Timer0Count++;
- if(Timer0Count>=SystemSpeed)
- {
- Timer0Count=0;
- Timer0EventRun();
- }
- TR1=1;
- EA=1;
- }
复制代码 全部资料51hei下载地址:
多模式带音乐跑马灯.rar
(61.6 KB, 下载次数: 81)
|