最近学习 51 单片机,写代码的时候感觉用二进制的形式更直观。就是每次都需要宏定义,太麻烦。干脆把所有的8位二进制数字全部用宏定义写出来,放进头文件,下次使用直接包含头文件就行。
软件环境:win10 专业版 + Keil C51
创建 BIN51.H 文件, 使用 GB2312 编码, 文件内容如下:
/*--------------------------------------------------------------------------
BIN51.H
使用说明
把 BIN51.H 放入 Keil 的目录, 例: C:\Keil\C51\INC
随后,可以直接在原文件中直接写二进制数字,例如:
#include
#include
void main(void){
P0 = B_1110_0001;
while(1);
}
--------------------------------------------------------------------------*/
#ifndef __BIN51_H__
#define __BIN51_H__
#define B_0000_0000 0X00
#define B_0000_0001 0X01
#define B_0000_0010 0X02
#define B_0000_0011 0X03
#define B_0000_0100 0X04
#define B_0000_0101 0X05
#define B_0000_0110 0X06
#define B_0000_0111 0X07
#define B_0000_1000 0X08
#define B_0000_1001 0X09
#define B_0000_1010 0X0A
#define B_0000_1011 0X0B
#define B_0000_1100 0X0C
#define B_0000_1101 0X0D
#define B_0000_1110 0X0E
#define B_0000_1111 0X0F
#define B_0001_0000 0X10
#define B_0001_0001 0X11
#define B_0001_0010 0X12
#define B_0001_0011 0X13
#define B_0001_0100 0X14
#define B_0001_0101 0X15
#define B_0001_0110 0X16
#define B_0001_0111 0X17
#define B_0001_1000 0X18
#define B_0001_1001 0X19
#define B_0001_1010 0X1A
#define B_0001_1011 0X1B
#define B_0001_1100 0X1C
#define B_0001_1101 0X1D
#define B_0001_1110 0X1E
#define B_0001_1111 0X1F
#define B_0010_0000 0X20
#define B_0010_0001 0X21
#define B_0010_0010 0X22
#define B_0010_0011 0X23
#define B_0010_0100 0X24
#define B_0010_0101 0X25
#define B_0010_0110 0X26
#define B_0010_0111 0X27
#define B_0010_1000 0X28
#define B_0010_1001 0X29
#define B_0010_1010 0X2A
#define B_0010_1011 0X2B
#define B_0010_1100 0X2C
#define B_0010_1101 0X2D
#define B_0010_1110 0X2E
#define B_0010_1111 0X2F
#define B_0011_0000 0X30
#define B_0011_0001 0X31
#define B_0011_0010 0X32
#define B_0011_0011 0X33
#define B_0011_0100 0X34
#define B_0011_0101 0X35
#define B_0011_0110 0X36
#define B_0011_0111 0X37
#define B_0011_1000 0X38
#define B_0011_1001 0X39
#define B_0011_1010 0X3A
#define B_0011_1011 0X3B
#define B_0011_1100 0X3C
#define B_0011_1101 0X3D
#define B_0011_1110 0X3E
#define B_0011_1111 0X3F
#define B_0100_0000 0X40
#define B_0100_0001 0X41
#define B_0100_0010 0X42
#define B_0100_0011 0X43
#define B_0100_0100 0X44
#define B_0100_0101 0X45
#define B_0100_0110 0X46
#define B_0100_0111 0X47
#define B_0100_1000 0X48
#define B_0100_1001 0X49
#define B_0100_1010 0X4A
#define B_0100_1011 0X4B
#define B_0100_1100 0X4C
#define B_0100_1101 0X4D
#define B_0100_1110 0X4E
#define B_0100_1111 0X4F
#define B_0101_0000 0X50
#define B_0101_0001 0X51
#define B_0101_0010 0X52
#define B_0101_0011 0X53
#define B_0101_0100 0X54
#define B_0101_0101 0X55
#define B_0101_0110 0X56
#define B_0101_0111 0X57
#define B_0101_1000 0X58
#define B_0101_1001 0X59
#define B_0101_1010 0X5A
#define B_0101_1011 0X5B
#define B_0101_1100 0X5C
#define B_0101_1101 0X5D
#define B_0101_1110 0X5E
#define B_0101_1111 0X5F
#define B_0110_0000 0X60
#define B_0110_0001 0X61
#define B_0110_0010 0X62
#define B_0110_0011 0X63
#define B_0110_0100 0X64
#define B_0110_0101 0X65
#define B_0110_0110 0X66
#define B_0110_0111 0X67
#define B_0110_1000 0X68
#define B_0110_1001 0X69
#define B_0110_1010 0X6A
#define B_0110_1011 0X6B
#define B_0110_1100 0X6C
#define B_0110_1101 0X6D
#define B_0110_1110 0X6E
#define B_0110_1111 0X6F
#define B_0111_0000 0X70
#define B_0111_0001 0X71
#define B_0111_0010 0X72
#define B_0111_0011 0X73
#define B_0111_0100 0X74
#define B_0111_0101 0X75
#define B_0111_0110 0X76
#define B_0111_0111 0X77
#define B_0111_1000 0X78
#define B_0111_1001 0X79
#define B_0111_1010 0X7A
#define B_0111_1011 0X7B
#define B_0111_1100 0X7C
#define B_0111_1101 0X7D
#define B_0111_1110 0X7E
#define B_0111_1111 0X7F
#define B_1000_0000 0X80
#define B_1000_0001 0X81
#define B_1000_0010 0X82
#define B_1000_0011 0X83
#define B_1000_0100 0X84
#define B_1000_0101 0X85
#define B_1000_0110 0X86
#define B_1000_0111 0X87
#define B_1000_1000 0X88
#define B_1000_1001 0X89
#define B_1000_1010 0X8A
#define B_1000_1011 0X8B
#define B_1000_1100 0X8C
#define B_1000_1101 0X8D
#define B_1000_1110 0X8E
#define B_1000_1111 0X8F
#define B_1001_0000 0X90
#define B_1001_0001 0X91
#define B_1001_0010 0X92
#define B_1001_0011 0X93
#define B_1001_0100 0X94
#define B_1001_0101 0X95
#define B_1001_0110 0X96
#define B_1001_0111 0X97
#define B_1001_1000 0X98
#define B_1001_1001 0X99
#define B_1001_1010 0X9A
#define B_1001_1011 0X9B
#define B_1001_1100 0X9C
#define B_1001_1101 0X9D
#define B_1001_1110 0X9E
#define B_1001_1111 0X9F
#define B_1010_0000 0XA0
#define B_1010_0001 0XA1
#define B_1010_0010 0XA2
#define B_1010_0011 0XA3
#define B_1010_0100 0XA4
#define B_1010_0101 0XA5
#define B_1010_0110 0XA6
#define B_1010_0111 0XA7
#define B_1010_1000 0XA8
#define B_1010_1001 0XA9
#define B_1010_1010 0XAA
#define B_1010_1011 0XAB
#define B_1010_1100 0XAC
#define B_1010_1101 0XAD
#define B_1010_1110 0XAE
#define B_1010_1111 0XAF
#define B_1011_0000 0XB0
#define B_1011_0001 0XB1
#define B_1011_0010 0XB2
#define B_1011_0011 0XB3
#define B_1011_0100 0XB4
#define B_1011_0101 0XB5
#define B_1011_0110 0XB6
#define B_1011_0111 0XB7
#define B_1011_1000 0XB8
#define B_1011_1001 0XB9
#define B_1011_1010 0XBA
#define B_1011_1011 0XBB
#define B_1011_1100 0XBC
#define B_1011_1101 0XBD
#define B_1011_1110 0XBE
#define B_1011_1111 0XBF
#define B_1100_0000 0XC0
#define B_1100_0001 0XC1
#define B_1100_0010 0XC2
#define B_1100_0011 0XC3
#define B_1100_0100 0XC4
#define B_1100_0101 0XC5
#define B_1100_0110 0XC6
#define B_1100_0111 0XC7
#define B_1100_1000 0XC8
#define B_1100_1001 0XC9
#define B_1100_1010 0XCA
#define B_1100_1011 0XCB
#define B_1100_1100 0XCC
#define B_1100_1101 0XCD
#define B_1100_1110 0XCE
#define B_1100_1111 0XCF
#define B_1101_0000 0XD0
#define B_1101_0001 0XD1
#define B_1101_0010 0XD2
#define B_1101_0011 0XD3
#define B_1101_0100 0XD4
#define B_1101_0101 0XD5
#define B_1101_0110 0XD6
#define B_1101_0111 0XD7
#define B_1101_1000 0XD8
#define B_1101_1001 0XD9
#define B_1101_1010 0XDA
#define B_1101_1011 0XDB
#define B_1101_1100 0XDC
#define B_1101_1101 0XDD
#define B_1101_1110 0XDE
#define B_1101_1111 0XDF
#define B_1110_0000 0XE0
#define B_1110_0001 0XE1
#define B_1110_0010 0XE2
#define B_1110_0011 0XE3
#define B_1110_0100 0XE4
#define B_1110_0101 0XE5
#define B_1110_0110 0XE6
#define B_1110_0111 0XE7
#define B_1110_1000 0XE8
#define B_1110_1001 0XE9
#define B_1110_1010 0XEA
#define B_1110_1011 0XEB
#define B_1110_1100 0XEC
#define B_1110_1101 0XED
#define B_1110_1110 0XEE
#define B_1110_1111 0XEF
#define B_1111_0000 0XF0
#define B_1111_0001 0XF1
#define B_1111_0010 0XF2
#define B_1111_0011 0XF3
#define B_1111_0100 0XF4
#define B_1111_0101 0XF5
#define B_1111_0110 0XF6
#define B_1111_0111 0XF7
#define B_1111_1000 0XF8
#define B_1111_1001 0XF9
#define B_1111_1010 0XFA
#define B_1111_1011 0XFB
#define B_1111_1100 0XFC
#define B_1111_1101 0XFD
#define B_1111_1110 0XFE
#define B_1111_1111 0XFF
#endif
以上就是 BIN51.H 头文件的全部内容.
找到 Keil 的安装目录,例如: D:\Program Files (x86)\Keil ,将 BIN51.H 复制到
D:\Program Files (x86)\Keil\C51\INC 目录中.
测试代码如下:
#include
#include
void main(void)
{
P1 = B_1111_0000;
while(1);
}
根据我的开发版上的接线,显示的效果应该是 LED18 ~ LED15 亮灯,其余灭.
如果在你的开发板子 LED 显示效果不一样,可以调整杜邦线的线序,或者把P1 = B_1111_0000; 改成 P1 = B_0000_1111; 再测试.