有时候在一些特殊寄存器配置赋值的时候用二进制比较方便直观,
在AVR分坛那边发现的,觉得很好
http://www.ourdev.cn/forum.php?mod=viewthread&tid=5469208&highlight=%E4%BA%8C%E8%BF%9B%E5%88%B6
//仿二进制赋值
#define HEX__(n) 0x##n##UL
#define B8__(x) ( (x & 0x0000000FUL) ? 1:0 )
+( (x & 0x000000F0UL) ? 2:0 )
+( (x & 0x00000F00UL) ? 4:0 )
+( (x & 0x0000F000UL) ? 8:0 )
+( (x & 0x000F0000UL) ? 16:0 )
+( (x & 0x00F00000UL) ? 32:0 )
+( (x & 0x0F000000UL) ? 64:0 )
+( (x & 0xF0000000UL) ? 128:0 )
#define B8(x) ((unsigned char)B8__(HEX__(x)))
#define B16(x_msb,x_lsb) (((unsigned int)B8(x_msb)<<8) + B8(x_lsb))
#define B32(x_msb,x_2,x_3,x_lsb) (((unsigned long)B8(x_msb)<<24) + ((unsigned long)B8(x_2)<<16) + ((unsigned long)B8(x_3)<<8) + B8(x_lsb))
使用简例:
unsigned char a;
unsigned int b;
unsigned long c;
P0 = B8(11110000); //相当于 P0 = 0xF0;
a = B8(11000000); //相当于 a = 0xC0;
b = B16(11000000,11000000); //相当于 b = 0xC0C0;
c = B32(11000000,11000000,11000000,11000000); //相当于 c = 0xC0C0C0C0;
一周热门 更多>