CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

running 2 loops at the same time?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
axmanjr



Joined: 07 Aug 2007
Posts: 25
Location: Las Vegas, NV

View user's profile Send private message

running 2 loops at the same time?
PostPosted: Tue Aug 28, 2007 12:02 pm     Reply with quote

This may be a weird question, but is it possible to run 2 loops at the same time. For the LCD screen, I want to be able to display 2 separate pieces of data at the same time, instead of one after the other. Currently, I have my code set up something like this:

Code:
void main(void)
{
   int a, b, c;
   
   while(1)
      {
         function1();
         function2();
      }
}

void function1(void)
{
// Do something
printf(print data to the LCD);
}

void function2(void)
{
// Do something else
printf(print data to the LCD);
}


So in this mockup code, function1's data is printed first and then function2's data is printed second, afterwards. Is there a way to have both functions working side-by-side and not one-after-the-other?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Aug 28, 2007 1:24 pm     Reply with quote

Tell us why you want to do this.

What is the current behavior of your program, that you don't like ?

How would you like it to behave ?
kevcon



Joined: 21 Feb 2007
Posts: 142
Location: Michigan, USA

View user's profile Send private message

PostPosted: Tue Aug 28, 2007 1:32 pm     Reply with quote

Kinda sorta but not really, there are two approaches you could take.

You could set up a timer a do some sort of context switching between the two functions but they would not execute simultaneously.


Have the two functions pass messages to a single LCD write handler that updated on a regular basis, again the two functions would not run simultaneously but the display would look like they were.
axmanjr



Joined: 07 Aug 2007
Posts: 25
Location: Las Vegas, NV

View user's profile Send private message

PostPosted: Tue Aug 28, 2007 1:43 pm     Reply with quote

Well, one of the functions is for a GPS unit and the other function is for an accelerometer unit (tilt). I already have code written for both and I have both displaying to the screen. However, with the GPS code, it displays the GPS data on the screen once every second. Now this is fine for the GPS, but it kinda poses a problem for the accelerometer. The accelerometer's data needs to be continuously displaying on the screen and updating itself, all the time. And the code does this, by itself. But when I add the 2 codes together, the accelerometer will only update itself every second due to the GPS function, instead of continuously like it should.

As for the LCD display, I'm trying to have the top row display the accelerometer data, which is constantly updating the data as tilt is detected. The GPS data will be displayed on the 2nd-4th rows, showing the neccesary data. The GPS data will update itself every second, which is fine.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Aug 28, 2007 1:52 pm     Reply with quote

If you need several processes to occur at different rates in a program,
a multi-tasking design will work.

Do a search on Multi-tasking in the forum archives. Here's the thread
with my post on it:
http://www.ccsinfo.com/forum/viewtopic.php?t=17189
axmanjr



Joined: 07 Aug 2007
Posts: 25
Location: Las Vegas, NV

View user's profile Send private message

PostPosted: Tue Aug 28, 2007 2:29 pm     Reply with quote

so basically, within the GPS function I should have a "timer interrupt" that will go off every X-milliseconds and activate the accelerometer function while still in the GPS function. Something like this:

Code:
#int_XXXX
void int_tilt_function(void);

void main(void)
{
   int a, b, c;
   
   while(1)
      {
         functionGPS(); 
      }
}

void functionGPS(void)
{
// enable timer interrupt
// Do GPS stuff
printf(print data to the LCD);
// disable timer interrupt
}

void function2(void)
{
// Do something else
printf(print data to the LCD);
}

#int_XXXX
void int_tilt_function(void)
{
// set up timer stuff.
// once timer is finished (10ms...)
// execute following:

   function2();
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Aug 28, 2007 2:37 pm     Reply with quote

Well, that's not what the sample code shows. It shows a central timer
tick interrupt that counts down several different 'timers' (variables).
The main() code contains a while() loop that sequentially calls several
'task' functions. Each task executes quickly, relative to the 'tick' period.
Each task checks its own 'timer' to see if it has expired (counted down
to 0). If so, it executes the task. If not, it just returns, without executing
the task.

If you want to do something else, that's OK.
axmanjr



Joined: 07 Aug 2007
Posts: 25
Location: Las Vegas, NV

View user's profile Send private message

PostPosted: Tue Aug 28, 2007 3:00 pm     Reply with quote

ok, I guess I was mis-interpretting what your sample code was doing. I understand it now.
Foppie



Joined: 16 Sep 2005
Posts: 138
Location: The Netherlands

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

PostPosted: Wed Aug 29, 2007 9:03 am     Reply with quote

I don't know which compiler you are using, but in version 4 is an RTOS included that could do this for you. (search the help file for RTOS)

but I don't know how well this is working...
RLScott



Joined: 10 Jul 2007
Posts: 465

View user's profile Send private message

PostPosted: Wed Aug 29, 2007 3:23 pm     Reply with quote

An RTOS may be overkill for this problem. Take a look at your GPS function and see if you can re-write it so that you can quickly check if a GPS response is ready. That will be made easie if you are using an interrupt-driven serial input with a buffer large enough to store one complete GPS response. Then your code becomes:

Code:

   while(1)
   {
      if(GPS_Response_Ready())
      {
          GPSFunction();
      }
      AccelFunction();
   }
[/code]
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group