#include "at89x51.h"
#define uchar unsigned char
#define uint unsinged int
sbit speaker = P1^5; //Buzzer. The default pin for the button is P3.2 (external interrupt 0)
uchar t0h,t0l,time,flag;
//--------------------------------------
//The microcontroller crystal oscillator uses 12MHz
// 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 note 234567
0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF}; // Ultra high-pitched 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 tone, 6 1/4 beats;
//5,2,2 represent: 5, middle tone, 2 1/4 beats;
//3,2,4 represent: 3, middle tone, 4 1/4 beats;
//5,2,4 represent: 5, middle tone, 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************************/
flying 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*************************/
flying 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 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 and stop pronunciation
}
//--------------------------------------
void singachar() //play a note
{
TR0=0;
TH0=t0h; //Control tone
TL0=t0l;
TR0=1; //Start T0, and T0 will output square wave to pronounce
delay(time); //Control the time length, i.e. the beat
}
//--------------------------------------
//**************Play a song***********************************//
void song()
{
flying k,i,*str;
i=0;
time=1;
while(time)
{
switch(flag)
{
case 1:str=sszymmh;break;
case 2:str=TwoButterfly;break;
case 3:str=Birthday;break;
case 4:str=TwoMouse;break;
}
k=str[i]+7*(str[i+1]); //The i-th note is the note, the i+1th note 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 out the time length value beat time
i+=3;
singachar();
}
}
//-----------------------------------------------------
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 int0() interrupt 0 //T0 interrupt program, control the tone of pronunciation
{
delay(1);
if(INT0==0)
{
flag++; //Close T0 first
if(flag>=5)
flag=0;
}
while(INT0==0);
}
//--------------------------------------
void main(void)
{
TMOD=0x01; //Set T0 timing working mode 1
IT0=1;
EX0=1; //External interrupt 0
ET0=1; //Open T0 interrupt
EA=1; //Open CPU interrupt
while(1)
{
song();
delay(2);
/* song(sszymmh);
delay(20);
song(TwoButterfly);
delay(20);
song(Birthday);
delay(20);*/
}
//Play a note
}
Previous article:AT89C52 MCU Proteus Simulation Music Player Operation Steps
Next article:51 MCU decimal integer and binary integer conversion assembly language subroutine
- Popular Resources
- Popular amplifiers
- Learn ARM development(14)
- Learn ARM development(15)
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
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
- 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
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- I have a question about the normalization calculation of multi-order low-pass filters. Thank you!
- Digital Signal Processing (DSP) Library for MSP430 Microcontrollers
- LM5117 WEBENCH 13.6V-17.6V to 5.00V@3A Design Solution
- CCS6.0 creates a DSPc2000 series project operation steps
- Live FAQ: Renesas RA MCU family members are growing rapidly, helping to build safe and stable industrial control systems
- Bluetooth protocol analysis (5)_Technical analysis related to BLE broadcast communication
- Check out the exclusive disassembly document! How do TWS earphones achieve hands-free and voice wake-up control?
- Shenmu Android Face Recognition SDK Compilation Instructions
- First time drawing board, newbie help
- Heart rate and blood oximeter chips are welcome to be added! If you have sensors or chips for thermometers and blood pressure meters, please add them too.