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 support@ccsinfo.com

Multitasking

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








Multitasking
PostPosted: Wed Oct 25, 2006 7:34 pm     Reply with quote

I've got this code but right now it runs in a loop.
How can I run this in the background and also do something else?
What I want todo is put this in the background, and have the count var updated every 1s then print out the count in a non-background process.

setup_timer_0 (RTCC_DIV_1|RTCC_EXT_L_TO_H);
while(true()) {
set_timer0(0);
output_high(PIN_A2);
delay_ms(1000);
output_low(PIN_A2);
count=get_timer0();
printf("Pulses: %lu \n\r",count);
}
newguy



Joined: 24 Jun 2004
Posts: 1903

View user's profile Send private message

PostPosted: Wed Oct 25, 2006 9:49 pm     Reply with quote

In general you're going to have to use a timer interrupt to do what you want. The idea is to set up a timer to "go off" at the interval you want (1 second), and use that event to signal that something needs to happen.

Here's a blurb regarding timers: http://www.ccsinfo.com/forum/viewtopic.php?t=22467

By the way, in your short code snippet you posted, you're not going to see pin A2 go low for very long. If A2 is hooked up to a LED, you'll need to add another delay_ms(1000) after setting A2 low.
acidice333



Joined: 14 Oct 2006
Posts: 33

View user's profile Send private message

PostPosted: Thu Oct 26, 2006 6:57 pm     Reply with quote

Thank you.

I added a timer2 and just made it printf so I could see it go every 1? second and it does but Im not sure if its exact or not.

I didnt bother putting that little bit of code in the timer2 yet, I just left it as is. It seemed to delay the original code a little compared to when I turned it off.

Code:

//=======================
// timer2
#int_timer2
void system_tick(void)
{
  static long tick=0;
  if (++tick == 1000) // 1000 = approx 1second
  {
    tick = 0;
    printf("TIMER REACHD!");
  }
}
//=======================

in the main:

   enable_interrupts(GLOBAL);
   enable_interrupts(INT_TIMER2);
   setup_timer_2(T2_DIV_BY_1,250,5);
   set_timer2(6);

      setup_timer_0 (RTCC_DIV_1|RTCC_EXT_L_TO_H);
      while(TRUE) {
          set_timer0(0);
          delay_ms(937);
          count=get_timer0();
          printf("Pulses (MPH): %lu \n\r",count);
       }
acidice333



Joined: 14 Oct 2006
Posts: 33

View user's profile Send private message

PostPosted: Sat Oct 28, 2006 9:22 pm     Reply with quote

Ok well I have a problem.

This code works fine on my 16f648A, but fails to work on the 16F917.

The timer2 works, but it sends gibberish to my terminal plus all the proper text. If I use timer1 instead, it works fine. What is the problem?
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