+ Post New Thread
Results 1 to 4 of 4
  1. #1
    Full Member level 5
    Points: 3,193, Level: 13

    Join Date
    Apr 2004
    Location
    Singapore
    Posts
    278
    Helped
    3 / 3
    Points
    3,193
    Level
    13

    A function which performs task every few seconds!

    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

    •   AltAdvertisement

        
       

  2. #2
    Full Member level 5
    Points: 3,825, Level: 14
    Regnum's Avatar
    Join Date
    Jun 2004
    Location
    Hurlingham
    Posts
    262
    Helped
    19 / 19
    Points
    3,825
    Level
    14

    Re: A function which performs task every few seconds!

    Quote Originally Posted by ericmar
    ...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.

    Quote Originally Posted by ericmar
    ...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.



    •   AltAdvertisement

        
       

  3. #3
    Advanced Member level 4
    Points: 11,937, Level: 26

    Join Date
    Jul 2001
    Posts
    1,059
    Helped
    93 / 93
    Points
    11,937
    Level
    26

    Re: A function which performs task every few seconds!

    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



    •   AltAdvertisement

        
       

  4. #4
    Full Member level 5
    Points: 3,825, Level: 14
    Regnum's Avatar
    Join Date
    Jun 2004
    Location
    Hurlingham
    Posts
    262
    Helped
    19 / 19
    Points
    3,825
    Level
    14

    Re: A function which performs task every few seconds!

    Quote Originally Posted by C-Man
    ooops Regnum was faster than me
    ...but your explanations were masterful. 8O
    regards.



--[[ ]]--