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 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 processingFunction generator:
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
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- An SI engineer who does not understand processing cannot be called Mr. High Speed
- EEWORLD University Hall----Live Replay: Use ModusToolbox? to build a system to flexibly respond to IoT design challenges
- Why is the square wave voltage generated by the inverter circuit measured using the AC voltage range of a digital multimeter much smaller than the theoretical value?
- Design of three-coordinated distributed control system based on TMS320F2812 DSP
- Intermec China General Agent and Authorized Repair Center---Shanghai Xichuangwangsi
- Several ways to accurately delay the microcontroller
- Commonly used filtering algorithms in software development
- MSP430FR5994 uses LEA for signal processing
- Bluetooth tester function details!
- Electric heater - "silent" transformation