[输入]:
地址:0x001c,16位宽。
重置:0x0
这是PIN 16..31的位域寄存器。
[问题]:如何选择PIN 17?
[我的解决方案]:这样做是正确的方法:
def select_pin(pin):
lowstate = 0x0000
highstate = 0x001c
pin_hex = int(str(pin), 16)
responsive = highstate-pin_hex
inverted = hex(responsive ^ 0xFFFF)
print(inverted)
select_pin(17)
老实说,我在这方面有理论上的空白,我什至不知道如何提出问题以在Google中找到有关此方面的信息,我们将不胜感激。
假设(寄存器)值位表示引脚值,其中每个引脚号表示LSB-> MSB(从右到左)中的bit索引,所有您需要做的是以下之间的简单bitwise and:
提取您感兴趣的bit
>>> reg = 0x001C0000 # Hi Word, Lo Word >>> >>> reg_bin_repr = "{0:032b}".format(0x001C0000) # For visualization purposes only >>> reg_bin_repr '00000000000111000000000000000000' >>> >>> for idx, val in enumerate(reversed(reg_bin_repr)): # Each bit with its value ... print("Bit {0:02d}: {1:s}".format(idx, val)) ... Bit 00: 0 Bit 01: 0 Bit 02: 0 Bit 03: 0 Bit 04: 0 Bit 05: 0 Bit 06: 0 Bit 07: 0 Bit 08: 0 Bit 09: 0 Bit 10: 0 Bit 11: 0 Bit 12: 0 Bit 13: 0 Bit 14: 0 Bit 15: 0 Bit 16: 0 Bit 17: 0 Bit 18: 1 Bit 19: 1 Bit 20: 1 Bit 21: 0 Bit 22: 0 Bit 23: 0 Bit 24: 0 Bit 25: 0 Bit 26: 0 Bit 27: 0 Bit 28: 0 Bit 29: 0 Bit 30: 0 Bit 31: 0 >>> >>> # And the function >>> def pin_value(register_value, pin_number): ... return 1 if register_value & (1 << pin_number) else 0 ... >>> >>> pin_value(reg, 17) 0 >>> pin_value(reg, 18) 1
位掩码只是整数符号中的2**pin
。