CODE: [Copy to clipboard]
#include <dos.h>
#define DELAY 168
#define SIZE 130
typedef unsigned char BYTE;
int main(int argc, char *argv[])
{
BYTE music[SIZE] = {
2,2,0,1,12,2,13,4,12,4,15,4,14,4,0,4,
12,2,0,1,12,1,13,4,12,4,16,4,15,4,0,4,
12,2,0,1,12,1,19,4,17,4,15,4,14,4,13,4,
18,2,0,1,18,1,17,4,15,4,16,4,15,4,0,4,
12,2,0,1,12,1,13,4,12,4,15,4,14,4,0,4,
12,2,0,1,12,1,13,4,12,4,16,4,15,4,0,4,
12,2,0,1,12,1,19,4,17,4,15,4,14,4,13,4,
18,2,0,1,15,1,17,4,15,4,16,4,15,4,0,4,
0ffh,0ffh
}
for ( i = 0 ; i < SIZE; i++ )
{
sound(music[i]);
delay(DELAY);
}
nosound();
return 0;
}
应该要把数组里的值再做一下运算,不然这么低的频率应该听不到。。。CODE: [Copy to clipboard]
short table1[] = { // frequency table
// 1 2 3 4 5 6 7
// 82,
// 87, 98, 110, 124, 131, 147, 165,
175, 196, 220, 247, 262, 294, 330,
349, 392, 440, 494, 523, 587, 659,
698, 784, 880, 988, 1047,
};
char table[] = { // music map, pairs of (tone, beat)
12,2,0,1,12,2,13,4,12,4,15,4,14,4,0,4,
12,2,0,1,12,1,13,4,12,4,16,4,15,4,0,4,
12,2,0,1,12,1,19,4,17,4,15,4,14,4,13,4,
18,2,0,1,18,1,17,4,15,4,16,4,15,4,0,4,
12,2,0,1,12,1,13,4,12,4,15,4,14,4,0,4,
12,2,0,1,12,1,13,4,12,4,16,4,15,4,0,4,
12,2,0,1,12,1,19,4,17,4,15,4,14,4,13,4,
18,2,0,1,15,1,17,4,15,4,16,4,15,4,0,4,-1,-1
};
void c51_delay(char beat)
{
int delay;
delay = beat * 187;
// sleep of delay ms
}
void c51_start_timer0()
{
// TR0 = 1;
}
void c51_stop_timer0()
{
// TR0 = 0;
}
void c51_make_sound(char tone)
{
union {
short word;
char byte[2];
}u;
u.word = table1[tone - 1];
// TH0 = u.byte[0];
// TL0 = u.byte[1];
}
void c51_happy_birthday(void)
{
int i;
c51_start_timer0();
for (i = 0; table[i] != -1; i += 2) {
if (table[i] == 0) {
c51_stop_timer0();
}else {
c51_make_sound(table[i]);
c51_start_timer0();
}
c51_delay(table[i + 1]);
}
c51_stop_timer0();
}
#include <windows.h>
void happy_birthday(void)
{
int i;
int delay;
for (i = 0; table[i] != -1; i += 2) {
delay = table[i + 1] * 187;
if (table[i] == 0) {
Sleep(delay);
}else {
Beep(table1[table[i] - 1], delay);
}
}
}
int main(void)
{
happy_birthday();
return 0;
}