Delay program is essential in the development of single chip microcomputer system. For example, we often use the length of the buzzer to represent the system post results, and the length of the buzzer is inseparable from the delay program. The delay program is divided into software delay and hardware delay. Hardware delay is realized by using the overflow of waiting timing counter. This is not discussed first, but after talking about the timer. Software delay is the use of executing empty operation, occupying CPU and realizing delay. The software delay is not accurate and is used in occasions with low requirements. Due to the limited internal resources of 51 single chip microcomputer, software delay is often used. The following demonstration completes the process of STC MCU software delay program, and completes the source code of common delay program.
1. Create a new library file Open C51 Template.uvproj under the Proj folder in the C51 Template folder created in the previous tutorial. Create two new files and store them in the Library folder under the C51 Template folder with the file names delay. H and delay. C.
2. Complete header file
2.1 add conditional compilation macros and include files. Enter the following code in the header file (please enter the content in the comments in the previous paragraph of the program according to your own situation):
/*dely.h Designed by Bill Liu Version 0.0 Modified last by Bill Liu on 11/07/2021 */ #ifndef __STCIO_H_ #define __STCIO_H_ #include "mtype.h" #endif
2.2 definition of software delay function stc15w Series MCU contains clock circuit inside, which does not need external crystal oscillator, and the system clock can be set when downloading the program. Before defining a function, define an enumeration to list common optional clocks, and then define a delay function. The code is as follows:
/*dely.h Designed by Bill Liu Version 0.0 Modified last by Bill Liu on 11/07/2021 */ #ifndef __STCIO_H_ #define __STCIO_H_ #include "mtype.h" typedef enum { F11_0592MHz = 1, //11.0592MHz F12MHz, F18_432MHz, //18.432MHz F20MHz, F22_1184MHz, //22.1184MHz F24MHz, F27MHz, F30MHz, F33MHz, F33_1776MHz //33.1776MHz }FSYSCLOCK; /**************************************** Function: Delay1us(FSYSCLOCK fsclk); Return value: void Discription: soft delay 1us at fsclk Example: Delay1us(F11_0592MHz); ****************************************/ void Delay1us(FSYSCLOCK fsclk); /**************************************** Function: Delay10us(FSYSCLOCK fsclk); Return value: void Discription: soft delay 10us at fsclk Example: Delay10us(F11_0592MHz); ****************************************/ void Delay10us(FSYSCLOCK fsclk); /**************************************** Function: Delay1ms(FSYSCLOCK fsclk); Return value: void Discription: soft delay 1ms at fsclk Example: Delay1ms(F11_0592MHz); ****************************************/ void Delay1m(FSYSCLOCK fsclk); /**************************************** Function: Delay10ms(FSYSCLOCK fsclk); Return value: void Discription: soft delay 10ms at fsclk Example: Delay10ms(F11_0592MHz); ****************************************/ void Delay10ms(FSYSCLOCK fsclk); /**************************************** Function: Delayxus(ui8 x,FSYSCLOCK fsclk); Return value: void Discription: soft delay xus at fsclk Example: Delayxus(5,F11_0592MHz); ****************************************/ void Delayxus(ui8 x,FSYSCLOCK fsclk); /**************************************** Function: Delay10xus(ui8 x, FSYSCLOCK fsclk); Return value: void Discription: soft delay 10xus at fsclk Example: Delay10us(5,F11_0592MHz); ****************************************/ void Delay10xus(ui8 x, FSYSCLOCK fsclk); /**************************************** Function: Delayxms(ui8 x,FSYSCLOCK fsclk); Return value: void Discription: soft delay xms at fsclk Example: Delay1ms(5,F11_0592MHz); ****************************************/ void Delayxms(ui8 x,FSYSCLOCK fsclk); /**************************************** Function: Delay10xms(ui8 x, FSYSCLOCK fsclk); Return value: void Discription: soft delay 10xms at fsclk Example: Delay10xms(5,F11_0592MHz); ****************************************/ void Delay10xms(ui8 x, FSYSCLOCK fsclk); #endif
3. Function implementation STC ISP download tool is embedded with software delay code generation tool. With this tool, you can Copy and paste the generated code into your own program without writing your own code. The code generation interface is as follows:
You can use this tool to complete the basic delay program. Then write a small amount of code yourself to complete the delay program code. The code after completion is as follows:
#include "delay.h" //****************************************/ void Delay1us(FSYSCLOCK fsclk) { ui8 i; switch(fsclk) { case F11_0592MHz: //at syslck 11.0592MHz _nop_(); _nop_(); _nop_(); break; case F12MHz: //at syslck 12.000MHz _nop_(); _nop_(); _nop_(); _nop_(); break; case F18_432MHz: //at syslck 18.432MHz i = 2; while (--i); break; case F20MHz: //at syslck 20.000MHz _nop_(); _nop_(); i = 2; while (--i); break; case F22_1184MHz: //at syslck 22.1184MHz i = 3; while (--i); break; case F24MHz: //at syslck 24.000MHz _nop_(); _nop_(); i = 3; while (--i); break; case F27MHz: //at syslck 27.000MHz _nop_(); i = 4; while (--i); break; case F30MHz: //at syslck 30.000MHz i = 5; while (--i); break; case F33MHz: //at syslck 33.000MHz _nop_(); _nop_(); _nop_(); i = 5; while (--i); break; case F33_1776MHz: //at syslck 33.1776MHz _nop_(); _nop_(); _nop_(); i = 5; while (--i); break; } } //End of Delay1us(FSYSCLOCK fsclk) //****************************************/ void Delay10us(FSYSCLOCK fsclk) { ui8 i; switch(fsclk) { case F11_0592MHz: //at syslck 11.0592MHz _nop_(); i = 25; while (--i); break; case F12MHz: //at syslck 12.000MHz _nop_(); _nop_(); i = 27; while (--i); break; case F18_432MHz: //at syslck 18.432MHz _nop_(); _nop_(); i = 43; while (--i); break; case F20MHz: //at syslck 20.000MHz _nop_(); _nop_(); i = 47; while (--i); break; case F22_1184MHz: //at syslck 22.1184MHz _nop_(); _nop_(); _nop_(); i = 52; while (--i); break; case F24MHz: //at syslck 24.000MHz _nop_(); _nop_(); i = 57; while (--i); break; case F27MHz: //at syslck 27.000MHz i = 65; while (--i); break; case F30MHz: //at syslck 30.000MHz _nop_(); _nop_(); i = 72; while (--i); break; case F33MHz: //at syslck 33.000MHz i = 80; while (--i); break; case F33_1776MHz: //at syslck 33.1776MHz _nop_(); _nop_(); i = 80; while (--i); break; } } //End of Delay10us(FSYSCLOCK fsclk) //****************************************/ void Delay1m(FSYSCLOCK fsclk) { ui8 i,j; switch(fsclk) { case F11_0592MHz: //at syslck 11.0592MHz _nop_(); _nop_(); _nop_(); i = 11; j = 190; do { while (--j); } while (--i); break; case F12MHz: //at syslck 12.000MHz i = 12; j = 169; do { while (--j); } while (--i); break; case F18_432MHz: //at syslck 18.432MHz i = 18; j = 235; do { while (--j); } while (--i); break; case F20MHz: //at syslck 20.000MHz i = 20; j = 113; do { while (--j); } while (--i); break; case F22_1184MHz: //at syslck 22.1184MHz _nop_(); _nop_(); i = 22; j = 128; do { while (--j); } while (--i); break; case F24MHz: //at syslck 24.000MHz i = 24; j = 85; do { while (--j); } while (--i); break; case F27MHz: //at syslck 27.000MHz i = 27; j = 64; do { while (--j); } while (--i); break; case F30MHz: //at syslck 30.000MHz i = 30; j = 43; do { while (--j); } while (--i); break; case F33MHz: //at syslck 33.000MHz i = 33; j = 22; do { while (--j); } while (--i); break; case F33_1776MHz: //at syslck 33.1776MHz _nop_(); _nop_(); i = 33; j = 66; do { while (--j); } while (--i); break; } } //End of Delay1m(FSYSCLOCK fsclk) //****************************************/ void Delay10ms(FSYSCLOCK fsclk) { ui8 i,j,k; switch(fsclk) { case F11_0592MHz: //at syslck 11.0592MHz i = 108; j = 145; do { while (--j); } while (--i); break; case F12MHz: //at syslck 12.000MHz i = 117; j = 184; do { while (--j); } while (--i); break; case F18_432MHz: //at syslck 18.432MHz _nop_(); _nop_(); i = 1; j = 180; k = 71; do { do { while (--k); } while (--j); } while (--i); break; case F20MHz: //at syslck 20.000MHz _nop_(); _nop_(); i = 1; j = 195; k = 136; do { do { while (--k); } while (--j); } while (--i); break; case F22_1184MHz: //at syslck 22.1184MHz _nop_(); _nop_(); i = 1; j = 216; k = 35; do { do { while (--k); } while (--j); } while (--i); break; case F24MHz: //at syslck 24.000MHz _nop_(); _nop_(); i = 1; j = 234; k = 113; do { do { while (--k); } while (--j); } while (--i); break; case F27MHz: //at syslck 27.000MHz _nop_(); _nop_(); i = 2; j = 7; k = 159; do { do { while (--k); } while (--j); } while (--i); break; case F30MHz: //at syslck 30.000MHz _nop_(); _nop_(); i = 2; j = 36; k = 206; do { do { while (--k); } while (--j); } while (--i); break; case F33MHz: //at syslck 33.000MHz _nop_(); _nop_(); i = 2; j = 65; k = 253; do { do { while (--k); } while (--j); } while (--i); break; case F33_1776MHz: //at syslck 33.1776MHz _nop_(); _nop_(); i = 2; j = 67; k = 183; do { do { while (--k); } while (--j); } while (--i); break; } } //End of Delay10ms(FSYSCLOCK fsclk) //****************************************/ void Delayxus(ui8 x,FSYSCLOCK fsclk) { while(x) { Delay1us(fsclk); x--; } } //End of Delayxus(ui8 x,FSYSCLOCK fsclk) //****************************************/ void Delay10xus(ui8 x, FSYSCLOCK fsclk) { while(x) { Delay10us(fsclk); x--; } } //End of Delay10xus(ui8 x, FSYSCLOCK fsclk) //****************************************/ void Delayxms(ui8 x,FSYSCLOCK fsclk) { while(x) { Delay1ms(fsclk); x--; } } //End of Delayxms(ui8 x,FSYSCLOCK fsclk) //****************************************/ void Delay10xms(ui8 x, FSYSCLOCK fsclk) { while(x) { Delay10ms(fsclk); x--; } } //End of Delay10xms(ui8 x, FSYSCLOCK fsclk)
The program will be uploaded to CSDN. The file name is Source Code of stc15w soft delay.rar. You can search and download it if necessary.