//proteus simulation
//The microcontroller generates a buzzer to simulate the tone of the song (using a timer), not to sing the song (I have not heard of 51 yet)
//The main program is as follows. The function is to press a key and the digital tube will display the corresponding key number. If it is the first three keys, it will emit the notes (dao, ruai, mi)
//If it is the fourth key, it will play (the tune of the song "Only Mom is Good in the World")
//The program is as follows
#include
#define uchar unsigned char
sbit dula=P2^6; //If you use a physical object,
sbit wela=P2^7; //If the latch is proteu, it is not needed
sbit sound=P2^3; //define buzzer pin
unsigned char i,j,key,k,m,time;
uchar STH0,STL0;
//Common anode digital tube code value
uchar code table[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e};
//1,2,3,4 corresponding to the encoding value
unsigned char code music[]={0x25,0x57,0x84,0x98};
//Bit selection encoding (this is not necessary if it is proteus, but it is necessary if it is a physical object)
uchar code table1[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
//Data table of "Only Mom is Good in the World"
code unsigned char sszymmh[]={
6,2,3, 5,2,1, 3,2,2, 5,2,2, 1,3,2, 6,2,1, 5,2,1,
6,2,4, 3,2,2, 5,2,1, 6,2,1, 5,2,2, 3,2,2, 1,2,1,
6,1,1, 5,2,1, 3,2,1, 2,2,4, 2,2,3, 3,2,1, 5,2,2,
5,2,1, 6,2,1, 3,2,2, 2,2,2, 1,2,4, 5,2,3, 3,2,1,
2,2,1, 1,2,1, 6,1,1, 1,2,1, 5,1,6, 0,0,0
};
// Scale frequency table high eight bits
code unsigned char FREQH[]={
0xF2,0xF3,0xF5,0xF5,0xF6,0xF7,0xF8,
0xF9,0xF9,0xFA,0xFA,0xFB,0xFB,0xFC,0xFC, //1,2,3,4,5,6,7,8,i
0xFC,0xFD,0xFD,0xFD,0xFD,0xFE,
0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF,
} ;
// Scale frequency table lower eight bits
code unsigned char FREQL[]={
0x42,0xC1,0x17,0xB6,0xD0,0xD1,0xB6,
0x21,0xE1,0x8C,0xD8,0x68,0xE9,0x5B,0x8F, //1,2,3,4,5,6,7,8,i
0xEE,0x44, 0x6B,0xB4,0xF4,0x2D,
0x47,0x77,0xA2,0xB6,0xDA,0xFA,0x16,
};
// Function declaration
void display(uchar Duan_value,uchar Wei_value);
void delay(unsigned char t);
void delay1(uchar );
void display1(uchar value);
//Function to play song notes
void song()
{
TH0=STH0;
TL0=STL0;
TR0=1;
delay1(time); //This delay is very important, it is used to determine how long a certain note will last
}
//Set the initial value function of the corresponding button timer
void key_on(unsigned char key)
{
m=0;
if(key!=3)
{
TMOD=0x01; //Set the timing working mode bit t0 mode 1 (16-bit timer)
TH0=0xfe;
TL0=music[key]; //Timer initial value (according to the code corresponding to the note)
EA=1; //Open each interrupt at a time
ET0=1;
TR0=1;
display1(key+1);
while(P3!=0xff); //Debounce when the button is pressed
TR0=0;
}
else //After the fourth key is pressed, play the song
{
TMOD=0x01; //Set the timing working mode bit t0 mode 1 (16-bit timer)
TH0=0xfe;
TL0=music[key]; //Timer initial value (according to the code corresponding to the note)
EA=1; //Open each interrupt at a time
ET0=1;
TR0=0;
display1(key+1);
while(P3!=0xff); //Debounce when the button is pressed
time=1;
while(time!=0) //After the song is played, time=0 exits (terminator)
{
k=sszymmh[m]+7*sszymmh[m+1]-1;
STH0=FREQH[k];
STL0=FREQL[k];
time=sszymmh[m+2]; //This mode is fixed, no need to study too much why +2
m=m+3;
song();
}
}
}
/*Function: Beat control of playing music*/
void delay1(unsigned char t)
{
unsigned char t1;
unsigned long t2;
for(t1=0;t1 { for(t2=0;t2<8000;t2++) { ; } } TR0=0; } //Delay function (unit: ms) void delay(unsigned char t) { unsigned char x,y; for(x=t;t>0;t--) for(y=111;y>0;y--); } //Display the corresponding digital tube button value (if it is a real object) /*void display(uchar Duan_value,uchar Wei_value) { dula=1; P0=table[Duan_value]; dula=0; wela=1; P0=table1[Wei_value]; wela=0; delay(3); }*/ //Display function void display1(uchar value) { P0=table[value]; } void main() { display1(key); while(1) { if(P3==0xef) //First key pressed { delay(10); //Debounce the button after pressing it if(P3==0xef) { key=0; key_on(key); //Detect key press (and display data in the function) } } if(P3==0xdf) //The second key is pressed { delay(10); //Debounce the button after pressing it if(P3==0xdf) { key=1; key_on(key); } } if(P3==0xbf) //The third key is pressed { delay(10); if(P3==0xbf) { key=2; key_on(key); } } if(P3==0x7f) //The fourth key is pressed { delay(10); if(P3==0x7f) { key=3; key_on(key); } } } } //Timer 1 interrupt function void t0() interrupt 1 { if(key!=3) { TH0=0xfe; TL0=music[key]; } else { TH0=STH0; TL0=STL0; } sound=~sound; //buzzer inversion (generate pulse) }
Previous article:Matrix keyboard plus electronic clock (c51)
Next article:Course Design: Matrix Keyboard (c51, implemented using scanning method)
- 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
- 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
- [GD32L233C] + 2. Sort out some configuration issues
- How to Make DSP Digital Oscillator Generate Phase-Shifted Sine Wave
- Unboxing Review (Part 3) Preliminary Understanding
- STM32F10 Chinese reference material
- Showing my products + my development board
- The relationship between instruction set architecture, arm core, SoC, processor, CPU, GPU, etc.
- [Raspberry Pi 4B Review] + Unboxing
- There are two types of switch circuits, such as MOS+transistor+diode+button and switch circuits with MCU IO port directly connected to the button...
- I3C standard information, sharing
- Bluetooth Low Energy GAP, GATT