Become a subscriber (Free)

Join 29,000 other subscribers to receive subscriber sale discounts and other free resources.
:
:
Don't worry -- your e-mail address is totally secure. I promise to use it only to send you MicroZine.

Multitasking for PIC micros

Every time you turn on your computer it is doing multitasking. I still find that amazing - to think of all that the PC is capable of doing and all with one instruction at a time!

It simply means switching attention from one task to another to make it appear that many tasks are happening all at the same time.

To make it work in a PC you need to balance the speed of the processor with the tasks you ask it to do and in some cases extra external hardware is used to take the load off the processor e.g. maths co-processor (built into Pentium processor), graphics cards etc.

This is exactly the same for a PIC microcontroller but since it is not going at GHz speeds you have to carefully decide what tasks you need to do and how to make it work distributing a fair share of processing time to each task (or peripheral).

RTOS

An RTOS (Real Time Operating System) is the ultimate multitasking system and it does make writing code easier using flag signals for inter-process communication and providing prioritized task scheduling.

The penalty for using them is they take up code space and RAM resource (and they have a learning curve as there are a lot of details for RTOS control and task switching).

On the web you can find RTOS code for PIC micros but this is probably more appropriate for the larger 18F series micros as RTOS's tend to require larger RAM and program resources (Ram is used to save all the current task data between task switching) so you wont fit many tasks into a mid-range PIC micro.

Simple Multiple task scheduling

You can make a simple multiple task scheduler using interrupts and careful coding and it won't need a huge amount of memory or resources.

The advantage of this method is that you have full control over the code generated and can therefore fit it into tiny devices e.g. a 12F675 with 1k of program memory.

The disadvantage is that you have to be completely on top of how your code is working. With an RTOS time slices are assigned to each task giving them a fair share of processing time. With this method you need to decide how tasks are assigned processor time as you design the code.

All you do is place all your code in an loop and use variables within the loop to test the conditions you need e.g polling an input. Each test section is more or less equivalent to a task in an RTOS.

Interrupts

As with any multitasking system the heart of a task scheduler is the main timer that can assign time slices to each process. For this method a timer interrupt operates every n milliseconds and calls subordinate functions that require the highest priority. Everything else is a low priority task that gets interrupted by the highest priority task.

The multitasking example I use here is taken from the 12F675 tutorial section - this uses a Timer 0 interrupt to execute the highest priority task(s) - in this case generating the servo motor output signal every 18ms. If this signal is delayed then the servo motor position will change so this must be updated regularly and must have the highest priority.

The subordinate tasks are to send and receive serial data coded in software to/from separate pins. Reception is done by continuously polling an input pin - obviously a better way would be to use another interrupt to detect the pin change - but there was not one available and it illustrates the principle well.

In this case the serial RS232 signals are not as important as the accurate servo motor signal as they are operating at 2400 Baud and can be interrupted (as the interrupt is slow compared to the transmission time of the RS232).

When you use this multitasking method you have to balance the processing time of the interrupt routine so that enough time is spent doing the RS232 communication and all this really means is make the ISR (Interrupt Service Routine) as short as possible. Also in this case the interrupt routine occurs very infrequently i.e. every 18ms leaving plenty of time for all the other tasks.

Note: This multitasking method is useful for fairly small programs but I have worked in companies that used this method for an entire large project - you end up with zillions of variables and state machine operations transferring operation to different sections within the main loop (about 20 pages long!). The whole thing becomes an unmanageable mess and if you have more than a few simple tasks it is better to switch to an RTOS as you can think of each task separately (even though the underlying code might be similar). The RTOS lets you think of solving the task rather than overcoming hardware operation (or code operation!).

Jump from multitasking page to

Best-Microcontroller-Projects Home Page

New! Comments

Have your say about what you just read! Leave me a comment in the box below.



Claim Your: Useful

"Arduino Software Guide"

   Right Now...





Privacy Policy | Contact | About Me

Site Map | Terms of Use



Visit our Facebook Page:

   Click Here



Sign up for MicroZine
''The'' Microcontroller Newsletter

Enter your first Name and primary email address in the form below:


And receive absolutely FREE a full project for:

"Measuring Analogue Voltages
Without An ADC"

(Using only one pin).

Instant Download:
You Can
Get It Right Now

Warning: This project could be  Removed 
at any time.  

It will  NOT be 
available indefinitely SO
To avoid 
disappointment  get it:

Now



:
:
Don't worry -- your e-mail address is totally secure. I promise to use it only to send you MicroZine
Remember this is a project with full description and fully debugged C Source code - and it's not available from the main website.

You can only get it through this newsletter.

To get exclusive access Enter your first name Name and primary email address Now in the form above.:



But wait !

There's more...

You'll receive more
free and exclusive reports as well as site information and site product updates


Scroll up to the form above and sign up NOW. Don't forget it's FREE and if you don't like it, you can unsubscribe at any time.

Click Here Now to use the form above to get your Valuable information absolutely free.



Readers Comments

"I wanted to thank
you so so so much
for all the information
you have provided in
your site it's

SUPERB and FANTASTIC."

- Ranish Pottath

"This site really is
the best and my favorite.
I find here many useful
projects and tips."

- Milan

bursach<at>gmail.com<

"Awesome site,
very, very easy and nice
to navigate!"


- Matt
matt_tr<at>
wolf359.cjb.net


Learn Microcontrollers

"Interested in
Microcontrollers?"

Sign up for The
Free 7 day guide:

FREE GUIDE : CLICK HERE


"I am a newbie to PIC
and I wanted to say
 how great your
site has been for me."


- Dave

de_scott<at>bellsouth.net

"Your site is a great
and perfect work.
congratulations."


- Suresh

integratredinfosys<at>
yahoo.com

"I couldn't find the correct
words to define
yourweb site.

Very useful, uncovered,
honest and clear.

Thanks so much for
your time and works.
Regards."


- Anon

Recent Articles

  1. How to use the DHT22 (or DHT11) with an Arduino; full code and description. Also including a comparison of the DHT11 vs DHT22.

    The Essential Guide to the DHT22/11 humidity sensor including a library code guide for the Arduino. Learn how to determine Humidity, Dew point and Heat Index.

    Read more

  2. How to Use the MAX7219 to drive an 8x8 LED display Matrix on the Arduino.

    The Essential Guide to the MAX7219; What it is and how you can easily use one in any of your projects.

    Read more

  3. How to use a DS18B20 (single or multiple on one wire) for temperature measurement using the Arduino Uno.

    How to Easily Use the DS18B20 Maxim One-Wire thermometer with Arduino example code showing external or parasitic power modes.

    Read more