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.

Multi Tasking...Sort of

Status
Not open for further replies.

2camjohn

Member
So far I have been coding a control program and have found it quite straightforward.

I am encountering problems while trying to make my chip do several things at once.

At a certain point in the program I want to set an output pin high for a short time, aswell as another pin high for a longer time. This loop may be executed again while the second pin is high, keeping it high.

This kind of thing would be easy to do in java or similar by sending two programing threads off doing the two different methods. I realise that is a high level concept and the only way I can think of reproducing that on a PIC would be using complex timing and checks, which I want to aviod to keep the performance.

Is there anything built into pics (16f877) to help me achieve this? Any good tutotials on this kind of thing?

Thanks
 
You could use timer interrupt routines, generate interrupts at regular intervals and decrement a number of counter registers (one for each pin you want to stay high), when the particular counter reaches zero simply turn the pin off. To set the pin high, simply set it high with BCF and reset the counter for the time you require.

What exactly are you trying to do, they could well be an easier solution for your specific problem - particularly if the requirements are fixed (such as the width is always the same, and one is always the longest).

Something like this:

Pin1 high
Pin2 high
Delay time1
Pin1 low
Delay time2-time1
Pin2 low

You get the idea? - this isn't in any kind of code of course!.
 
I like to use a setup where each separate thread is a function call that either does something or exits quickly to allow the other task functions to run.
Code:
void Task_1 (void)
{
   if (counter == TIME1)
   { 
       Pin1 = 0;
   }
}

void Task_2 (void)
{
   if (counter == TIME2)
   {
       Pin2 = 0;
   }
}

void main ()
{
   Task_1();
   Task_2();
}

If you get much more complicated than that you will end up building your own operating system. Simple operating systems are actually fairly easy to build if you want to give it a shot. Like Nigel suggested you can use a timer triggered interrupt to generate regualr interrupts. Inside the interrupt you use a function pointer to set the next task function to be executed from a list of task functions. Your main loop just calls the function that the pointer points to. I've left out some details but thats the basic idea. This type of operating system is called a real-time operating system (google RTOS for more info) beacuse of the guaranteed latency between tasks.

Hope this helps
Brent
 
HI Guys

I have been looking into using interrupt routines to perform the timing checks, the problem is I am already using an interrupt routine.

I am using pic basic compiler which only has one command for responding to an interrupt which I am already using. There doesnt appear to be a way of differentiating between the timer triggered interrupts and the ones triggered by a leading edge on pin 6 INT,RB0

Is this a limitation in the language I am using or the pic itself (16f628)?

Thanks
John
 
2camjohn said:
Is this a limitation in the language I am using or the pic itself (16f628)?

It's not the PIC, all you need to do is test what caused the interrupt within the interrupt routine, each interrupt causing event sets a flag, which you can simply test with a BTFSC or BTFSS instruction.

If your language doesn't support this you may be in trouble!.
 
Status
Not open for further replies.

Latest threads

Back
Top