• AVR Freaks

Hot!Problem compiling with __delay_us

Author
alejandrodaniel
New Member
  • Total Posts : 4
  • Reward points : 0
  • Joined: 2020/06/07 03:44:09
  • Location: 0
  • Status: offline
2020/07/08 18:07:40 (permalink)
2 (1)

Problem compiling with __delay_us

Sorry my english, but i'm using google translator.
I have a problem compiling this part, it gives me an error because it does not accept variables.
void Servo(unsigned int Tiempo)
     {
      unsigned int Delta;
      for(Delta=0;Delta<50;Delta++)
         {
          LATDbits.LATD4=1;
          __delay_us(Tiempo);
          LATDbits.LATD4=0;
          __delay_us(20000-Tiempo);
         }
     }
How could I solve this problem?
thanks in advance for your collaboration
#1

10 Replies Related Threads

    du00000001
    Just Some Member
    • Total Posts : 3851
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Problem compiling with __delay_us 2020/07/09 10:18:57 (permalink)
    +1 (1)
    Start enlightening us with the error message(s).

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #2
    NKurzman
    A Guy on the Net
    • Total Posts : 18858
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: Problem compiling with __delay_us 2020/07/09 10:44:57 (permalink)
    +2 (2)
    __delay_us(Tiempo);  It Must be a Constant, it can not be a variable.
    #3
    andersm
    Super Member
    • Total Posts : 2834
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: offline
    Re: Problem compiling with __delay_us 2020/07/09 10:48:38 (permalink)
    +2 (2)
    As explained in the compiler documentation, the argument to __delay_us() must be a constant expression, ie. it must be known and fixed at compile time. If you need variable length delays, you can eg. use a timer peripheral, or implement your own busy-looping macros.
    #4
    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Problem compiling with __delay_us 2020/07/09 13:26:31 (permalink)
    +1 (1)
    andersm
    ... or implement your own busy-looping macros.

    e.g. by simply creating a function which calls __delay_us(1), or __delay_us(10) a variable number of times using a for() or while() loop.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #5
    alejandrodaniel
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2020/06/07 03:44:09
    • Location: 0
    • Status: offline
    Re: Problem compiling with __delay_us 2020/07/09 15:32:18 (permalink)
    0
    __delay_us(Tiempo);  It Must be a Constant, it can not be a variable.

    yes, it is this error. reading the documentation of XC8 I made the mistake.
    __delay_us is a macro and not a function that accepts variable values ​​during compilation.
    forces with a function like:

    void Delay_us(unsigned long Contador)
         {
          do
            {
             Contador--;
             __delay_us(1);
            }while(Contador);
         }

    void Delay_ms(unsigned int Contador)
         {
          do
            {
             Contador--;
             __delay_us(1000);
            }while(Contador);
         }
    #6
    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Problem compiling with __delay_us 2020/07/09 16:18:55 (permalink)
    +1 (1)
    You can make it easier for the compiler by recoding the first function as:
    void Delay_us(unsigned long Contador)
    {
        do
        {
            __delay_us(1);
        } while(--Contador);
    }

    Do you really need to use a "long" variable?
    That adds lots of unnecessary code if an "unsigned int" would be big enough!
    Even better if an "unsigned char" will do the job. That means the compiler can do the decrement and test in a single machine instruction.
     
     
    post edited by ric - 2020/07/09 16:28:26

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #7
    RISC
    Super Member
    • Total Posts : 5776
    • Reward points : 0
    • Status: offline
    Re: Problem compiling with __delay_us 2020/07/09 16:22:18 (permalink)
    +3 (3)
    Hi,
    And most important....you must declare the macro to let the compiler know what is the Fosc frequency you use (internal or external clock) x PLL if activated..
    It cannot be guessed by the compiler
    #define _XTAL_FREQ 20000000

    https://www.microchip.com/forums/m673703.aspx
    Regards
     
    post edited by RISC - 2020/07/09 16:24:27

    For support make sure to check first here : http://microchipdeveloper.com
    There are hundreds of PIC, AVR, SAM...which one do YOU use ?
    #8
    alejandrodaniel
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2020/06/07 03:44:09
    • Location: 0
    • Status: offline
    Re: Problem compiling with __delay_us 2020/07/10 01:28:12 (permalink)
    0
    the insertion of the oscillator configuration (be it LP, XT, HS, HS + PLL, etc ....) and clock goes without saying.
    if I ask this question on the __delay_xx it is because up to now I managed the timings with the timers. in this project the four timers are used for other tasks and I am forced to use delays (which does not make me happy).
    I solved it using a function that manages them.
    Thank you all.
    #9
    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Problem compiling with __delay_us 2020/07/10 01:34:55 (permalink)
    +1 (1)
    Please don't put code tags around text.
    alejandrodaniel
    the insertion of the oscillator configuration (be it LP, XT, HS, HS + PLL, etc ....) and clock goes without saying.

    The "_XTAL_FREQ" define is a very different beast to the CONFIG settings for the oscillator.
    It is an instruction to the compiler to let it know how fast your oscillator is running.
    Many people misunderstand this requirement, which is why it does NOT go without saying...
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #10
    alejandrodaniel
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2020/06/07 03:44:09
    • Location: 0
    • Status: offline
    Re: Problem compiling with __delay_us 2020/07/10 01:50:16 (permalink)
    +1 (1)
    the insertion of the oscillator configuration (be it LP, XT, HS, HS + PLL, etc ....) and clock goes without saying..........

    GRAZIAS DE NUEVO.
    #11
    Jump to:
    © 2020 APG vNext Commercial Version 4.5