TOP Contributors

  1. MIKROE (2653 codes)
  2. Alcides Ramos (351 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 (136658 times)
  2. FAT32 Library (69888 times)
  3. Network Ethernet Library (55914 times)
  4. USB Device Library (46246 times)
  5. Network WiFi Library (41881 times)
  6. FT800 Library (41122 times)
  7. GSM click (28970 times)
  8. PID Library (26405 times)
  9. mikroSDK (26340 times)
  10. microSD click (25349 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

Task Scheduler

Rating:

10

Author: Richard Lowe

Last Updated: 2016-02-25

Package Version: 1.1.0.5

Category: Timers (Real time clock)

Downloaded: 2915 times

Followed by: 6 users

License: MIT license  

This is a light Round Robin style task scheduler. You can define the maximum number of tasks and add those tasks to be ran at scheduled intervals. You will need to provide a clock source.

No Abuse Reported

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

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

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

  • Information
  • Comments (0)

Library Blog

Example of usage: 

Used STM32 Mini M4.  This example shows that with a 1 second timer and the scheduler blink the LED at the same rate.  

#include "scheduler.h"

#define ARM

sbit STAT at ODR13_GPIOC_ODR_bit;
sbit DATA at ODR12_GPIOC_ODR_bit;

void InitTimer()
{
#ifdef AVR
    // use 1/8 of system clock frequency
    TCCR0B |= (1<<CS01);
    // inital timer value = 0
    TCNT0 = 0x00;
    // enable Timer0 Overflow interrupt
    TIMSK0 |= (1<<TOIE0);
#endif
#ifdef ARM
    RCC_APB1ENR.TIM2EN = 1;
    TIM2_CR1.CEN = 0;
    TIM2_PSC = 1;
    TIM2_ARR = 60000;
    NVIC_IntEnable(IVT_INT_TIM2);
    TIM2_DIER.UIE = 1;
    TIM2_CR1.CEN = 1;
#endif
}

#ifdef ARM
void initCaliTimer()
{
    RCC_APB1ENR.TIM3EN = 1;
    TIM3_CR1.CEN = 0;
    TIM3_PSC = 1874;
    TIM3_ARR = 64000;
    NVIC_IntEnable(IVT_INT_TIM3);
    TIM3_DIER.UIE = 1;
    TIM3_CR1.CEN = 1;

}
#endif

void task1()
{
    UART_Write_Text("1 Second has passed\n");
    STAT = ~STAT;                            // Toggle STAT LED

}

void task2()
{
    UART_Write_Text("5 Seconds has passed\n");

}

void main()
{
#ifdef AVR
    asm sei;
    UART1_Init(38400);
    UART_Write_Text("System Startup\n");
#endif
    schedulerInit();
    addTask(0, task1, SECONDS_1);
    addTask(1, task2, SECONDS_5);
    InitTimer();
#ifdef ARM
    initCaliTimer();
    // configure DATA and STAT pins as output
    GPIO_Digital_Output(&GPIOC_BASE, _GPIO_PINMASK_12 | _GPIO_PINMASK_13);
    STAT = 0;                                  // turn OFF the STAT LED
    DATA = 1;                                  // turn OFF the DATA LED
    UART1_Init(115200);
    Delay_ms(10);
    EnableInterrupts();
    UART1_Write_Text("System Startup\n");
#endif

    while(1) {
        dispatchTasks();
    }
}

#ifdef AVR
void Timer0Overflow_ISR() iv IVT_ADDR_TIMER0_OVF ics ICS_AUTO {
    schedulerClock();
}
#endif
#ifdef ARM
void Timer2_interrupt() iv IVT_INT_TIM2 {
    TIM2_SR.UIF = 0;
    schedulerClock();
}

void Timer3_interrupt() iv IVT_INT_TIM3 {
    TIM3_SR.UIF = 0;
    DATA = ~DATA;                            // Toggle DATA LED
}

#endif


This version adds some flexibility in your clock source.  When initialized, use any clock from 10ms to 1000ms ( must be % 2 ), no 545 values.


Tasks can be run every 1 second up to 45 days.


Also added a stop and start.  Some weird behavior could have happened with very fast MCU's where the tasks started counting down before the dispatch was called.  This would have rolled the period over making it run 45 days from initialization.  When ready, call task_scheduler_start() and that issue is avoided.

ALSO FROM THIS AUTHOR

Webserver - Full Featured

5

Example of what can be done with the Ethernet library. Server serves both dynamic / static content, retrieves time from NTP servers, downloads files from remote servers. Requires: Ethernet Library, FAT32 Library, Standard Bool Library

[Learn More]

BarGraph Library

5

BarGraphs are wonderful for visual feedback. This library makes it easy to add one. Based on BarGraph click board.

[Learn More]

STM DMA Library

15

This is the first of DMA functions that will be available for the ST line of ARM microcontrollers. The idea was born from a forum post. The idea worked so well I made it into a convenient MikroC library. So far.... memory to memory transfers is what is functional

[Learn More]