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
  • Don't you think you need even further corrections than the following?

    void delay(U32 cycles)
     {
      U32 tmp = *DWT_CYCCNT + cycles;
    
      while(*DWT_CYCCNT < tmp) {}
     }
    

    What if DWT_CYCCNT + cycles overflows, resulting in tmp being set to a value less than DWT_CYCCNT? How long will your delay become then?

    That is of course if DWT_CYCCNT has the same numeric range as your U32. If it has less range, then tmp can be set to a value that is larger than DWT_CYCCNT can ever reach, which can cause an infinite delay.

    It doesn't hurt to make a full analysis of written code, to check all boundary cases, and figure out if the code can always be trusted or if there might be situations not covered.

Reply
  • Don't you think you need even further corrections than the following?

    void delay(U32 cycles)
     {
      U32 tmp = *DWT_CYCCNT + cycles;
    
      while(*DWT_CYCCNT < tmp) {}
     }
    

    What if DWT_CYCCNT + cycles overflows, resulting in tmp being set to a value less than DWT_CYCCNT? How long will your delay become then?

    That is of course if DWT_CYCCNT has the same numeric range as your U32. If it has less range, then tmp can be set to a value that is larger than DWT_CYCCNT can ever reach, which can cause an infinite delay.

    It doesn't hurt to make a full analysis of written code, to check all boundary cases, and figure out if the code can always be trusted or if there might be situations not covered.

Children