Use of buzzer:
The buzzer frequency is roughly set to 20ms or 50hz.
#include
#define uint unsigned int
#define uchar unsigned char
sbit fen = P2^6;
void delay(uint z);
main()
{
while(1)
{
delay(1); //Delay 10 milliseconds Note that the output here is a 20 millisecond square wave
//Because a square wave must have positive and negative transitions to count as a cycle, here?
//It jumps every 10 milliseconds, so the period is 200ms and the frequency is 50Hz
fen = ~fen;
}
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
Precise control of buzzer sound: using a timer
#include
#define uint unsigned int
#define uchar unsigned char
sbit fen = P2^6;
uchar num,ben;
void init();
void main()
{
init();
while(1);
}
void init()
{
TMOD = 0x11;
TH1 = (65536-50000)/256;
TL1 = (65536-50000)%256;
ET1 = 1;
EA = 1;
TR1 =1;
}
void time1()interrupt 3
{
TH1 = (65536-5000)/256; //The timing is 50 milliseconds
TL1 = (65536-5000)%256;
num++;
if(num==2)
{
num = 0;
fen = ~fen;
}
}
The buzzer can emit precise sound (control the frequency of 100ms, 200ms, 300ms, 400ms, 500ms)
error code:
//I have been debugging this program for a whole morning, and I feel something is wrong. The problem is
//Timer interruption, that is, when two timers are working, will the original timer be interrupted?
//The function of this program is to make the speaker sound.
//100ms, 200, 300, 400, 500
//Originally, 5 frequencies should be heard, but only 3 are actually heard, indicating that the interrupts are misaligned and the interrupts affect each other
#include
#define uint unsigned int
#define uchar unsigned char
sbit fen = P2^6;
uchar num,flag,tt;
void init();
void main()
{
init();
while(1);
}
void init()
{
TMOD = 0x11;
TH1 = (65536-50000)/256;
TL1 = (65536-50000)%256;
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
ET1 = 1;
ET0 =1;
EA = 1;
TR0 =1;
TR1 =1;
}
void time0()interrupt 1
{
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
tt++;
switch(tt/20)
{
case 0:
flag = 1;
break;
case 1:
flag = 2;
break;
case 2:
flag = 3;
break;
case 3:
flag = 4;
break;
case 4:
tt=0;
flag = 5;
break;
}
}
void time1()interrupt 3
{
TH1 = (65536-5000)/256; //The timing is 50 milliseconds
TL1 = (65536-5000)%256;
num++;
if(num==flag)
{
num = 0;
fen = ~fen;
}
}
Correct code
/*
Use two timers to control the buzzer sound at the same time.
Timer 0 controls the frequency, and Timer 1 controls the same
The duration of the frequency is output in sequence at 2s intervals.
1, 10, 50, 100, 200, 400, 800,
1k (hz) square wave?
Assume the crystal frequency is 12MHz.
*/
#include
#include
#define uint unsigned int
#define uchar unsigned char
sbit beep = P2^3;
uchar tt;
uint fre,flag;
void main()
{
fre = 50000;
beep = 0;
TMOD = 0x11;
TH0 = (65536-fre)/256;
TL0 = (65536-fre)%256;
TH1 = (65536- 50000)/256;
TL1 = (65536-50000)%256;
EA = 1;
ET0 = 1;
ET1 =1;
TR0 =1;
TR1 = 1;
while(1);
}
void timer0()interrupt 1
{
TR0 = 0; //After entering the interrupt function, turn off timer 0 to prevent the internal program interrupt from being lost
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
tt++;
if(flag<40)
if(tt==10)
{
tt = 0;
fre = 50000; //Use fre to select the frequency timing of 50ms, so the frequency of the interrupt is 10hz
beep = ~beep;
}
if(flag>=40&&flag<80)
{
tt = 0;
fre = 50000; //Use fre to select the frequency timing of 50ms, so the frequency of the interrupt is 10hz
beep =~beep;
}
if(flag>=80&&flag<120)
{
tt = 0;
fre = 10000; //timing 10ms, period 20ms, frequency 50hz
beep =~beep;
}
if(flag>=120&&flag<160)
{
tt = 0;
fre = 5000; // Timing 5 milliseconds, period 10ms, frequency 100zh
beep =~beep;
}
if(flag>=160&&flag<200)
{
tt = 0;
fre = 2500; //timing 2.5, period 5ms, frequency 200
beep =~beep;
}
if(flag>=200&&flag<240)
{
tt = 0;
fre = 1250; //timing 1.25, period 2.5ms, frequency 400
beep =~beep;
}
if(flag>=240&&flag<280)
{
tt = 0;
fre = 625; //timing 2.5, period 5ms, frequency 800
beep =~beep;
}
if(flag>=280&&flag<320)
{
tt = 0;
fre = 312; // frequency is 1600
beep =~beep;
}
if(flag>=320&&flag<360)
{
tt = 0;
fre = 156; // frequency is 3200
beep =~beep;
}
TR0 = 1;
}
void timer1()interrupt 3
{
TH1 = (65536 - 50000)/256;
TL1 = (65536 - 50000)%256;
flag++;
if(flag == 360)
{
flag = 0;
fre = 50000;
}
}
Previous article:51 Writing multiple file systems
Next article:Timer counter application improvement
- 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
- Industry first! Xiaopeng announces P7 car chip crowdfunding is completed: upgraded to Snapdragon 8295, fluency doubled
- P22-009_Butterfly E3106 Cord Board Solution
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- A fire broke out at Foxconn in Shenzhen, with thick smoke billowing from the scene
- PCB copper plating
- Color card combination lock
- Taking a “family-first” approach to op amp design
- CCS5.5.0 compilation problem: "different version of compiler"
- On the first day back to work, the company CEO wants to fire me? Can I get N+1 compensation?
- Hard disk control and encryption based on FPGA
- Redeem mooncakes with your core points during the Mid-Autumn Festival! Exchange your core points for core points and you will get endless surprises!
- I don't understand the GPIO operation statements of the HAL library. Please help me
- High-definition smart camera based on TMS320DM8127