The following routines are demonstrations of generating microsecond and millisecond delays respectively. If you want to achieve different delays, just change the actual parameters in the program. When calling this program, the actual parameters must be numbers, and variables cannot be used as actual parameters. Theoretically, each delay function can achieve the following accuracy: delay_us(1); //Delay 1us delay_ms(1); //Delay 1ms delay_us(4.2); //Delay 4.2us delay_ms(4.2); //Delay 4.2ms I used the MSP430F448 platform to test the above routine. The program used and the measured results are published as follows for your reference: Software timing under 1MHZ main frequency: Program: #include /*********************************************Software delay****************************************/ #define CPU_F ((double)1000000) #define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0)) #define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0)) /******************************************************************************************/ //1000000 is the CPU's main frequency, i.e. MCLK, which needs to change with the system void main(void) { WDTCTL = WDTPW + WDTHOLD; P1DIR = 0x22; P1SEL = 0x22; P2DIR=0X01; for(;;) { delay_us(1); P2OUT^=0X01; } } Result: //delay_us(1): The actual delay time is 6.8us //delay_us(10); The actual delay time is 15.6us //delay_us(20); the actual delay time is 24.8us //delay_us(90); the actual delay time is 92us //delay_us(100); the actual delay time is 100us //delay_us(900); the actual delay time is 880us //delay_us(1000); the actual delay time is 0.96ms //delay_ms(1); the actual delay time is 0.96ms //delay_ms(10); the actual delay time is 9.6ms //delay_ms(100); the actual delay time is 96ms //delay_ms(500); the actual delay time is 480ms //delay_ms(1000); the actual delay time is 950ms //delay_ms(10000); the actual delay time is 10s 2MHZ main frequency is as follows: Program #include /*********************************************Software delay****************************************/ #define CPU_F ((double)2000000) #define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0)) #define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0)) /******************************************************************************************/ //2000000 is the CPU's main frequency, i.e. MCLK, which needs to change with the system void main(void) { WDTCTL = WDTPW + WDTHOLD; FLL_CTL0 |= EL = 0x22; P2DIR=0X01; for(;;) { delay_ms(1000); P2OUT^=0X01; } } Result: //delay_us(1): The actual delay time is 4us //delay_us(10); The actual delay time is 13.2us //delay_us(20); The actual delay time is 23.2us //delay_us(90); The actual delay time is 92us //delay_us(100); The actual delay time is 104us //delay_us(900); The actual delay time is 900us //delay_us(1000); The actual delay time is 1.04ms //delay_ms(1); The actual delay time is 1.04ms //delay_ms(10); the actual delay time is 10ms //delay_ms(100); the actual delay time is 100ms //delay_ms(500); the actual delay time is 500ms //delay_ms(1000); the actual delay time is 1000ms //delay_ms(10000); the actual delay time is 10s 8MHZ main frequency: Program: #include /*********************************************Software delay****************************************/ #define CPU_F ((double)8000000) #define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0)) #define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0)) /******************************************************************************************/ //8000000 is the main frequency of the CPU, i.e. MCLK, which needs to change with the changes of the system void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer FLL_CTL0 |= DCOPLUS+XCAP18PF; // Set load capacity for xtal SCFI0 |= FN_4; // x2 DCO, 4MHz nominal DCO SCFQCTL = 121; // (121+1) x 32768x 2= 8Mhz P1DIR = 0x22; // P1.1 & P1.5 to output direction P1SEL = 0x22; P2DIR=0X01; // P1.1 & P1.5 to output MCLK & ACLK for(;;) { delay_ms(1000); P2OUT^=0X01; } } Result: //delay_us(1): The actual delay time is 1.75us //delay_us(10); The actual delay time is 10.80us //delay_us(20); The actual delay time is 20.8us //delay_us(90); The actual delay time is 90.5us //delay_us(100); The actual delay time is 100us //delay_us(900); the actual delay time is 900us //delay_us(1000); the actual delay time is 1ms //delay_ms(1); the actual delay time is 1ms //delay_ms(10); the actual delay time is 10ms //delay_ms(100); the actual delay time is 100ms //delay_ms(500); the actual delay time is 500ms //delay_ms(1000); the actual delay time is 1s //delay_ms(10000); the actual delay time is 10s The above test description: The program is used for delays below 20us, the error will be relatively large, and the higher the main frequency, the smaller the error; For delays greater than 20us and less than 1000ms, there is almost no error in the timing time. When the system has high real-time requirements, it is not a good choice to use software to achieve delays above 10ms. It is recommended to use hardware. For delays at the us level, the program provided in this article is very practical.