Single-chip computer delay C language Program

Source: Internet
Author: User
(Crystal oscillator 12MHz, a machine cycle of 1us.)
A. 500MS Delay sub-Program
Program:
void delay500ms (void) {
unsigned char i,j,k;
for (i=15;i>0;i--)
for (j=202;j>0;j--)
for (k=81;k>0;k--);
}
Produced by the Assembly:
c:0x0800 7f0f MOV R7, #0x0F
c:0x0802 7ECA MOV R6, #0xCA
c:0x0804 7d51 MOV R5, #0x51
c:0x0806 ddfe djnz r5,c:0806
c:0x0808 DEFA djnz r6,c:0804
c:0x080a DFF6 djnz r7,c:0802
c:0x080c RET
Calculation Analysis:
Program has a total of three layers of loops
One-layer cycle n:r5*2 = 81*2 = 162us djnz 2us
Two-layer cycle m:r6* (n+3) = 202*165 = 33330us djnz 2us + R5 Assignment 1us = 3us
Three-layer cycle: r7* (m+3) = 15*33333 = 499995us djnz 2us + R6 Assignment 1us = 3us
Outside the loop: 5us subroutine calls 2us + subroutine returns 2us + R7 Assignment 1us = 5us
Total time delay = three-layer cycle + cycle = 499995+5 = 500000us =500ms
Calculation formula: Delay Time =[(2*r5+3) *r6+3]*r7+5

Two. 200ms Delay sub-Program
Program:
void delay200ms (void) {
unsigned char i,j,k;
for (i=5;i>0;i--)
for (j=132;j>0;j--)
for (k=150;k>0;k--);
}
Produced by the Assembly
c:0x0800 7f05 MOV R7, #0x05
c:0x0802 7E84 MOV R6, #0x84
c:0x0804 7d96 MOV R5, #0x96
c:0x0806 ddfe djnz r5,c:0806
c:0x0808 DEFA djnz r6,c:0804
c:0x080a DFF6 djnz r7,c:0802
c:0x080c RET

Three. 10ms Delay Sub-Program
Program:
void delay10ms (void) {
unsigned char i,j,k;
for (i=5;i>0;i--)
for (j=4;j>0;j--)
for (k=248;k>0;k--);
}
Produced by the Assembly
c:0x0800 7f05 MOV R7, #0x05
c:0x0802 7E04 MOV R6, #0x04
c:0x0804 7df8 MOV R5, #0xF8
c:0x0806 ddfe djnz r5,c:0806
c:0x0808 DEFA djnz r6,c:0804
c:0x080a DFF6 djnz r7,c:0802
c:0x080c RET

four. 1s delay sub-program
Program:
void Delay1s (void) {
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--);
}
Produced by the Assembly
c:0x0800 7f05 MOV R7, #0x05
c:0x0802 7E04 MOV R6, #0x04
c:0x0804 7d74 MOV R5, #0x74
c:0x0806 7cd6 MOV R4, #0xD6
c:0x0808 Dcfe djnz r4,c:0808
c:0x080a DDFA djnz r5,c:0806
c:0x080c DEF6 djnz r6,c:0804
c:0x080e DFF2 djnz r7,c:0802

c:0x0810 RET



Clock cycle clock Cycle is also known as the oscillation cycle, defined as the countdown to the clock pulse (it can be understood that the clock cycle is the reciprocal of the external crystal oscillator, such as 12M Crystal oscillator, its time period is 1/12 us), is the most basic computer, the smallest unit of time. Within a clock cycle, the CPU completes only one of the most basic actions. For a single chip, if the use of 1MHZ clock frequency, the clock cycle is 1us; if the 4MHZ clock frequency, the clock cycle is 250us. Because the clock pulse is the basic work pulse of the computer, it controls the working rhythm of the computer (so that every step of the computer is unified to its pace). Obviously, the higher the clock frequency for a computer of the same model, the faster the computer will work. However, due to different computer hardware circuits and devices are not exactly the same, so the need for the clock cycle frequency range is not necessarily the same.

   We learn the 8051 SCM Clock range is 1.2mhz-12mhz.

In the 8051 SCM, a clock cycle is defined as a beat (expressed in P), and two beats are defined as a state cycle (expressed in s). Machine cycle in the computer, in order to facilitate management, the execution of an instruction is often divided into several stages, each phase to complete a work. For example, fetching instructions, memory reading, memory writing, and so on, each of these tasks is called a basic operation. The time required to complete a basic operation is called the machine cycle. In general, a machine cycle consists of several S-cycles (state cycles). A machine cycle of 8051 series MCU is composed of 6 s cycles (state cycle).

As mentioned earlier, a clock cycle is defined as a beat (expressed in P), two beats are defined as a state cycle (in s), and the 8051 SCM Machine Cycle consists of 6 state cycles, i.e. one machine cycle = 6 state cycles = 12 clock cycles. The instruction cycle instruction cycle is the time required to execute a single instruction, typically consisting of several machine cycles. instruction is different, the number of machine cycles required is also different. For some simple single byte instruction, in the fetch instruction period, the instruction is taken out to the instruction register, the decoding executes immediately, no longer need the other machine cycle.
    For some more complex instructions, such as transfer instructions, multiplication instructions, two or more machine cycles are required.

An instruction usually containing a machine cycle is called a single cycle instruction, and an instruction containing two machine cycles is called a two-cycle instruction. &LT;H1 class= "mb-5" style= "margin:0px; padding:0px; " ><span class= "Ask-title" > when the crystal frequency of the 80c51 monolithic microcomputer is 12MHz, the clock cycle. What is the machine cycle? </span&gT; 

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.