Current location - Education and Training Encyclopedia - Graduation thesis - Single chip microcomputer music box paper
Single chip microcomputer music box paper
# Including

# Define uchar unsigned characters

# Define uint unsigned integer

int cnt_h,CNT _ l;

sbit spk=p2^3;

//sbit led=p2^0;

uchar cnt,I,step,s _ bit

Code uintyin [7] = {1912170416143212761136/. // 1: low octave 3: high octave

//"Welcome to Beijing" symbol code

Code uint song[]={ 123, 125, 123, 122, 123, 123.

, 122, 12 1, 1 16, 12 1, 122, 123, 125, 122, 123, 126, 125, 125, 123, 123, 122, 123, 122, 12 1, 125, 126,222, 1 15, 123, 123,222,223,

1 16, 123, 122, 122,22 1,423,425, 13 1, 125,226,226, 125, 123, 123, 125,225, 123, 125, 126, 13 1, 132, 13 1, 125, 123, 122, 125,223,223, 123, 125, 13 1, 125,226,23 1, 132, 13 1, 125, 123, 125, 127,226,223, 122, 123, 125, 133,232, 127,33 1,999};

Invalid delay (uinta); //

void init(); //notation code explanation: each tone and its beat are represented by a three-digit number.

Void main() // The hundredth digit of three digits represents an integer multiple of the rhythm.

{//Ten digits indicate pitch (1 bass, 3 for treble).

init(); //Numbers indicate which basic phoneme it is.

while( 1)

{

step =(song[I]/ 100)* 4; //Changing 2 to other numbers can change the beat.

//th 1 =(65536-65535)/256;

//TL 1 =(65536-65535)% 256;

Switch ((song[i]/ 10)% 10)

{

Case 1:

{

Cnt_h=- (Yin [Song [I]%10-1] * 2)/256;

Cnt_l=- (Yin [Song [i]%10-1] * 2)% 256;

Break;

}

Case 2:

{

CNT _ h =-yin[song[I]% 10- 1]/256;

CNT _ l =-yin[song[I]% 10- 1]% 256;

Break;

}

Case 3:

{

Cnt_h=- (Yin [Song [I]%10-1]/2)/256;

Cnt_l=- (Yin [Song [i]%10-1]/2)% 256;

Break;

}

}

TH0 = CNT _ h;

TL0 = CNT _ l;

tr 1 = 1;

TR0 = 1;

Next:

while(cnt! = step)

Go to the next;

//led = ~ led;

TR0 = 0;

CNT = 0;

tr 1 = 0;

Delay (200);

i++;

If (Song [i]==999)

{

I = 0;

Delay (900);

}

}

}

Void initialization ()

{

EA = 1;

ET0 = 1;

et 1 = 1;

TMOD = 0x 1 1;

I = 0;

CNT = 0;

TR0 = 0;

tr 1 = 0;

}

Void t0(void) interrupt 1

{

TH0 = CNT _ h;

TL0 = CNT _ l;

spk = ~ spk

}

Void t 1 (invalid) interrupt 3

{

//th 1 =(65536-65535)/256;

//TL 1 =(65536-65535)% 256;

cnt++;

}

Invalid delay

{

uint q,k;

for(q = 0; q & lt84; q++)

for(k = 0; k & lta; k++);

}

===================

I wrote it a long time ago. Take a look.