11: Music Experiment

Publisher:程序界的行者Latest update time:2019-08-13 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#Basic knowledge
Speaker, commonly known as horn, is a generator that can emit a variety of audio frequencies and is widely used in real life.
Write the picture description here
Sound-generating principle:

The production of sound is an effect of audio vibration. A high frequency of vibration is a high pitch, and a low frequency is a low pitch.
The range of audio is 20Hz-200kHz. The sound that human ears can easily identify is about 200Hz-20kHz.
In general, the audio circuit drives the speaker with a sine wave signal to produce pleasant music; in digital circuits, the speaker is generally driven by a pulse signal to produce sound, and the effect is relatively worse.
Write the picture description here
Write the picture description here
1) Tone
Do, Re, Mi, Fa, So, La, St represent a certain frequency of sound, which is called "tone".
2) Beat
The beat can make music melody (fixed rhythm), and can also adjust the speed of each sound. "Beat" is Beat.
C3/4 represents C key, 4 bars, and three beats per bar; one bar is between two straight lines.
If the speed of each bar is 1.5s, it means 0.5s per beat, 0.375s for 3/4 beats, 0.25s for 1/2 beats, 0.125s for 1/4 beats... and so on.
Write the picture description here
3) The contrast between the C scale and frequency.
Write the picture description here

#experiment
Write the picture description here

1) Definition of frequency table
Frequency table data
Write the picture description here
2) Audio playback table
Write the picture description here
3) Program flow
Write the picture description here

C Code

**********************************************************************/

* 【Course 12】 ****Music Experiment***********

* 【illustrate】 ****

* 【Description】 ****Experiment on the Happy Birthday song.

**********************************************************************/

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

51 single chip microcomputer music playing experiment

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

#include //51 header file,


//---------Two commonly used macro definitions-------------- 

#define uint8 unsigned char

#define uint16 unsigned int


sbit Music=P2^0;


uint16 code FreTab_Low[7]={262,294,330,349,392,440,494}; //Bass frequency table

uint16 code FreTab_Mid[7]={523,578,659,698,784,880,988}; //Mid-range frequency table

uint16 code FreTab_Hight[7]={1046,1175,1318,1397,1568,1760,1976}; //treble frequency table


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

Music score storage format uchar code MusicName{pitch, note length, ...., 0,0};

End: 0,0 means the end.

The pitch is determined by the frequency, and the length of the note, that is, the beat, can be set by yourself. In this program,

Timer T1 is used for beat timing, and the timing time is 50ms. So in this score, 1 beat

Indicates 50ms.

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

uint8 code Happy_Birthday[]={0x11,0x05,0x11,0x05,0x12,0x0a,0x11,0x0a,

0x14,0x0a,0x13,0x14,

0x11,0x05,0x11,0x05,0x12,0x0a,0x11,0x0a,

0x15,0x0a,0x14,0x14,

0x11,0x05,0x11,0x05,0x21,0x0a,0x16,0x0a,

0x14,0x0a,0x13,0x0a,0x22,0x0a,

0x17,0x05,0x17,0x05,0x16,0x0a,0x14,0x0a,

0x15,0x0a,0x14,0x14,0x00,0x00

};

uint16 Fre; //Variable to store the obtained frequency value


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

Function name: Timer_Init()

Function: Initialize two timers

Entry parameters: None

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

void Timer_Init()

{

TMOD=0x11;

EA=1;

ET0=1;

ET1=0;

TR0=0;

TR1=0;

}

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

Function name: Timer0_Ser()

Function: The interrupt service routine of timer 0 is mainly used to generate the required frequency

          This drives the speaker to emit the corresponding note.

Entry parameters: None

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

void Timer0_i() interrupt 1

{

TH0=Fre/256;

TL0=Fre%256;

if(Music==0)

Music=1;

else Music=0;

}


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

Function name: Music_Play(uchar *MusicTab)

Function: music playback function,

Entry parameter: *MusicTab The first address of the music to be played

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

void Music_Play(uint8 *MusicTab)

{

uint8 *pMusic;

uint8 temp,i;


pMusic=MusicTab; //Give the first address of the song to the pointer variable

while(*pMusic!=0)

{

if((*pMusic&0xf0)==0x00)

Fre=65536-1000000/FreTab_Low[*pMusic&0x0f];

else if((*pMusic&0xf0)==0x10)

Fre=65536-1000000/FreTab_Mid[*pMusic&0x0f];

else if((*pMusic&0xf0)==0x20)

Fre=65536-1000000/FreTab_Hight[*pMusic&0x0f];

TH0=Fre/256; //Calculate the initial value of frequency and assign the high eight bits

TL0=Fre%256; //Calculate the initial value of frequency and assign the lower eight bits

TH1=0x3c; //Assign initial value to timer 1, set timer to 50ms

TL1=0xb0;

pMusic++; //Get the beat value

temp=*pMusic;

TR0=1; //Start timer T0

TR1=1; //Start timer T1

for(i=0;i {

while(TF1==0); //Judge whether timer 1 overflows

TH1=0x3c;

TL1=0xb0;

TF1=0;

}

pMusic++;

}

}

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

Function name: main()

Function: Main function

Entry parameters: None

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

void main()

{

Timer_Init();

while(1)

{

Music_Play(Happy_Birthday);

}

}


Reference address:11: Music Experiment

Previous article:10. Minimum system and power saving working mode
Next article:12: Communication between MCU and chip 164

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号