1 s latency Program

Source: Internet
Author: User

# Include <reg52.h>
Sbit P1_0 = P1 ^ 0;
Void Delay (); // the case must be the same as that in the following example. Otherwise, a warning or error occurs.

Void Main ()
{
While (1) // start to keep repeating
{
P1_0 = 0;
Delay (); // Add () if there is no parameter. Otherwise, there will be a Warning.
P1_0 = 1;
Delay ();
}
}

Void Delay () // 12 m crystal oscillator 1 s 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; number.

}

// ========= The following non-code ================== //

Principle: The use of h, I, j, k cycle instructions consume a certain period of time to achieve the delay, why the use of cycle delay, in 12 m crystal delay 1 second requires h, I, j and k are four variables. What is the value of each variable in the code above? Starting from the period of the microcontroller, The Microcontroller has the command period, machine cycle, state cycle, and clock cycle;

Clock cycle: the period of the crystal oscillator. Take 12 MHz as an example. The clock cycle is 1/12000000 = 1/12us;

Machine cycle: 1 machine cycle = 6 State cycles = 12 clock cycles = 1us;

Command cycle: the time required by a single-chip microcomputer to execute a command is called the command cycle. The command cycle is based on the machine cycle. The machine cycles required for different commands are not necessarily the same (1-4 ), the single-chip microcomputer needs 1 us to execute a single-cycle command, 2 us to execute a dual-cycle command, and 8 command cycles for a for loop;

The time consumed by running the for loop can be measured in the Keil code:

# Include <reg52.h>
Sbit P1_0 = P1 ^ 0;
Void Delay ();

Void Main ()
{
While (1) // start to keep repeating
{
P1_0 = 0;
Delay ();
P1_0 = 1;
Delay ();
}
}

Void Delay ()
{
Unsigned char h;

For (h = 1000; h> 0; h --); // breakpoint

}

The time point after 1000 cycles of execution

1000 cycles consume 0.00042850 s-0.00019600 s = 0.00023250 s = 232.5us; 1 cycle consumes about us,

1 s = 000000us, 000000us/0.2us = 500000 times, while 5*4*116*214 = 496480; because the unsigned char is 1 byte, the range is 0 ~ 255, so we can only get a 1 s latency through nested loops.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.