#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.
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.
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.
3) The contrast between the C scale and frequency.
#experiment
1) Definition of frequency table
Frequency table data
2) Audio playback table
3) Program flow
C Code
**********************************************************************/
* 【Course 12】 ****Music Experiment***********
* 【illustrate】 ****
* 【Description】 ****Experiment on the Happy Birthday song.
**********************************************************************/
/*---------------------------------------------
51 single chip microcomputer music playing experiment
-----------------------------------------------*/
#include //---------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); } }
Previous article:10. Minimum system and power saving working mode
Next article:12: Communication between MCU and chip 164
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- MSP430FR5964 program download problem
- Take the survey and register for the Zurich Instruments seminar to win a Swiss Army Knife!
- Date Spring - Spring Harvest
- 【TouchGFX Design】+Simple solution to previous problems+
- DIY creative ideas come in~
- Setting trial time limit
- Anxinke PB-02 module evaluation (2) --- PHY62XX architecture introduction && design task introduction
- Migrate from M2M to IIoT
- What is your answer to the Huawei interview question that is trending on the Internet?
- IGBT application circuit for high current drive