#include
sbit P1_0 = P1^0;
void Delay(); // The following references must be consistent with the uppercase and lowercase letters here, otherwise there will be warnings or errors
void Main()
{
while (1) // Start looping
{
P1_0 = 0;
Delay(); // Even if there is no parameter, add () or there will be a Warning
P1_0 = 1;
Delay();
}
}
void Delay() // 12M crystal oscillator 1s delay
{
unsigned char h, i, j, k;
for (h = 5; h 》 0; h--)
for (i = 4; i 》 0; i--)
for (j = 116; j 》 0; j--)
for (k = 214; k > 0; k--); // The last one must have a ;
}
//=======The following is non-code==========//
Principle, using h, i, j, k loop instructions to consume a certain amount of time to achieve delay. Why do we need h, i, j, k four variables in the 12M crystal oscillator delay of 1 second, and the values of each variable are as shown in the above code? This starts with the cycle of the single-chip microcomputer. The single-chip microcomputer has instruction cycle, machine cycle, state cycle, and clock cycle;
Clock cycle: also known as the oscillation period of the crystal oscillator, taking 12MHz as an example, the clock cycle is 1/12000000 = 1/12us;
Machine cycle: 1 machine cycle = 6 state cycles = 12 clock cycles = 1us;
Instruction cycle: The time required for a microcontroller to execute an instruction is called an instruction cycle. The instruction cycle is in machine cycles. Different instructions may require different machine cycles (1-4). A microcontroller takes 1us to execute a single-cycle instruction, 2us to execute a double-cycle instruction, and a for loop takes 8 instruction cycles.
The time consumed by executing the for loop can be measured in Keil through the code:
#include
sbit P1_0 = P1^0;
void Delay();
void Main()
{
while (1) // Start looping
{
P1_0 = 0;
Delay();
P1_0 = 1;
Delay();
}
}
void Delay()
{
unsigned char h;
for (h = 1000; h > 0; h--); // breakpoint here
}
The time point after the loop is executed 1000 times
1000 cycles take 0.00042850s - 0.00019600s = 0.00023250s = 232.5us; 1 cycle takes about 0.2us,
1s = 1000000us, 1000000us / 0.2us = 500000 times, and 5 * 4 * 116 * 214 = 496480; because the size of unsigned char is 1 byte and the range is 0~255, we can only get an approximate 1s delay through loop nesting.
Previous article:Design of dynamic scanning driver for 51 single-chip digital tube
Next article:Design of buzzer based on AT89C52 single chip microcomputer
- Popular Resources
- Popular amplifiers
- 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)
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
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Three steps to govern hybrid multicloud environments
- Three steps to govern hybrid multicloud environments
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Thank you for being there + thank you for everyone who appears in my life
- Use NucleiStudio to import GD32VF103_Demo_Suites routine
- [Project source code] Operating FPGA side registers under FPGA-based Linux
- Newbies need help with a few questions about RA2A1
- The virtual machine started to freeze again
- cortex-M4 K66 implements PPIT timing interrupt source program
- How to solve the problem of solder balls caused by mismatched pads during production
- Vim developed entirely in Python
- [ESK32-360 Review] 5. Run a real serial port example
- Google Translate no longer provides translation services to China (temporary solution)