-3

It is very strange problem because it should works but it doesn't. My task is to do a a simply function which delay a program.

So time before use a function

And after using a function: time after using I want to have a 1 second delay so i increased a number of iteration for 100000 (before 100) , but time hasn't change. after increasing iteration

why? it is logical that if i increase the number of iteration, the time should be longer...

EDIT:

#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;

}

I use a uVision Keil.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • Please include the source of your program in your question, formatted as code (highlight it and use the "{}" button). Pictures of text are far less useful. – Keith Thompson Feb 28 '19 at 19:44
  • https://www.geeksforgeeks.org/time-delay-c/ – OldProgrammer Feb 28 '19 at 19:45
  • @KeithThompson OK. – Always Dissapointed Feb 28 '19 at 19:52
  • 1
    I'm pretty sure your C compiler will see that cycle as doing nothing and just skip foward. To make sure the cycle is executed put something like int a=0; inside the cycle. That way the compiler will not ignore the cycle. – H. Figueiredo Feb 28 '19 at 19:55
  • Here are 2 ways you may do this: 1. Use the _nop_ () function to insert a number of NO-OP instructions into your C code. Figure out the amount of time it takes for a single NOP on your target and use as many as necessary. 2. Create a function that starts a timer that generates an interrupt on overflow. The interrupt sets a flag that the function waits for after starting the timer. When the flag is set, the function stops the timer and returns. Source: http://www.keil.com/support/docs/606.htm code: https://stackoverflow.com/questions/21553964/how-to-create-a-delay-function-in-keil-uvision – Horacio Goetendia Feb 28 '19 at 20:01
  • @H.Figueiredo I did this and the time of executing a loop in 100 iteration is 10-7 s and in 1 000 000 iteration is the same... what is wrong with this? – Always Dissapointed Feb 28 '19 at 20:01
  • @HoracioGoetendiaBonilla I am a little confused about this information and i still don't know how to cope with this problem. – Always Dissapointed Feb 28 '19 at 20:05
  • @KrzysztofBolek Use volatile int a=0; Volatile will force the compiler to evaluate the variable each time, instead of trying to optimize. But you have an answer from Horatio, maybe you prefer his answer. – H. Figueiredo Feb 28 '19 at 20:18
  • @H.Figueiredo The compiler will probably see that the variable `a` is never used, either, and will eliminate it, too. – FredK Feb 28 '19 at 20:18
  • @FredK if its volatile I think the compiler is forced to not skip it. https://stackoverflow.com/questions/7083482/how-to-prevent-gcc-from-optimizing-out-a-busy-wait-loop – H. Figueiredo Feb 28 '19 at 20:19
  • What size is `long int`? 48000000000 is far too big for 32 bits. – Dipstick Feb 28 '19 at 20:40

1 Answers1

0

Use the nop () function to insert a number of NO-OP instructions into your C code. Figure out the amount of time it takes for a single NOP on your target and use as many as necessary. Source: keil.com/support/docs/606.htm

For this "sample" y try with Delay(1000) you can make changes to this value;

#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;

}
  • It can be do in a simpler way? And in instruction in my homework i must use a for loop, so the _nop_(); is too advanced for my class level of programming. – Always Dissapointed Feb 28 '19 at 20:17
  • I added a function DelayWithotNop with a simple operation, in this case you have to test it with the right value to reach 1 second of delay based in your current hardware – Horacio Goetendia Feb 28 '19 at 20:27