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

HOW TO IMPLEMENT DEALY IN CODING

I have written code for sensing input and then output port to respond. I want to add delay between the two actions. Can any one help me out.

Parents
  • Function init_delay() enables internal Cortex-M3 to count processor cycles
    Function delay() waits for the desired processor cycles

    #include <RTL.h>
    
    volatile U32 *DWT_CTRL    = (U32 *) 0xE0001000;         // Data Watchpoint and Trace counter register Control Register
    volatile U32 *DWT_CYCCNT  = (U32 *) 0xE0001004;         // Data Watchpoint and Trace counter register Cycle Count Register
    volatile U32 *SCB_DEMCR   = (U32 *) 0xE000EDFC;         // address of the register
    
    
    #define TRCENA                          (1<<24)
    
    void init_delay(void)
     {
      *SCB_DEMCR = *SCB_DEMCR | TRCENA;
      *DWT_CTRL = *DWT_CTRL | 1;
     }
    
    void delay(U32 cycles)
     {
      U32 tmp = *DWT_CYCCNT + cycles;
    
      while(*DWT_CYCCNT < cycles) {}
     }
    
    

Reply
  • Function init_delay() enables internal Cortex-M3 to count processor cycles
    Function delay() waits for the desired processor cycles

    #include <RTL.h>
    
    volatile U32 *DWT_CTRL    = (U32 *) 0xE0001000;         // Data Watchpoint and Trace counter register Control Register
    volatile U32 *DWT_CYCCNT  = (U32 *) 0xE0001004;         // Data Watchpoint and Trace counter register Cycle Count Register
    volatile U32 *SCB_DEMCR   = (U32 *) 0xE000EDFC;         // address of the register
    
    
    #define TRCENA                          (1<<24)
    
    void init_delay(void)
     {
      *SCB_DEMCR = *SCB_DEMCR | TRCENA;
      *DWT_CTRL = *DWT_CTRL | 1;
     }
    
    void delay(U32 cycles)
     {
      U32 tmp = *DWT_CYCCNT + cycles;
    
      while(*DWT_CYCCNT < cycles) {}
     }
    
    

Children