1s delay program design for 51 single chip microcomputer

Publisher:泉地水无痕Latest update time:2021-01-14 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#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


}


1s delay program design for 51 single chip microcomputer


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.


Reference address:1s delay program design for 51 single chip microcomputer

Previous article:Design of dynamic scanning driver for 51 single-chip digital tube
Next article:Design of buzzer based on AT89C52 single chip microcomputer

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号