How to have delay without using library?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to have delay without using library?

3,880 Views
hunter18
Contributor II

Hello Everyone,

 

I'm a newbie in terms of using freedom boards so i really need your help to do this. I'm currently using FRDM-KL26Z128VLH4 and i need to have delays in my code for at least 1 second or more (depends on the user). The problem is i need to do this without using library so that i can determine how it works. So, if there is anyone here that can help me please help. Any help is appreciated.

 

Thank you very much!

 

Best Regards,

Ken

Labels (1)
5 Replies

2,727 Views
bobpaddock
Senior Contributor III

Attached is Bare Metal code that does ms and us delays that uses no libraries or frame works.

Putting long delays, like a second for the user, is rarely the best approach.

Investigate how to use the SysTic timer for doing delays without busy loops such as the attached uses.

Look up Cooperative Multitasking.

2,727 Views
hunter18
Contributor II

Hello Bob Paddock,

Thank you for your reply. I put the function delay_ms() from delay.c and it works. Now, i currently investigating how can i make SysTick working. Do you know how to implement it? if it is okay to you, can you help me to make SysTick work? i don't ask for a code just tell me the step by step procedure on how to implement it. Honestly i don't understand the function of SysTick such as SysTick_CTRL_COUNTFLAG_Pos, SysTick_CTRL_COUNTFLAG_Msk, SysTick_CTRL_CLKSOURCE_Pos, etc.

I hope you can help me again.

Thank You,

Ken

0 Kudos

2,727 Views
bobpaddock
Senior Contributor III

System Tick is a timer meant to be a base ticker for the system.

In a simple system it will just increment a value.  In a complex RTOS system it will be part of the task scheduler.

The registers you mention configure the base tick time, typically 10ms and enable a clock source that gets divided down to get the 100 Hz/ 10 ms tick.

Example code is attached.

0 Kudos

2,727 Views
Alice_Yang
NXP TechSupport
NXP TechSupport

Hello Ken,

David's suggestion is good .

Also you can create PE project on KDS, then add the component of  "TimerInt_ldd",configuration the  Interrupt period to 1ms,

pastedImage_0.png

void delayms(int t)

{

    EventCount = 0;

  

    while(t > EventCount);

}

In the timer interrupt function :

void TU2_OnCounterRestart(LDD_TUserData *UserDataPtr)

{

  /* Write your code here ... */

    EventCount++;                                      /* Increment counter of events */

}

We can use the delay function :

void main ()

{

  delayms(20*1000);  // delay 20s

}

Hope it helps

Alice

-----------------------------------------------------------------------------------------------------------------------

Note: If this post answers your question, please click the Correct Answer button. Thank you!

-----------------------------------------------------------------------------------------------------------------------

2,727 Views
DavidS
NXP Employee
NXP Employee

Hi Ken,

I suggest grabbing the KSDK_v2 for the FRDM-KL26Z:

Software Development Kit for Kinetis MCUs|NXP

Then you can use the KDS_3.2 to open the following low power timer example:

C:\NXP\KSDK_v2\SDK_2.0_FRDM-KL26Z_KDS\boards\frdmkl26z\driver_examples\lptmr\kds

This example is setting up LPTMR to interrupt every second.  That can be changed to whatever time you want more or less.

By default the LPTMR is using a 1000Hz input clock and the compare register field is set to 1000 (0x3e8).  Once the LPTMR starts clocking, 1 second later it generates an interrupt.  In the ISR you can disable the LPTMR to prevent repeated interrupts.

Lastly there is a text file to help with understanding of the example:

C:\NXP\KSDK_v2\SDK_2.0_FRDM-KL26Z_KDS\boards\frdmkl26z\driver_examples\lptmr\readme.txt

Regards,

David