Music player and function generator based on 51 microcontroller Proteus simulation

Publisher:快乐的舞蹈Latest update time:2023-01-30 Source: zhihu Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

music player:

Actual operating effect:

51 microcontroller Proteus simulation music player

Music player schematic diagram

Music player code link: https://pan.baidu.com/s/1Yrjb2BtYoAcUZDdtUpbyMw

Extraction code: vhth

Code:

#include

#include


typedef unsigned char uchar;

typedef unsigned int uint;


sbit K1 = P3^2;

sbit Beep = P3^7;

uchar i;


uchar song_Index,Tone_Index=0;

uchar * song_Tone, *song_Time;


const LED_tab[] = { 0xC0,0xF9,0xA4,0xB0,0x99,0x92,

                    0x82,0xF8,0x80,0x90,0x88,0x83,

                    0xC6,0xA1,0x86,0x81,0xBF,0xFF };


const Tone_tab[] = { 62018,62401,62491,62895,63184,63441,63506,

                    63773,63965,64137,64215,64360,64488,64603,

                    64654,64751,64836,64876,64948,65012,65067,65535};

/* "Farewell" */

uchar code song1_Tone[] = { 11,9,11,14,12,14,12,11,11,7,8,9,8,7,8,

                            11,9,11,14,13,12,14,11,11,7,8,9,6,7,

                            12,14,14,13,12,13,14,12,13,14,12,12,11,10,7,8,

                            11,9,11,14,13,12,14,11,11,8,9,10,6,7,0xff};

uchar code song1_Time[] = { 4,2,2,8,4,2,2,8,4,2,2,4,2,2,12,

                            4,2,2,4,2,4,4,8,4,2,2,4,2,12,

                            4,4,8,4,2,2,8,2,2,2,2,2,2,2,2,16,

                            4,2,2,4,2,4,4,8,4,2,2,4,2,12,0xff};


/* "Two Tigers" */

uchar code song2_Tone[] = { 7,8,9,7,7,8,9,7,9,10,11,9,10,11,

                            11,12,11,10,9,7,11,12,11,10,9,7,7,4,7,7,4,7,0xff};

uchar code song2_Time[] = { 4,4,4,4,4,4,4,4,4,4,8,4,4,8,

                            2,2,2,2,2,2,2,2,2,2,4,4,4,4,8,4,4,8,0xff};


/* "Pug" */

uchar code song3_Tone[] = { 7,7,7,8,9,9,9,9,10,11,12,12,11,10,9,11,11,8,9,7,

                            7,7,7,8,11,9,9,9,10,11,12,12,11,10,9,11,8,9,7,0xff};

uchar code song3_Time[] = { 2,2,2,2,4,2,2,2,2,4,2,2,2,2,4,2,2,2,2,4,

                            2,2,2,2,4,2,2,2,2,4,2,2,2,2,4,2,2,2,2,4,0xff};


/* "Orchid Grass" */

uchar code song4_Tone[] = { 5,9,9,9,9,8,7,8,7,6,5,12,12,12,12,12,11,

                            2,11,11,10,9,9,12,12,11,9,8,7,8,7,6,5,9,

                            2,7,7,6,5,9,8,7,6,4,6,0xff};

uchar code song4_Time[] = { 2,2,2,2,2,2,2,2,2,2,8,2,2,2,2,4,2,

                            2,2,2,8,2,2,2,2,4,2,2,2,2,2,4,2,

                            2,2,2,2,4,2,2,2,2,2,8,0xff};


/* "Tumbler" */

uchar code song5_Tone[] = { 11,12,11,9,8,9,11,9,8,7,9,11,7,9,8,

                            11,12,11,9,8,9,11,9,8,7,8,7,8,9,7,0xff};

uchar code song5_Time[] = { 4,4,8,4,4,8,4,4,4,4,2,2,2,2,8,

                            4,4,8,4,4,8,4,4,4,4,2,2,2,2,8,0xff};


void delayms( uint ms )

{

    uchar a;

    while (ms--)

    {

        for (a=230; a>0; a--);

    }   

}


void int0() interrupt 0

{

   delayms(100);

   if (INT0==0)

   {

       TR0 = 0;

       song_Index++;

   }

   if (song_Index==1)

   {

       song_Tone = song2_Tone;

       song_Time = song2_Time;

   }


    if (song_Index==2)

   {

       song_Tone = song3_Tone;

       song_Time = song3_Time;

   }

   

    if (song_Index==3)

   {

       song_Tone = song4_Tone;

       song_Time = song4_Time;

   }


    if (song_Index==4)

   {

       song_Tone = song5_Tone;

       song_Time = song5_Time;

   }


      if (song_Index==5)

   {

       song_Tone = song1_Tone;

       song_Time = song1_Time;

   }

    

}


void Timer0() interrupt 1

{

    TH0 = Tone_tab[Tone_Index]/256;

    TL0 = Tone_tab[Tone_Index]%256;

    Beep = ~Beep;

}


void display(void)

{

    P0 = LED_tab[song_Index];

}


void Init(void)

{

    TMOD = 0x01;

    ET0 = 1;

    EX0 = 1;

    IT0 = 1;

    EA = 1;

    TR0 = 0;

}

void main()

{

    Init();

    song_Tone = song1_Tone;

    song_Time = song1_Time;


    while (1)

    {

        display();

        Tone_Index = song_Tone[i];

        if (Tone_Index==0xFF)

        {

            i = 0;

            TR0 = 0;

        }

        TR0 = 1;

        delayms(song_Time[Tone_Index]+100);

        TR0 = 0;

        i++;

    }

    

}

