Current location - Music Encyclopedia - QQ Music - Looking for the C code for music playback on a microcontroller
Looking for the C code for music playback on a microcontroller

#include "reg52.h"

unsigned char Count;

sbit _Speak =P2^3; //Speaker control pin

< p>unsigned char code SONG[] ={ //Wish you peace

0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80,0x26,0x20, 0x30,0x20,

0x30,0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x20,

0x20,0x80,0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x20,

0x30,0x20,0x30,0x20,0x39,0x10 ,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x20,

0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x80,0x26, 0x20,0x2b,0x10,0x2b,0x10,

0x2b,0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x20,< /p>

0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x20,

0x20,0x20,0x20 ,0x40,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20,

0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c,0x20, 0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10,

0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26,0x10,0x26,0x10,0x2b,0x10 ,0x2b,0x80,

0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,0x10,0x2b,0x20,0x30,0x20,

0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30,0x10,0x30,0x20,

0x39,0x20,0x26,0x10,0x2b, 0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,0x10,

0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x00,

//Don’t pick wild flowers on the roadside

0x30,0x1C,0x10,0x20,0x40,0x1C,0x10,0x18,0x10,0x20,0x10,0x1C,0x10,0x18,0x40, 0x1C,

0x20,0x20,0x20,0x1

C,0x20,0x18,0x20,0x20,0x80,0xFF,0x20,0x30,0x1C,0x10,0x18,0x20,

0x15,0x20,0x1C,0x20,0x20,0x20,0x26,0x40,0x20 ,0x20,0x2B,0x20,0x26,0x20,0x20,0x20,

0x30,0x80,0xFF,0x20,0x20,0x1C,0x10,0x18,0x10,0x20,0x20,0x26,0x20,0x2B, 0x20,0x30,

0x20,0x2B,0x40,0x20,0x20,0x1C,0x10,0x18,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,

0x20,0x2B,0x40,0x20,0x30,0x1C,0x10,0x18,0x20,0x15,0x20,0x1C,0x20,0x20,0x20,0x26,

0x40,0x20,0x20,0x2B,0x20,0x26 ,0x20,0x20,0x20,0x30,0x80,0x20,0x30,0x1C,0x10,0x20,

0x10,0x1C,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,0x20, 0x2B,0x40,0x20,0x15,0x1F,

0x05,0x20,0x10,0x1C,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,0x20,0x2B,0x40,0x20,< /p>

0x30,0x1C,0x10,0x18,0x20,0x15,0x20,0x1C,0x20,0x20,0x20,0x26,0x40,0x20,0x20,0x2B,

0x20,0x26,0x20 ,0x20,0x20,0x30,0x30,0x20,0x30,0x1C,0x10,0x18,0x40,0x1C,0x20,0x20,

0x20,0x26,0x40,0x13,0x60,0x18,0x20,0x15, 0x40,0x13,0x40,0x18,0x80,0x00,

};

void Time0_Init()

{

TMOD = 0x01 ;

IE = 0x82;

TH0 = 0xD8;

TL0 = 0xEF; //12MZ crystal oscillator, 10ms

}

void Time0_Int() interrupt 1

{

TH0 = 0xD8;

TL0 = 0xEF;

Count++; //Add 1 to the length

}

/*---------------------------- ---------------------

Function: 1MS delay subroutine

-------- --------------------------------------------------*/

void Delay_xMs(unsigned int x)

{

unsigned int i,j;

for( i =0;i < x;i++ )

{

for( j =0;j<3;j++ );

}

}

/

*------------------------------------------------

Function: Which track is played by the song playback subroutine i

------------------------ -----------------------*/

void Play_Song(unsigned char i)

{< /p>

unsigned char Temp1,Temp2;

unsigned int Addr;

Count = 0; //Clear the interrupt counter to 0

Addr = i * 217;

while(1)

{

Temp1 = SONG[Addr++];

if ( Temp1 == 0xFF ) //Rest

{

TR0 = 0;

Delay_xMs(100);

}

else if ( Temp1 == 0x00 ) //Song end character

{

return;

}

else

< p> {

Temp2 = SONG[Addr++];

TR0 = 1;

while(1)

{

_Speak = ~_Speak;

Delay_xMs(Temp1);

if ( Temp2 == Count )

{

Count = 0;

break;

}

}

}

}

< p>}

/*---------------------------------------------- ----------

Function: Main program

--------------------- ----------------------------*/

void main()

{< /p>

Time0_Init(); //Timer 0 interrupt initialization

while(1)

{

Play_Song(0); // Play

}

}