|
|
原C程序代碼如下:僅供學(xué)習(xí)之用。。
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit SPK = P2^7;
uchar tone_h; //高音
uchar tone_l; //低音
uint note = 1; //音符
uchar beat; //節(jié)拍
uchar code chuzhi[3][16]={ //音調(diào)對應(yīng)的計數(shù)初值
0xff,0xff,//占0位
0xf8,0x8c,//低1
0xf9,0x5b,// 2
0xfa,0x15,// 3
0xfa,0x67,// 4
0xfb,0x04,// 5
0xfb,0x90,// 6
0xfc,0x0c,//低7
0xff,0xff,//占0位
0xfc,0x44,//中1
0xfc,0xac,// 2
0xfd,0x09,// 3
0xfd,0x34,// 4
0xfd,0x82,// 5
0xfd,0xc8,// 6
0xfe,0x06,//中7
0xff,0xff,//占0位
0xfe,0x22,//高1
0xfe,0x56,// 2
0xfe,0x85,// 3
0xfe,0x9a,// 4
0xfe,0xc1,// 5
0xfe,0xe4,// 6
0xff,0x03 //高7
};
//****** 測試曲譜 ****** 個位:音調(diào) ,十位:低中高音 對應(yīng) 0 1 2,千百位:節(jié)拍 1~16
uint code test2[] = { 120, //曲速 暫時未用到
401,402,403,404,405,406,407,
611,612,613,614,615,616,617,
821,822,823,824,825,826,827,
0xffff //0xffff表示結(jié)尾
};
//****** 生日快樂 ******
uint code test[] = { 120,
205,205,406,405,411,807,
205,205,406,405,412,811,
205,205,415,413,411,407,406,
314,114,413,411,413,812,
305,105,406,405,411,807,
305,105,406,405,412,811,
305,105,415,413,411,
407,406,314,114,413,411,412,811,410,
0xffff
};
void delayms(uint ms) /*ms 等于1時,延時1ms 等于2時,延時2ms,以此類推*/
{
uchar a,b,c; //+3
while(ms--)
{
for(c=1;c>0;c--) // 1+2 = 3
for(b=142;b>0;b--) // 1*( 1+142*2)) = 285
for(a=2;a>0;a--); // 142*(1+2*2) = 710
}
}
void delay125ms(uint pai) //延時 ?*125ms 即?個節(jié)拍
{
uchar a,b,c;
while(pai--)
{
for(c=239;c>0;c--)
for(b=104;b>0;b--)
for(a=1;a>0;a--);
}
}
void timer0() interrupt 1
{
TH0 = tone_h;
TL0 = tone_l;
SPK = ~SPK;
}
void timer_init()
{
EA = 1;
ET0 = 1;
PT0 = 1;
TMOD = 0x11;
TH0 = 255;
TL0 = 255;
}
void main()
{
timer_init();
note = 1;
while(1)
{
if(test[note] == 0xffff)
{
note = 1;
}
if(test[note]%10 != 0) //音調(diào)不為0 【音調(diào)為0時表示不發(fā)聲,但有節(jié)拍】
{
tone_h = TH0 = chuzhi[test[note]%100/10][(test[note]%10)*2 ]; //音調(diào)高位
tone_l = TL0 = chuzhi[test[note]%100/10][(test[note]%10)*2 + 1]; //音調(diào)低位
TR0 = 1;
}
delay125ms(test[note]/100); //節(jié)拍
TR0 = 0;
delayms(30);
note++; //下一個音符
}
}
|
-
-
生日快樂歌proteus仿真.rar
2019-6-27 20:49 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
40.6 KB, 下載次數(shù): 37, 下載積分: 黑幣 -5
51單片機仿真實例
|