文档详情

51单片机电子程序.doc

发布:2017-09-06约9.33千字共13页下载文档
文本预览下载声明
#includereg51.h #includeabsacc.h #includestdio.h #includemath.h #define uchar unsigned char #define uint unsigned int uchar STH0; //定时器计数初值 uchar STL0; bit FY=0; //放乐曲时FY=1,电子琴弹奏时FY=0 uchar Song_Index=0,Tone_Index=0; //放音乐的参数 uchar k,key; sbit SPK=P3^7; sbit LED1=P1^0; sbit LED2=P1^1; uchar code DSY_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6f,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; uchar code Song[][50]={{1,2,3,5,7,8,4,3,4,3,4,5,4,6,3,4,5}, {5,5,3,5,4,2,4,5,7,4,2,10,10,10,2,1,2,1,2,10,10}, {5,5,10,9,8,5,5,5,5,10,9,8,6,6,6,11,12,9,6,8-1}, {13,14,13,12,12,10,12,13,14,15,14,14}, {6,6,11,10,9,12,12,12,12,13,12,11,9,8,10,10,10,-1}, {9,13,13,13,13,8,13,13,13,13,14,15,14,13,13,14,12,13}, }; uchar code Len[][50]={{1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,1,1,1,2,2,2,1,2,2,1,2,2}, {1,1,1,1,1,1,2,1,1,1,2,2,1,1,1,1,-1}, {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,-1}, {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,-1}, {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,-1}, {1,1,2,0,1,1,2,0,1,1,1,1,1,1,1,1,1,1,1}, {1,1,1,1,1,1,1,1,2,0,1,2,1,2,1,2,1,2,1,2}, {2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,-1},}; //音符与计数值对应表 uint code tab[]={0,63628,63835,64021,64103,64260,64400, 64524,64580,64684,64777, 64820,64898,64968,65030, 65058,65110,65157,65178,65217,65252,65283}; void delay1(uint ms) //播放歌曲时实现节拍的延时函数 { uchar t; while(ms--) for(t=0;t120;t++); } //键消抖延时函数 void delay(void) { uchar i; for(i=300;i0;i--); } //键扫描函数 uchar getkey(void) { uchar scancode,tmpcode; if((P00xf0)==0xf0) return(0); scancode=0xfe; while((scancode0x10)!=0) { P0=scancode; //输入行扫描码 if((P00xf0)!=0xf0) //本行有键按下 { tmpcode=(P00xf0)|0x0f; return((~scancode)+~(t
显示全部
相似文档