Hello,
The discussion seems to pass over part of bloodsheddragon’s question: how to make a program that does multiple things at once. Lets see if I can help with that idea.
Lets assume you are using the MPLAB software to program a PIC microcontroller in microcode for this example.
First lets define a couple things so we are sure we are communicating without any confusion.
The PIC CPU has a single working register. The working register is where you actually make it do useful things by performing copy, compare or calculate operations. Generally speaking most of a microcode program’s efforts are spent moving data (or the contents of registers) to/from the working register.
Next it is true that by using PIC’s on-board perpherials you can get it to do more than one thing at a time. However, its also true that your program always has to be doing something, and whatever that something is, it can’t be doing anything else. To start an independent process on a PIC, your program must set up the proper registers and then set the perpherial action in motion. Once that is done, you can then go on to do other things with your program.
Finally interrupts are just that. When an interrupt occurs, your program stops doing whatever it was doing and is sent to an ‘interrupt vector’ (in PIC that means line 4 of your program) and starts executing the code you have written there. Interrupt processes are under complete control of the programmer. That means you have to figure out what caused the interrupt, what to do about it, and how to gracefully resume your program where it left off. Its a lot easier said than done, but you can do it.
Let explain the idea with a simple example. Say you have a PIC and you want it to monitor two motors, each with a sensor. The aim is to keep the motors running at a constant speed. One classic approach to this situation is to use a timer to trigger an interrupt.
Lets say you decide that monitoring the motors every 25mS is good enough for your application. Just preset a timer to overflow after 25mS and use that overflow to generate an interrupt. Next set the program to go into an infinite loop where it will stay until it is interrupted.
When interrupted, your code then goes out and reads the status of both sensors (one at a time of course), and compares the readings to some constant that represents the desired speed. It can then update the PWM settings as needed.
Once the motors are updated, you next reset the interrupt registers so they will work again, restart the timer and set the program back into its infinite loop. It will remain in the loop until the next interrupt and the whole thing repeats.
When running the program it will appear as if the PIC is controlling both motors (or any reasonable number of things) all at once. In reality its doing them one at a time. However, since the PIC processes instructions so quickly it appears as if its all happening at once.
This approach is far too simple to be used to run a complex robot. But the intention was to give you an idea of how to use the basic parts of a microcontroller to make it appear as if it is actually monitoring and controlling several things all at once. You can build on the idea. Do a little research and testing and I’m sure you’ll be able to make it all work. Enjoy!
Hope this helps. Have a safe and Happy 4th of July eveyone!
KHall