Current location - Music Encyclopedia - NetEase Cloud Music - The buzzer of single chip microcomputer plays music wave by wave. How can you tell how many beats there are in the score?
The buzzer of single chip microcomputer plays music wave by wave. How can you tell how many beats there are in the score?
/* Note frequency correspondence table

Note frequency /HZ half period /us(N)

-

● 1do 262 1908 # 1do # 277 1805

●2RE 294 1700 # 2RE # 3 1 1 1608

Low 3mi3301516 # 3mi # 3401470

●4fa 349 1433 # 4fa # 370 1350

●5SO 392 1276 # 5SO # 4 15 1205

●6LA 440 1 136 # 6LA # 466 1072

●7si 494 10 12 # 7si # 524 0954

Zhong1do5230956 #1do # 5540903

Clock 2RE 578 0842 #2RE# 622 0804

Zhong Sanmi 659 0759 # Sanmi # 682 0733

Zhong 4FA 698 07 16 #4FA# 740 0676

Medium-sized 5SO 784 0638 #5SO# 83 1 0602

Clock 6LA 880 0568 #6LA# 932 0536

7SI 988 0506 #7SI# 1046 478。

Height1do1046478 #1do #1109451.

Gao 2Re1175426 # 2Re #1245402

Gao 3mi1318372 # 3mi #1356368

Gao 4fa1397358 # 4fa #1480338

Gao 5SO1568 319 # 5S0 #1661292

Gao 6la1760284 # 6la1865268

Gao 7SI 1976 253 #7SI# 2066 242

-

("#" means semitone, which is used to raise semitone) */

/* - */

# include & ltreg52.h & gt// contains header files, which generally do not need to be changed. The header file contains definitions of special function registers.

# Define uint unsigned integer

# Define uchar unsigned characters

sbit spk=p 1^2; //Define the horn port

Unsigned int I;; //Define global variables

Uchar table[ 14]={ // the initial value of the timer corresponding to the alto scale.

0xfc,0x44,// 1

0xfc,0xb6,//2

0xfd,0x09,//3

0xfd,0x34,//4

0xfd,0x82,//5

0xfd,0xc8,//6

0xfe,0x06 //7

};

/******************************************************************/

/* Delay function statement */

/******************************************************************/

Void delayms(unsigned int xms) // millisecond delay

{

uint i,j;

for(I = xms; I>0; I-)

for(j = 125; j & gt0; j-);

}

Void init() // timer initialization function

{

TMOD | = 0x 0 1; //Timer working mode, Timer 0 is used for timing.

TH0 = Table [0]; //Timer scale 1 corresponds to the initial value, which is 0xfc high and 0x44 low.

TL0 = table[ 1];

ET0 = 1; //Timer 0 allows

EA = 1; //Open host interrupt

}

/******************************************************************/

/* Main function */

/******************************************************************/

Master ()

{

init(); //Timer initialization

while( 1)

{

for(I = 0; I<7; I++) // Play 7 scales in a loop.

{

TR0 = 1; //Start Timer 0, start counting, and generate the horn driving frequency.

Delems (500); //Each scale lasts about the playing time.

TR0 = 0; //Stop timing

delayms( 1000); //The horn stops working, and the intermittent time is about 1s, which can be changed.

}

}

}

Invalid timer 0 () interrupt 1 // timer interrupt function

{

TH0 = table[I * 2]; //Decimal digits 1234567 corresponds to the high value of the initial value of the timer.

//table[i*2] means the number 02468101214 in the table[] array.

TL0 = table[I * 2+ 1]; //table[i*2+ 1] indicates the value of1357911315 in the table[] array.

SPK = ~ SPK; //When the timing is up, the horn level is reversed to realize frequency drive.

}