Thread: required delay before function execution!!

  1. #1
    Registered User
    Join Date
    Dec 2012
    Location
    Goregaon, Maharashtra, India
    Posts
    6

    required delay before function execution!!

    Hello,

    Please refer the function mentioned below.
    Code:
    void ringAlarm(int8* ai)
    {  
       int i,j;
        static int16 time_delay=10000;
        time_delay=time_delay-1;
    
     for(i=0;i<8;i++)
     {    
         if(ai[i]==1 && time_delay==0)
         {   
               
              switch(i)
              {
                case 0:
                   output_high(pin_A0);
                   break;
                case 1:
                   output_high(pin_A1);
                   break;
                case 2:
                   output_high(pin_A2);
                   break;
                case 3:
                   output_high(pin_A3);
                   break;
                case 4:
                   output_high(pin_A4);
                   break;
                case 5:
                   output_high(pin_A5);
                   break;
                case 6:
                   output_high(pin_E0);
                   break;
                case 7:
                   output_high(pin_E1);
                   break;
               }//switch ends
           
        }// if ends
    }//for ends
     
    }
    a[i] gives alarm conditions.

    My requirement is
    1. Check a[i]==1 i.e checking condition
    2. Wait for some delay
    3. Again check for a[i]
    4. And if a[i]==1 after delay then only execute the function

    But i can not use delay function which halts execution of next code.

    With the above mentioned code i am not getting expected result.

    Please suggest what i need to do?

    thanks,
    paddy
    Last edited by paddy611; 12-03-2012 at 06:39 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps you could start by telling both boards which OS and compiler you're using.

    Also, what sort of delay are you looking for?
    - A few microseconds
    - A few seconds
    - less, more?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    To delay program execution, use the sleep or usleep functions. Sometimes the behaviour of this function depends on the operation system and/or compiler.

  5. #5
    Registered User
    Join Date
    Dec 2012
    Location
    Goregaon, Maharashtra, India
    Posts
    6
    Quote Originally Posted by Salem View Post
    Perhaps you could start by telling both boards which OS and compiler you're using.

    Also, what sort of delay are you looking for?
    - A few microseconds
    - A few seconds
    - less, more?
    I am really sorry for the incomplete information.

    I am using windows os and CCS compiler and I am looking for the delay of 1 minute.

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I know that when programming a PIC with the CCS compiler, you have a function named delay_ms()

    See if it's in your help manual
    Fact - Beethoven wrote his first symphony in C

  7. #7
    Registered User
    Join Date
    Dec 2012
    Location
    Goregaon, Maharashtra, India
    Posts
    6
    Quote Originally Posted by Click_here View Post
    I know that when programming a PIC with the CCS compiler, you have a function named delay_ms()

    See if it's in your help manual

    yeah. I have tried with that.

    But it halts the next program execution as i need delay of around 1 to 2 minutes.

    so that is why i am trying something with static value as mentioned in code, but it is not working.

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Are you making a program for a PIC microprocessor? If so, what one?

    [edit]
    On a second look at your code, yes you are

    Which PIC are you using?

    [/edit]
    Last edited by Click_here; 12-04-2012 at 12:03 AM.
    Fact - Beethoven wrote his first symphony in C

  9. #9
    Registered User
    Join Date
    Dec 2012
    Location
    Goregaon, Maharashtra, India
    Posts
    6
    Yes i am programming for PIC18F4520.

    In normal case that function executes when alarm condition occurs.

    But to avoid false triggering i need some delay, say 1 or 2 minutes and after that delay if alarm condition is present then only that function should execute.

  10. #10
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    The reason I ask is because you can set up a timer interrupt which counts milliseconds and then seconds with this

    Code:
    setup_timer_2(T2_DIV_BY_1,200,10);
    Fact - Beethoven wrote his first symphony in C

  11. #11
    Registered User
    Join Date
    Dec 2012
    Location
    Goregaon, Maharashtra, India
    Posts
    6
    Actually i have used that port pins as an I/O.

    So i can not use interrupts.

  12. #12
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    OK, the 18F4520 has a timer 2 interrupt

    Code:
    #int_TIMER2
    TIMER2_isr()
    {
       ... interrupt code
    }
    
    in your main...
    setup_timer_2(T2_DIV_BY_1,200,10);
    enable_interrupts(INT_TIMER2);
    enable_interrupts(GLOBAL);
    You can work out what setting you want for you timer here - PIC Timer Calculator
    Fact - Beethoven wrote his first symphony in C

  13. #13
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Also, CCS has a great forum here - CCS :: Index
    Fact - Beethoven wrote his first symphony in C

  14. #14
    Registered User
    Join Date
    Dec 2012
    Location
    Goregaon, Maharashtra, India
    Posts
    6
    yeah. I will check out with this.

    Thanks a lot for your support.

  15. #15
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Good luck with your project, Paddy
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to use delay() in another function?
    By shansajid in forum C Programming
    Replies: 4
    Last Post: 11-25-2012, 03:58 AM
  2. Is there a delay function?
    By Digga in forum Windows Programming
    Replies: 2
    Last Post: 05-06-2005, 10:56 AM
  3. The 'delay()' function and its alternatives...
    By Nutshell in forum Game Programming
    Replies: 1
    Last Post: 04-27-2002, 01:45 AM
  4. Delay function
    By knight543 in forum C++ Programming
    Replies: 4
    Last Post: 01-14-2002, 03:04 PM
  5. Use a delay function
    By Robert_Ingleby in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2001, 06:58 AM