NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » How do I run two subroutines or functions simultaneously?

April 14, 2012
by jmuthe
jmuthe's Avatar

I have two questions. First, are subroutines and functions the same thing in programming and if not then what are the differences? My second question is how do I run two subroutines or functions simultaneously? For example, let's say that I connect one LED to PC5 and one LED to PC4. I create one function that causes the first LED to blink at a certain uninterrupted frequency (let's say 50 Hz) and I create another function that causes the second LED to blink at another frequency (let's say 20 Hz). If I want to get the program to run these two functions simultaneously so that the two LEDs blink at their own frequency simultaneously without affecting each other then how could I do that. If you could provide a simple example that would be appreciated. Thank you.

April 14, 2012
by pcbolt
pcbolt's Avatar

Hi jmuthe -

There is some arguments about your first question. Most say they are the same thing, others will tell you a function "returns" a value to the calling program. So if you have a section of code called "light_my_led(led_1)" , that's all the "subroutine" will do. If you have a section of code called "compute_area(x, y)" you can assign the return value to a variable directly from your "function"...

my_area = compute_area(5, 8);

In C, there isn't much any difference to the compiler.

For your second question, there are programming tricks you can use to simulate running two pieces of code at the same time, but generally those tricks take up a whole bunch of code space to do correctly. It's better to keep it simple. Using interrupts is pretty simple method to get code execute "nearly" simultaneously but it's not exactly what you are talking about.

For your specific problem you would not need to use any programming tricks. For LED 1, the time between pulses is 0.02 sec (1/50 Hz) or 20 mSec. For LED 2, the pulse time is 0.05 sec or 50 mSec. All you really need is some code like this;

while(1){
  count++;
  if ((count % 2) == 0) PORTC ^= (1<<PC5);   // toggle LED every 20 mSec
  if ((count % 5) == 0) PORTC ^= (1<<PC4);   // toggle LED every 50 mSec
  delay_ms(10);
}
April 14, 2012
by jmuthe
jmuthe's Avatar

Thank you pcbolt. That is a good trick and might come in handy in the future. However, when I asked about running multiple functions simultaneously, I just mentioned those LEDs as an example. I am not actually doing a real project where I have to simultaneously blink two LEDs at different frequency. If there is an official way to run two functions simultaneously then I would still like to know. It isn't for any specific project but I just want to know it for my general knowledge in case I might need to know it for a future project.

April 15, 2012
by pcbolt
pcbolt's Avatar

No "official" way that I know of. A search of "avr multithreading" comes up with some interesting results. One answer was that "since MCU are so cheap now, just get a bunch of them and use one as a master"

April 15, 2012
by Rick_S
Rick_S's Avatar

The processor inside the microcontroller is single core and can only perform one function at any given time. However, it performs that function quite fast. That is how you can "simulate" simultaneous operations. There are also several built in periferals (UART, TWI, ADC, etc...) that can do things along side the processor but their function is similar to that of a shift register in that they don't do any process to the data other than shift / sample / toggle register bits and so on.

What pcbolt said is VERY true. The use of timers is likely the most common way people perform "simultaneous" tasks. I've also seen people use multiple mcu's like the NK guys did on their expanded LED Array project and a user (NOTER) did with an LCD display.

It really boils down to what you need to get done, how precise the timing of operations is, and what your programming skills and resources are.

Rick

Post a Reply

Please log in to post a reply.

Did you know that a microcontroller can measure an RC time constant? Learn more...