Get PIC LINUX at SourceForge.net. Fast, secure and Free Open Source software downloads
Support This Project

Para los usuarios de Linux que programan microcontroladores PIC. Sitio de debate y recursos compartidos.


Español
idioma español
English
english language



INDEX
Links:


GcBasic oficial site:

Forum
Web
Online help






multitasking.h

Description:


Allow an easy way to program tasks that must be executed periodically.
In a wide meant this helps to do time based programming.

Timer0 is used to generate an interrupt with a period determined by
base_time ,for each task a counter  is increased until the predefined task-time value is reached, then sets a task-flag that indicates the task can be executed.

This library is included in GcBasic-PL installer, but you can download it directly:
multitasking.h


Use:

Actually a maximum of 16 Tasks and 8 LTasks are supported.

The base time must be defined:

     #define base_time value

where value is the desired period in us, for example:

     #define base_time 100      ' for 100 us

Maximum base_time value with 20 MHz clock is 13107.
Maximum base_time value with 4 MHz clock is 65535.
Maximum base_time value with other clocks: 65535/ChipMhz/4.


The subroutine containing each task must be defined:

     #define Task1 Sub_name1
     #define Task2 Sub_name2
     #define LTask1 Sub_name3


The period of each Task must be defined, for example:

     #define Task1_us 500            ' Task1 period is 500 us
     #define Task2_ms 10             ' Task2 period is 10 ms
     #define LTask1_s 1                ' LTask1 period is 1 s

In this example, Sub_name1 will be executed every 500 us, Sub_name2 every 10 ms and Sub_name3 every 1 seg.

Maximum value for Task period in us is: base_time*255

For example, with a 100 us base_time, maximum value for Tasks is:
100*255 = 25500 us => 25 ms

LTask is for long period tasks, a period is long or not depending on the base_time
When the needed period > base_time*255 then you need an LTask.

Maximun value for LTask period in us is: base_time*65535

For example, with a 100 us base_time, maximum value for LTasks is:
100*65535 = 6553500 us => 6553 ms => 6 s


Task will be executed with the command: Do_Taskx,
   for example  in a main loop:

     do
         Do_Task2
         Do_LTask1
     loop

or any other way... for example:

     do
         Do_Task2
         if PORTB.0 on then Do_LTask1  
     loop


Is possible to execute a Task directly inside Interrupt subroutine:

     #define Run_Task1               'Run Task inside interrupt subroutine

There is a counter variable for each task: cont_Taskx
A period variable:                                          time_Taskx
And a task flag:                                              flag_Taskx

These variables can be used in the program, reading or writting to achieve different behaivors; see next example where time_task3 variable is used to have a variable period task.


Example:

'________________________________________________________________________________
'
'                                           Example for multitasking.h
'
'
'           One led atached to PORTB.0 toggles at 1 s rate
'           One led atached to PORTB.1 toggles at 300 ms s rate
'           One led atached to PORTB.3 toggles at a variable rate, defined by ReadAD
'
'________________________________________________________________________________
'
#chip 16F876A, 20
'MHz
#config HS_OSC, WDT_OFF, LVP_OFF, PWRTE_ON

'________________________________________________________________________________

#include <multitasking.h>

#define base_time 500                'Base time in us

#define Task1 read_input            'Task1 subroutine
#define Task1_us 500                 'This task will run every  500 us

#define Task2 led2                      'Task2 subroutine
#define Task2_ms 300                'This task will run every  300 ms

#define Task3 led3                      'Task3 subroutine
                                                    'We will have the period for this task from ReadAD

#define LTask1 led1                    'LTask1 subroutine
#define LTask1_s 1                     'This task will run every  1 s
#define Run_LTask1                   'Run Task inside interrupt subroutine

'________________________________________________________________________________

DIR PORTA.0 IN
DIR PORTB OUT

do                                               
'Main Loop
    Do_Task1
    Do_Task3
    Do_Task2
loop

'________________________________________________________________________________

Sub read_input
     time_Task3 = ReadAD(An0)        'Read ADC at PORTA.0
End Sub

Sub led1
     PORTB = PORTB xor 1        'Toggle led at PORTB.0
End Sub

Sub led2
     PORTB = PORTB xor 2        '
Toggle led at PORTB.1
End Sub

Sub led3
     PORTB = PORTB xor 4        '
Toggle led at PORTB.2
End Sub



Downloads:



GcBasic-PL:
      
multitasking.h




View all files