当前位置:网站首页>Stc-b learning board buzzer plays music
Stc-b learning board buzzer plays music
2022-07-06 15:00:00 【Wuhu hanjinlun】
function : After the single chip computer burns the program , Press key1 Key play 《IF YOU》, Press again key1 Pause the playback , Press key2 You can switch songs to 《 Internationale 》, Press again key3 You can switch songs to 《 Lone brave 》
chip :STC15F2K61S2
Complete engineering documents and hex The document is here
Extraction code :1111
Preface : I believe that your favorite thing to play after welding the board is the buzzer playing music , Every classroom passing by is basically full of all kinds of noise , After all, this thing is really fun , So I wrote a file to play other songs according to the teacher's code and score ( International song is the music code of my previous senior students , Let's make up the number ), To be honest, this thing looks very simple , It's actually quite difficult , I have to learn music theory again , Then we can get an ideal state by constantly adjusting to various beats and tones , however if you And the lonely brave always feel that some places are not perfect , But for lousy people, it's OK to listen . Anyway, it's also entertainment
I don't say much nonsense , The code is as follows :( The score is at the end )
/**********************
File name :beep.c
explain : Routine for playing music , Press down key1 Key to start playing music
Modify the record :
***********************/
/**********************
be based on STC15F2K60S2 Series MCU C Language programming implementation
Use the following header file , There is no need to include "REG51.H"
***********************/
#include <STC15F2K60S2.h>
#define uint unsigned int
#define uchar unsigned char
sbit beep=P3^4; // Buzzer
sbit key1=P3^2; // Key 1
sbit key2=P3^3; // Key 2
uchar timeh,timel,jindu=0,gequ=0; // Define the reload value of the timer
bit flag; // Play flag bit
uchar code music1[] = // Music code , The song is 《IF YOU》, The format is : note , Rhythm , note , Rhythm ,
{
0x26,0x10,
0x25,0x10,
0x23,0x10,
0x21,0x10,
0x22,0x10,
0x23,0x10,
0x16,0x20,
0x22,0x08,
0x21,0x08,
0x22,0x08,
0x21,0x08,
0x22,0x08,
0x21,0x08,
0x22,0x10,
0x21,0x08,
0x22,0x08,
0x22,0x08,
0x21,0x08,
0x23,0x20,
0x26,0x10,
0x25,0x10,
0x23,0x10,
0x21,0x10,
0x22,0x10,
0x23,0x10,
0x16,0x20,
0x16,0x08,
0x17,0x10,
0x21,0x08,
0x21,0x08,
0x16,0x08,
0x21,0x08,
0x16,0x08,
0x21,0x08,
0x22,0x08,
0x23,0x08,
0x22,0x08,
0x22,0x10,
0x21,0x08,
0x21,0x08,
0x21,0x10,
0x15,0x08,
0x21,0x20,
0x31,0x10,
0x31,0x08,
0x31,0x08,
0x27,0x08,
0x26,0x08,
0x21,0x10,
0x32,0x10,
0x33,0x10,
0x36,0x10,
0x35,0x10,
0x33,0x10,
0x32,0x10,
0x31,0x10,
0x32,0x30,
0x32,0x08,
0x31,0x08,
0x32,0x08,
0x23,0x08,
0x32,0x10,
0x25,0x24,
0x25,0x08,
0x32,0x08,
0x31,0x08,
0x32,0x08,
0x33,0x24,
0x31,0x08,
0x31,0x10,
0x32,0x10,
0x33,0x10,
0x36,0x10,
0x35,0x10,
0x33,0x10,
0x32,0x10,
0x31,0x08,
0x31,0x08,
0x32,0x10,
0x31,0x30,
0x26,0x08,
0x26,0x08,
0x31,0x08,
0x26,0x08,
0x31,0x08,
0x31,0x24,
0x31,0x08,
0x31,0x08,
0x32,0x08,
0x31,0x08,
0x31,0x08,
0x31,0x24,
0x00,0x10,
};
uchar code music2[] = // The song is the international song
{
0x25, 0x10, 0x31, 0x18, 0x27, 0x08, 0x32, 0x08, 0x31, 0x08, 0x25, 0x08, 0x23, 0x08, 0x26, 0x18, 0x26, 0x08, 0x24, 0x10, 0x20, 0x08,
0x26, 0x08, 0x32, 0x18, 0x31, 0x08, 0x27, 0x08, 0x26, 0x08, 0x25, 0x08, 0x24, 0x08, 0x23, 0x30,
0x25, 0x10, 0x31, 0x18, 0x27, 0x08, 0x32, 0x08, 0x31, 0x08, 0x25, 0x08, 0x23, 0x08, 0x26, 0x20, 0x24, 0x08,
0x26, 0x08, 0x32, 0x08, 0x31, 0x08, 0x27, 0x10, 0x32, 0x10, 0x34, 0x10, 0x27, 0x10, 0x31, 0x20, 0x31, 0x08, 0x30, 0x08,
0x33, 0x08, 0x32, 0x08, 0x27, 0x20, 0x26, 0x08, 0x27, 0x08, 0x31, 0x08, 0x26, 0x08, 0x27, 0x20, 0x25, 0x08,
0x25, 0x08, 0x24, 0x08, 0x25, 0x08, 0x26, 0x18, 0x26, 0x08, 0x32, 0x18, 0x31, 0x08, 0x27, 0x20, 0x27, 0x08, 0x20, 0x08,
0x32, 0x10, 0x32, 0x18, 0x27, 0x08, 0x25, 0x08, 0x25, 0x08, 0x24, 0x08, 0x25, 0x08, 0x33, 0x20, 0x31, 0x08,
0x26, 0x08, 0x27, 0x08, 0x31, 0x08, 0x27, 0x10, 0x32, 0x10, 0x31, 0x10, 0x26, 0x10, 0x25, 0x20, 0x25, 0x08, 0x20, 0x08,
0x33, 0x08, 0x32, 0x08, 0x31, 0x20, 0x25, 0x18, 0x23, 0x08, 0x26, 0x20, 0x24, 0x08, 0x20, 0x08,
0x32, 0x0c, 0x31, 0x04, 0x27, 0x20, 0x26, 0x10, 0x25, 0x10, 0x25, 0x20, 0x25, 0x08, 0x20, 0x08,
0x25, 0x10, 0x33, 0x20, 0x32, 0x10, 0x25, 0x10, 0x31, 0x20, 0x27, 0x18,
0x27, 0x08, 0x26, 0x18, 0x25, 0x08, 0x26, 0x10, 0x32, 0x10, 0x32, 0x20, 0x32, 0x08, 0x30, 0x08,
0x33, 0x0c, 0x32, 0x04, 0x31, 0x20, 0x25, 0x18, 0x23, 0x08, 0x26, 0x20, 0x24, 0x08, 0x20, 0x08,
0x32, 0x0c, 0x31, 0x04, 0x27, 0x20, 0x26, 0x10, 0x25, 0x10, 0x33, 0x30,
0x33, 0x10, 0x35, 0x20, 0x34, 0x10, 0x33, 0x10, 0x32, 0x18, 0x33, 0x08, 0x34, 0x10, 0x30, 0x08,
0x34, 0x08, 0x33, 0x18, 0x33, 0x08, 0x32, 0x18, 0x32, 0x08, 0x31, 0x30,
0x00, 0x00
};
uchar code music3[] = // The song is for the lone brave
{
0x16,0x08,0x17,0x08,
0x21,0x08,0x22,0x08,
0x17,0x08,0x21,0x08,
0x21,0x10,0x21,0x08,
0x17,0x08,0x21,0x08,
0x22,0x08,0x17,0x08,
0x21,0x08,0x21,0x10,
0x21,0x08,0x22,0x08,
0x23,0x08,0x22,0x08,
0x23,0x08,0x22,0x08,
0x23,0x10,0x23,0x08,
0x22,0x08,0x23,0x10,
0x25,0x10,0x23,0x10,
0x16,0x08,0x17,0x08,
0x21,0x08,0x22,0x08,
0x17,0x08,0x21,0x08,
0x21,0x10,0x21,0x08,
0x17,0x08,0x21,0x08,
0x22,0x08,0x17,0x08,
0x21,0x08,0x21,0x10,
0x21,0x08,0x22,0x08,
0x23,0x08,0x22,0x08,
0x23,0x08,0x22,0x08,
0x23,0x10,0x23,0x08,
0x22,0x08,0x23,0x10,
0x25,0x10,0x23,0x10,
0x25,0x10,0x23,0x14,
0x25,0x08,0x23,0x14,
0x25,0x08,0x23,0x08,
0x25,0x08,0x26,0x08,
0x23,0x08,0x25,0x10,
0x25,0x08,0x23,0x14,
0x25,0x08,0x23,0x14,
0x25,0x08,0x23,0x08,
0x25,0x08,0x26,0x08,
0x23,0x08,0x25,0x10,
0x25,0x08,0x25,0x08,
0x23,0x08,0x22,0x08,
0x22,0x10,0x22,0x10,
0x21,0x08,0x23,0x08,
0x23,0x08,0x22,0x08,
0x22,0x10,0x22,0x10,
0x21,0x08,0x21,0x08,
0x16,0x20,0x25,0x08,
0x25,0x08,0x23,0x08,
0x22,0x08,0x22,0x10,
0x22,0x10,0x21,0x08,
0x23,0x08,0x23,0x08,
0x22,0x08,0x22,0x10,
0x22,0x10,0x21,0x08,
0x21,0x08,0x16,0x20,
0x00
};
uchar code quzi[] = // This array data is the reload value of each note in the timer , The first column is high , The second column is low // Time
{
0xf8,0x8c, // Low octave , low 1
0xf9,0x5b,
0xfa,0x15, // low 3
0xfa,0x67,
0xfb,0x04, // low 5
0xfb,0x90,
0xfc,0x0c, // low 7
0xfc,0x44, // The central C transfer
0xfc,0xac, // in 2
0xfd,0x09,
0xfd,0x34, // in 4
0xfd,0x82,
0xfd,0xc8, // in 6
0xfe,0x06,
0xfe,0x22, // High octave , high 1
0xfe,0x56,
0xfe,0x6e, // high 3
0xfe,0x9a,
0xfe,0xc1, // high 5
0xfe,0xe4,
0xff,0x03 // high 7
};
/**********************
The name of the function :void delay(unsigned int xms)
Function description : Time delay
Entrance parameters :xms: Enter the millisecond value that needs to be delayed
Export parameters : nothing
remarks :
***********************/
void delay(unsigned int xms)
{
uint i,j;
for(i=xms; i>0; i--)
for(j=124; j>0; j--);
}
/**********************
The name of the function :uchar quyin(uchar tem)
Function description : stay quzi Array , find music The reload value of the simplified musical note defined by the array , And return it to quzi Position in array
Entrance parameters :tem:music The musical notes defined in the array
Export parameters : The return is tem The notes are quzi The position value in the array
remarks :
***********************/
uchar quyin(uchar tem)
{
uchar qudiao,jp,weizhi; // Define the tune , Notes and positions
qudiao=tem/16; // high 4 Bit is the tune value
jp=tem%16; // low 4 Bit is a note
if(qudiao==1) // When the tune value is 1 when , It's a low octave , The lower octave is quzi The base address in the array is 0
qudiao=0;
else if(qudiao==2) // When the tune value is 2 when , It's the middle octave , The middle octave is at quzi The base address in the array is 14
qudiao=14;
else if(qudiao==3) // When the tune value is 3 when , It's a high octave , The high octave is quzi Array , The base address is 28
qudiao=28;
weizhi=qudiao+(jp-1)*2; // Add a note to the base address as an offset , You can locate this note at quzi Position in array
return weizhi; // Return this location value
}
/**********************
The name of the function :void playmusic()
Function description : Play music
Entrance parameters : nothing
Export parameters : nothing
remarks :
***********************/
void playmusic()
{
uchar p,m,tem; //m For the beat
while(1)
{
if(flag==1) // If the flag bit for playback is 1 Then play music
{
if(gequ==0) // Song selection bit is 0, Then play the same song
{
p=music1[jindu];
if(p==0x00) // If you come across an end , Time delay 1 second , Go back to the beginning and do it again
{
jindu=0;
delay(1000);
break;
}
else if(p==0xff) // If you encounter a pause , Time delay 100ms, Continue to take the next note
{
jindu=jindu+1;
delay(100);
TR0=0;
break;
}
else // Normally take notes and beats
{
tem=quyin(music1[jindu]); // Take out the current note at quzi The position value in the array
timeh=quzi[tem]; // Reload the timer corresponding to the note to timeh and timel
timel=quzi[tem+1];
jindu++;
TH0=timeh; // hold timeh and timel Assign timer
TL0=timel;
m=music1[jindu]; // Get the beat
jindu++;
}
TR0=1; // Turn on the timer 1
delay(m*180); // Wait for the beat to complete , adopt P3^4 Audio output port
TR0=0; // Turn off timer 1
beep=0; // send beep Port set 0, Protect buzzer
}
else if(gequ==1) // Song selection bit is 1, Then play the little donkey
{
p=music2[jindu];
if(p==0x00) // If you come across an end , Time delay 1 second , Go back to the beginning and do it again
{
jindu=0;
delay(1000);
break;
}
else if(p==0xff) // If you encounter a pause , Time delay 100ms, Continue to take the next note
{
jindu=jindu+1;
delay(100);
TR0=0;
break;
}
else // Normally take notes and beats
{
tem=quyin(music2[jindu]); // Take out the current note at quzi The position value in the array
timeh=quzi[tem]; // Reload the timer corresponding to the note to timeh and timel
timel=quzi[tem+1];
jindu++;
TH0=timeh; // hold timeh and timel Assign timer
TL0=timel;
m=music2[jindu]; // Get the beat
jindu++;
}
TR0=1; // Turn on the timer 1
delay(m*300); // Wait for the beat to complete , adopt P3^4 Audio output port
TR0=0; // Turn off timer 1
beep=0; // send beep Port set 0, Protect buzzer
}
else if(gequ==2) // Song selection bit is 2, Then play the city of the sky
{
p=music3[jindu];
if(p==0x00) // If you come across an end , Time delay 1 second , Go back to the beginning and do it again
{
jindu=0;
delay(1000);
break;
}
else if(p==0xff) // If you encounter a pause , Time delay 100ms, Continue to take the next note
{
jindu=jindu+1;
delay(100);
TR0=0;
break;
}
else // Normally take notes and beats
{
tem=quyin(music3[jindu]); // Take out the current note at quzi The position value in the array
timeh=quzi[tem]; // Reload the timer corresponding to the note to timeh and timel
timel=quzi[tem+1];
jindu++;
TH0=timeh; // hold timeh and timel Assign timer
TL0=timel;
m=music3[jindu]; // Get the beat
jindu++;
}
TR0=1; // Turn on the timer 1
delay(m*200); // Wait for the beat to complete , adopt P3^4 Audio output port
TR0=0; // Turn off timer 1
beep=0; // send beep Port set 0, Protect buzzer
}
}
else // The playback flag bit is not 1 when , Pause music
while(flag!=1);
}
}
/**********************
The name of the function :void init_sys()
Function description : System initialization , The function is to configure IO mouth
Entrance parameters : nothing
Export parameters : nothing
remarks :
***********************/
void init_sys()
{
P0M0=0xff; // Set push-pull mode
P0M1=0x00;
P2M0=0x08;
P2M1=0x00;
P3M0=0x10;
P3M1=0x00;
P4M0=0x00;
P4M1=0x00;
P5M0=0x00;
P5M1=0x00;
}
/**********************
The name of the function :void init()
Function description : Initialization of timers and external interrupts
Entrance parameters : nothing
Export parameters : nothing
remarks :
***********************/
void init()
{
TMOD=0x01; // Set the timer 0, Timing mode 1,16 Bit manual reload mode
TH0=0xD8; // Set initial value of timing
TL0=0xEF;
IE=0x87; // 1000 0111 EA=1,EX0=1,ET0=1,EX1=0;
IP=0x02; // 0000 0010 PT0=1; Timer 0 High priority
TR0=0; // Timer 0 Began to run
beep=0; // send beep Port set 0, Protect buzzer
}
void main()
{
init_sys(); // System initialization
init();
P0=0x00;
key1=1; // Set the two keys to the input state , Detection interrupt
key2=1;
flag=0; // The playback flag bit is cleared
while(1)
{
playmusic(); // Play music function
}
}
/**********************
The name of the function :void tim1() interrupt 1
Function description : Timer 0 Interrupt handling , Reinstall the value , And put beep Value reversal , Generate square waves
Entrance parameters : nothing
Export parameters : nothing
remarks :
***********************/
void tim1() interrupt 1 // The timer controls the frequency
{
TH0=timeh; // Assign initial value to
TL0=timel;
beep=~beep; // Interruption makes beep The flip produces a square wave
}
/**********************
The name of the function :void ex1() interrupt 0
Function description : Press the button 1 External interruption of , Invert the flag bit , The function is to pause and play music
Entrance parameters : nothing
Export parameters : nothing
remarks :
***********************/
void ex1() interrupt 0
{
delay(5);
if(key1==0) // Judge key1 Whether to press
{
delay(5);
if(key1==0)
{
while(!key1);
flag=~flag; // The playback interrupt bit is reversed , Play or pause
}
}
}
/**********************
The name of the function :void ex2() interrupt 2
Function description : Press the button 2 External interruption of , The function is to switch songs
Entrance parameters : nothing
Export parameters : nothing
remarks :
***********************/
void ex2() interrupt 2
{
delay(5);
if(key2==0) // Key key2 Press down , Switch songs
{
jindu=0; // The progress of the song is cleared , Sing from the beginning
gequ++; // Next song
if(gequ==3) // Get to the last song and tune to the first song
gequ=0;
}
}
边栏推荐
- Statistics 8th Edition Jia Junping Chapter 4 Summary and after class exercise answers
- 【指针】删除字符串s中的所有空格
- [pointer] solve the last person left
- Global and Chinese markets of MPV ACC ECU 2022-2028: Research Report on technology, participants, trends, market size and share
- Transplant hummingbird e203 core to Da Vinci pro35t [Jichuang xinlai risc-v Cup] (I)
- Public key box
- Statistics, 8th Edition, Jia Junping, Chapter 11 summary of knowledge points of univariate linear regression and answers to exercises after class
- 《统计学》第八版贾俊平第一章课后习题及答案总结
- Build your own application based on Google's open source tensorflow object detection API video object recognition system (I)
- 线程的实现方式总结
猜你喜欢
Query method of database multi table link
[HCIA continuous update] advanced features of routing
Statistics 8th Edition Jia Junping Chapter IX summary of knowledge points of classified data analysis and answers to exercises after class
STC-B学习板蜂鸣器播放音乐
Express
servlet中 servlet context与 session与 request三个对象的常用方法和存放数据的作用域。
Statistics 8th Edition Jia Junping Chapter 4 Summary and after class exercise answers
Logstack introduction and deployment -- elasticstack (elk) work notes 019
Transplant hummingbird e203 core to Da Vinci pro35t [Jichuang xinlai risc-v Cup] (I)
"If life is just like the first sight" -- risc-v
随机推荐
Vysor uses WiFi wireless connection for screen projection_ Operate the mobile phone on the computer_ Wireless debugging -- uniapp native development 008
[pointer] find the largest string
四元数---基本概念(转载)
【指针】删除字符串s中的所有空格
Statistics 8th Edition Jia Junping Chapter 2 after class exercises and answer summary
Statistics 8th Edition Jia Junping Chapter 4 Summary and after class exercise answers
Statistics 8th Edition Jia Junping Chapter 1 after class exercises and answers summary
Statistics 8th Edition Jia Junping Chapter 5 probability and probability distribution
《统计学》第八版贾俊平第一章课后习题及答案总结
CSAPP家庭作业答案7 8 9章
Why can swing implement a form program by inheriting the JFrame class?
【指针】求二维数组中最大元素的值
函数:求方程的根
Statistics 8th Edition Jia Junping Chapter 10 summary of knowledge points of analysis of variance and answers to exercises after class
Es full text index
Fundamentals of digital circuits (II) logic algebra
[pointer] counts the number of times one string appears in another string
c语言学习总结(上)(更新中)
[oiclass] share prizes
[issue 18] share a Netease go experience