C中的延迟函数不延迟

Delay function in C not delaying

这是一个非常奇怪的问题,因为它应该可以工作但没有。 我的任务是做一个简单的函数来延迟程序。

所以time before use a function

并且在使用函数之后: time after using 我想延迟 1 秒,所以我增加了 100000(100 之前)的迭代次数,但时间没有改变。 after increasing iteration

为什么?合乎逻辑的是,如果我增加迭代次数,时间应该更长...

编辑:

#include <LPC21xx.H>

void Delay(){
    long int i;
    for(i=0; i<48000000000;i++){
    }
}

int main(){
    //set pin 16 P1 as out
    IO1DIR = 0x10000;
    //set pin 16  P1 on 1
    IO1SET = 0x10000;
    Delay();
    //set pin 16 port P1 on 0
    IO1CLR = 0x10000;

}

我用的是uVision Keil。

使用 nop () 函数将一些 NO-OP 指令插入到您的 C 代码中。计算出在您的目标上执行单个 NOP 所需的时间,并根据需要使用尽可能多的时间。来源:keil.com/support/docs/606.htm

为此 "sample" y 尝试使用 Delay(1000) 您可以更改此值;

#include <LPC21xx.H>
#include <intrins.h>

#pragma O0
void Delay(volatile uint32_t cnt) {
    while(cnt--)
        _nop_();
}

void DelayWithoutNop(volatile uint32_t cnt) {
    while(cnt--);
}


int main(){
    //set pin 16 P1 as out
    IO1DIR = 0x10000;
    //set pin 16  P1 on 1
    IO1SET = 0x10000;
    Delay(1000);
    DelayWithotNop(3000);
    //set pin 16 port P1 on 0
    IO1CLR = 0x10000;

}