two Loops running on the same programme together

Thread Starter

kasun1993

Joined Jun 13, 2016
8
i am writing two loop programmes to two pins,, I want to start this two programmes together and run in mikroc,, is there any method of running these two programmes parallely???? help me
I have attached my programme image,,,,MY.jpg
 

AlbertHall

Joined Jun 4, 2014
12,344
The short answer is no, not like that, but you can do it like this:
Set up a timer to generate an interrupt, perhaps every 200mS. Then on every 3 interrupts toggle port d, and every 10 interrupts toggle port a. This also leaves the uC with lots of free time to do something else.
 

Picbuster

Joined Dec 2, 2013
1,047
  • Please identify what you want to do and why.
  • two loops creating pulses?
  • pulses in phase if so please identify accuracy and max jitter allowed.(nS/uS/mS)
  • processor used clock speed.
  • I like to help you but need more information
  • Picbuster
 

Thread Starter

kasun1993

Joined Jun 13, 2016
8
  • Please identify what you want to do and why.
  • two loops creating pulses?
  • pulses in phase if so please identify accuracy and max jitter allowed.(nS/uS/mS)
  • processor used clock speed.
  • I like to help you but need more information
  • Picbuster
yes my task is to generate clock pulse from second loop and generate a random digital signal(10011100111111 like thisl,,,finally send this clock and data through a serial in parallel out shift register and shift that data on seven segment parallel,,, I tried with ne555 cloc it is difficult to keep the clock in steady frequency,,,,
 

Thread Starter

kasun1993

Joined Jun 13, 2016
8
The short answer is no, not like that, but you can do it like this:
Set up a timer to generate an interrupt, perhaps every 200mS. Then on every 3 interrupts toggle port d, and every 10 interrupts toggle port a. This also leaves the uC with lots of free time to do something else.

tnxx,,, I will try with interuppts
 

AlbertHall

Joined Jun 4, 2014
12,344
You can use a pseudo random generator in PIC code which will generate apparently random numbers if that would do. Most C compilers include one so it easy to use.
 

ErnieM

Joined Apr 24, 2011
8,377
While using interrupts will get you more processing time it comes at the expense of more complicated programming.

For your simple case you can use a simple dumb delay: both loops have a common factor of 100 ms, so you can make one loop with that delay and just check how many times it has run. Every sixth time do one thing, every tenth time do the other.
 

NorthGuy

Joined Jun 28, 2014
611
In nearly any MCU project there are a number of things you want to do more or less concurrently. Therefore, starting your MCU learning from linear programs (such as blinking LEDs with delays) is really a bad idea, because then you need to re-write all your linear programs to make them work simultaneously. There are a number of ways you can use to ensure concurrent executions. It would take too long to discuss them here in details, so I'm only going to list them:

- Interleaving the code into a single linear program. Usually a bad idea.
- Using state machines. Very common and generally good way, but requires more programming.
- Offloading tasks to interrupts and hardware modules. Very efficient if you can do it right.
- Cooperative multitasking. A scheduler (usually developed by a third party) is called to switch threads.
- Using RTOS. Lets you run multiple linear programs, but it's a resource hog
 

Picbuster

Joined Dec 2, 2013
1,047
AlbertHall is correct use the random (rand command ) from std lib.
Each C compiler should have it.
If not available in micro C then move to a Microchip compiler it's also free and does the job.

set by timer interrupt a flag. ( save time stamp)
in main calculate random number.
output the lot.
reset flag.
remark: make sure that timer interval > time main loop.
calculate error and caused by main time loop correct time stamp if needed.
Picbuster.
 
Top