PDA

View Full Version : multi-tasking



malc-c
- 26th December 2009, 19:49
Guy's, just need some general pointers on how to move something forward. I don't want to go into actual code at this time for personal reasons, more simply for guidance as to what to use

Lets say I had a routine that calculated a value and then I used that value in a pause statement to control how long a pin remains high. This works fine for one output and is achieving the desired result in my tests. However I now want to replicate this 4 times, which causes me a slight problem.

If I had something like:

Calculate value 1
Calculate value 2
Calculate value 3
Calculate value 4

high pin 1
pause value 1
low pin 1

high pin 2
pause value 2
low pin 2

etc etc

The problem is that the total time all pins are off is the sum of all the other pause values. ie pin one is low for pause value 2 + value 3 + value 4. This is not what I want. I want to have pin 1 pulse at the rate set by value 1 and pin 2 at the rate set by value 2 etc etc..

What would be the the best way forward.

peu
- 26th December 2009, 21:08
lets say you need to pause these pins starting from the same initial port status and time:

porta.1 15 secs
porta.2 10 secs
porta.3 5 secs

you can do this:

porta=%00000111
pause 5
porta=%00000011
pause 5
porta=%00000001

is this what you are looking for?

malc-c
- 26th December 2009, 22:12
Thanks for the input, but not quite as the pause period is not static.

Each pause will be variable each time the loop runs round, and needs to be totally independent. In effect I need to run 4 loops simultaneously so that the pause delay in each has no affect on the others.

spcw1234
- 26th December 2009, 22:25
Driving fuel injectors? :)

Depending on what kind of time frame you are talking it could be done by running a timer.

#1 on at x time
#1 off at x time
#2 on at x time
#3 off at x time
#4 on at x time
#4 off at x time

Each loop can adjust the values that the timer falls on for the on and off times to adjust your output periods. Interupts could be used depending on how time sensitive you need it.

rmteo
- 27th December 2009, 00:08
Driving fuel injectors? :)


More likely simultaneous output of 4 R/C PWM pulses - 1-2mS width, 50Hz frequency. :rolleyes:

malc-c
- 27th December 2009, 00:22
More likely simultaneous output of 4 R/C PWM pulses - 1-2mS width, 50Hz frequency. :rolleyes:

LOL - on this occasion you're not even warm..... ;)

tiger_bel
- 27th December 2009, 01:17
multi tasking ??? one processor does always one thing.....

quite easy problem:
all depends on your timing - use high speed oscillator (20Mhz)
create a timer under interupt ( interupt 1ms) relaod timer
create counters as much as you need.
increment the counters on interupt and compare with value
if match - relaod counters with new value ...
beware that interupttime is smaller than 1ms !

all other calculations can be done out of interupt.

rgds
tb

mackrackit
- 27th December 2009, 01:27
The only way you can have a PIC do more than one thing at a time is to have part of it done with hardware and the other in code. Sounds like you are going to need to use a timer so it might be doable.

Look at interrupts in general and DT's instant to be specific for ideas.

But do not be embarrassed to tell us about your project, unless... :D

Jerson
- 27th December 2009, 02:51
Malcolm

Your idea seems to fit the "Soft PWM" concept very well. Search here to find how to do it.

A little pseudo code might show you how



' In a timer interrupt

DutyCycle = DutyCycle+1
if DutyCycle < Value1 then
Port1 = On
else
Port1 = Off
endif
if DutyCycle < Value2 then
Port2 = On
else
Port2 = Off
endif
if DutyCycle < Value3 then
Port3 = On
else
Port3 = Off
endif
if DutyCycle < Value4 then
Port4 = On
else
Port4 = Off
endif

Acetronics2
- 27th December 2009, 10:22
Hi, Malc

The trick is to fire all 4 pulses at the same time ... and use the COMPARE function of a CCP to turn pulses off ...

of course, you must calculate at first which is turned off in 1st, 2nd ...etc.

and load the COMPARE value according to that.

http://mathieu.agopian.free.fr/prog_pic/servoter/

Alain

PS: another way is to use the CCS RTOS tool ... but ... not PbP anymore !

re P.S. ... You have too much things running at the same time, these days ... LOL

malc-c
- 27th December 2009, 10:57
Thanks guys for all your suggestions.

Sorry to be secretive over this project guys, but I'm being mentored / tutored and most of the code so far (99%) has been supplied by that Friend and Mentor so I don't feel that it's appropriate for me to list it here. Hopefully once the festive holiday is over he will be able to assist me further. I'm hoping that once we've got the fundamentals tested and the project is completed we may be able to release the code to the forum community.

His code produces the variable that I have used in my output routine, which I've tested in the real world and now need to replicate 4 times.

I'll take the suggestions and search the forum for information on interrupts.

Many thanks