• AVR Freaks

Hot!__delay_us function not working accurate

Page: 12 > Showing page 1 of 2
Author
Killerpiraat
Starting Member
  • Total Posts : 80
  • Reward points : 0
  • Joined: 2020/03/09 07:40:17
  • Location: 0
  • Status: offline
2021/01/13 15:38:52 (permalink)
0

__delay_us function not working accurate

Hello, long time no seen
 
Using curiosity board, pic16f18875, mplabv5.45, XC8

Beginners question: I am using the easiest program that one can make, to check if everything is working correctly. Well, it isnt.
I set my settings to this, and the __delay_ms function worked fine with it. But when i use __delay_us is changes
 
I also changed the XTAL freq to the internal clock. (why isnt this changed automaticly btw?)
And even if the delay is not accurate, how is it possible that the duration of the high period is different to the duration of the low period?
post edited by Killerpiraat - 2021/01/13 15:52:09

Attached Image(s)

#1

22 Replies Related Threads

    ric
    Super Member
    • Total Posts : 29435
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: __delay_us function not working accurate 2021/01/13 15:48:10 (permalink)
    +1 (1)
    You cannot drag an image into the edit window on this board.
    Click "Open Full Version" to open the full editor window, and use the "Attach Image" button to attach an image file to a post.
     

    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!
    #2
    Killerpiraat
    Starting Member
    • Total Posts : 80
    • Reward points : 0
    • Joined: 2020/03/09 07:40:17
    • Location: 0
    • Status: offline
    Re: __delay_us function not working accurate 2021/01/13 15:49:43 (permalink)
    0
    Sorry forgot about that. Its done. Thanks :D
    #3
    ric
    Super Member
    • Total Posts : 29435
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: __delay_us function not working accurate 2021/01/13 15:51:53 (permalink)
    +1 (1)
    You don't mention what you expected to see, or explain exactly what your scope trace is showing.
    Be aware, with a 4MHz clock, you are executing instructions at a 1MHz rate, so 1us per machine instruction.
    The "while()" loop itself takes a few instructions to implement, which will add several us to your delays.
     
     

    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!
    #4
    1and0
    Access is Denied
    • Total Posts : 11747
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: __delay_us function not working accurate 2021/01/13 15:52:38 (permalink)
    +1 (1)
    Low will be 2 us longer for the branch instruction back to the beginning of the while() loop.
     
    #5
    Killerpiraat
    Starting Member
    • Total Posts : 80
    • Reward points : 0
    • Joined: 2020/03/09 07:40:17
    • Location: 0
    • Status: offline
    Re: __delay_us function not working accurate 2021/01/13 15:54:14 (permalink)
    0
    ric
    You don't mention what you expected to see, or explain exactly what your scope trace is showing.
    Be aware, with a 4MHz clock, you are executing instructions at a 1MHz rate, so 1us per machine instruction.
    The "while()" loop itself takes a few instructions to implement, which will add several us to your delays.
     
     


    So you are saying the __delay_us function is not accurate? But why is the high time different of the low time?
    I expect to see a stable square wave with 10us high and 10us low. Is that too much to ask from the PIC?
    #6
    Killerpiraat
    Starting Member
    • Total Posts : 80
    • Reward points : 0
    • Joined: 2020/03/09 07:40:17
    • Location: 0
    • Status: offline
    Re: __delay_us function not working accurate 2021/01/13 15:56:23 (permalink)
    +1 (1)
    The project i am doing is designing a step up converter. I am trying to find the best frequency for it and with adjustable duty cycle. I want my frequency of my square wave to be between 60KHz (17us) and 10KHz (100us).
     
    #7
    1and0
    Access is Denied
    • Total Posts : 11747
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: __delay_us function not working accurate 2021/01/13 15:56:54 (permalink)
    +1 (1)
    Killerpiraat
    So you are saying the __delay_us function is not accurate? But why is the high time different of the low time?
    I expect to see a stable square wave with 10us high and 10us low. Is that too much to ask from the PIC?

    No, the delays will be 10 us. The high and low and branch instructions take time too. ;)
    #8
    ric
    Super Member
    • Total Posts : 29435
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: __delay_us function not working accurate 2021/01/13 15:58:31 (permalink)
    +2 (2)
    Killerpiraat
    So you are saying the __delay_us function is not accurate?

    No, it is perfectly accurate, but the instructions to manipulate the LAT bits and implement the while() loop themselves take time to execute, which you are not allowing for.
     

    But why is the high time different of the low time?

    Because of how long it takes to execute the GOTO instruction, as 1and0 just explained.

    I expect to see a stable square wave with 10us high and 10us low. Is that too much to ask from the PIC?

    It is a bad expectation. That is NOT what I would expect the code you have written to do.
     

    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!
    #9
    1and0
    Access is Denied
    • Total Posts : 11747
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: __delay_us function not working accurate 2021/01/13 16:00:56 (permalink)
    +2 (2)
    Give this a try:
    while(1) {
      LATA6 = 1;
      __delay_us(9);  // 1 + 9 us
      LATA6 = 0;
      __delay_us(7);  // 1 + 7 + 2 us
    }

    #10
    Killerpiraat
    Starting Member
    • Total Posts : 80
    • Reward points : 0
    • Joined: 2020/03/09 07:40:17
    • Location: 0
    • Status: offline
    Re: __delay_us function not working accurate 2021/01/13 16:01:38 (permalink)
    -1 (1)
    Thanks guys, great help, as always.
    Is there a (simple) solution for this? Do you have suggestions to make the delay functions accurate? (without diving in to deep, i used microchip before, but its been a while so im figuring it out again)
    #11
    Killerpiraat
    Starting Member
    • Total Posts : 80
    • Reward points : 0
    • Joined: 2020/03/09 07:40:17
    • Location: 0
    • Status: offline
    Re: __delay_us function not working accurate 2021/01/13 16:04:07 (permalink)
    0
    1and0
    Give this a try:
    while(1) {
      LATA6 = 1;
      __delay_us(9);  // 1 + 9 us
      LATA6 = 0;
      __delay_us(7);  // 1 + 7 + 2 us
    }





    Thanks, but even with this,my upper period is 15us, and my lower period is 20us
    #12
    ric
    Super Member
    • Total Posts : 29435
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: __delay_us function not working accurate 2021/01/13 16:05:27 (permalink)
    +2 (2)
    The delay functions ARE accurate.
    The invocation of __delay_us(7) takes precisely 7us.
    The problem is that you are trying to achieve delays close to the time to execute a single machine instruction, and you are NOT allowing for those instructions.
    This is close to unnoticeable when you have delays much bigger than the time to execute one instruction.
    You'd be better using a peripheral to produce the output if you want a precise waveform, or write the code in raw assembly language.
    It's never a good idea to use a high level language to produce a precise (small) number of instructions.
     

    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!
    #13
    1and0
    Access is Denied
    • Total Posts : 11747
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: __delay_us function not working accurate 2021/01/13 16:06:47 (permalink)
    +1 (1)
    Killerpiraat
    Is there a (simple) solution for this? Do you have suggestions to make the delay functions accurate? (without diving in to deep, i used microchip before, but its been a while so im figuring it out again)

    The __delay_xs() macros are instruction cycle accurate. However, interrupt that occurs inside these delays will extend the time. Other options are to use the hardware timers.
    #14
    ric
    Super Member
    • Total Posts : 29435
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: __delay_us function not working accurate 2021/01/13 16:07:11 (permalink)
    +2 (2)
    n.b. part of the problem is because you are running the PIC so slow. Why not up the speed to 32MHz?

    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!
    #15
    Killerpiraat
    Starting Member
    • Total Posts : 80
    • Reward points : 0
    • Joined: 2020/03/09 07:40:17
    • Location: 0
    • Status: offline
    Re: __delay_us function not working accurate 2021/01/13 16:08:57 (permalink)
    0
    ric
     
    You'd be better using a peripheral to produce the output if you want a precise waveform, or write the code in raw assembly language.
     


    which peripheral would you suggest?
     
    #16
    1and0
    Access is Denied
    • Total Posts : 11747
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: __delay_us function not working accurate 2021/01/13 16:09:02 (permalink)
    0
    Killerpiraat
    1and0
    Give this a try:
    while(1) {
      LATA6 = 1;
      __delay_us(9);  // 1 + 9 us
      LATA6 = 0;
      __delay_us(7);  // 1 + 7 + 2 us
    }


    Thanks, but even with this,my upper period is 15us, and my lower period is 20us

    You MUST be using the Free mode with optimization level 0, where branches are "nut".  Take a look to the disassembly listing.  Or use opt level 2. ;)
    #17
    ric
    Super Member
    • Total Posts : 29435
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: __delay_us function not working accurate 2021/01/13 16:10:17 (permalink)
    +2 (2)
    Killerpiraat
    The project i am doing is designing a step up converter. I am trying to find the best frequency for it and with adjustable duty cycle. I want my frequency of my square wave to be between 60KHz (17us) and 10KHz (100us).
     

    I missed this post before.
    It's insane to try to get those delays with instruction delays.
    Use TMR2 to produce the waveform, it's perfect for this.

    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!
    #18
    Killerpiraat
    Starting Member
    • Total Posts : 80
    • Reward points : 0
    • Joined: 2020/03/09 07:40:17
    • Location: 0
    • Status: offline
    Re: __delay_us function not working accurate 2021/01/13 16:13:14 (permalink)
    0
    ric
    n.b. part of the problem is because you are running the PIC so slow. Why not up the speed to 32MHz?




    No idea, its the basic settings the IDE gives me when i open a new project. I expected that should be good for overall applications. But maybe the delay_us is not a overall function?
    Anyway, changed it to 32MHz with clock divider of 1, and offcourse, its accurate now!
    #19
    Killerpiraat
    Starting Member
    • Total Posts : 80
    • Reward points : 0
    • Joined: 2020/03/09 07:40:17
    • Location: 0
    • Status: offline
    Re: __delay_us function not working accurate 2021/01/13 16:14:16 (permalink)
    -1 (1)
    ric
     
    It's insane to try to get those delays with instruction delays.
     




    I am called insane much to often lately
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2021 APG vNext Commercial Version 4.5