問題:
How do I monitor the state of a contact switch using a digital I/O board?
答案:
Figure 1
Most digital inputs on the digital I/O board come in groups of 8 lines. When these groups are read in software, the states of the 8 input lines are represented by an 8-bit decimal number from 0 to 255. This value is equal to the sum of the decimal weightings of each bit that is high. A value of 0 means that all lines are low, while a value of 255 means that all 8 lines are high. The states of individual lines can be determined by performing a bitwise AND with the value corresponding to that bit shown in Figure 2.
Figure 2
For example, if a value of 139 is read in the software and this value is ANDed with 128 (bit8), the answer would be 128, which means that bit8 is high. If 139 is also ANDed with 8 (bit4), we get 8, which means that bit4 is also high. Now, if we AND 139 with 16 (bit5), we get 0, which means that bit5 is low.
The sum of 139 corresponds to 128+8+2+1, so we can determine that bits 1, 2, 4, and 8 are all high.
此常見問答集適用於:
沒有任何產品系列
產品:
常見問答集 ID 70581
檢視所有常見問答集 »