当前位置:网站首页>Stc-b learning board buzzer plays music

Stc-b learning board buzzer plays music

2022-07-06 15:00:00 Wuhu hanjinlun

function : After the single chip computer burns the program , Press key1 Key play 《IF YOU》, Press again key1 Pause the playback , Press key2 You can switch songs to 《 Internationale 》, Press again key3 You can switch songs to 《 Lone brave 》

chip :STC15F2K61S2

Complete engineering documents and hex The document is here
Extraction code :1111

Preface : I believe that your favorite thing to play after welding the board is the buzzer playing music , Every classroom passing by is basically full of all kinds of noise , After all, this thing is really fun , So I wrote a file to play other songs according to the teacher's code and score ( International song is the music code of my previous senior students , Let's make up the number ), To be honest, this thing looks very simple , It's actually quite difficult , I have to learn music theory again , Then we can get an ideal state by constantly adjusting to various beats and tones , however if you And the lonely brave always feel that some places are not perfect , But for lousy people, it's OK to listen . Anyway, it's also entertainment

I don't say much nonsense , The code is as follows :( The score is at the end )

/**********************
 File name :beep.c
 explain : Routine for playing music , Press down key1 Key to start playing music 
 Modify the record :
***********************/

/**********************
 be based on STC15F2K60S2 Series MCU C Language programming implementation 
 Use the following header file , There is no need to include "REG51.H"
***********************/
#include <STC15F2K60S2.h>
#define uint unsigned int
#define uchar unsigned char
sbit beep=P3^4;			  // Buzzer 
sbit key1=P3^2;			// Key 1
sbit key2=P3^3;			// Key 2
uchar timeh,timel,jindu=0,gequ=0;		  // Define the reload value of the timer 
bit flag;	// Play flag bit 
uchar code music1[] =     // Music code , The song is 《IF YOU》, The format is :  note ,  Rhythm ,  note ,  Rhythm ,
{
    0x26,0x10,
    0x25,0x10,
    0x23,0x10,
    0x21,0x10,
    0x22,0x10,
    0x23,0x10,
    0x16,0x20,

    0x22,0x08,
    0x21,0x08,
    0x22,0x08,
    0x21,0x08,
    0x22,0x08,
    0x21,0x08,
    0x22,0x10,
    0x21,0x08,
    0x22,0x08,
    0x22,0x08,
    0x21,0x08,
    0x23,0x20,

    0x26,0x10,
    0x25,0x10,
    0x23,0x10,
    0x21,0x10,
    0x22,0x10,
    0x23,0x10,
    0x16,0x20,

    0x16,0x08,
    0x17,0x10,
    0x21,0x08,
    0x21,0x08,
    0x16,0x08,
    0x21,0x08,
    0x16,0x08,
    0x21,0x08,
    0x22,0x08,
    0x23,0x08,
    0x22,0x08,
    0x22,0x10,
    0x21,0x08,
    0x21,0x08,
    0x21,0x10,
    0x15,0x08,
    0x21,0x20,

    0x31,0x10,
    0x31,0x08,
    0x31,0x08,
    0x27,0x08,
    0x26,0x08,
    0x21,0x10,
    0x32,0x10,
    0x33,0x10,
    0x36,0x10,
    0x35,0x10,
    0x33,0x10,
    0x32,0x10,
    0x31,0x10,
    0x32,0x30,

    0x32,0x08,
    0x31,0x08,
    0x32,0x08,
    0x23,0x08,
    0x32,0x10,
    0x25,0x24,

    0x25,0x08,
    0x32,0x08,
    0x31,0x08,
    0x32,0x08,
    0x33,0x24,

    0x31,0x08,
    0x31,0x10,
    0x32,0x10,
    0x33,0x10,
    0x36,0x10,
    0x35,0x10,
    0x33,0x10,
    0x32,0x10,
    0x31,0x08,
    0x31,0x08,
    0x32,0x10,
    0x31,0x30,

    0x26,0x08,
    0x26,0x08,
    0x31,0x08,
    0x26,0x08,
    0x31,0x08,
    0x31,0x24,

    0x31,0x08,
    0x31,0x08,
    0x32,0x08,
    0x31,0x08,
    0x31,0x08,
    0x31,0x24,

    0x00,0x10,


};
uchar code music2[] =  			  // The song is the international song 
{
    0x25, 0x10, 0x31, 0x18, 0x27, 0x08, 0x32, 0x08, 0x31, 0x08, 0x25, 0x08, 0x23, 0x08, 0x26, 0x18, 0x26, 0x08, 0x24, 0x10, 0x20, 0x08,
    0x26, 0x08, 0x32, 0x18, 0x31, 0x08, 0x27, 0x08, 0x26, 0x08, 0x25, 0x08, 0x24, 0x08, 0x23, 0x30,
    0x25, 0x10, 0x31, 0x18, 0x27, 0x08, 0x32, 0x08, 0x31, 0x08, 0x25, 0x08, 0x23, 0x08, 0x26, 0x20, 0x24, 0x08,
    0x26, 0x08, 0x32, 0x08, 0x31, 0x08, 0x27, 0x10, 0x32, 0x10, 0x34, 0x10, 0x27, 0x10, 0x31, 0x20, 0x31, 0x08, 0x30, 0x08,
    0x33, 0x08, 0x32, 0x08, 0x27, 0x20, 0x26, 0x08, 0x27, 0x08, 0x31, 0x08, 0x26, 0x08, 0x27, 0x20, 0x25, 0x08,
    0x25, 0x08, 0x24, 0x08, 0x25, 0x08, 0x26, 0x18, 0x26, 0x08, 0x32, 0x18, 0x31, 0x08, 0x27, 0x20, 0x27, 0x08, 0x20, 0x08,
    0x32, 0x10, 0x32, 0x18, 0x27, 0x08, 0x25, 0x08, 0x25, 0x08, 0x24, 0x08, 0x25, 0x08, 0x33, 0x20, 0x31, 0x08,
    0x26, 0x08, 0x27, 0x08, 0x31, 0x08, 0x27, 0x10, 0x32, 0x10, 0x31, 0x10, 0x26, 0x10, 0x25, 0x20, 0x25, 0x08, 0x20, 0x08,
    0x33, 0x08, 0x32, 0x08, 0x31, 0x20, 0x25, 0x18, 0x23, 0x08, 0x26, 0x20, 0x24, 0x08, 0x20, 0x08,
    0x32, 0x0c, 0x31, 0x04, 0x27, 0x20, 0x26, 0x10, 0x25, 0x10, 0x25, 0x20, 0x25, 0x08, 0x20, 0x08,
    0x25, 0x10, 0x33, 0x20, 0x32, 0x10, 0x25, 0x10, 0x31, 0x20, 0x27, 0x18,
    0x27, 0x08, 0x26, 0x18, 0x25, 0x08, 0x26, 0x10, 0x32, 0x10, 0x32, 0x20, 0x32, 0x08, 0x30, 0x08,
    0x33, 0x0c, 0x32, 0x04, 0x31, 0x20, 0x25, 0x18, 0x23, 0x08, 0x26, 0x20, 0x24, 0x08, 0x20, 0x08,
    0x32, 0x0c, 0x31, 0x04, 0x27, 0x20, 0x26, 0x10, 0x25, 0x10, 0x33, 0x30,
    0x33, 0x10, 0x35, 0x20, 0x34, 0x10, 0x33, 0x10, 0x32, 0x18, 0x33, 0x08, 0x34, 0x10, 0x30, 0x08,
    0x34, 0x08, 0x33, 0x18, 0x33, 0x08, 0x32, 0x18, 0x32, 0x08, 0x31, 0x30,
    0x00, 0x00
};
uchar code music3[] =  			  // The song is for the lone brave 
{
    0x16,0x08,0x17,0x08,
    0x21,0x08,0x22,0x08,
    0x17,0x08,0x21,0x08,
    0x21,0x10,0x21,0x08,
    0x17,0x08,0x21,0x08,
    0x22,0x08,0x17,0x08,
    0x21,0x08,0x21,0x10,
    0x21,0x08,0x22,0x08,
    0x23,0x08,0x22,0x08,
    0x23,0x08,0x22,0x08,
    0x23,0x10,0x23,0x08,
    0x22,0x08,0x23,0x10,
    0x25,0x10,0x23,0x10,
    0x16,0x08,0x17,0x08,
    0x21,0x08,0x22,0x08,
    0x17,0x08,0x21,0x08,

    0x21,0x10,0x21,0x08,
    0x17,0x08,0x21,0x08,
    0x22,0x08,0x17,0x08,
    0x21,0x08,0x21,0x10,
    0x21,0x08,0x22,0x08,
    0x23,0x08,0x22,0x08,
    0x23,0x08,0x22,0x08,

    0x23,0x10,0x23,0x08,
    0x22,0x08,0x23,0x10,
    0x25,0x10,0x23,0x10,
    0x25,0x10,0x23,0x14,
    0x25,0x08,0x23,0x14,
    0x25,0x08,0x23,0x08,
    0x25,0x08,0x26,0x08,
    0x23,0x08,0x25,0x10,
    0x25,0x08,0x23,0x14,
    0x25,0x08,0x23,0x14,
    0x25,0x08,0x23,0x08,
    0x25,0x08,0x26,0x08,
    0x23,0x08,0x25,0x10,
    0x25,0x08,0x25,0x08,
    0x23,0x08,0x22,0x08,
    0x22,0x10,0x22,0x10,
    0x21,0x08,0x23,0x08,
    0x23,0x08,0x22,0x08,
    0x22,0x10,0x22,0x10,
    0x21,0x08,0x21,0x08,
    0x16,0x20,0x25,0x08,
    0x25,0x08,0x23,0x08,
    0x22,0x08,0x22,0x10,

    0x22,0x10,0x21,0x08,
    0x23,0x08,0x23,0x08,
    0x22,0x08,0x22,0x10,
    0x22,0x10,0x21,0x08,
    0x21,0x08,0x16,0x20,
    0x00
};
uchar code quzi[] =  	 // This array data is the reload value of each note in the timer , The first column is high , The second column is low 	// Time 
{
    0xf8,0x8c,	  // Low octave , low 1
    0xf9,0x5b,
    0xfa,0x15,	  // low 3
    0xfa,0x67,
    0xfb,0x04,	  // low 5
    0xfb,0x90,
    0xfc,0x0c,	  // low 7
    0xfc,0x44,	  // The central C transfer 
    0xfc,0xac,	  // in 2
    0xfd,0x09,
    0xfd,0x34,	  // in 4
    0xfd,0x82,
    0xfd,0xc8,	  // in 6
    0xfe,0x06,
    0xfe,0x22,	  // High octave , high 1
    0xfe,0x56,
    0xfe,0x6e,	  // high 3
    0xfe,0x9a,
    0xfe,0xc1,	  // high 5
    0xfe,0xe4,
    0xff,0x03	  // high 7
};
/**********************
 The name of the function :void delay(unsigned int xms)
 Function description : Time delay 
 Entrance parameters :xms: Enter the millisecond value that needs to be delayed 
 Export parameters : nothing 
 remarks :

***********************/
void delay(unsigned int xms)
{
    uint i,j;
    for(i=xms; i>0; i--)
        for(j=124; j>0; j--);
}
/**********************
 The name of the function :uchar quyin(uchar tem)
 Function description : stay quzi Array , find music The reload value of the simplified musical note defined by the array , And return it to quzi Position in array 
 Entrance parameters :tem:music The musical notes defined in the array 
 Export parameters : The return is tem The notes are quzi The position value in the array 
 remarks :

***********************/
uchar quyin(uchar tem)
{
    uchar qudiao,jp,weizhi;		  // Define the tune , Notes and positions 
    qudiao=tem/16;				  // high 4 Bit is the tune value 
    jp=tem%16;					  // low 4 Bit is a note 
    if(qudiao==1)				  // When the tune value is 1 when , It's a low octave , The lower octave is quzi The base address in the array is 0
        qudiao=0;
    else if(qudiao==2)			  // When the tune value is 2 when , It's the middle octave , The middle octave is at quzi The base address in the array is 14
        qudiao=14;
    else if(qudiao==3)			  // When the tune value is 3 when , It's a high octave , The high octave is quzi Array , The base address is 28
        qudiao=28;
    weizhi=qudiao+(jp-1)*2;		  // Add a note to the base address as an offset , You can locate this note at quzi Position in array 
    return weizhi;				  // Return this location value 
}
/**********************
 The name of the function :void playmusic()
 Function description : Play music 
 Entrance parameters : nothing 
 Export parameters : nothing 
 remarks :

***********************/
void playmusic()
{
    uchar p,m,tem;   //m For the beat 
    while(1)
    {
        if(flag==1)					  // If the flag bit for playback is 1 Then play music 
        {
            if(gequ==0)				  // Song selection bit is 0, Then play the same song 
            {
                p=music1[jindu];
                if(p==0x00)			  // If you come across an end , Time delay 1 second , Go back to the beginning and do it again 
                {
                    jindu=0;
                    delay(1000);
                    break;
                }
                else if(p==0xff) 	  // If you encounter a pause , Time delay 100ms, Continue to take the next note 
                {
                    jindu=jindu+1;
                    delay(100);
                    TR0=0;
                    break;
                }
                else				   // Normally take notes and beats 
                {
                    tem=quyin(music1[jindu]);		// Take out the current note at quzi The position value in the array 
                    timeh=quzi[tem];				// Reload the timer corresponding to the note to timeh and timel
                    timel=quzi[tem+1];
                    jindu++;
                    TH0=timeh;						// hold timeh and timel Assign timer 
                    TL0=timel;
                    m=music1[jindu];					 // Get the beat 
                    jindu++;
                }
                TR0=1;                            // Turn on the timer 1
                delay(m*180);    				  // Wait for the beat to complete ,  adopt P3^4 Audio output port 
                TR0=0;                            // Turn off timer 1
                beep=0;							  // send beep Port set 0, Protect buzzer 
            }
            else if(gequ==1)		   // Song selection bit is 1, Then play the little donkey 
            {
                p=music2[jindu];
                if(p==0x00)			 // If you come across an end , Time delay 1 second , Go back to the beginning and do it again 
                {
                    jindu=0;
                    delay(1000);
                    break;
                }
                else if(p==0xff)   // If you encounter a pause , Time delay 100ms, Continue to take the next note 
                {
                    jindu=jindu+1;
                    delay(100);
                    TR0=0;
                    break;
                }
                else			   // Normally take notes and beats 
                {
                    tem=quyin(music2[jindu]);		// Take out the current note at quzi The position value in the array 
                    timeh=quzi[tem];			// Reload the timer corresponding to the note to timeh and timel
                    timel=quzi[tem+1];
                    jindu++;
                    TH0=timeh;					// hold timeh and timel Assign timer 
                    TL0=timel;
                    m=music2[jindu];					 // Get the beat 
                    jindu++;
                }
                TR0=1;                    // Turn on the timer 1
                delay(m*300);             // Wait for the beat to complete ,  adopt P3^4 Audio output port 
                TR0=0;                    // Turn off timer 1
                beep=0;					  // send beep Port set 0, Protect buzzer 
            }
            else if(gequ==2)		  // Song selection bit is 2, Then play the city of the sky 
            {
                p=music3[jindu];
                if(p==0x00)			 // If you come across an end , Time delay 1 second , Go back to the beginning and do it again 
                {
                    jindu=0;
                    delay(1000);
                    break;
                }
                else if(p==0xff)   // If you encounter a pause , Time delay 100ms, Continue to take the next note 
                {
                    jindu=jindu+1;
                    delay(100);
                    TR0=0;
                    break;
                }
                else			   // Normally take notes and beats 
                {
                    tem=quyin(music3[jindu]);		// Take out the current note at quzi The position value in the array 
                    timeh=quzi[tem];			// Reload the timer corresponding to the note to timeh and timel
                    timel=quzi[tem+1];
                    jindu++;
                    TH0=timeh;					// hold timeh and timel Assign timer 
                    TL0=timel;
                    m=music3[jindu];					 // Get the beat 
                    jindu++;
                }
                TR0=1;                    // Turn on the timer 1
                delay(m*200);             // Wait for the beat to complete ,  adopt P3^4 Audio output port 
                TR0=0;                    // Turn off timer 1
                beep=0;					  // send beep Port set 0, Protect buzzer 
            }
        }
        else							   // The playback flag bit is not 1 when , Pause music 
            while(flag!=1);
    }
}
/**********************
 The name of the function :void init_sys()
 Function description : System initialization , The function is to configure IO mouth 
 Entrance parameters : nothing 
 Export parameters : nothing 
 remarks :

***********************/
void init_sys()
{
    P0M0=0xff;			   // Set push-pull mode 
    P0M1=0x00;
    P2M0=0x08;
    P2M1=0x00;
    P3M0=0x10;
    P3M1=0x00;
    P4M0=0x00;
    P4M1=0x00;
    P5M0=0x00;
    P5M1=0x00;
}
/**********************
 The name of the function :void init()
 Function description : Initialization of timers and external interrupts 
 Entrance parameters : nothing 
 Export parameters : nothing 
 remarks :

***********************/
void init()
{
    TMOD=0x01;			  // Set the timer 0, Timing mode 1,16 Bit manual reload mode 
    TH0=0xD8;			  // Set initial value of timing 
    TL0=0xEF;
    IE=0x87;			  // 1000 0111  EA=1,EX0=1,ET0=1,EX1=0;
    IP=0x02;			  // 0000 0010  PT0=1;    Timer 0 High priority 
    TR0=0;				  // Timer 0 Began to run 
    beep=0;				  // send beep Port set 0, Protect buzzer 
}
void main()
{
    init_sys();			  // System initialization 
    init();
    P0=0x00;
    key1=1;				// Set the two keys to the input state , Detection interrupt 
    key2=1;
    flag=0;				// The playback flag bit is cleared 
    while(1)
    {
        playmusic();	// Play music function 
    }
}
/**********************
 The name of the function :void tim1() interrupt 1
 Function description : Timer 0 Interrupt handling , Reinstall the value , And put beep Value reversal , Generate square waves 
 Entrance parameters : nothing 
 Export parameters : nothing 
 remarks :

***********************/
void tim1() interrupt 1						// The timer controls the frequency 
{
    TH0=timeh;								// Assign initial value to 
    TL0=timel;
    beep=~beep;							   // Interruption makes beep The flip produces a square wave 
}
/**********************
 The name of the function :void ex1() interrupt 0
 Function description : Press the button 1 External interruption of , Invert the flag bit , The function is to pause and play music 
 Entrance parameters : nothing 
 Export parameters : nothing 
 remarks :

***********************/
void ex1() interrupt 0
{
    delay(5);
    if(key1==0)							 // Judge key1 Whether to press 
    {
        delay(5);
        if(key1==0)
        {
            while(!key1);
            flag=~flag;							 // The playback interrupt bit is reversed , Play or pause 
        }
    }
}
/**********************
 The name of the function :void ex2() interrupt 2
 Function description : Press the button 2 External interruption of , The function is to switch songs 
 Entrance parameters : nothing 
 Export parameters : nothing 
 remarks :

***********************/
void ex2() interrupt 2
{
    delay(5);
    if(key2==0)				    // Key key2 Press down , Switch songs 
    {
        jindu=0;			   // The progress of the song is cleared , Sing from the beginning 
        gequ++;				   // Next song 
        if(gequ==3)			   // Get to the last song and tune to the first song 
            gequ=0;
    }
}

 Insert picture description here
 Insert picture description here

原网站

版权声明
本文为[Wuhu hanjinlun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060919097984.html