• AVR Freaks

Hot!How to generate 1ms delay

Author
codeKnight
New Member
  • Total Posts : 22
  • Reward points : 0
  • Joined: 2019/06/03 02:03:56
  • Location: 0
  • Status: offline
2020/07/03 02:26:27 (permalink)
0

How to generate 1ms delay

Hi ,everyone
I am using the cpu type of picmx775f256h and complier xc32,HarmonyV2.06.How can i generate a 1ms delay?
Thank you
#1

3 Replies Related Threads

    yts
    Super Member
    • Total Posts : 632
    • Reward points : 0
    • Joined: 2005/09/06 17:45:30
    • Status: offline
    Re: How to generate 1ms delay 2020/07/04 22:21:16 (permalink)
    0
    There are several methods, I guess.

    Here is the simplest method: see delay.c. You need to change the value of FCY to suit your clock speed. usage: DelayMs(1);

    yts


    #2
    dan1138
    Super Member
    • Total Posts : 3731
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: How to generate 1ms delay 2020/07/05 02:56:56 (permalink)
    5 (1)
    Try this:
    void DelayMS( unsigned long Delay )
    {
    #define TICKS_IN_ONE_MILLISECOND (GetSystemClock() / 2000ul)
        unsigned long Time0, Time1;

        Time0 = _CP0_GET_COUNT(); /* 2 system clocks per count */

        if (Delay < (unsigned long)(0xFFFFFFFF) / TICKS_IN_ONE_MILLISECOND)
            Delay = Delay * TICKS_IN_ONE_MILLISECOND;
        else
            Delay = (unsigned long)(0xFFFFFFFF);

        for(;;)
        {
            Time1 = _CP0_GET_COUNT();
            Time1 = Time1 - Time0;      /* Get cycle from start of spin */
            if (Time1 >= Delay)
                break;
        }
    }

    #3
    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: How to generate 1ms delay 2020/07/05 03:13:45 (permalink)
    0
    Of course, this assumes that you don't want to do anything else at all during this one millisecond (apart from servicing any interrupts).
    Dan's code could be broken into two sections if you did want to.
    i.e. the initialisation section up until just before the for(;;) loop
     
    Then the loop section, which could inlcude something useful...
     
     

    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
    Jump to:
    © 2020 APG vNext Commercial Version 4.5