|
- #include <STC15F2K60S2.H>
- #include<intrins.h>
- //***************************用户数据类型定义*********************************
- typedef unsigned int uint;
- typedef unsigned char uchar;
- //*****************************用户引脚定义***********************************
- sbit led_dr = P3^3;
- sbit beep_dr = P3^7;
- //***************************共阳极数码管段码数组*****************************
- uchar code DigSegBuff[] =
- {
- 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90
- };
- //******************************函数声明区************************************
- void DelayMs(uint uiXms); //有阻塞延时函数声明
- void IniSystem(); //系统初始化函数声明
- void IniPeripheral(); //外围器件初始化函数声明
- void LedFlick(uint uiLedFlickNum);//LED闪烁函数声明
- void DelayDo_956us(); //发do音的延时函数声明
- void DelayRe_852us(); //发re音的延时函数声明
- void DelayMi_759us(); //发mi音的延时函数声明
- void DelayFa_716us(); //发fa音的延时函数声明
- void DelaySo_638us(); //发so音的延时函数声明
- void DelayLa_568us(); //发la音的延时函数声明
- void DelayXi_507us(); //发xi音的延时函数声明
- void DelaySo_1276us(); //发低音SO的延时函数声明
- void KeySongplay(); //按键发声函数声明
- //******************************主函数区*************************************
- void main()
- {
- IniSystem();
- DelayMs(200);
- IniPeripheral();
- DelayMs(200);
- while(1)
- {
- KeySongplay();
- }
- }
- //*********************函数定义区*********************************************
- void DelayMs(uint uiXms)//有阻塞延时函数定义
- {
- uint h,l;
- for(h=uiXms;h>0;h--)
- {
- for(l=110;l>0;l--);
- }
- }
- void IniSystem() //系统初始化函数定义
- {
- P2 = 0xff;
- P1 = 0xff;
- led_dr = 1;
- beep_dr = 0;
- }
- void IniPeripheral()//外围器件初始化函数定义
- {
- LedFlick(5);
- }
- void LedFlick(uint uiLedFlickNum)//LED闪烁函数定义
- {
- uint k;
- for(k=uiLedFlickNum*2;k>0;k--)
- {
- led_dr = ~led_dr;
- DelayMs(400);
- }
- }
- void DelayDo_956us() //发do音的延时
- {
- unsigned char i, j;
- i = 12;
- j = 37;
- do
- {
- while (--j);
- } while (--i);
- }
- void DelayRe_852us() //发re音的延时
- {
- unsigned char i, j;
- i = 10;
- j = 239;
- do
- {
- while (--j);
- } while (--i);
- }
- void DelayMi_759us() //发mi音的延时
- {
- unsigned char i, j;
- i = 9;
- j = 217;
- do
- {
- while (--j);
- } while (--i);
- }
- void DelayFa_716us() //发fa音的延时
- {
- unsigned char i, j;
- i = 9;
- j = 88;
- do
- {
- while (--j);
- } while (--i);
- }
- void DelaySo_638us() //发so音的延时
- {
- unsigned char i, j;
- i = 8;
- j = 111;
- do
- {
- while (--j);
- } while (--i);
- }
- void DelayLa_568us() //发la音的延时
- {
- unsigned char i, j;
- i = 7;
- j = 158;
- do
- {
- while (--j);
- } while (--i);
- }
- void DelayXi_507us() //发xi音的延时
- {
- unsigned char i, j;
- i = 6;
- j = 232;
- do
- {
- while (--j);
- } while (--i);
- }
- void DelaySo_1276us() //发低音so音延时
- {
- unsigned char i, j;
- i = 15;
- j = 226;
- do
- {
- while (--j);
- } while (--i);
- }
- void KeySongplay()//按键发声函数定义
- {
- while(P1 == 0xff);
- DelayMs(10);
- if(P1 != 0xff)
- {
- switch(P1)
- {
- case 0xfe:
- P2 = DigSegBuff[1];
- while(P1==0xfe)
- {
- beep_dr = ~beep_dr;
- DelayDo_956us(); //do
- }
- beep_dr = 0;
- break;
- case 0xfd:
- P2 = DigSegBuff[2];
- while(P1==0xfd)
- {
- beep_dr = ~beep_dr;
- DelayRe_852us(); //re
- }
- beep_dr = 0;
- break;
- case 0xfb:
- P2 = DigSegBuff[3];
- while(P1==0xfb)
- {
- beep_dr = ~beep_dr;
- DelayMi_759us(); //发mi音
- }
- beep_dr = 0;
- break;
- case 0xf7:
- P2 = DigSegBuff[4];
- while(P1==0xf7)
- {
- beep_dr = ~beep_dr;
- DelayFa_716us(); //发fa音
- }
- beep_dr = 0;
- break;
- case 0xef:
- P2 = DigSegBuff[5];
- while(P1==0xef)
- {
- beep_dr = ~beep_dr;
- DelaySo_638us(); //发so音
- }
- beep_dr = 0;
- break;
- case 0xdf:
- P2 = DigSegBuff[6];
- while(P1==0xdf)
- {
- beep_dr = ~beep_dr;
- DelayLa_568us(); //发la音
- }
- beep_dr = 0;
- break;
- case 0xbf:
- P2 = DigSegBuff[7];
- while(P1==0xbf)
- {
- beep_dr = ~beep_dr;
- DelayXi_507us(); //发xi音
- }
- beep_dr = 0;
- break;
- case 0x7f:
- P2 = DigSegBuff[5];
- while(P1==0x7f)
- {
- beep_dr = ~beep_dr;
- DelaySo_1276us(); //do
- }
- beep_dr = 0;
- break;
- }
- }
- }
复制代码
|
|