按键音乐随机播放
单片机源程序如下:
- #include <reg52.h>
- sbit buzzer = P2^5; // 声明输出端
- unsigned char keys,i; // 声明按钮及播放谱变量
- unsigned char tone_H, tone_L; // 声明计时量变量
- #define uint unsigned int
- #define uchar unsigned char
- uchar timeh,timel;//用于存放定时器的高8
- void beat_125(unsigned char); // 声明节拍函数
- void play6(void);
- sbit key=P3^4;
- uchar code zbjbxf[]=
- {
- 0x64,0xA3,0xC1,0xA2,0x62,0x84,0x61,0x81,0x61,0xA4,
- 0xA1,0x91,0xA1,0x81,0x64,0xA3,0xC1,0xD2,0xD2,0xD2,
- 0xA2,0xC4,0xA1,0xC1,0xA1,0xC1,0xD2,0xD2,0xD2,0xA2,
- 0x25,0xC2,0x62,0xC2,0x62,0xA2,0xA2,0x84,0x94,0x94,
- 0x92,0x81,0x91, 0xA2,0xC2,0xD4,0xE4,0xA4,0xE4,0xA2,
- 0xE2,0xA2,0xE2,0xA2, 0xA2,0x84,0x94,0x94,0x92,0x81,
- 0x91,0xA2,0xC2,0xD8
- };
- uchar code chuzhi[]=
- {
- 0xff,0xff,0xFC,0x8E,0xFC, 0xED,0xFD,0x43,0xFD,0x6A,
- 0xFD,0xB3,0xFD,0xF3,0xFE,0x2D,0xFE, 0x47,0xFE,0x76,
- 0xFE,0xA1,0xFE,0xC7,0xFE,0xD9,0xFE,0xF9,0xFF,0x16
- };
- void delay(uint z)
- { uint y;
- for(z;z>0;z--)
- for(y=19000;y>0;y--);
- }
-
- void delay1(uint z)
- { uint y;
- for(z;z>0;z--)
- for(y=112;y>0;y--);
- }
- unsigned char code song1[]={ 1, 1, 2, 1, 4, 3, 1, 1, 2, 1, 5, 4,
- 1, 1, 8, 6, 4, 3, 2, 11, 11, 6, 4, 5, 4, 15 };
- unsigned char code beat1[]={ 4, 4, 8, 8, 8, 16, 4, 4, 8, 8, 8, 16,
- 4, 4, 8, 8, 8, 8, 8, 4, 4, 8, 8, 8, 16 };
- unsigned char code song2[]={ 1, 1, 1, 3, 2, 1, 3, 3, 3, 5, 4, 3, 5, 4, 3, 2,
- 2, 1, 0, 1, 2, 3, 4, 3, 2, 3, 4, 5, 5, 4, 3, 2, 1, 15};
- unsigned char code beat2[]={ 4, 4, 4, 6, 2, 4, 4, 4, 4, 6, 2, 4, 4, 4, 4, 12 ,
- 8, 2, 2, 4, 4, 4, 8, 2, 2, 4, 4, 4, 2, 2, 4, 4, 12 };
- unsigned char code song3[]={ 8, 7, 6, 5, 5, 6, 8, 5, 6, 5, 3, 2, 5, 3, 12,
- 8, 7, 6, 5, 5, 6, 8, 5, 6, 5, 3, 2, 5, 1, 12,
- 2, 3, 2, 5, 6, 5, 6, 8, 9, 8, 7, 6, 9, 5, 12,
- 6, 8, 5, 6, 3, 5, 6, 5, 3, 2, 3, 5, 3, 2, 1, 0, 15 };
- unsigned char code beat3[]={ 8, 4, 4, 8, 8, 8, 8, 16, 8, 4, 4, 8, 8, 24, 8,
- 8, 4, 4, 8, 8, 8, 8, 16, 8, 4, 4, 8, 8, 24, 8,
- 8, 4, 4, 16, 8, 4, 4, 16, 8, 4, 4, 8, 8, 24, 8,
- 8, 8, 8, 8, 8, 4, 4, 8, 8, 8, 4, 4, 8, 8, 32 };
- unsigned char code song4[]={ 2, 2, 3, 5, 6, 5, 6, 7, 9, 7, 7, 6, 5, 6,
- 7, 9, 9, 7, 9, 5, 6, 6, 2, 7, 7, 6, 5, 5,
- 6, 6, 7, 6, 5, 3, 2, 3, 5, 3, 5, 6, 7, 9,
- 9, 9, 10, 9, 7, 7, 6, 5, 3, 2, 7, 7, 6, 5, 5, 15};
- unsigned char code beat4[]={ 12, 4, 8, 8, 8, 4, 4, 16, 12, 4, 4, 4, 8, 32,
- 12, 4, 8, 4, 4, 12, 4, 16, 12, 4, 8, 4, 4, 32,
- 12, 4, 8, 4, 4, 8, 4, 4, 16, 12, 8, 8, 8, 32,
- 12, 4, 8, 4, 4, 8, 4, 4, 16, 12, 4, 8, 4, 4, 32 };
- unsigned char code song5[]={ 6,2,3, 5,2,1, 3,2,2, 5,2,2, 1,3,2, 6,2,1, 5,2,1,
- 6,2,4, 3,2,2, 5,2,1, 6,2,1, 5,2,2, 3,2,2, 1,2,1,
- 6,1,1, 5,2,1, 3,2,1, 2,2,4, 2,2,3, 3,2,1, 5,2,2,
- 5,2,1, 6,2,1, 3,2,2, 2,2,2, 1,2,4, 5,2,3, 3,2,1,
- 2,2,1, 1,2,1, 6,1,1, 1,2,1, 5,1,6, 0,0,0 };
- unsigned int code tone[]={ 1012, 956, 852, 759, 716, 638, 568, 506, 478, 426, 379,531, 10 };//音阶定义
- void play1(void); // 声明play1函数
- void play2(void); // 声明play2函数
- void play3(void); // 声明play3函数
- void play4(void);
- void play5(void); // 声明play5函数
- void beat_125(unsigned char); // 声明节拍函数
- //================================================
- //==第一首歌=================================
- void play1(void)
- { i=0;
- while(song1[i]!=15) // while循环开始
- { tone_H=(65536-tone[song1[i]])/256;// 读取音阶计数量之高八位元
- tone_L=(65536-tone[song1[i]])%256;// 读取音阶计数量之低八位元
- TH0=tone_H; // 填入音阶计数量之高八位元
- TL0=tone_L; // 填入音阶计数量之低八位元
- TR0=1; // 启动TImer 0
- beat_125(beat1[i]); // 指定节拍
- i++; // 下一个音
- TR0=0; // 关闭TImer 0
- } // 结束播放
- }
- //==第二首歌=================================
- void play2(void)
- { i=0;
- while(song2[i]!=15) // while循环开始
- { tone_H=(65536-tone[song2[i]])/256;// 读取音阶计数量之高八位元
- tone_L=(65536-tone[song2[i]])%256;// 读取音阶计数量之低八位元
- TH0=tone_H; // 填入音阶计数量之高八位元
- TL0=tone_L; // 填入音阶计数量之低八位元
- TR0=1; // 启动TImer 0
- beat_125(beat2[i]); // 指定节拍
- i++; // 下一个音
- TR0=0; // 关闭Timer 0
- }
- } // 结束播放
- //==第三首歌=================================
- void play3(void)
- { i=0;
- while(song3[i]!=15) // while循环开始
- { tone_H=(65536-tone[song3[i]])/256;// 读取音阶计数量之高八位元
- tone_L=(65536-tone[song3[i]])%256;// 读取音阶计数量之低八位元
- TH0=tone_H; // 填入音阶计数量之高八位元
- TL0=tone_L; // 填入音阶计数量之低八位元
- TR0=1; // 启动Timer 0
- beat_125(beat3[i]); // 指定节拍
- i++; // 下一个音
- TR0=0; // 关闭Timer 0
- }
- } // 结束播放
- //==第四首歌=================================
- void play4(void)
- { i=0;
- while(song4[i]!=15) // while循环开始
- { tone_H=(65536-tone[song4[i]])/256;// 读取音阶计数量之高八位元
- tone_L=(65536-tone[song4[i]])%256;// 读取音阶计数量之低八位元
- TH0=tone_H; // 填入音阶计数量之高八位元
- TL0=tone_L; // 填入音阶计数量之低八位元
- TR0=1; // 启动Timer 0
- beat_125(beat4[i]); // 指定节拍
- i++; // 下一个音
- TR0=0; // 关闭Timer 0
- }
- } // 结束播放
- void play5(void)
- { i=0;
- while(song5[i]!=15) // while循环开始
- { tone_H=(65536-tone[song5[i]])/256;// 读取音阶计数量之高八位元
- tone_L=(65536-tone[song5[i]])%256;// 读取音阶计数量之低八位元
- TH0=tone_H; // 填入音阶计数量之高八位元
- TL0=tone_L; // 填入音阶计数量之低八位元
- TR0=1; // 启动Timer 0
- beat_125(beat4[i]); // 指定节拍
- i++; // 下一个音
- TR0=0; // 关闭Timer 0
- }
- }
- void play6()
- {
- uint temp; //存放简谱数组中的每一个音符的临时变量
- uchar jp; //jp 用于取出temp
- uchar i=0;
- while(1)
- {
- temp=zbjbxf[i];
- if(temp==0xff) break;
- jp=temp/16;
- if(jp!=0)
- {
- tone_H=chuzhi[jp*2];
- tone_L=chuzhi[jp*2+1];
- TR0=1;//开定时器
- }
- else {TR0=0;buzzer=1;}
- delay(temp%16);TR0=0;buzzer=1; delay1(5); TR0=1;i++;//关定时器
- }
- TR0=0;//关定时器
- buzzer=1; //关蜂鸣器
- }
- //====Timer 0中断子程序==================
- void tone_timer(void) interrupt 1 // Timer 0中断子程序开始
- { TH0=tone_H; // 填入计时量之高八位元
- TL0=tone_L; // 填入计时量之低八位元
- buzzer=~buzzer; // 蜂 器反相输出
- } // 结束中断子程序
- //====节拍函数==================
- void beat_125(unsigned char x) // 节拍函数开始
- { unsigned char i,j,k; // 声明变量
- for (i=0;i<x;i++) // i循环
- for (j=0;j<125;j++) // j循环
- for (k=0;k<120;k++);// k循环
- } // 结束节拍函数
- void delay3(unsigned int x)
- {
- unsigned int i;
- while(x--)
- {
- for(i=0;i<113;i++);
- }
- }
- void suiji()
- {
-
- uint num;
- num=rand();
- if(key==0)
- {delay(1);if(key==0)
- { switch (num%10){
- case 0x01 : delay3(800);play1();break; // 按下S1,播放第一首歌
- case 0x02 : delay3(800);play2(); break; // 按下S2,播放第二首歌
- case 0x03 : delay3(800);play3(); break; // 按下S3,播放第三首歌
- case 0x04 : delay3(800);play4(); break; // 按下S4,播放第四首歌
- case 0x05: delay3(800);play5(); break; //
- case 0x06: delay3(800);play6();break;
- case 0x07: delay3(800);play1();break;
- case 0x08: delay3(800);play3();break;
- case 0x09: delay3(800);play2();break;
- case 0x00: delay3(800);play6();break;
- }
- }
- }
-
- }
- main()
- {
- TMOD=0x01; // 设定MODE1
- //buzzer=1; // 蜂 器初始值
- IE=0x82; // 启用TImer 0
- while (1) // while循环
- { suiji();
- // P3=0xff; // 将Port2规划为输入埠
- //keys=~P2; // 读取按钮
- // switch (keys) // 判读
- // { case 0x01: play1(); break; // 按下S1,播放第一首歌
- // case 0x02: play2(); break; // 按下S2,播放第二首歌
- // case 0x04: play3(); break; // 按下S3,播放第三首歌
- // case 0x08: play4(); break; // 按下S4,播放第四首歌
- // case 0x12: play5(); break; //
- // case 0x10: play6(); break;
-
- // } buzzer=1; // 蜂 器不动作
-
- } // while循环结束
- } // 主程序结束
复制代码
所有资料51hei提供下载:
随机播放.doc
(50.5 KB, 下载次数: 12)
|