51 single-chip microcomputer (Puzhong HC6800-EM3 V3.0) experimental routine software analysis experiment 2 LED flickering

Table of contents

foreword

1. Schematic diagram and introduction of knowledge points

2. Code analysis

Knowledge point 4: Why is the function delay(u16 i) delayed by about 10us when i=1?


foreword

This is already the second experiment. The previous experiment was to light up the first LED light, and this experiment is to flicker the LED.


1. Schematic diagram and introduction of knowledge points

The schematic diagram is the same as the previous section, please refer to the previous section:

51 single-chip microcomputer (Puzhong HC6800-EM3 V3.0) experimental routine software analysis experiment one point lights up the first LED_ManGo CHEN's Blog-CSDN Blog

2. Code analysis

Let me introduce the project first:

Let's go directly to the code:

/**************************************************************************************
*		              LED闪烁实验												  *
实现现象:下载程序后D11指示灯闪烁
注意事项:无																				  
***************************************************************************************/

#include "reg52.h"			 //此文件中定义了单片机的一些特殊功能寄存器
typedef unsigned int u16;	  //对数据类型进行声明定义
typedef unsigned char u8;


sbit led=P0^0;		 //将单片机的P0.0端口定义为led

/*******************************************************************************
* 函 数 名         : delay
* 函数功能		   : 延时函数,i=1时,大约延时10us
*******************************************************************************/
void delay(u16 i)
{
	while(i--);	
}

/*******************************************************************************
* 函 数 名       : main
* 函数功能		 : 主函数
* 输    入       : 无
* 输    出    	 : 无
*******************************************************************************/
void main()
{
	while(1)
	{
		led=0;
		delay(50000); //大约延时450ms
		led=1;
		delay(50000); //大约延时450ms	
	}		
}

 The previous knowledge points 1, 2, and 3 are all in the previous section: 51 single-chip microcomputer (Puzhong HC6800-EM3 V3.0) experimental routine software analysis experiment point lights up the first LED_ManGo CHEN's Blog-CSDN Blog

Knowledge point 4: Why is the function delay(u16 i) delayed by about 10us when i=1?

 Here's the exact words from the MCU manual:

 

 

 It can be seen from the above that an assembly instruction (machine cycle) = 12 CPU working clocks. If you want to know that the while statement in the delay function is translated into assembly, it consists of several assembly instructions:

 We use a 12MHZ crystal oscillator, that is, a machine cycle is 1us

Let's simulate and do experiments below:

1. Run the program to the beginning of the while statement, and the system running time is 394us

 2. Then run the program to the end of the while statement. At this time, the system running time is 401us, which consumes 7us, and just executes 7 assembly instructions.

 3. Then run the program to the beginning of the while statement. At this time, the system running time is 403us, which consumes a total of 9us, about 10us (not very accurate)

 Because it is not very accurate, in the routine

delay(50000); //about 450ms delay

It is indeed the delay calculated by 9us.


Now you know how the delay in the microcontroller is derived.

Guess you like

Origin blog.csdn.net/qq_42700289/article/details/132087500