As embedded engineers code and debug in C language, macro definitions are a very common programming syntax.Through continuous accumulation of engineering experience, I have summarized many useful macro definitions that can be applied to most embedded C language projects, with high portability and readability.Below are some commonly used macro definitions that engineers can bookmark for future use!
1. Prevent a header file from being included multiple times
#ifndef COMDEF_H
#define COMDEF_H
// Header file content
#endif
#ifndef COMDEF_H
#define COMDEF_H
// Header file content
#endif
2. Redefine some types to prevent differences in type byte sizes due to various platforms and compilers, facilitating portability.
typedef unsigned char boolean; /* Boolean value type. */
typedef unsigned long int uint32; /* Unsigned 32 bit value */
typedef unsigned short uint16; /* Unsigned 16 bit value */
typedef unsigned char uint8; /* Unsigned 8 bit value */
typedef signed long int int32; /* Signed 32 bit value */
typedef signed short int16; /* Signed 16 bit value */
typedef signed char int8; /* Signed 8 bit value */
typedef unsigned char boolean; /* Boolean value type. */
typedef unsigned long int uint32; /* Unsigned 32 bit value */
typedef unsigned short uint16; /* Unsigned 16 bit value */
typedef unsigned char uint8; /* Unsigned 8 bit value */
typedef signed long int int32; /* Signed 32 bit value */
typedef signed short int16; /* Signed 16 bit value */
typedef signed char int8; /* Signed 8 bit value */
Not recommended:
typedef unsigned char byte; /* Unsigned 8 bit value type. */
typedef unsigned short word; /* Unsigned 16 bit value type. */
typedef unsigned long dword; /* Unsigned 32 bit value type. */
typedef unsigned char uint1; /* Unsigned 8 bit value type. */
typedef unsigned short uint2; /* Unsigned 16 bit value type. */
typedef unsigned long uint4; /* Unsigned 32 bit value type. */
typedef signed char int1; /* Signed 8 bit value type. */
typedef signed short int2; /* Signed 16 bit value type. */
typedef long int int4; /* Signed 32 bit value type. */
typedef signed long sint31; /* Signed 32 bit value */
typedef signed short sint15; /* Signed 16 bit value */
typedef signed char sint7; /* Signed 8 bit value */
3. Get a byte or word at a specified address
#define MEM_B( x ) ( *( (byte *) (x) ) )
#define MEM_W( x ) ( *( (word *) (x) ) )
#define MEM_B( x ) ( *( (byte *) (x) ) )
#define MEM_W( x ) ( *( (word *) (x) ) )
4. Get maximum and minimum values
#define MAX( x, y ) ( ((x) > (y)) ? (x) : (y) )
#define MIN( x, y ) ( ((x) < (y)) ? (x) : (y) )
#define MAX( x, y ) ( ((x) > (y)) ? (x) : (y) )
#define MIN( x, y ) ( ((x) < (y)) ? (x) : (y) )
5. Get the offset of a field in a structure
#define FPOS( type, field ) \
/*lint -e545 */ ( (dword) &(( type *) 0)->field ) /*lint +e545 */
#define FPOS( type, field ) \
/*lint -e545 */ ( (dword) &(( type *) 0)->field ) /*lint +e545 */
6. Get the size in bytes of a field in a structure
#define FSIZ( type, field ) sizeof( ((type *) 0)->field )
#define FSIZ( type, field ) sizeof( ((type *) 0)->field )
7. Convert two bytes to a Word in LSB format
#define FLIPW( ray ) ( (((word) (ray)[0]) * 256) + (ray)[1] )
#define FLIPW( ray ) ( (((word) (ray)[0]) * 256) + (ray)[1] )
8. Convert a Word to two bytes in LSB format
#define FLOPW( ray, val ) \
(ray)[0] = ((val) / 256); \
(ray)[1] = ((val) & 0xFF)
#define FLOPW( ray, val ) \
(ray)[0] = ((val) / 256); \
(ray)[1] = ((val) & 0xFF)
9. Get the address of a variable (word width)
#define B_PTR( var ) ( (byte *) (void *) &var )
#define W_PTR( var ) ( (word *) (void *) &var )
#define B_PTR( var ) ( (byte *) (void *) &var )
#define W_PTR( var ) ( (word *) (void *) &var )
10. Get the high and low bytes of a word
#define WORD_LO(xxx) ((byte) ((word)(xxx) & 255))
#define WORD_HI(xxx) ((byte) ((word)(xxx) >> 8))
#define WORD_LO(xxx) ((byte) ((word)(xxx) & 255))
#define WORD_HI(xxx) ((byte) ((word)(xxx) >> 8))
11. Return the nearest multiple of 8 greater than X
#define RND8( x ) ((((x) + 7) / 8 ) * 8 )
#define RND8( x ) ((((x) + 7) / 8 ) * 8 )
12. Convert a letter to uppercase
#define UPCASE( c ) ( ((c) >= 'a' && (c) <= 'z') ? ((c) - 0x20) : (c) )
#define UPCASE( c ) ( ((c) >= 'a' && (c) <= 'z') ? ((c) - 0x20) : (c) )
13. Check if a character is a decimal digit
#define DECCHK( c ) ((c) >= '0' && (c) <= '9')
#define DECCHK( c ) ((c) >= '0' && (c) <= '9')
14. Check if a character is a hexadecimal digit
#define HEXCHK( c ) ( ((c) >= '0' && (c) <= '9') ||\
((c) >= 'A' && (c) <= 'F') ||\
((c) >= 'a' && (c) <= 'f') )
#define HEXCHK( c ) ( ((c) >= '0' && (c) <= '9') ||\
((c) >= 'A' && (c) <= 'F') ||\
((c) >= 'a' && (c) <= 'f') )
15. A method to prevent overflow
#define INC_SAT( val ) (val = ((val)+1 > (val)) ? (val)+1 : (val))
#define INC_SAT( val ) (val = ((val)+1 > (val)) ? (val)+1 : (val))
16. Return the number of elements in an array
#define ARR_SIZE( a ) ( sizeof( (a) ) / sizeof( (a[0]) ) )
#define ARR_SIZE( a ) ( sizeof( (a) ) / sizeof( (a[0]) ) )
17. Return the value of an unsigned number n tail MOD_BY_POWER_OF_TWO(X,n)=X%(2^n)
#define MOD_BY_POWER_OF_TWO( val, mod_by ) \
( (dword)(val) & (dword)((mod_by)-1) )
#define MOD_BY_POWER_OF_TWO( val, mod_by ) \
( (dword)(val) & (dword)((mod_by)-1) )
18. For IO space mapped in storage space, input and output processing:
#define inp(port) (*((volatile byte *) (port)))
#define inpw(port) (*((volatile word *) (port)))
#define inpdw(port) (*((volatile dword *)(port)))
#define outp(port, val) (*((volatile byte *) (port)) = ((byte) (val)))
#define outpw(port, val) (*((volatile word *) (port)) = ((word) (val)))
#define outpdw(port, val) (*((volatile dword *) (port)) = ((dword) (val)))
#define inp(port) (*((volatile byte *) (port)))
#define inpw(port) (*((volatile word *) (port)))
#define inpdw(port) (*((volatile dword *)(port)))
#define outp(port, val) (*((volatile byte *) (port)) = ((byte) (val)))
#define outpw(port, val) (*((volatile word *) (port)) = ((word) (val)))
#define outpdw(port, val) (*((volatile dword *) (port)) = ((dword) (val)))
19. Use some macros for debugging
The ANSI standard specifies five predefined macro names, which are:
__LINE__
__FILE__
__DATE__
__TIME__
__STDC__
If the compilation is not standard, it may only support a few of the above macro names, or not support them at all. Remember that the compiler may also provide other predefined macro names.<span><span>__LINE__</span></span>and<span><span>__FILE__</span></span>macro directives have been discussed in the section regarding<span><span>#line</span></span>, here we discuss the remaining macro names.<span><span>__DATE__</span></span>macro directive contains a string in the form of month/day/year, indicating the date when the source file was translated into code.The time when the source code is translated into target code is contained in<span><span>__TIME__</span></span>. The string is in the form of hour:minute:second.If the implementation is standard, the macro<span><span>__STDC__</span></span>contains the decimal constant 1. If it contains any other number, the implementation is non-standard.Macros can be defined, for example: when<span><span>_DEBUG</span></span>is defined, output data information and the line number in the file.
#ifdef _DEBUG
#define DEBUGMSG(msg,date) printf(msg);printf("%d%d%d",date,__LINE__,__FILE__)
#else
#define DEBUGMSG(msg,date)
#endif
20. Macro definitions to prevent errors when used should be enclosed in parentheses.
For example:
#define ADD(a,b) (a+b)
#define ADD(a,b) (a+b)
Using<span><span>do{}while(0)</span></span>statement to enclose multiple statements to prevent errors,for example:
#define DO(a,b) a+b;\
a++;
When applied:
if(…)
DO(a,b); // This will cause an error
else
Solution:
#define DO(a,b) do{a+b;\
a++;}while(0)
Source: A certain excellent engineer
↑↑↑ Click to receive a 6-layer board sample coupon ↑↑↑