求大大们帮我看看这个音乐摇摇棒程序有什么问题
时间:10-02
整理:3721RD
点击:
- #include <reg52.h>
- #define uchar unsigned char
- #define uint unsigned int //宏定义
- sbit KEY=P3^0; //定义画面切换按键
- sbit speaker=P2^0;
- uchar KY; //KY作用在后面说明
- uchar disp; //显示汉字指针
- uchar pic=0,num=0; //pic为按键次数;num为中断次数
- uchar timer0h,timer0l,time; //歌曲发音
- // 频率-半周期数据表 高八位 本软件共保存了四个八度的28个频率数据
- code unsigned char FREQH[] = {
- 0xF2, 0xF3, 0xF5, 0xF5, 0xF6, 0xF7, 0xF8, //低音1234567
- 0xF9, 0xF9, 0xFA, 0xFA, 0xFB, 0xFB, 0xFC, 0xFC,//1,2,3,4,5,6,7,i
- 0xFC, 0xFD, 0xFD, 0xFD, 0xFD, 0xFE, //高音 234567
- 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF}; //超高音 1234567
- // 频率-半周期数据表 低八位
- code unsigned char FREQL[] = {
- 0x42, 0xC1, 0x17, 0xB6, 0xD0, 0xD1, 0xB6, //低音1234567
- 0x21, 0xE1, 0x8C, 0xD8, 0x68, 0xE9, 0x5B, 0x8F, //1,2,3,4,5,6,7,i
- 0xEE, 0x44, 0x6B, 0xB4, 0xF4, 0x2D, //高音 234567
- 0x47, 0x77, 0xA2, 0xB6, 0xDA, 0xFA, 0x16}; //超高音 1234567
- //一辈子的孤单数据表 要想演奏不同的乐曲, 只需要修改这个数据表
- code unsigned char ybzdgd[] = {
- //一个音符有三个数字。前为第几个音、中为第几个八度、后为时长(以半拍为单位)。
- //6, 2, 3 分别代表:6, 中音, 3个半拍;
- //5, 2, 1 分别代表:5, 中音, 1个半拍;
- //3, 2, 2 分别代表:3, 中音, 2个半拍;
- //5, 2, 2 分别代表:5, 中音, 2个半拍;
- //1, 3, 2 分别代表:1, 高音, 2个半拍;
- //
- 5,0,1, 5,0,1, 3,1,1, 2,1,1,
- 2,1,1, 1,1,1, 3,1,1, 3,1,1, 2,1,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,0x18,0x00,0x60,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x81,0x80,
- 0x81,0x80,0x81,0x80,0x81,0x80,0x81,0x80,0x81,0x80,0x81,0x80,0x81,0x80,0x81,0x80,
- 0x81,0x80,0x81,0x80,0x81,0x80,0x81,0x80,0x81,0x80,0x81,0x80,0x81,0x80,0x81,0x80,
- 0x81,0x80,0xff,0xff,0x00,0x00,0x00,0x1c,0x00,0x22,0x01,0x41,0x81,0x80,0x81,0x80,
- 0x81,0x80,0x81,0x80,0x81,0x80,0x81,0x80,0x81,0x80,0x81,0x80,0x81,0x80,0x81,0x80,
- 0x81,0x80,0x81,0x80,0x81,0x80,0x81,0x80,0x82,0x80,0x44,0x80,0x38,0x80,0x00,0x00,
- /*LOVE*/
- };
- uchar code loveyou[] = {
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x78,0x00,0xFC,0x00,0xFE,0x01,0xFE,0x03,0xFE,0x07,0xFE,0x0F,0xFE,0x1F,0xFC,0x3F,
- 0xF8,0x7F,0xFC,0x3F,0xFE,0x1F,0xFE,0x0F,0xFE,0x07,0xFE,0x03,0xFE,0x01,0xFC,0x00,
- 0x78,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*心形图案*/
- };
- uchar code hehe[] = {
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0x01,0x40,0x01,0xC0,0x01,0x00,0x00,0x00,0x00,
- 0xF0,0x0F,0x08,0x10,0x04,0x20,0x00,0x00,0x00,0x00,0xF0,0x3F,0x08,0x00,0x04,0x00,
- 0x04,0x00,0x04,0x00,0x08,0x00,0xF0,0x3F,0x00,0x00,0x00,0x00,0x00,0x20,0x00,0x20,
- 0x00,0x20,0x00,0x20,0x00,0x20,0x00,0x00,0x00,0x00,0xF0,0x3F,0x08,0x00,0x04,0x00,
- 0x04,0x00,0x04,0x00,0x08,0x00,0xF0,0x3F,0x00,0x00,0x00,0x00,0x04,0x20,0x08,0x10,
- 0xF0,0x0F,0x00,0x00,0x00,0x00,0xC0,0x01,0x40,0x01,0xC0,0x01,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*呵呵o(∩_∩)o图案*/
- };
- unsigned char code hanzi[] = {
- //-- 哈士奇队 --
- 0x00,0x10,0x00,0x60,0x00,0x80,0x00,0x01,
- 0x00,0x02,0xf7,0x0c,0x00,0x02,0x00,0x01,
- 0x00,0x80,0x00,0x60,0x13,0x1c,0xa4,0x02,
- 0xa4,0x02,0x64,0x04,0xf7,0xff,0x00,0x00,
- 0x00,0x00,0x10,0x00,0x10,0xff,0x31,0x20,
- 0x51,0x00,0x91,0xc7,0x13,0x44,0x17,0x44,
- 0x13,0x44,0x91,0x44,0x51,0x44,0x31,0xc7,
- 0x11,0x00,0x10,0x00,0x10,0x00,0x00,0x00,
- 0x00,0x00,0x20,0x00,0x20,0x00,0x20,0x20,
- 0x20,0x20,0x20,0x20,0x20,0x20,0xf7,0xef,
- 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
- 0x20,0x00,0x20,0x00,0x20,0x00,0x00,0x00,
- 0x20,0x00,0x40,0xe7,0x90,0x24,0x11,0x24,
- 0x12,0x24,0x14,0x24,0x12,0x24,0x11,0x24,
- 0x90,0x24,0x40,0xe7,0x20,0x00,0x10,0x8f,
- 0x10,0x80,0x10,0x80,0x10,0x80,0x10,0x8f,
- //-- 摇摇棒!--
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x7C,0x10,0xFE,0x3B,
- 0xFE,0x3B,0x7C,0x10,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x40,0x02,0x44,0x06,0x56,0x12,
- 0x54,0x15,0xD4,0x14,0x5F,0xFF,0x74,0x14,
- 0xD4,0x15,0x44,0x12,0x54,0x05,0x90,0x00,
- 0xFF,0xFF,0xD0,0x00,0x10,0x03,0x10,0x04,
- 0x00,0x00,0x00,0x04,0x82,0x06,0x9B,0xF4,
- 0xA2,0x44,0x82,0x44,0xBC,0x7F,0x84,0x44,
- 0xB4,0x44,0xCC,0xF4,0x54,0x05,0x90,0x04,
- 0xFF,0x7F,0x10,0x81,0x10,0x42,0x10,0x02,
- 0x00,0x00,0x00,0x04,0x82,0x06,0x9B,0xF4,
- 0xA2,0x44,0x82,0x44,0xBC,0x7F,0x84,0x44,
- 0xB4,0x44,0xCC,0xF4,0x54,0x05,0x90,0x04,
- 0xFF,0x7F,0x10,0x81,0x10,0x42,0x10,0x02,
- };
- /*****函数声明*****/
- void display1(void);
- void display2(void);
- void display3(void);
- void display4(void);
- /*****n(us)延时子程序*****/
- void DelayUs(uint N) //亮灯的延时程序
- {
- uint x;
- for(x=0; x<=N;x++);
- }
- void delay(unsigned char t) //延时程序,控制发音的时间长度
- {
- uchar t1;
- unsigned long t2;
- for(t1=0;t1<t;t1++) //双重循环, 共延时t个半拍
- for(t2=0;t2<8000;t2++); //延时期间, 可进入T0中断去发音
- TR0=0; //关闭T0, 停止发音
- }
- void song() //演奏一个音符
- {
- TH0 = timer0h; //控制音调
- TL0 = timer0l;
- TR0 = 1; //启动T0, 由T0输出方波去发音
- delay(time); //控制时间长度
- }
- /*****中断服务程序*****/
- void t0int() interrupt 1 using 2 //T0中断程序,控制发音的音调
- {
- TR0 = 0; //先关闭T0
- speaker = !speaker; //输出方波, 发音
- TH0 = timer0h; //下次的中断时间, 这个时间, 控制音调高低
- TL0 = timer0l;
- TR0 = 1; //启动T0
- }
- void intersvr0(void) interrupt 0 using 1
- {
- KY=~KY; //每个摇动来回水银开关会在摆幅两端分别产生下降沿中断,只提取其中一次(从左向右摇才显示)
- if(KY==0)
- {
- num++; //计算中断次数
- switch(pic) //选择画面
- {
- case 0:{display1();}break;
- case 1:{display2();}break;
- case 2:{display3();}break;
- case 3:{display4();}break;
- default:{display1();}
- }
- }
- }
- /*****显示子程序1(汉字)*****/
- void display1(void)
- {
- uchar i;
- if(num>10){disp++;num=0;} //12个汉字分为3次显示完(每次显示4个),每中断10次切换
- if(disp>1)disp=0;
- DelayUs(6000); //此处延时时间依各硬件差别而各不相同,试着调整使得显示内容居中即可
- for(i=0;i<64;i++)
- {
- P0=~hanzi[disp*128+i*2];
- P1=~hanzi[disp*128+i*2+1];
- DelayUs(80);
- }
- }
- /*****显示子程序2(LOVE)*****/
- void display2(void)
- {
- uchar i;
- DelayUs(4000);
- for(i=0;i<64;i++)
- {
- P0=~love[i*2];
- P1=~love[i*2+1];
- DelayUs(120);
- }
- }
- /*****显示子程序3(心形图案)*****/
- void display3(void)
- {
- uchar i;
- DelayUs(4000);
- for(i=0;i<64;i++)
- {
- P0=~loveyou[i*2];
- P1=~loveyou[i*2+1];
- DelayUs(120);
- }
- }
- /*****显示子程序4(呵呵o(∩_∩)o图案)*****/
- void display4(void)
- {
- uchar i;
- DelayUs(4000);
- for(i=0;i<64;i++)
- {
- P0=~hehe[i*2];
- P1=~hehe[i*2+1];
- DelayUs(120);
- }
- }
- /*****主函数*****/
- void main(void)
- {
- uchar k,i;
- PT0=1;
- TMOD=1; //置T0定时工作方式1
- ET0=1; //开T0中断
- EA=1; //开CPU中断
- IT0=1;
- EX0=1;
- KY=0;
- while(1)
- {
- if(KEY==0) //画面切换键按下
- {
- DelayUs(10000); //按键去抖
- if(KEY==0);
- pic++;
- }
- if(pic>3)
- pic=0;
- i=0;
- time=1;
- while(time)
- {
- k=sszymmh[i]+7*ybzdgd[i+1]-1/;/第i个是音符, 第i+1个是第几个八度
- timer0h=FREQH[k]; //从数据表中读出频率数值
- timer0l=FREQL[k]; //实际上, 是定时的时间长度
- time=ybzdgd[i+2]; //读出时间长度数值
- i+=3;
- song(); //发出一个音符
- }
- }
- }
- /*****END*****/
一边音乐一边摇摇会不会有点抢时间?
受教了
好东西
我也想做个这东西