I just wanna delay loop!

Discussion in 'Embedded Systems and Microcontrollers' started by mikewax, Jan 10, 2017.

  1. mikewax

    Thread Starter Member

    Apr 11, 2016
    59
    0
    why do i hafta go around in circles? i can't use delay or delay_ms or ellapsedmilis because all three timers are busy and can't be interrupted.
    but this:
    for (t = 0; t<10000; t++){ for (x = 0; x<10000; x++){} }
    shouldn't be a nightmare. but nooooooooo, gcc won't stand for it.
    so there's this file, C:\Program Files (x86)\Arduino\hardware\arduino\avr\platform.txt, that supposedly i can edit and tell gcc to getthehelloutamyway!

    and it has this paragraph:
    # Default "compiler.path" is correct, change only if you want to override the initial value
    compiler.c.flags=-c -g -Os {compiler.warning_flags} -std=gnu11 -ffunction-sections -fdata-sections -MMD -flto -fno-fat-lto-objects
    compiler.c.elf.flags={compiler.warning_flags} -Os -flto -fuse-linker-plugin -Wl,--gc-sections
    compiler.cpp.flags=-c -g -Os {compiler.warning_flags} -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD
    compiler.objcopy.eep.flags=-O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0
    compiler.elf2hex.flags=-O ihex -R .eeprom

    where the -O option appears no less than 5 times. can someone who speaks gcc tell me what to do here?

    thanx, CrazyInLA
     
  2. mtonge

    Member

    Apr 19, 2016
    56
    18
    What does the compiler say? The nested 'for' loops just use a lot of processor time to create a delay, correct? You should declare your loop counter variables as integers. Perhaps the syntax should be:


    for(int t = 0; t<10000; t++){
    for(int x = 0; x<10000; x++){
    }
    }
     
    Last edited: Jan 10, 2017
  3. mikewax

    Thread Starter Member

    Apr 11, 2016
    59
    0
    yes they are unsigned integers. the problem is that the compiler is configured to "optimize" the code for faster execution. it sees those statements and goes "he's wasting time. i'll modify the code so that no matter what #s he uses for x or t, i'll minimize the time delay"
    and that's what i see. whatever #s i use i still get a delay of about 100ms.
    but now i may have a solution. somebody over at http://www.electro-tech-online.com/threads/pwm-problem-cant-set-duty-cycle.149671/#post-1283925 electrotech online said that i could create an interrupt routine that will execute every time the timer overflows that will increment a variable. this will not interfere with the timer and will give me a way to create a delay. unless of course the compiler gives me <snipped language> about that, too.
     
    Last edited by a moderator: Jan 10, 2017
  4. ClassOfZero

    Member

    Dec 28, 2016
    114
    20
  5. mtonge

    Member

    Apr 19, 2016
    56
    18
    How much delay do you need? A 555 timer could trigger an interrupt too.
     
  6. mikewax

    Thread Starter Member

    Apr 11, 2016
    59
    0
  7. ClassOfZero

    Member

    Dec 28, 2016
    114
    20
    Cool, no probs bro.
     
  8. mikewax

    Thread Starter Member

    Apr 11, 2016
    59
    0
    there's always that. i actually used a 555 on a design last year. they will never go out of style. but i got many delays in my code of different duration.
    thanx
     
  9. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,894
    1,786
    Go have a peek at what the "volatile" keyword does.

    Hint: it tells the compiler don't assume you know what value is in that variable.

    Hint 2: it is transportable between compilers.
     
    Stuntman likes this.
  10. mikewax

    Thread Starter Member

    Apr 11, 2016
    59
    0
    hmmm.... i never thoughta that. that's even simpler. thanx i'll try it :)
     
Loading...