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

C Macro Expansion Question

Is there a way to create a C #define macro such that a call to it:

nop_delay(5)

would yield the following expanded code?:

_nop()_;
_nop()_;
_nop()_;
_nop()_;
_nop()_;


Thanks,

Tom

Parents Reply Children
  • Drew,

    Thanks a bunch. That was exactly what I was looking for.

    -Tom

  • Hi,

    I'm using already for quite some time the following macro construction, but it finally boils down to what Jon gave as an example:

    #define USEC()  _nop_()
    
    #define DELAY2()   ( USEC()   , USEC()    )
    #define DELAY4()   ( DELAY2() , DELAY2()  )
    #define DELAY8()   ( DELAY4() , DELAY4()  )
    #define DELAY16()  ( DELAY8() , DELAY8()  )
    #define DELAY32()  ( DELAY16(), DELAY16() )
    #define DELAY64()  ( DELAY32(), DELAY32() )
    #define DELAY128() ( DELAY64(), DELAY64() )
    
    #define DELAY(T)  ( ( T & 1   ) ? USEC()     :0 ), \ 
                      ( ( T & 2   ) ? DELAY2()   :0 ), \ 
                      ( ( T & 4   ) ? DELAY4()   :0 ), \ 
                      ( ( T & 8   ) ? DELAY8()   :0 ), \ 
                      ( ( T & 16  ) ? DELAY16()  :0 ), \ 
                      ( ( T & 32  ) ? DELAY32()  :0 ), \ 
                      ( ( T & 64  ) ? DELAY64()  :0 ), \ 
                      ( ( T & 128 ) ? DELAY128() :0 )  \ 
    


    To use it, I simply ask for e.g. DELAY( 27 ) and the rest is done automatically.

    The only difference is that you don't have to type so much _nop_()'s every time and you might have a better overview. Keep in mind that I wrote it for a 8051, running on 12 Mc, hence the following equation:
    #define USEC _nop_()

    However, don't exaggerate too much with the _nop_()'s (as I maybe might have done :-) ). At the end, it's better to use other techniques to introduce delays.

    Rgds

    Geert

  • Hi,

    I'm using already for quite some time the following macro construction, but it finally boils down to what Jon gave as an example:

    #define USEC()  _nop_()
    
    #define DELAY2()   ( USEC()   , USEC()    )
    #define DELAY4()   ( DELAY2() , DELAY2()  )
    #define DELAY8()   ( DELAY4() , DELAY4()  )
    #define DELAY16()  ( DELAY8() , DELAY8()  )
    #define DELAY32()  ( DELAY16(), DELAY16() )
    #define DELAY64()  ( DELAY32(), DELAY32() )
    #define DELAY128() ( DELAY64(), DELAY64() )
    
    #define DELAY(T)  ( ( T & 1   ) ? USEC()     :0 ), \ 
                      ( ( T & 2   ) ? DELAY2()   :0 ), \ 
                      ( ( T & 4   ) ? DELAY4()   :0 ), \ 
                      ( ( T & 8   ) ? DELAY8()   :0 ), \ 
                      ( ( T & 16  ) ? DELAY16()  :0 ), \ 
                      ( ( T & 32  ) ? DELAY32()  :0 ), \ 
                      ( ( T & 64  ) ? DELAY64()  :0 ), \ 
                      ( ( T & 128 ) ? DELAY128() :0 )  \ 
    


    To use it, I simply ask for e.g. DELAY( 27 ) and the rest is done automatically.

    The only difference is that you don't have to type so much _nop_()'s every time and you might have a better overview. Keep in mind that I wrote it for a 8051, running on 12 Mc, hence the following equation:
    #define USEC _nop_()

    However, don't exaggerate too much with the _nop_()'s (as I maybe might have done :-) ). At the end, it's better to use other techniques to introduce delays.

    Rgds

    Geert