Code and principle explanation:

The human ear can hear sound in the air because the sound source drives the air to vibrate, reaching the range that the human ear can hear (20~20Khz); of course, it can also be transmitted through other media, so we will not discuss it here.

Then, if you want to produce sound, you only need to vibrate to push the air to the range that the human ear can hear. Commonly used speakers are typical conversion devices. The core idea of ​​the entire code here is to use the timer 0 interrupt of the 51 microcontroller to generate the tone of music. Then adding the sound length of each tone can form a complete piece of music.

In order to switch between different music, we can encode the tones of different music and the duration of the tones during the actual performance in advance.

Pitch and per-note delay array encoding

Timer 0 interrupt, used to generate the tone

Each tone delay length processing part

Function generator:

Actual operating effect:

51 microcontroller Proteus function generator simulation

Function generator schematic

Function generator code link: https://pan.baidu.com/s/1izoD5KOfXFZP2sgXjLin0g

Extraction code: 1lbq

Function generator code:

#include


sbit UPKEY = P3^3;

sbit DOWNKEY = P3^4;

sbit SHKEY = P3^5;

sbit FOUT = P3^2;


unsigned char fun = 0;

unsigned char th0_reg,tl0_reg;


void delay_ms( unsigned int dt );


void main()

{

    unsigned int f = 500;

    TMOD = 0x01;

    th0_reg = (unsigned int)(65539.0-(19531.25/f))>>8;

    tl0_reg = (unsigned int)(65539.0-(19531.25/f))&0x00ff;


    TH0 = th0_reg;

    TL0 = tl0_reg;

    TR0 = 1;

    IE = 0x82;

    P3 = 0xff;


    while (1)

    {

        if((UPKEY==0)&&(f<1000))

        {

            f += (f/10)?(f/10):1;

            th0_reg = (unsigned int)(65539.0-(19531.25/f))>>8;

            tl0_reg = (unsigned int)(65539.0-(19531.25/f))&0x00ff;

            delay_ms(100);

        }


        if((DOWNKEY==0)&&(f>1))

        {

            f -= (f/10)?(f/10):1;

            th0_reg = (unsigned int)(65539.0-(19531.25/f))>>8;

            tl0_reg = (unsigned int)(65539.0-(19531.25/f))&0x00ff;

            delay_ms(100);

        }


        if(SHKEY==0)

        {

            fun = !fun;

            delay_ms(100);

        }

    }

    

}


void delay_ms( unsigned int dt )

{

    register unsigned char bt,ct;

    for(;dt;dt--)

        for(ct=2; ct; ct--)

            for(bt=248; --bt;);

}


void intt0(void) interrupt 1

{

    unsigned char code sin_table[]={

    0,0,0,0,0,0,0,0,1,1,1,1,1,2,2,2,2,3,3,3,4,4,5,5,6, 6,6,7,8,8,9,9,

    10,10,11,12,12,13,14,14,15,16,17,17,18,19,20,21,22,23,23,24,25,

    26,27,28,29,30,31,32,33,34,35,37,38,39,40,41,42,43,45,46,47,48,

    49,51,52,53,54,56,57,58,60,61,62,64,65,66,68,69,71,72,73,75,76,

    78,79,81,82,84,85,87,88,90,91,93,94,96,97,99,100,102,103,105,106,

    108,109,111,113,114,116,117,119,120,122,124,125,127,128,130,131,

    133,135,136,138,139,141,142,144,146,147,149,150,152,153,155,156,

    158,159,161,162,164,165,167,168,170,171,173,174,176,177,179,180,

    182,183,184,186,187,189,190,191,193,194,195,197,198,199,201,202,

    203,204,206,207,208,209,210,212,213,214,215,216,217,218,220,221,

    222,223,224,225,226,227,228,229,230,231,232,232,233,234,235,236,

    237,238,238,239,240,241,241,242,243,243,244,245,245,246,246,247,

    247,248,249,249,249,250,250,251,251,252,252,252,253,253,253,253,

    254,254,254,254,254,255,255,255,255,255,255,255,255

    };


    static unsigned char wave_index = 0;

    static bit index_sign = 1;

    TH0 = th0_reg;

    TL0 = tl0_reg;

    if(fun){ P2 = sin_table[wave_index]; }

    else { P2 = wave_index; }


    FOUT = !FOUT;

    if (index_sign==1){ if(++wave_index==255) index_sign = 0; }

    else { if(--wave_index==0) index_sign = 1; }


}

Code and principle explanation:

The core part of the entire code is completed using the timer 0 interrupt

The square wave generation part is relatively simple. The square wave generation of the P3.2 pin is directly controlled by changing the delay length of the timer.

Square wave pin definition

Square wave generation pin

The triangular wave and sine wave generation part is generated using DAC0832. By loading different values ​​​​to the P2 port, different voltages are generated.

Sine wave voltage coding table

Sine wave and triangle wave switching part

The sine wave outputs all the values ​​of the entire coding table, while the triangle wave directly outputs the value of wave_index.

The change of frequency is achieved by changing the interrupt value of the timer.

Waveform frequency addition and subtraction part button processing


Reference address:Music player and function generator based on 51 microcontroller Proteus simulation

Previous article:Multi-story elevator control system based on AT89C51
Next article:Can microcontroller debugging still be played like this? Keil and Proteus achieve perfect joint debugging of 51 microcontroller

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号