This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C123GH6PGE: Regarding the system control delay function

Part Number: TM4C123GH6PGE


Good morning to every one,

I am developing some codes to my embedded applications using TM4C123GH6PGE Microcontroller. I am using below function to set clock frequency .

SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN  | SYSCTL_XTAL_16MHZ);

 


From the above instruction i want to set a delay 1 second delay, 1 millisecond delay 1 micro second delay.

I am requesting you to can any one please send me the instructions to the above delay in the form of   

SysCtlDelay(SysCtlClockGet()  / number ); 

or

SysCtlDelay(160000); 

 

 


Thanks in advance.

  • Hi Srinu,
    I thought you already answer your own question.

    The SysCtlClockGet() will return the current operating frequency. With what you have configured in the SysCtlClockSet, the operating frequency will be 80MHz. The SysCtlClockGet() will return 80000000. If you want to create a delay that is close to 1s then you will just use SysCtlDelay(SysCtlClockGet() / 1). If you wan to create a delay that is close to 1ms then you will use SysCtlDelay(SysCtlClockGet() / 1000). Note this is a software way of creating a delay. It is not going to be precise. For example, if you get an interrupt in the middle then it will disrupt the execution time of the SysCtlDelay. In another word, the SysCtlDelay will depend on the application's interrupt environment. For precise delay you will want to use the hardware like the timer module.
  • SysCtlDelay(SysCtlClockGet() / 3); will produce a 1 second delay.
    SysCtlDelay(SysCtlClockGet() / 3000 ); will procuce 1 mS delay.
    SysCtlDelay(SysCtlClockGet() / 3000000); will produce 1uS delay.

    As you see SysCtlDelay(number); causes number* 3 cylces of delay.
  • What does SysCtlClockGet return?
  • SysCtlClockGet() returns the clock frequency. That is true, but SysCtlDelay() does a delay of cycles = 3 times the number passed in as the argument. Thus to get a 1 second delay, you use SysCtlDelay(SysCtlClockGet() / 3);.
    SysCtlDelay(SysCtlClockGet() / 1); will produce a 3 second delay.

    I think this is due to the loop implementation in the SysCtlDelay();. Each iteration takes 3 clock cycles to complete.

  • Yes, I think I agree with you. The SysCtlDelay is implemented using a 3-line assembly instructions. To get 1s you will do SysCtlDelay(SysCtlClockGet() / 3). I thought the SysCtlDelay might have first divided by 3 inside the function and it does not. In any case, please take note that the software delay loop is not precise with the explanation I gave earlier.
  • Thanks to reply to me.
    Here i am using
    SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);

    For setting clock i.e i am using (SYSCTL_SYSDIV ) 200/4 = 50MHZ PLL clock and 16MZ External clock.
    so can you please explain me how you calculate the above delay functions for 1s , 1milli second, 1 micro second.


    Thanks in advance.
  • So if you configure the clock for 50MHz then the SysCtlClockGet() would have returned 50000000. This means there are 50000000 clock cycles in 1 seccond. Since it takes 3 cycles to execute the SysCtlDelay function, you need to need to do SysCtlDelay(SysCtlClockGet() / 3) or SysCtlDelay(50000000 / 3) to achieve 1s of software delay.
  • Posters without instruments may employ a simple Led to determine the "rough" measure of such delays.

    "Led ON"
    "Delay"
    "Led OFF"

    One may note that "Self HELP" proves the "Best Help!"     (the time in which the Led remains ON - after "running that code" - reflects the delay)

  • Thank you so much for your explanation.
    From above your explanations what i understood is SysCtlDelay(SysCtlClockGet() / 3) means SysCtlDelay(3*(SysCtlClockGet() / 3)) so it gives SysCtlDelay(SysCtlClockGet() ) value it is taken as 1 second delay.
    I request you to, is it correct or not please verify my answer.
  • Srinu,
    Your understanding is not correct yet. The SysCtlDelay() takes 3 cycles to execute. For example if you call SysCtlDelay(1) this will take 3 cycles. If you call SysCtlDelay(2) this will take 6 cycles. If you operate at 50MHz then there are 50M cycles in one second. You need to call SysCtlDelay(50000000 / 3) to get one 1 second. SysCtlDelay (16666666) will take 50000000 cycles to execute. If each cycle is 20ns (@50MHz) then 50000000 * 20ns = 1 second.
  • Thank you so much sir for your explanation.