TOP Contributors

  1. MIKROE (2658 codes)
  2. Alcides Ramos (355 codes)
  3. Shawon Shahryiar (307 codes)
  4. jm_palomino (112 codes)
  5. Chisanga Mumba (90 codes)
  6. S P (73 codes)
  7. dany (71 codes)
  8. MikroBUS.NET Team (35 codes)
  9. NART SCHINACKOW (34 codes)
  10. Armstrong Subero (27 codes)

Most Downloaded

  1. Timer Calculator (136918 times)
  2. FAT32 Library (70042 times)
  3. Network Ethernet Library (56008 times)
  4. USB Device Library (46310 times)
  5. Network WiFi Library (41935 times)
  6. FT800 Library (41240 times)
  7. GSM click (29026 times)
  8. PID Library (26435 times)
  9. mikroSDK (26410 times)
  10. microSD click (25389 times)
Libstock prefers package manager

Package Manager

We strongly encourage users to use Package manager for sharing their code on Libstock website, because it boosts your efficiency and leaves the end user with no room for error. [more info]

< Back
Library

Utility Task Scheduler

Rating:

11

Author: MIKROE

Last Updated: 2018-02-13

Package Version: 1.0.0.0

Category: Other Codes

Downloaded: 3840 times

Followed by: 10 users

License: MIT license  

Contains common utilities that are often implemented repetitively.

This library contains a task scheduler. Incredibly handy for those systems doing multiple things.

By using the libraries here, you save yourself some time and have the convenience of MikroCs' library management system.

No Abuse Reported

Do you want to subscribe in order to receive notifications regarding "Utility Task Scheduler" changes.

Do you want to unsubscribe in order to stop receiving notifications regarding "Utility Task Scheduler" changes.

Do you want to report abuse regarding "Utility Task Scheduler".

  • Information
  • Comments (3)

Library Blog

This is one of the more handy tools to have in your library of tricks.  As applications grow larger and don't quite yet need a RTOS, scheduling tasks can make the system more efficient.  One of the best practices is to utilize the MCU sleep modes while scheduled pooled tasks are placed in the scheduler.   Requirements:

  • 1 Timer with ISR
  • ~1k flash

The idea of a scheduler is found in the name, scheduler. In most systems, we do more than one thing and we also want to take advantage of lower power states. This scheduler is a simple one, but will prove to be one of your favorites.


Usage Scenario:

  1. Read from the RTC every 1 minute.
  2. Update network tcp stack every 1 second
  3. Store accumulated sensor data to flash every 5 minutes

How to: Task scheduled tasks needs to be functions that take no arguments and returns nothings. Example: void read_rtc( void )
{
    int hour;
    ...
    I2C_Read( DS1307_ADDR );
    ...
    update_global_hour( hour );
    return;
} void update_network( void )
{
    Net_Ethernet_24j600_userTimerSec++;
} void write_data_to_flash( void )
{
    write_to_flash( global_sensor_data );
} Now that we have an idea of how a task is defined in a function, how do we setup the scheduler? First thing we need is a reliable timer that generates an interrupt. It also needs a interrupt time that is a multiple of 100. 100, 200, .... 500, 2000, etc. This is when we want to use the amazing Timer Calculator tool: //Timer2 Prescaler :575; Preload = 62499; Actual Interrupt Time = 500 ms
void InitTimer2()
{
    RCC_APB1ENR.TIM2EN = 1;
    TIM2_CR1.CEN = 0;
    TIM2_PSC = 575;
    TIM2_ARR = 62499;
    NVIC_IntEnable(IVT_INT_TIM2);
    TIM2_DIER.UIE = 1;
    TIM2_CR1.CEN = 1;
} For our interrupt we need to call the function task_scheduler_clock(); void Timer2_interrupt() iv IVT_INT_TIM2
{
    TIM2_SR.UIF = 0;
    task_scheduler_clock();
} That has setup our scheduler to be able to keep track of time. Now let's get to the business of using it. The scheduler needs to know how often the interrupt will be called. We do that with: task_scheduler_init( NUMBER OF MS INT IS CALLED ); Using the above timer it would be: task_scheduler_init( 500 ); Next we need to add some tasks: task_add( read_rtc, SCH_MINUTES_1 ); task_add( update_network, SCH_SECONDS_1 ); task_add( write_data_to_flash, SCH_MINUTES_5 ); The first argument is the function to call and the second is when in ms. The library has built in defines for time, but you can also declare the time in ms just the same. ex. task_add( update_network, 1000 ); Another scenario would be a task that you add and remove from time to time. To do that you can capture the id of the task: int once_in_awhile_taskid; once_in_awhile_taskid = task_add( send_sms, SCH_MINUTES_10 ); Then if you don't need it anymore or just want to pause the task, you can use the following: task_delete( once_in_awhile_taskid ); or task_stop( once_in_awhile_taskid ); Later you can re-enable the task: task_resume( once_in_awhile_taskid ); We've added some tasks, time to get started. You will need to call task_scheduler_start() before the while loop. task_scheduler_init( 500 );
....
task_scheduler_start();
while( 1 )
{
    ....
} Lastly, we need the scheduler to execute the tasks. To do this we add the following inside the while loop. while( 1 )
{
    task_dispatch();
} This is one of my favorite libraries to use. For the ~1k of flash storage, it greatly simplifies the execution of repetitive tasks and allows the usage of low power states. while( 1 )
{
    task_dispatch();
    sleep();
} Enjoy.  

 

*NOTE: You will need to add "scheduler.h" to your project search path.  All libraries are installed into \YOUR_MIKROC_INSTALLATION\Packages\PACKAGE_NAME\Uses\ Example:
C:\Program Files (x86)\Mikroelektronika\mikroC PRO for ARM\Packages\Utilities Scheduler\Uses\

Add to Search Path

Add to Search Path

Where to add to search path.

View full image
Search Paths

Search Paths

Search Paths for Project

View full image

ALSO FROM THIS AUTHOR

Hall Current click

5

This project is a simple demonstration of working with the Hall Current Click board based on the TLI4970 high precision miniature coreless magnetic current sensor with SPI Interface. This example demonstrates measuring the current drawn by the consumer.

[Learn More]

N-PLC click

0

N-PLC Click is a compact add-on board that uses existing electrical power lines to transmit data signals. This board features the SM2400, an advanced multi-standard Narrow-band Power Line Communication (N-PLC) modem from Semitech. The SM2400 features a dual-core architecture, a DSP core for N-PLC modulations, and a 32-bit core for running protocols for superior communication performance and flexibility for various open standards and customized implementations. It includes firmware options for IEEE 1901.2 compliant PHY and MAC layers, a 6LoWPAN data link layer, and special modes for industrial IoT applications. In addition to the ability to accept signals from another PLC modem or the power line communication AC coupling circuit, this board also has a handful of other features, such as a selectable interface and power supply, firmware update capabilities, LED indicators, and many others.

[Learn More]

Vacuum click

10

Vacuum click is an accurate pressure-sensor click board that is capable of measuring pressure values down to -115kPa. This click boardâ„¢ utilizes a very precise and low thermal drift, absolute pressure-sensor from NXP, labeled as MPXV6115V.

[Learn More]