Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Cooperative Multitasking Tutorial

Status
Not open for further replies.

3v0

Coop Build Coordinator
Forum Supporter
@C18 Tutorial Testing Group
We are jumping ahead a bit to a more advanced subject, Cooperative Multitasking. After a few false starts I came up with a good one. You can help make it better.

@Everyone
Feel free to download the tutorial and MPLAB project but keep in mind that it is a draft. Please do not distribute it or talk about it outside this forum. If you want to help with the development please join the C18 Tutorial Testing Group.
picture.php
 
This is not a hard tutorial. If you know what interrupts and timers are (you do not need to know how to set them up) you can work the tutorial and write multi tasking programs. The only advanced topic introduced is "finite state machines". They are not totaly unlike flow charts. :)
 
Thanks. Great tutorial :)

I wonder how many tasks it's possible to run adequately with chip like 18f1320 :rolleyes:
 
Thanks. Great tutorial :)

I wonder how many tasks it's possible to run adequately with chip like 18f1320 :rolleyes:

That depends on a lot of things. Most tasks will spend a lot of there time waiting for their kTimer to run down. The beepTask is the exception in that it is flipping the speaker cone back and forth.

You can find out how much headroom you have without guessing. Add a counter to the bottom of the main loop. Big numbers are betters lower numbers mean your are running near the edge. Zero mean you are up against the wall. If you do go to zero it only means that some timing may be stretched but everything will happen in order.

Code:
  unsigned int waitCount, waitCountMin;   // declare inside main
  void main(void)
  {
    waitCountMin=0xFFFF; // prior to main loop
    ...
    while(1) // main loop
    {
        ... list of task and the guarding if statements

      while(waitForTimer)   
      {
         waitCount++;
      }
      if (waitCount<waitCountMin)
      {
         waitCountMin = waitCountM;
      }
      waitForTimer = TRUE;
  } // end main loop
}
You could always go with a faster processor.

If you order task from high to low priority and test the TMR0 after each task you could skip to the end of the loop when you were out of time and never miss a beat. Maybe another tutorial. All is fun.

EDIT: The kTimers can also be used to block a task waition for IO. When a kTimer is set to 0xFFFF the TMR0 interrupt code will not decrement it. The task will not run till that value is change. The interrupt code for a device can change the ktimer to 0. Very useful and no additional coded to speak of.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top