Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

A function which performs task every few seconds!

Status
Not open for further replies.

ericmar

Full Member level 5
Joined
Apr 14, 2004
Messages
278
Helped
3
Reputation
6
Reaction score
4
Trophy points
1,298
Location
Singapore
Activity points
2,928
Hi everybody,

May I know how to write an independent function which does some checkings every few seconds without affecting or affected by other functions?

Basically, I just want to check if the client program at the software side is available or not by sending a string and expected a specified string from it within a certain time period!

Besides, I hv a doubt on the timing in prgramming PIC using Hightech C. I'm using 4Mhz crystal oscillator with PIC16F877. If I'm going to display a countdown timer on 4 seven-segments LEDs, how can I make sure the timer works accurately without affected by the functions processing in the PIC after the timer is started?

I'm truly appreciate for any help provided!

Regards,
Eric
 

ericmar said:
...how to write an independent function which does some checkings every few seconds without affecting or affected by other functions?

-Setup a timer, write the task in a timer ISR, and verify that the time took for completion isn't conflicting.

ericmar said:
...how can I make sure the timer works accurately without affected by the functions processing in the PIC after the timer is started?

The timers won't be affected by any instruction but the timer specific ones!

Use interrupts to handle the timer events (it prevents your code from losing events), and if you're using multiple interrupt sources, set the interrupt priorities carefully.
 

It is best if you program timer(s) which are altered inside a timerX interrupt function. Something like this:

static volatile unsigned char Timer_10ms;

void interrupt isr(void)
{
if(TMR2IF)
{
if(Timer_10ms)
Timer_10ms--;
TMR2IF=FALSE;
}

Now you can set your timer inside your function and it will automatically decrement in the time interval that has been specified by your timerX interrupt.

When you need to call a function every X seconds use a similiar principle by programming a callback function which sends for example a string:

void handle_timed_activities(void)
{
if(!Modem_Timer)
{
Modem_Timer=XXXX
string_out("Test")
}
}

Now all you need to do is again set the timer for desired interval and call handle_timed_activities() whenever you have nothing to do in your function or main. You must also process answer that will be received but I hope you see the principle that can be used.

Also try to keep your interrupt routine as short as possible and try to not call a function inside an interrupt routine this will block your main routine too long.

hope this helps and best regards

ooops Regnum was faster than me :)
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top