基于单片机的电子琴(电子系统设计的题目),希望可以帮到大家
制作成功的实物图:
Altium Designer画的电子琴原理图和PCB图如下:(51hei附件中可下载工程文件)
单片机源程序如下:
- #include <reg51.h>
- #include "lcd.h"
- #include "key.h"
- //---重定义关键词---//
- #ifndef u8
- #define u8 unsigned char
- #endif
- #ifndef u32
- #define u32 unsigned int
- #endif
- sbit BEEP = P2^0;
- u8 m,n;
- u8 i=0;
- u8 play_flag = 1;
- u8 num;
- u8 code T[49][2]={{0,0},
- {0xF8,0x8B},{0xF8,0xF2},{0xF9,0x5B},{0xF9,0xB7},{0xFA,0x14},{0xFA,0x66},{0xFA,0xB9},{0xFB,0x03},{0xFB,0x4A},{0xFB,0x8F},{0xFB,0xCF},{0xFC,0x0B},
- {0xFC,0x43},{0xFC,0x78},{0xFC,0xAB},{0xFC,0xDB},{0xFD,0x08},{0xFD,0x33},{0xFD,0x5B},{0xFD,0x81},{0xFD,0xA5},{0xFD,0xC7},{0xFD,0xE7},{0xFE,0x05},
- {0xFE,0x21},{0xFE,0x3C},{0xFE,0x55},{0xFE,0x6D},{0xFE,0x84},{0xFE,0x99},{0xFE,0xAD},{0xFE,0xC0},{0xFE,0x02},{0xFE,0xE3},{0xFE,0xF3},{0xFF,0x02},
- {0xFF,0x10},{0xFF,0x1D},{0xFF,0x2A},{0xFF,0x36},{0xFF,0x42},{0xFF,0x4C},{0xFF,0x56},{0xFF,0x60},{0xFF,0x69},{0xFF,0x71},{0xFF,0x79},{0xFF,0x81}
- };
- u8 code music_xyj[][2]=
- {
- {17,8},{17,8},{10,16},{13,8},{10,4},{8,4},{10,8},{0,8},
- {10,4},{13,4},{13,4},{13,4},{13,8},{13,4},{10,4},{13,8},
- {15,4},{17,4},{17,16},{17,8},{20,8},{10,8},{13,8},{10,8},
- {10,4},{8,4},{5,16},{15,4},{15,4},{15,4},{17,2},{17,2},
- {8,8},{12,8},{10,24},{22,4},{22,4},{22,4},{22,4},{17,8},
- {20,2},{20,6},{20,4},{20,4},{17,4},{22,4},{20,4},{0,8},
- {18,4},{18,4},{18,4},{18,4},{18,8},{13,2},{13,6},{13,4},
- {15,4},{13,4},{15,4},{17,12},{22,4},{22,4},{22,4},{22,4},
- {17,8},{20,2},{20,6},{20,4},{20,4},{17,4},{22,4},{20,4},
- {0,8},{22,4},{22,4},{22,4},{22,4},{22,2},{20,6},{20,4},
- {20,4},{20,4},{17,4},{20,12},{15,4},{15,4},{13,4},{15,4},
- {17,4},{15,4},{13,4},{15,2},{15,2},{17,4},{15,4},{17,4},
- {20,20},{13,4},{15,4},{17,4},{20,4},{20,8},{12,8},{10,24},
- {0xFF,0xFF}
- };
- u8 code music_htys[][2] =
- {
- {0,4},{13,4},{13,8},{10,4},{8,8},{10,8},{13,8},{13,4},
- {15,4},{17,16},{15,4},{15,8},{13,4},{13,4},{15,8},{15,4},
- {20,4},{20,4},{17,4},{17,4},{15,4},{17,16},{13,4},{13,8},
- {10,4},{8,8},{20,8},{17,4},{15,4},{17,4},{15,4},{13,16},
- {15,4},{15,8},{13,4},{15,4},{15,8},{17,4},{15,4},{13,4},
- {10,4},{15,4},{13,16},{13,4},{13,8},{10,4},{8,8},{10,8},
- {13,4},{13,8},{15,4},{17,16},{15,4},{15,8},{13,4},{15,8},
- {15,4},{20,4},{20,4},{17,4},{17,4},{15,4},{17,16},{13,4},
- {13,4},{13,4},{10,4},{8,8},{8,8},{17,4},{15,4},{17,4},
- {15,4},{13,16},{15,4},{15,8},{13,4},{15,4},{15,8},{17,4},
- {15,4},{13,4},{10,4},{15,4},{13,16},{17,4},{20,8},{20,4},
- {20,8},{20,8},{22,4},{20,4},{17,4},{15,4},{13,16},{22,4},
- {25,4},{22,4},{20,4},{17,4},{15,4},{13,4},{10,4},{15,8},
- {15,4},{17,4},{17,4},{15,12},{17,4},{20,8},{20,4},{20,8},
- {20,8},{22,4},{20,4},{17,4},{15,4},{13,16},{10,4},{13,4},
- {10,4},{8,4},{15,8},{17,8},{13,24},{0,8},{0xFF,0xFF}
- };
- u8 code music_cq[][2]=
- {
- {0,4},{13,2},{13,2},{13,4},{17,4},{15,4},{15,2},{15,2},
- {13,2},{13,4},{13,2},{15,4},{15,4},{13,2},{10,2},{10,4},
- {10,16},{0,4},{12,2},{12,2},{12,4},{13,2},{15,2},{15,4},
- {12,4},{10,2},{8,4},{5,2},{5,16},{0,8},{0,8},{0,4},
- {17,2},{15,2},{17,4},{17,2},{15,2},{15,4},{15,2},{13,2},
- {13,8},{15,4},{10,4},{10,2},{10,2},{15,2},{13,2},{13,8},
- {0,8},{0,4},{12,2},{12,2},{12,4},{13,2},{15,2},{15,4},
- {15,4},{10,2},{8,6},{5,16},{0,8},{0,8},{20,6},{15,2},
- {15,4},{17,4},{20,6},{15,2},{15,4},{13,4},{10,16},{0,8},
- {0,8},{15,6},{10,2},{10,4},{17,4},{15,6},{13,2},{13,4},
- {13,4},{8,16},{0,8},{0,8},{20,6},{15,2},{15,4},{17,4},
- {20,6},{15,2},{15,4},{13,4},{10,16},{0,8},{0,8},{15,6},
- {10,2},{10,4},{17,4},{15,6},{13,2},{13,4},{13,2},{15,2},
- {15,16},{0,8},{0,8},{0,4},{13,2},{13,2},{13,4},{8,2},
- {13,2},{13,4},{20,4},{18,4},{17,4},{15,6},{13,2},{13,16},
- {0,2},{13,2},{17,2},{20,2},{22,4},{20,2},{23,2},{22,2},
- {20,6},{22,4},{20,2},{17,2},{17,2},{15,4},{17,2},{17,16},
- {0,8},{0,8},{0,4},{13,2},{13,2},{13,4},{8,2},{13,2},
- {13,4},{20,4},{18,4},{17,4},{15,6},{13,2},{13,16},{0,2},
- {13,2},{17,2},{20,2},{22,4},{20,2},{22,2},{22,2},{20,6},
- {22,4},{20,4},{17,2},{20,6},{20,24},{0,4},{13,2},{13,2},
- {13,4},{17,4},{15,4},{15,2},{15,2},{13,2},{13,4},{13,2},
- {15,4},{15,4},{15,2},{13,4},{10,2},{10,16}, {0xFF,0xFF}
- };
- void delay(u8 p)
- {
- u8 i,j;
- for(;p>0;p--)
- for(i=181;i>0;i--)
- for(j=181;j>0;j--);
- }
- void pause()
- {
- u8 i,j;
- for(i=150;i>0;i--)
- for(j=150;j>0;j--);
- }
- void T0_int() interrupt 1
- {
- if((play_flag == 4)&&(num == 0))
- {
- //消除杂音 - 静音
- }else
- {
- BEEP =! BEEP;
- }
-
- if(play_flag != 4)
- {
- TH0 = T[m][0];
- TL0 = T[m][1];
- }else
- {
- switch(num)
- {
- case 1:m=13; TH0=T[m][0];TL0=T[m][1];break; //音调‘1’
- case 2:m=15; TH0=T[m][0];TL0=T[m][1];break; //音调‘2’
- case 3:m=17; TH0=T[m][0];TL0=T[m][1];break; //音调‘3’
- case 4:m=18; TH0=T[m][0];TL0=T[m][1];break; //音调‘4’
- case 5:m=20; TH0=T[m][0];TL0=T[m][1];break; //音调‘5’
- case 6:m=22; TH0=T[m][0];TL0=T[m][1];break; //音调‘6’
- case 7:m=24; TH0=T[m][0];TL0=T[m][1];break; //音调‘7’
- case 8:m=25; TH0=T[m][0];TL0=T[m][1];break; //音调‘8’
- default:break;
- }
- }
- }
- void play_music(u8 _play_flag)
- {
- if(_play_flag == 1)
- {
- m = music_xyj[i][0];
- n = music_xyj[i][1];
- if(m == 0x00)
- {
- TR0=0;
- delay(n);
- i++;
- }else if(m == 0xFF)
- {
- TR0=0;
- delay(30);
- i=0;
- }else if(m == music_xyj[i+1][0])
- {
- TR0=1;
- delay(n);
- TR0=0;
- pause();
- i++;
- }else
- {
- TR0=1;
- delay(n);
- i++;
- }
- }else if(_play_flag == 2)
- {
- m = music_htys[i][0];
- n = music_htys[i][1];
- if(m == 0x00)
- {
- TR0=0;
- delay(n);
- i++;
- }else if(m == 0xFF)
- {
- TR0=0;
- delay(30);
- i=0;
- }else if(m == music_htys[i+1][0])
- {
- TR0=1;
- delay(n);
- TR0=0;
- pause();
- i++;
- }else
- {
- TR0=1;
- delay(n);
- i++;
- }
- }else if(_play_flag == 3)
- {
- m = music_cq[i][0];
- n = music_cq[i][1];
- if(m == 0x00)
- {
- TR0=0;
- delay(n);
- i++;
- }else if(m == 0xFF)
- {
- TR0=0;
- delay(30);
- i=0;
- }else if(m == music_cq[i+1][0])
- {
- TR0=1;
- delay(n);
- TR0=0;
- pause();
- i++;
- }else
- {
- TR0=1;
- delay(n);
- i++;
- }
- }else
- {
- // TR0=0;
- }
- }
- void Timer0Init()
- {
- TMOD &= 0xF0;
- TMOD |= 0x01;
- EA = 1;
- ET0 = 1;
- }
- void IntInit()
- {
- IT0 = 1;
- EX0 = 1;
- IT1 = 1;
- EX1 = 1;
- EA = 1;//打开总中断
- }
- sbit K_1 = P3^2;
- sbit K_2 = P3^3;
- bit updata_flag = 1;
- void main(void)
- {
- Timer0Init();
- IntInit();
- LcdInit();
-
- LcdWriteCom(0x80);
- LcdWriteData('M');
- LcdWriteData('u');
- LcdWriteData('s');
- LcdWriteData('i');
- LcdWriteData('c');
- LcdWriteData(':');
-
- while(1)
- {
- play_music(play_flag);
-
- if(updata_flag == 1)
- {
- updata_flag = 0;
-
- switch(play_flag)
- {
- case 1:
- LcdWriteCom(0x80+6);
- LcdWriteData('x');
- LcdWriteData('y');
- LcdWriteData('j');
- LcdWriteData(' ');
- break;
- case 2:
- LcdWriteCom(0x80+6);
- LcdWriteData('h');
- LcdWriteData('t');
- LcdWriteData('y');
- LcdWriteData('s');
- break;
- case 3:
- LcdWriteCom(0x80+6);
- LcdWriteData('c');
- LcdWriteData('q');
- LcdWriteData(' ');
- LcdWriteData(' ');
- break;
- case 4:
- LcdWriteCom(0x80+6);
- LcdWriteData('n');
- LcdWriteData('u');
- LcdWriteData('l');
- LcdWriteData('l');
- break;
- }
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
14基于C51单片机电子琴设计.zip
(9.55 MB, 下载次数: 45)
|