This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

delay milisecond

Hi all Iam using STR912FW44 based board and I want a delay function that should give the delay in milisecond I made a function


void delay_ms (unsigned long nCount){   /* Wait function                    */
  nCount=nCount+16000000L;

  while (nCount--);
}


when I call

delay_ms(10000);

it gives arround 10 second delay but when I call

delay_ms(20000);
it should give arround 20 second delay but it does not happen

but when I call

delay_ms(10000);
delay_ms(10000);

it gives arround 15 sec delay

please tell me exact way to build a delay function

regards
rupesh

Parents
  • things to take into account:
    * you are not using a hardware timer, so haw can you guarantee anything? your code depends on how fast the CPU can crunch instructions, not the frequency of a peripheral designed to help you (hence a hardware timer).
    * you are using C to write a delay loop - which makes you sensitive to optimization, compiler whims etc. use assembly for exact delays.

    /*
    ** Copyright (C) 2008 Tamir Michael
    **
    ** This program is free software; you can redistribute it and/or modify
    ** it under the terms of the GNU General Public License as published by
    ** the Free Software Foundation; either version 2 of the License, or
    ** (at your option) any later version.
    **
    ** This program is distributed in the hope that it will be useful,
    ** but WITHOUT ANY WARRANTY; without even the implied warranty of
    ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    ** GNU General Public License for more details.
    **
    ** You should have received a copy of the GNU General Public License
    ** along with this program; if not, write to the Free Software
    ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    */
    
    #ifndef __TIMER_H
    #define __TIMER_H
    
    #include "general_definitions.h"
    
    #define TIMER_TICKING     1
    #define TIMER_NOT_TICKING 2
    
    typedef enum
    {
            e_timer_not_ticking = 0,
            e_timer_ticking,
            e_timer_expired
    } timer_state ;
    
    typedef enum
    {
            e_timer_poll_mode,
            e_timer_exact_period_mode,
            e_timer_exact_oneshot_mode
    } timer_mode ;
    
    typedef struct
    {
            timer_state             state ; // 0 means not ticking, 1 means ticking
            timer_mode              mode ;
            callback_ptr    callback ;
            int32s                  *callback_parameter ;
            int32u                  deadline_countdown ; // a user provided timeout in units of 10 milliseconds
            int32u                  late_counter ; // this user provided flag is incremented every 10 milliseconds to indicate how late the timer actually was
            int32u                  period ;
    } timer_t ;
    
    typedef struct
    {
            callback_ptr callback ;
            int32s           *parameter ;
    } timer_callback_prop_t ;
    
    void    timer_module_init(void) ;
    int32s  timer_poll(int32s, int32s, int32s *) ;
    int32s  timer_exact(int32s, int32s, timer_callback_prop_t, timer_mode) ;
    int32s  timer_stop(int32s) ;
    void    timer_stop_all(void) ;
    int32u  timer_ticks_remaining(int32s) ;
    
    void    TIM1_callback(void) ;
    
    #endif
    
    

Reply
  • things to take into account:
    * you are not using a hardware timer, so haw can you guarantee anything? your code depends on how fast the CPU can crunch instructions, not the frequency of a peripheral designed to help you (hence a hardware timer).
    * you are using C to write a delay loop - which makes you sensitive to optimization, compiler whims etc. use assembly for exact delays.

    /*
    ** Copyright (C) 2008 Tamir Michael
    **
    ** This program is free software; you can redistribute it and/or modify
    ** it under the terms of the GNU General Public License as published by
    ** the Free Software Foundation; either version 2 of the License, or
    ** (at your option) any later version.
    **
    ** This program is distributed in the hope that it will be useful,
    ** but WITHOUT ANY WARRANTY; without even the implied warranty of
    ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    ** GNU General Public License for more details.
    **
    ** You should have received a copy of the GNU General Public License
    ** along with this program; if not, write to the Free Software
    ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    */
    
    #ifndef __TIMER_H
    #define __TIMER_H
    
    #include "general_definitions.h"
    
    #define TIMER_TICKING     1
    #define TIMER_NOT_TICKING 2
    
    typedef enum
    {
            e_timer_not_ticking = 0,
            e_timer_ticking,
            e_timer_expired
    } timer_state ;
    
    typedef enum
    {
            e_timer_poll_mode,
            e_timer_exact_period_mode,
            e_timer_exact_oneshot_mode
    } timer_mode ;
    
    typedef struct
    {
            timer_state             state ; // 0 means not ticking, 1 means ticking
            timer_mode              mode ;
            callback_ptr    callback ;
            int32s                  *callback_parameter ;
            int32u                  deadline_countdown ; // a user provided timeout in units of 10 milliseconds
            int32u                  late_counter ; // this user provided flag is incremented every 10 milliseconds to indicate how late the timer actually was
            int32u                  period ;
    } timer_t ;
    
    typedef struct
    {
            callback_ptr callback ;
            int32s           *parameter ;
    } timer_callback_prop_t ;
    
    void    timer_module_init(void) ;
    int32s  timer_poll(int32s, int32s, int32s *) ;
    int32s  timer_exact(int32s, int32s, timer_callback_prop_t, timer_mode) ;
    int32s  timer_stop(int32s) ;
    void    timer_stop_all(void) ;
    int32u  timer_ticks_remaining(int32s) ;
    
    void    TIM1_callback(void) ;
    
    #endif
    
    

Children
No data