51單片機課程設計分享,制作音樂花樣燈數組法法的仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
0.png (19.47 KB, 下載次數: 54)
下載附件
2018-5-23 03:50 上傳
0.png (53.38 KB, 下載次數: 58)
下載附件
2018-5-23 03:50 上傳
單片機花樣燈源程序如下:
- /*12Mhz晶振*/
- #include <reg52.h>
- #define uint unsigned int
- #define uchar unsigned char
- sbit key2=P3^3;//key2外部中斷1 控制花樣燈的切換速度
- sbit key1=P3^2;//key1外部中斷0 控制播放哪首曲子
- sbit voice=P3^7;//蜂鳴器定義
- uchar code sound[]={0xff, //每個音階 不同拍子的數據
- 0x60,0x10,0x55,0x10,0x4c,0x10,0x48,0x10,0x40,0x10,0x39,0x10,0x33,0x10,
- 0x30,0x10,0x2b,0x10,0x26,0x10,0x24,0x10,0x20,0x10,0x1c,0x10,0x19,0x10,
- 0x18,0x10,0x15,0x10,0x13,0x10,0x12,0x10,0x10,0x10,0x0e,0x10,0x0d,0x10,//1/4拍子低音1到高音7
- 0x60,0x20,0x55,0x20,0x4c,0x20,0x48,0x20,0x40,0x20,0x39,0x20,0x33,0x20,
- 0x30,0x20,0x2b,0x20,0x26,0x20,0x24,0x20,0x20,0x20,0x1c,0x20,0x19,0x20,
- 0x18,0x20,0x15,0x20,0x13,0x20,0x12,0x20,0x10,0x20,0x0e,0x20,0x0d,0x20,//2/4拍子低音1到高音7
- 0x60,0x30,0x55,0x30,0x4c,0x30,0x48,0x30,0x40,0x30,0x39,0x30,0x33,0x30,
- 0x30,0x30,0x2b,0x30,0x26,0x30,0x24,0x30,0x20,0x30,0x1c,0x30,0x19,0x30,
- 0x18,0x30,0x15,0x30,0x13,0x30,0x12,0x30,0x10,0x30,0x0e,0x30,0x0d,0x30,//3/4拍子低音1到高音7
- 0x60,0x40,0x55,0x40,0x4c,0x40,0x48,0x40,0x40,0x40,0x39,0x40,0x33,0x40,
- 0x30,0x40,0x2b,0x40,0x26,0x40,0x24,0x40,0x20,0x40,0x1c,0x40,0x19,0x40,
- 0x18,0x40,0x15,0x40,0x13,0x40,0x12,0x40,0x10,0x40,0x0e,0x40,0x0d,0x40,//4/4拍子低音1到高音7
- 0x60,0x60,0x55,0x60,0x4c,0x60,0x48,0x60,0x40,0x60,0x39,0x60,0x33,0x60,
- 0x30,0x60,0x2b,0x60,0x26,0x60,0x24,0x60,0x20,0x60,0x1c,0x60,0x19,0x60,
- 0x18,0x60,0x15,0x60,0x13,0x60,0x12,0x60,0x10,0x60,0x0e,0x60,0x0d,0x60,//5/4拍子低音1到高音7
- 0x60,0x80,0x55,0x80,0x4c,0x80,0x48,0x80,0x40,0x80,0x39,0x80,0x33,0x80,
- 0x30,0x80,0x2b,0x80,0x26,0x80,0x24,0x80,0x20,0x80,0x1c,0x80,0x19,0x80,
- 0x18,0x80,0x15,0x80,0x13,0x80,0x12,0x80,0x10,0x80,0x0e,0x80,0x0d,0x80,//6/4拍子低音1到高音7
- 0x60,0xc0,0x55,0xc0,0x4c,0xc0,0x48,0xc0,0x40,0xc0,0x39,0xc0,0x33,0xc0,
- 0x30,0xc0,0x2b,0xc0,0x26,0xc0,0x24,0xc0,0x20,0xc0,0x1c,0xc0,0x19,0xc0,
- 0x18,0xc0,0x15,0xc0,0x13,0xc0,0x12,0xc0,0x10,0xc0,0x0e,0xc0,0x0d,0xc0,//7/4拍子低音1到高音7
- 0xff,0x10,0xff,0x20,0xff,0x30,0xff,0x40,0xff,0x60,0xff,0x80,0xff,0xc0,//休止符,延時不同
- 0x00};
- uchar code kanong[]={0xff, //卡農
- 0x13,0x20,0x15,0x20,0x18,0x20,0x19,0x20,0x1c,0x20,0x20,0x20,0x1c,0x20,0x19,0x20,//第一行
- 0x18,0x20,0x19,0x20,0x1c,0x20,0x20,0x20,0x24,0x20,0x26,0x20,0x24,0x20,0x2b,0x20,//第二行
- 0x18,0x08,0x19,0x08,0x18,0x08,0x30,0x08,0x33,0x08,0x20,0x08,0x2b,0x08,0x26,0x08,//第三行
- 0x30,0x08,0x18,0x08,0x19,0x08,0x1c,0x08,0x19,0x08,0x13,0x08,0x10,0x08,0x0e,0x08,
- 0x12,0x08,0x13,0x08,0x15,0x08,0x12,0x08,0x12,0x08,0x13,0x08,0x18,0x08,0x19,0x08,
- 0x1c,0x08,0x20,0x08,0x24,0x08,0x26,0x08,0x2b,0x08,0x24,0x08,0x26,0x08,0x2b,0x08,
- 0x30,0x08,0x2b,0x08,0x26,0x08,0x24,0x08,0x20,0x08,0x2b,0x08,0x20,0x08,0x24,0x08,//第四行
- 0x26,0x08,0x1c,0x08,0x20,0x08,0x24,0x08,0x20,0x08,0x24,0x08,0x26,0x08,0x2b,0x08,
- 0x30,0x08,0x39,0x08,0x1c,0x08,0x19,0x08,0x18,0x08,0x19,0x08,0x1c,0x08,0x20,0x08,
- 0x24,0x08,0x26,0x08,0x2b,0x08,0x1c,0x08,0x20,0x08,0x1c,0x08,0x20,0x08,0x24,0x08,
- 0x26,0x10,0x13,0x10,0x15,0x20,0x18,0x20,0x15,0x20,0x18,0x10,0x13,0x10,0x15,0x10,0x12,0x10,//第五行
- 0x10,0x08,0x13,0x04,0x12,0x04, 0x10,0x08,0x13,0x04,0x12,0x04, 0x10,0x04,0x20,0x04,0x1c,0x04,0x19,0x04, 0x18,0x04,0x15,0x04,0x13,0x04,0x12,0x04,//第六行
- 0x13,0x08,0x18,0x04,0x15,0x04, 0x13,0x08,0x26,0x04,0x24,0x04, 0x20,0x04,0x1c,0x04,0x20,0x04,0x24,0x04, 0x20,0x04,0x26,0x04,0x24,0x04,0x20,0x04,
- 0x24,0x08,0x1c,0x04,0x20,0x04, 0x24,0x08,0x26,0x04,0x2b,0x04, 0x26,0x04,0x2b,0x04,0x30,0x04,0x2b,0x04, 0x26,0x04,0x24,0x04,0x20,0x04,0x1c,0x04,
- 0x24,0x08,0x1c,0x04,0x20,0x04, 0x1c,0x08,0x19,0x04,0x18,0x04, 0x20,0x04,0x1c,0x04,0x19,0x04,0x18,0x04, 0x15,0x04,0x13,0x04,0x12,0x04,0x10,0x04,//第七行
- 0x13,0x08,0x18,0x04,0x15,0x04, 0x13,0x08,0x15,0x04,0x18,0x04, 0x15,0x04,0x19,0x04,0x18,0x04,0x15,0x04, 0x13,0x04,0x15,0x04,0x18,0x04,0x19,0x04,
- 0x18,0x08,0x1c,0x04,0x19,0x04, 0x18,0x08,0x30,0x04,0x2b,0x04, 0x26,0x04,0x24,0x04,0x26,0x04,0x2b,0x04, 0x26,0x04,0x18,0x04,0x19,0x04,0x18,0x04,
- 0x1c,0x08,0x18,0x04,0x19,0x04, 0x1c,0x08,0x20,0x04,0x24,0x04, 0x20,0x04,0x24,0x04,0x26,0x04,0x24,0x04, 0x20,0x04,0x1c,0x04,0x19,0x04,0x30,0x04,//第八行
- 0x1c,0x08,0x18,0x04,0x19,0x04, 0x18,0x08,0x19,0x04,0x1c,0x04, 0x19,0x04,0x18,0x04,0x15,0x04,0x18,0x04, 0x19,0x04,0x18,0x04,0x1c,0x04,0x19,0x04,
- 0x13,0x08,0x26,0x08,0x24,0x08,0x26,0x08, 0x2b,0x08,0x15,0x08,0x13,0x08,0x15,0x08, 0x18,0x08,0x26,0x08,0x30,0x08,0x1c,0x08, 0x20,0x08,0x40,0x08,0x48,0x08,0x40,0x08,//第九行
- 0x39,0x08,0x1c,0x08,0x19,0x08,0x1c,0x08, 0x19,0x08,0x40,0x08,0x48,0x08,0x40,0x08, 0x39,0x08,0x1c,0x08,0x20,0x08,0x1c,0x08, 0x19,0x08,0x19,0x08,0x1c,0x08,0x19,0x08,
- 0x30,0x08,0x18,0x08,0x15,0x08,0x18,0x08, 0x19,0x08,0x33,0x08,0x30,0x08,0x33,0x08, 0x39,0x08,0x1c,0x08,0x20,0x08,0x1c,0x08, 0x19,0x08,0x33,0x08,0x26,0x08,0x2b,0x08,//第十行
- 0x30,0x08,0x18,0x08,0x15,0x08,0x12,0x08, 0x13,0x08,0x26,0x08,0x20,0x08,0x13,0x08, 0x18,0x08,0x12,0x08,0x13,0x08,0x12,0x08, 0x15,0x08,0x20,0x08,0x24,0x08,0x20,0x08,
- 0x26,0x08,0x18,0x04,0x19,0x04, 0x18,0x08,0x26,0x08, 0x20,0x08,0x20,0x04,0x1c,0x04, 0x19,0x08,0x20,0x08,//第十一行
- 0x26,0x08,0x18,0x04,0x15,0x04, 0x13,0x08,0x18,0x08, 0x13,0x08,0x13,0x04,0x15,0x04, 0x18,0x08,0x19,0x08,
- 0x1c,0x08,0x1c,0x04,0x20,0x04, 0x1c,0x08,0x19,0x08, 0x18,0x08,0x13,0x04,0x15,0x04, 0x18,0x08,0x13,0x08,
- 0x00};
- code unsigned long Pattern[]={ //24顆LED燈花樣數據
- 0x00fffffc,0x00fffff9,0x00fffff3,0x00ffffe7,//兩個燈順時針流水
- 0x00ffffcf,0x00ffff9f,0x00ffff3f,0x00fffe7f,
- 0x00fffcff,0x00fff9ff,0x00fff3ff,0x00ffe7ff,
- 0x00ffcfff,0x00ff9fff,0x00ff3fff,0x00fe7fff,
- 0x00fcffff,0x00f9ffff,0x00f3ffff,0x00e7ffff,
- 0x00cfffff,0x009fffff,0x003fffff,0x007fffff,0x00ffffff,
- 0x00e7e7e7,0x00dbdbdb,0x00bdbdbd,0x007e7e7e,0x00bdbdbd,0x00dbdbdb,0x00e7e7e7,0x00ffffff,//
- 0x00e7e7e7,0x00c3c3c3,0x00818181,0x00000000,0x00818181,0x00c3c3c3,0x00e7e7e7,0x00ffffff,//
- 0x00aaaaaa,0x00ffffff,0x00555555,0x00ffffff,0x00aaaaaa,0x00ffffff,0x00555555,//錯位點亮
- 0x00181818,0x00818181,0x00181818,0x00818181,//三個相連的燈移位
- 0x00181818,0x00818181,0x00181818,0x00818181,0x00ffffff,
- 0x00f0f0f0,0x000f0f0f,0x00000000,0x00ffffff,
- 0x00f8f8f8,0x00f1f1f1,0x00e3e3e3,0x00c7c7c7,0x008f8f8f,0x001f1f1f,0x003f3f3f,0x007f7f7f,0x007f7f7f,//三個相連的燈流水
- 0x003f3f3f,0x001f1f1f,0x008f8f8f,0x00c7c7c7,0x00e3e3e3,0x00f1f1f1,0x00f8f8f8,0x00ffffff,
- 0x00000000,0x00000000,0x00ffffff,0x00ffffff,//全亮全滅
- 0x000f0f0f,0x00f0f0f0,0x000f0f0f,0x00f0f0f0,//相連的四個燈全亮全滅
- 0x00fffffe,0x00fffffd,0xfffffffb,0x00fffff7,//一個燈順時針流
- 0x00ffffef,0x00ffffdf,0x00ffffbf,0x00ffff7f,
- 0x00fffeff,0x00fffdff,0x00fffbff,0x00fff7ff,
- 0x00ffefff,0x00ffdfff,0x00ffbfff,0x00ff7fff,
- 0x00feffff,0x00fdffff,0x00fbffff,0x00f7ffff,
- 0x00efffff,0x00dfffff,0x00bfffff,0x007fffff,0x00ffffff,
- 0x00fffffe,0x00fffffc,0x00fffff8,0x00fffff0,//順時針從一個亮到全部亮
- 0x00ffffe0,0x00ffffc0,0x00ffff80,0x00ffff00,
- 0x00fffe00,0x00fffc00,0x00fff800,0x00fff000,
- 0x00ffe000,0x00ffc000,0x00ff8000,0x00ff0000,
- 0x00fe0000,0x00fc0000,0x00f80000,0x00f00000,
- 0x00e00000,0x00c00000,0x00800000,0x00000000};
- uchar zdjs=0,jp,i=0;
- uchar num=0,numtop=5,count=0;
- void del(yj);
- void delay(uint z);
- void play(void)
- {
- uint dpjs=0;
- uchar yj;
- while(1)
- {
- zdjs=0;
- if(count==0)
- {
- TR0=0;
- break;
- }
- if(count==1) //選曲
- {
- dpjs++; yj=sound[dpjs]; //取出音階的頻率
- dpjs++; jp=sound[dpjs]; //取出延時時長
- }
- if(count==2)
- {
- dpjs++; yj=kanong[dpjs];
- dpjs++; jp=kanong[dpjs];
- jp=jp*4; //這句是調音的
- }
- while(zdjs!=(jp))
- {
- if(yj!=0xff)
- {
- if(yj!=0) //判斷音樂是否播放完畢
- {
- voice=!voice;
- del(yj);
- }
- else
- {
- dpjs=0;
- break;
- }
- }
- else
- {
- voice=0;
- del(jp);
- }
- }
- }
- }
- void Time0() interrupt 1 using 1 //產生音樂基準
- {
- TH0=0xd8, TL0=0xf0;
- zdjs++;
- }
- void Time1() interrupt 3 //控制花樣燈的切換
- {
-
- TH1=0x3C, TL1=0xB0;
- num++;
- if(num==numtop)
- {
- num=0;
- P0=Pattern[i];
- P1=Pattern[i]>>8;
- P2=Pattern[i]>>16;
- i++;
- if(i==135) i=0;
- }
- }
- void int0() interrupt 0 //key1外部中斷0 控制播放哪首曲子
- {
- del(5);
- EA=0;
- if(key1==0)
- {
- count++; //count決定播放哪一首曲子
- while(!key1);
- if(count==4) count=0;
- }
- EA=1;
- }
- void int1() interrupt 2 //key2外部中斷1 控制花樣燈的切換速度
- {
- del(5); //去抖
- EA=0; //關總中斷
- numtop++;
- // while(!key2); //去抖動
- if(numtop==10) numtop=0; //numtop 代表最長延時時間
- EA=1; //開總中斷
- }
- void del(yj) //簡單延時函數
- {
- uchar yj2=2;
- while(yj!=0)
- {
- while(yj2!=0)
- {
- yj2--;
- }
- yj2=2;
- yj--;
- }
-
- }
- void main()
- {
- TMOD=0x11,IE=0x8a;
- TH0=0xd8, TL0=0xf0; //10ms音樂基準
- TH1=0x3C, TL1=0xB0; //50MS
- EX0=1;//開外部中斷0
- IT0=0;//外部中斷0下降沿觸發方式
- EX1=1;//開外部中斷1
- IT1=0;//外部中斷1下降沿觸發方式
- TR0=1; TR1=1;
- play();
- }
復制代碼
所有資料51hei提供下載:
51單片機課程制作數組法.rar
(745.65 KB, 下載次數: 26)
2018-5-20 22:01 上傳
點擊文件名下載附件
51單片機課程制作數組法 下載積分: 黑幣 -5
|