TOP Contributors

  1. MIKROE (2784 codes)
  2. Alcides Ramos (387 codes)
  3. Shawon Shahryiar (307 codes)
  4. jm_palomino (120 codes)
  5. Bugz Bensce (97 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 (140167 times)
  2. FAT32 Library (72621 times)
  3. Network Ethernet Library (57641 times)
  4. USB Device Library (47954 times)
  5. Network WiFi Library (43553 times)
  6. FT800 Library (42942 times)
  7. GSM click (30140 times)
  8. mikroSDK (28669 times)
  9. PID Library (27055 times)
  10. microSD click (26552 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: 3031 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

AVR Webserver

0

AVR Webserver: Dependencies - FAT32 library, Ethernet library Will allow you to load any size website, including images, css, and js files from SD media. This is my first version, but the example project included will give you a starting point.

[Learn More]

Printf Support AVR / ARM

0

Adds printf and sprintf functionality to MikroC. Uses about 1.5k.

[Learn More]

Static Data Structures

5

Add static data structures to your project.

[Learn More]