View previous topic :: View next topic |
Author |
Message |
Guest
|
Multitasking |
Posted: Wed Oct 25, 2006 7:34 pm |
|
|
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: 1909
|
|
Posted: Wed Oct 25, 2006 9:49 pm |
|
|
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
|
|
Posted: Thu Oct 26, 2006 6:57 pm |
|
|
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
|
|
Posted: Sat Oct 28, 2006 9:22 pm |
|
|
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? |
|
|
|