//Use the 51 single-chip microcomputer as a signal generator to output square waves of four frequencies at the same time.
Reference address:51 MCU as a signal generator example programming
//Requirement: The crystal oscillator is 12MHz, T0 is used as the timer, and the lower four bits of P1 output four frequencies of square waves:
// P1.3 = 1.25kHz, P1.2 = 2.5kHz, P1.1 = 5kHz, P1.0 = 10kHz
//In addition, the above four frequencies require a four-to-one data selector to select one of them for output. //
The outputs of P1.6 and P1.7 are used to control the selection bits of the four-to-one data selector, which are controlled by the P3.0 button.
//====================================================
//Now I have added PROTEUS simulation pictures to the blog.
//The title requires that the four frequencies output on the four adjacent interface lines have a double relationship, which can be completed using a timer interrupt.
//The title also requires the use of a data selector to select a frequency to output by pressing a button.
//The screenshots of PROTEUS simulation are shown below.
//In the figure, a frequency meter and a speaker are connected to the output end for frequency detection.
//When the button is pressed, the output frequency can be switched in turn.
//The screenshots of the detection results of the four frequencies are all shown in the figure. It can be seen that the output frequency is very ideal.
//The PC speaker can also hear the sound, the frequency of 10kHz, which is quite harsh.
The outputs of P1.6 and P1.7 are used to control the selection bits of the four-to-one data selector, which are controlled by the P3.0 button.
//====================================================
//Now I have added PROTEUS simulation pictures to the blog.
//The title requires that the four frequencies output on the four adjacent interface lines have a double relationship, which can be completed using a timer interrupt.
//The title also requires the use of a data selector to select a frequency to output by pressing a button.
//The screenshots of PROTEUS simulation are shown below.
//In the figure, a frequency meter and a speaker are connected to the output end for frequency detection.
//When the button is pressed, the output frequency can be switched in turn.
//The screenshots of the detection results of the four frequencies are all shown in the figure. It can be seen that the output frequency is very ideal.
//The PC speaker can also hear the sound, the frequency of 10kHz, which is quite harsh.
//The following program has been simulated successfully.
//---------------------------------------------------
#include
#define uchar unsigned char
#define TL_0SET (256 - 50) //The timing time is selected as 50us
//The output square wave period will be 100us, 10kHz
sbit P3_2 = P3^2; //For buttons
sbit P1_0 = P1^0; //Four frequency outputs
sbit P1_1 = P1^1; //
sbit P1_2 = P1^2; //
sbit P1_3 = P1^3; //
sbit P1_6 = P1^6; //Two selection control outputs
sbit P1_7 = P1^7; //
//---------------------------------------------------
#include
#define uchar unsigned char
#define TL_0SET (256 - 50) //The timing time is selected as 50us
//The output square wave period will be 100us, 10kHz
sbit P3_2 = P3^2; //For buttons
sbit P1_0 = P1^0; //Four frequency outputs
sbit P1_1 = P1^1; //
sbit P1_2 = P1^2; //
sbit P1_3 = P1^3; //
sbit P1_6 = P1^6; //Two selection control outputs
sbit P1_7 = P1^7; //
bdata uchar x = 0;
sbit p16 = x^0;
sbit p17 = x^1;
sbit p16 = x^0;
sbit p17 = x^1;
uchar i, j;
//---------------------------------------------------
void e0int() interrupt 0 //External interrupt 0 acts as a switch to control the four-choice data selector
{
while(P3_2 == 0) {;} //Wait for the button to be released
x++;
if(x == 4) x = 0; //Change the current output selection
P1_7 = p16;
P1_6 = p17;
}
//---------------------------------------------------
void time0() interrupt 1 //T0's interrupt service routine
{ //T0 works in mode 2 and automatically loads the initial value
i++;
j = i % 16; //Get the lower four bits of i
j += x << 6; //Combined data selector control signal
P1 = j; //Output four frequencies of square wave and control signal
}
//---------------------------------------------------
void main()
{
TMOD = 0x02; //T0 timing mode 2
TH0 = TL_0SET; //Self-loaded timing initial value
TR0 = 1; //Start T0
ET0 = 1;
IT0 = 1; //External interrupt 0 is triggered by falling edge
EX0 = 1; //External interrupt 0 enables
EA = 1;
while(1) {;}
}
//---------------------------------------------------
//---------------------------------------------------
void e0int() interrupt 0 //External interrupt 0 acts as a switch to control the four-choice data selector
{
while(P3_2 == 0) {;} //Wait for the button to be released
x++;
if(x == 4) x = 0; //Change the current output selection
P1_7 = p16;
P1_6 = p17;
}
//---------------------------------------------------
void time0() interrupt 1 //T0's interrupt service routine
{ //T0 works in mode 2 and automatically loads the initial value
i++;
j = i % 16; //Get the lower four bits of i
j += x << 6; //Combined data selector control signal
P1 = j; //Output four frequencies of square wave and control signal
}
//---------------------------------------------------
void main()
{
TMOD = 0x02; //T0 timing mode 2
TH0 = TL_0SET; //Self-loaded timing initial value
TR0 = 1; //Start T0
ET0 = 1;
IT0 = 1; //External interrupt 0 is triggered by falling edge
EX0 = 1; //External interrupt 0 enables
EA = 1;
while(1) {;}
}
//---------------------------------------------------
Previous article:MCU serial communication source code
Next article:51 4WD wireless remote control car (200m effective distance)
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
- 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)
- Learn ARM development (4)
- Learn ARM development (6)
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- 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
MoreDaily News
- 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?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
Guess you like
- Introduction to PID Control Algorithm and Strategy
- DSP28335 Peripheral Clock
- Please explain the role of the window comparator here
- EEWORLD University Hall----Live Replay: TI MSP430 low-power analog peripherals help home portable healthcare products
- [Sipeed LicheeRV 86 Panel Review] I Unboxing and Basic Function Test
- [Atria Development Board AT32F421 Review] 4. Run RTX
- [MM32 eMiniBoard Review] Initial Impressions of Receiving Data Download Environment Setup
- A brief discussion on several application solutions of GPRS
- 【Embedded Qt】Building the Qt runtime environment for embedded Linux
- Project Submission