Hot!Using Nop() instructions to create short accurate timer

Author
arigead
Super Member
  • Total Posts : 402
  • Reward points : 0
  • Joined: 2011/02/07 06:58:31
  • Location: 0
  • Status: offline
2018/03/14 06:33:29 (permalink)
0

Using Nop() instructions to create short accurate timer

dsPIC33EP256MU806 & XC16 (v1.33)
 
I've got a synchronous delay function to stall the uC if required. Normally it creates a HW Timer, waiting on the ISR, but that is a bit verbose as I set up data structures to manage that. So if the timer is suitably short there's no point in creating a HW Timer and you might as well calculate a number of Nop(); instructions and do those before returning to the caller.
 
That all sounds fine and dandy but the compiler is ignoring, or appears to be ignoring, my code. So if I put 4 Nop(); instructions in a function I get a delay of X. If I now double the number of Nop(); Instructions I still get exactly the same delay.
 
I suppose this is a case of looking at the disassembly for the code. Man it's going to be one of those days.
#1

7 Replies Related Threads

    mbrowning
    Just a Member
    • Total Posts : 1154
    • Reward points : 0
    • Joined: 2005/03/16 14:32:56
    • Location: Melbourne, FL
    • Status: online
    Re: Using Nop() instructions to create short accurate timer 2018/03/14 07:10:51 (permalink)
    5 (1)
    Or use the compilers built in delay function,
    void __delay32(unsigned long cycles)
    It can go down to 12 cycles.

    Oh well - there's always next year
    #2
    du00000001
    Just Some Member
    • Total Posts : 2238
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Using Nop() instructions to create short accurate timer 2018/03/14 07:34:40 (permalink)
    0
    This is one of these days ...
    Could beyour call overhead takes much more time than the 4 Nop()s, could be the whole delay is currently optimized away. Risk an eye on the assembly code ...

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #3
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 2768
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Using Nop() instructions to create short accurate timer 2018/03/14 08:14:41 (permalink)
    0
    Risk assembly:
     
    repeat #20-1-1
    nop
     
    -1 for repeat cycle = 1, nop = 1
    -1 for repeat count
     
    To get 20 nops, repeat would be 18.
     

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #4
    arigead
    Super Member
    • Total Posts : 402
    • Reward points : 0
    • Joined: 2011/02/07 06:58:31
    • Location: 0
    • Status: offline
    Re: Using Nop() instructions to create short accurate timer 2018/03/14 09:20:20 (permalink)
    0
    Ah man what was I thinking! Fool!
     
    I was trying to write a general routine to calculate the required number of Nop() calls. That would depend on the current setting of the Instruction clock frequency. That requires maths and as soon as you try and multiply or divide things it takes so long why bother! No wonder I was getting strange results.
     
    Thanks a million to the delay of the compiler didn't realise it had one!
    #5
    1and0
    Access is Denied
    • Total Posts : 8531
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Using Nop() instructions to create short accurate timer 2018/03/14 09:26:02 (permalink)
    5 (1)
    arigead
    Thanks a million to the delay of the compiler didn't realise it had one!

    That would be __delay32() with one two underscores. There are also __delay_ms() and __delay_us() with two underscores.
     
    Edit: You'd have to #define FCY to represent the instruction clock frequency before #include <libpic30.h>.
     
    post edited by 1and0 - 2018/03/14 09:31:55
    #6
    du00000001
    Just Some Member
    • Total Posts : 2238
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Using Nop() instructions to create short accurate timer 2018/03/14 09:26:41 (permalink)
    0
    Dividing? Don't tell me ...  LoL

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #7
    NorthGuy
    Super Member
    • Total Posts : 5101
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: Using Nop() instructions to create short accurate timer 2018/03/14 16:35:03 (permalink)
    0
    arigead
    Normally it creates a HW Timer, waiting on the ISR, but that is a bit verbose as I set up data structures to manage that.



    This is oxymoron. Interrupts are there to interrupt the normal course of the execution. If you wait for them, they're completely useless. Just read the timer (or its interrupt flag) in a loop.
     
    Using delay_ms() is generally unpredictable if interrupts are enabled.
    #8
    Jump to:
    © 2018 APG vNext Commercial Version 4.5