Course Design: Electronic Piano (There is only one good mother in the world)

Publisher:温暖拥抱Latest update time:2016-09-13 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Course Design: Electronic Keyboard (The World is Better Than Mother's) - gududesiling - suixin

                                                         //proteus simulation

//The microcontroller generates a buzzer to simulate the tone of the song (using a timer), not to sing the song (I have not heard of 51 yet)

//The main program is as follows. The function is to press a key and the digital tube will display the corresponding key number. If it is the first three keys, it will emit the notes (dao, ruai, mi)

//If it is the fourth key, it will play (the tune of the song "Only Mom is Good in the World")

//The program is as follows

#include

 

#define uchar unsigned char

sbit dula=P2^6; //If you use a physical object,

sbit wela=P2^7; //If the latch is proteu, it is not needed

sbit sound=P2^3; //define buzzer pin

unsigned char i,j,key,k,m,time;

uchar STH0,STL0;

 

//Common anode digital tube code value

uchar code table[]={

0xc0,0xf9,0xa4,0xb0,

0x99,0x92,0x82,0xf8,

0x80,0x90,0x88,0x83,

0xc6,0xa1,0x86,0x8e};

 

//1,2,3,4 corresponding to the encoding value

unsigned char code music[]={0x25,0x57,0x84,0x98};

 

//Bit selection encoding (this is not necessary if it is proteus, but it is necessary if it is a physical object)

uchar code table1[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};

 

//Data table of "Only Mom is Good in the World"

code unsigned char sszymmh[]={

                               6,2,3, 5,2,1, 3,2,2, 5,2,2, 1,3,2, 6,2,1, 5,2,1,

                               6,2,4, 3,2,2, 5,2,1, 6,2,1, 5,2,2, 3,2,2, 1,2,1,

                               6,1,1, 5,2,1, 3,2,1, 2,2,4, 2,2,3, 3,2,1, 5,2,2,

                               5,2,1, 6,2,1, 3,2,2, 2,2,2, 1,2,4, 5,2,3, 3,2,1,

                               2,2,1, 1,2,1, 6,1,1, 1,2,1, 5,1,6, 0,0,0

                             };

 

// Scale frequency table high eight bits

code unsigned char FREQH[]={

                                0xF2,0xF3,0xF5,0xF5,0xF6,0xF7,0xF8,

                                   0xF9,0xF9,0xFA,0xFA,0xFB,0xFB,0xFC,0xFC, //1,2,3,4,5,6,7,8,i

                                   0xFC,0xFD,0xFD,0xFD,0xFD,0xFE,

                                   0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF,

                           } ;

 

// Scale frequency table lower eight bits

code unsigned char FREQL[]={

                                0x42,0xC1,0x17,0xB6,0xD0,0xD1,0xB6,

                                 0x21,0xE1,0x8C,0xD8,0x68,0xE9,0x5B,0x8F, //1,2,3,4,5,6,7,8,i

                                   0xEE,0x44, 0x6B,0xB4,0xF4,0x2D,

                                   0x47,0x77,0xA2,0xB6,0xDA,0xFA,0x16,

                           };

 

 

// Function declaration 

void display(uchar Duan_value,uchar Wei_value);

void delay(unsigned char t);

void delay1(uchar );

void display1(uchar value);

 

//Function to play song notes

void song()

{

       TH0=STH0;

       TL0=STL0;

       TR0=1;

       delay1(time); //This delay is very important, it is used to determine how long a certain note will last

}

 

//Set the initial value function of the corresponding button timer

void key_on(unsigned char key)

{

              m=0;

              if(key!=3) 

              {

                             TMOD=0x01; //Set the timing working mode bit t0 mode 1 (16-bit timer)

                             TH0=0xfe;

                             TL0=music[key]; //Timer initial value (according to the code corresponding to the note)

                             EA=1; //Open each interrupt at a time

                             ET0=1;

                             TR0=1;

                             display1(key+1);

                             while(P3!=0xff); //Debounce when the button is pressed

                             TR0=0;

               }

               else //After the fourth key is pressed, play the song

               {

                   

                           TMOD=0x01; //Set the timing working mode bit t0 mode 1 (16-bit timer)

                           TH0=0xfe;

                           TL0=music[key]; //Timer initial value (according to the code corresponding to the note)

                           EA=1; //Open each interrupt at a time

                           ET0=1;

                           TR0=0;

                           display1(key+1);

                           while(P3!=0xff); //Debounce when the button is pressed

                           time=1;

                           while(time!=0) //After the song is played, time=0 exits (terminator)

                            {

                                          k=sszymmh[m]+7*sszymmh[m+1]-1;

                                          STH0=FREQH[k];

                                          STL0=FREQL[k];

                                          time=sszymmh[m+2]; //This mode is fixed, no need to study too much why +2

                                          m=m+3;

                                          song();

                         }

               }

}

 

/*Function: Beat control of playing music*/

void delay1(unsigned char t)

{

       unsigned char t1;

       unsigned long t2;

       for(t1=0;t1

       {                              

              for(t2=0;t2<8000;t2++)

              {

                     ;

              }                                

       }

       TR0=0;

}

 

//Delay function (unit: ms)

void delay(unsigned char t)

{

              unsigned char x,y;

             for(x=t;t>0;t--)

                  for(y=111;y>0;y--);

}

 

//Display the corresponding digital tube button value (if it is a real object)

/*void display(uchar Duan_value,uchar Wei_value)

{

      dula=1;

      P0=table[Duan_value];

      dula=0;

      wela=1;

      P0=table1[Wei_value];

      wela=0;

      delay(3);

}*/

 

//Display function

void display1(uchar value)

{

       P0=table[value];

}

        

 

void main()

{

             display1(key); 

              while(1)

             {

                    if(P3==0xef) //First key pressed

                     {

                                      delay(10); //Debounce the button after pressing it

                                      if(P3==0xef)

                                         {

                                                 key=0;

                                                key_on(key); //Detect key press (and display data in the function)

                                          }

                      }

        

                 if(P3==0xdf) //The second key is pressed

                  {

                                 delay(10); //Debounce the button after pressing it

                                if(P3==0xdf)

                                  {

                                                 key=1;

                                                key_on(key);

                                   }

                    }

      

                   if(P3==0xbf) //The third key is pressed

                   {

                                delay(10);

                                if(P3==0xbf)

                                  {

                                                 key=2;

                                                key_on(key);

                                   }

                   }

      

                   if(P3==0x7f) //The fourth key is pressed

                   {

                              delay(10);

                             if(P3==0x7f)

                             {

                                      key=3;

                                      key_on(key);

                           }

                  }

     }

}

 

 

//Timer 1 interrupt function

void t0() interrupt 1

{

               if(key!=3)

               {

                             TH0=0xfe;

                             TL0=music[key];

               }

               else

               {                      

                                   TH0=STH0;

                                   TL0=STL0;

                }

                sound=~sound; //buzzer inversion (generate pulse)

}

Reference address:Course Design: Electronic Piano (There is only one good mother in the world)

Previous article:Matrix keyboard plus electronic clock (c51)
Next article:Course Design: Matrix Keyboard (c51, implemented using scanning method)

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号