单片机音乐盒仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include<reg51.h>
- int code Yinfu[]={63628,63835,64021,64103,64260,64400,64524,64580};
- unsigned char duanma[10]={0xc0,0xf9,0xa4,0XB0,0x99,0x92,0x82,0xF8,0x80,0x90};
- sbit beep = P3^3;
- unsigned char KEY;
- void delay()
- {
- int i;
- for(i=0;i<240;i++);
- }
- void play() interrupt 1
- {
- TH0 = Yinfu[KEY]/256;
- TL0 = Yinfu[KEY]%256;
- beep = ~beep;
- }
- unsigned char scan()
- {
- unsigned char res;
- switch(P1)
- {
- case 0xfe:res=0;P0=duanma[1];break;
- case 0xfd:res=1;P0=duanma[2];break;
- case 0xfb:res=2;P0=duanma[3];break;
- case 0xf7:res=3;P0=duanma[4];break;
- case 0xef:res=4;P0=duanma[5];break;
- case 0xdf:res=5;P0=duanma[6];break;
- case 0xbf:res=6;P0=duanma[7];break;
- case 0x7f:res=7;P0=duanma[8];break;
- }
- return res;
- }
- void main()
- {
- TMOD = 0x01;
- IE = 0x82;
- while(1)
- {
- P1 = 0xff;
- if(P1 != 0xff)
- {
- delay();
- if(P1 != 0xff)
- {
- KEY = scan();
- TR0 = 1;
- }
- }
- else
- {
- TR0 = 0;
- beep = 1;
- }
- delay();
- }
- }
复制代码- #include <reg51.h>
- sbit speaker=P3^6;
- unsigned char timer0h,timer0l,time,led=1,j=0;
- unsigned char flagd=0;
- //世上只有妈妈好数据表
- code unsigned char sszymmh[]={ 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
- } ;
- // 音阶频率表 高八位
- code unsigned char FREQH[]={
- 0xF2,0xF3,0xF5,0xF5,0xF6,0xF7,0xF8,
- 0xF9,0xF9,0xFA,0xFA,0xFB,0xFB,0xFC,0xFC, //1,2,3,4,5,6,7,8,i
- 0xFC,0xFD,0xFD,0xFD,0xFD,0xFE,
- 0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF,
- } ;
- // 音阶频率表 低八位
- code unsigned char FREQL[]={
- 0x42,0xC1,0x17,0xB6,0xD0,0xD1,0xB6,
- 0x21,0xE1,0x8C,0xD8,0x68,0xE9,0x5B,0x8F, //1,2,3,4,5,6,7,8,i
- 0xEE,0x44, 0x6B,0xB4,0xF4,0x2D,
- 0x47,0x77,0xA2,0xB6,0xDA,0xFA,0x16,
- };
- void delay(unsigned char t)
- {
- unsigned char t1;
- unsigned long t2;
- for(t1=0;t1<t;t1++)
- {
- for(t2=0;t2<6000;t2++)
- {
- ;
- }
- }
- TR0=0;
- }
- void t0int() interrupt 1
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
单片机期中.rar
(77.18 KB, 下载次数: 91)
|