#include#define uchar unsigned char #define uint unsigned int sbit speaker = P1^7; uchar t0h,t0l,time; //----------------------------------------- //The MCU crystal oscillator uses 11.0592MHz // Frequency-half-cycle data table high eight bits This software saves a total of 28 frequency data of four octaves uchar code FREQH[] = {0x01, //No note when 0 0xF2, 0xF3, 0xF5, 0xF5, 0xF6, 0xF7, 0xF8, //Bass 1234567 0xF9, 0xF9, 0xFA, 0xFA, 0xFB, 0xFB, 0xFC, 0xFC,//1,2,3,4,5,6,7,i 0xFC, 0xFD, 0xFD, 0xFD, 0xFD, 0xFE, // high pitch 234567 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF}; //Super treble 1234567 //---------------------------------------- // Frequency-half-cycle data table lower eight bits uchar code FREQL[] = {0x01, //No note when 0 0x42, 0xC1, 0x17, 0xB6, 0xD0, 0xD1, 0xB6, //Bass 1234567 0x21, 0xE1, 0x8C, 0xD8, 0x68, 0xE9, 0x5B, 0x8F, //1,2,3,4,5,6,7,i 0xEE, 0x44, 0x6B, 0xB4, 0xF4, 0x2D, // high pitch 234567 0x47, 0x77, 0xA2, 0xB6, 0xDA, 0xFA, 0x16}; //Super high pitch 1234567 //----------------------------------------- //A note has three numbers. The first one is the note number, the middle one is the octave, and the last one is the duration (in 1/4 beats). //6, 2, 6 represent: 6, middle note, 6 1/4 beats; //5, 2, 2 represent: 5, middle note, 2 1/4 beats; //3, 2, 4 represent: 3, middle note, 4 1/4 beats; //5, 2, 4 represent: 5, middle note, 4 1/4 beats; //1, 3, 4 represent: 1, high note, 4 1/4 beats; //Bass is 1, midrange is 2, treble is 3, and super treble is 4; ///////////////////////////////////////////////////// //////// /************************There is only one good mother in the world************************/ uchar code sszymmh[] = { 6,2,6, 5,2,2, 3,2,4, 5,2,4, 1,3,4, 6,2,2, 5,2,2, 6,2,8, 3,2,4, 5,2,2, 6,2,2, 5,2,4, 3,2,4, 1,2,2, 6,1,2, 5,2,2, 3,2,2, 2,2,8, 2,2,6, 3,2,2, 5,2,4, 5,2,2, 6,2,2, 3,2,4, 2,2,4, 1,2,8, 5,2,6, 3,2,2, 2,2,2, 1,2,2, 6,1,2, 1,2,2, 5,1,16, 0,0,0}; /**********************Two mice*************************/ uchar code TwoMouse[]={ 3,2,2, 6,2,2, 7,2,2, 5,2,2, 3,2,2, 6,2,2, 7,2,2, 5,2,2, 7,2,2, 1,3,2, 2,3,4, 7,2,2, 1,3,2, 2,3,4, 2,3,1, 3,3,1, 2,3,1, 1,3,1, 7,2,2, 5,2,2, 2,3,1, 3,3,1, 2,3,1, 1,3,1, 7,2,2, 5,2,2, 6,2,2, 2,2,2, 5,2,4, 6,2,2, 2,2,2, 5,2,4, 0,0,0}; /***********************Two butterflies**********************/ uchar code TwoButterfly[]={ 3,2,2, 2,2,2, 3,2,6, 3,2,2, 3,2,2, 2,2,2, 1,2,6, 6,1,2, 1,2,2, 2,2,4, 2,2,2, 3,2,2, 2,2,2, 1,2,2, 6,1,2, 1,2,2, 5,1,12,3,2,2, 2,2,2, 3,2,8, 3,2,2, 2,2,2, 3,2,2, 3,2,2, 1,2,12,6,1,2, 1,2,2, 2,2,4, 2,2,2, 3,2,2, 2,2,2, 1,2,2, 6,1,2, 1,2,2, 2,2,12,3,2,2, 2,2,2, 3,2,8, 3,2,2, 2,2,2, 3,2,2, 2,2,2, 1,2,12,6,1,2, 1,2,2, 2,2,4, 2,2,2, 3,2,2, 2,2,2, 1,2,2, 6,1,2, 1,2,2, 5,2,12,3,2,2, 5,2,2, 5,2,8, 5,2,2, 5,2,2, 6,2,2, 5,2,2, 3,2,12,2,2,2, 3,2,2, 2,2,4, 2,2,2, 3,2,2, 2,2,2, 1,2,2, 6,1,2, 6,1,1, 1,2,1, 1,2,1, 1,2,1, 1,2,12,0,0,4, 5,2,2, 5,2,2, 6,2,2, 1,3,2, 7,2,2, 7,2,2, 6,2,2, 3,2,2, 2,2,2, 2,2,2, 3,2,2, 3,2,8, 3,2,2, 3,2,2, 5,2,2, 6,2,4, 6,2,6, 6,1,2, 3,2,2, 2,2,2, 2,2,12,3,2,2, 5,2,2, 5,2,2, 3,2,2, 5,2,4, 1,3,4, 7,2,2, 6,2,2, 7,2,2, 3,2,4, 6,2,2, 6,2,2, 7,2,2, 6,2,2, 5,2,2, 3,2,2, 2,2,4, 3,2,2, 2,2,2, 3,2,2, 5,2,6, 5,2,2, 5,2,2, 6,2,2, 6,2,2, 5,2,2, 3,2,2, 2,2,4, 5,1,2, 5,1,2, 6,1,2, 1,2,12, 0,0,0}; /**********************happy birthday************************/ uchar code Birthday[]={ 5,1,2, 5,1,2, 6,1,4, 5,1,4, 1,2,4, 7,1,8, 5,1,2, 5,1,2, 6,1,4, 5,1,4, 2,2,4, 1,2,8, 5,1,2, 5,1,2, 5,2,4, 3,2,4, 1,2,4, 7,1,4, 6,1,4, 4,2,2, 4,2,2, 3,2,4, 1,2,4, 2,2,4, 1,2,4, 0,0,0}; ///////////////////////////////////////////////////// ////// //------------------------------------------------ ----- void t0int() interrupt 1 //T0 interrupt program, control the tone of pronunciation { TR0 = 0; //Close T0 first speaker = ~speaker; //output square wave, pronunciation TH0 = t0h; //The next interruption time, this time, controls the pitch TL0 = t0l; TR0 = 1; //Start T0 } //----------------------------------------- void delay(uchar t) //delay program, control the pronunciation time length 120ms (1/4 beat) { uchar a,b,c; while(t--) //Quadruple loop, total delay t 1/4 beats { for(c=193;c>0;c--) for(b=114;b>0;b--) for(a=1;a>0;a--); } //During the delay period, you can enter T0 interrupt to pronounce TR0 = 0; //Turn off T0, stop pronunciation } //----------------------------------------- void singachar() //play a note { TR0=0; TH0 = t0h; //Control tone TL0 = t0l; TR0 = 1; //Start T0, and T0 outputs square wave to pronounce delay(time); //Control the time length, i.e. the beat } //----------------------------------------- //**************Play a song***********************************// void song(uchar *str) { uchar k,i; i = 0; time = 1; while(time) { k = str[i] + 7 * (str[i + 1]); //The i-th is a note, the i+1th is the octave
t0h = FREQH[k]; //Read the frequency value from the data table t0l = FREQL[k]; //Actually, it is the time length of the timing time = str[i + 2]; //Read the time length value beat time i += 3; singachar(); } } void main(void) { TMOD = 0x01; //Set T0 timing working mode 1 ET0 = 1; //Open T0 interrupt EA = 1; // Enable CPU interrupt while(1) { song(TwoMouse); delay(20); song(sszymmh); delay(20); // song(TwoButterfly); song(Birthday); delay(20); } //Play a note }
/****************Music score beats, high and low notes***********************************
Beat: Two horizontal bars below indicate a quarter beat; one horizontal bar below indicates a half beat; a dot at the end indicates adding half the beat of the note;
2 indicates one beat, 2- indicates two beats; 2--- indicates four beats
Pitch: A dot above indicates a high note, a dot below indicates a low note, usually indicating a middle note;
set the beat speed to 125 beats/minute, then 2.08/beat, two beats are 960ms, one beat is 0.48s, and 1/4 beat is 120ms
*************************************************************/
Previous article:Power-off protection and watchdog reset C51 program
Next article:Gateway to serial communication based on TCP/IP
Recommended ReadingLatest update time:2024-11-16 22:46
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Modern Electronic Technology Training Course (Edited by Yao Youfeng)
- Modern arc welding power supply and its control
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- MSP430 MCU Example 3 - Colored Light Control
- EEWORLD University-What is an isolated gate driver?
- Burning image and mounting USB disk with busybox file system failed
- Switching Circuit
- EEWORLD University Hall----Open Source PWM Robotic Arm (Version 51)
- MakeCode Extension for the LSM6DSO Sensor
- PCB reliability test
- It is rare for home appliance manufacturers to issue price increase letters, simply because of rising costs of chips and other items!
- The project in Xinlinx SDK is working fine but suddenly can't find the header file
- How should we say the "positive and negative poles" of alternating current?