基于单片机的简易电子琴毕业设计 - 图文

嘉兴学院南湖学院本科生毕业设计

0xfd,0x92, //La 0xfd,0xd0, //Si 0xfd,0xee, //Do#

0x00,0x00 //音符之间的间隔,只要间隔时间小于65ms时,

//喇叭不会发出声音,用作拍子之间的短暂停顿

};

/*****生日快乐歌音调表*****/

unsigned char code shengri_tone[]={ 1,0,1,2,1,4,3,0, 1,0,1,2,1,5,4,0,

1,0,1,8,6,4,3,2,0, 7,0,7,6,4,5,4,0 //0代表不发声,即停顿;数字即为音调

};

/*****生日快乐歌节拍表*****/

unsigned char code shengri_beat[]={ 24,1,24,48,48,48,72,5, 24,1,24,48,48,48,72,5, 24,1,24,48,48,48,48,72,5, 24,1,24,48,48,48,72,5 //节拍,即tone表各音调的延时

};

/*****15ms延时子程序,用于节拍*****/ void delay(void) {

unsigned char n=15; while(n--) {

unsigned char i; for(i=0;i<125;i++); } }

23

嘉兴学院南湖学院本科生毕业设计

/*****定时器0初始化*****/ void initTimer(void) {

TMOD=0x01; //定时器0,工作方式1;定时器1,工作方式1 TH0=tone1; TL0=tone2; }

/*****定时器0中断服务程序*****/ void timer0(void) interrupt 1 {

TH0=tone1; TL0=tone2;

SPK=~SPK; }

/*****演奏子程序1*****/ void play1(void) {

unsigned char m=0; unsigned char s; unsigned char a=1; while(1) {

EA=0; LED=0;

a=shengri_tone[m];

shumaguan=a;

s=shengri_beat[m]; tone1=yinfu[2*a-2]; tone2=yinfu[2*a-1]; EA=1; while(s--) {

24

//取反,以产生方波 //取音符 //取节拍

嘉兴学院南湖学院本科生毕业设计

delay(); }

if((!K1)||(!K2)) //发现按键,立即退出播放

{

EA=0;SPK=0;shumaguan=0x00; m=34; return; } else

LED=1; m++;

if(m>=33) return; //数值是shengri相关表中的元素数量 } }

/*****按键检测*****/ void check_key(void) {

P2=0xff; P3=0xff; //设置为输入状态

switch(P2) //检测按键,输出数码管、载入定时器初值、允许中断

{

case0xfe:shumaguan=0x1;tone1=0xfb;tone2=0xe9;EA=1;break; case0xfd:shumaguan=0x2;tone1=0xfc;tone2=0x5c;EA=1;break; case0xfb:shumaguan=0x3;tone1=0xfc;tone2=0xc1;EA=1;break;

case0xf7:shumaguan=0x4;tone1=0xfc;tone2=0xef;EA=1;break; case0xef:shumaguan=0x5;tone1=0xfd;tone2=0x45;EA=1;break; case0xdf:shumaguan=0x6;tone1=0xfd;tone2=0x92;EA=1;break; case0xbf:shumaguan=0x7;tone1=0xfd;tone2=0xd0;EA=1;break; case0x7f:shumaguan=0x1;tone1=0xfd;tone2=0xee;EA=1;break;

default: EA=0;SPK=0;shumaguan=0x00;//没有键按下则关闭中断和数码管 } switch(P3) {

25

嘉兴学院南湖学院本科生毕业设计

case 0xfb:play1();break; } }

/*****主程序*****/ void main(void) {

initTimer(); shumaguan=0x00; TR0=1; ET0=1; SPK=0; while(1) {

check_key(); } }

26

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4