4.3.3 Using Bits to Read and Control Devices
A computer-controlled system can represent several device states within one byte. Input bits can report conditions detected by sensors, while output bits can determine whether actuators are switched on or off. Bit masks allow the program to inspect or change one position without disturbing the others.
By the end of this section, you should be able to:
- Explain how one byte can represent several sensor or actuator states.
- Distinguish an input/status byte from an output/control byte.
- Use AND masks to test whether an input flag is set.
- Use OR masks to switch selected output bits on.
- Use AND masks to switch selected output bits off while preserving the rest.
- Trace a load–mask–compare–update–store sequence.
- Explain how repeated sensor readings provide feedback to a control program.
- Design an original bit allocation and masks for a simple device-control scenario.
Where This Page Fits
| Related content | Role in this page |
|---|---|
| Monitoring and control systems | Provides the sensor–controller–actuator–feedback context. |
| 4.3.1 Binary Shifts and Their Effects | Explains how bits move within a register. |
| 4.3.2 Bitwise Logic and Masks | Introduces AND, OR, XOR, test, set, clear and toggle. |
| 4.3.3 Using Bits to Read and Control Devices | Applies those operations to device status and control bytes. |
Representing Device States in Bytes
A device interface can expose an 8-bit status value to the processor. Each bit has an agreed meaning. A second 8-bit value can be used to send control decisions to actuators.
Input/status byte
Bits are read by the program.
00010110
Sensor conditions currently reported
Output/control byte
Bits are written by the program.
00001001
Actuators currently requested
Original Example: Aquaculture Tank Controller
The example system uses one byte for sensor states and one byte for actuator commands. Bit 0 is always the rightmost bit.
Input byte: SENSOR_STATE
| Bit | Meaning when 1 | Single-bit test mask |
|---|---|---|
| 4 | Service hatch is open | B00010000 |
| 3 | Filter pressure is too high | B00001000 |
| 2 | Water level is too low | B00000100 |
| 1 | Dissolved oxygen is too low | B00000010 |
| 0 | Water temperature is too high | B00000001 |
Output byte: CONTROL_STATE
| Bit | Meaning when 1 | Single-bit set mask |
|---|---|---|
| 4 | Maintenance lock enabled | B00010000 |
| 3 | Warning beacon on | B00001000 |
| 2 | Cooling fan on | B00000100 |
| 1 | Refill pump on | B00000010 |
| 0 | Aerator on | B00000001 |
The Repeated Control Cycle
A control program normally repeats its work while the system is operating. The exact hardware arrangement varies, but the software logic follows a clear cycle:
The new sensor byte acts as feedback: it shows the current condition after earlier actuator commands have had time to affect the physical system.
Read a Sensor Bit with AND
Suppose SENSOR_STATE contains:
00010110
Bit 1 reports low oxygen. Isolate that position with the mask
B00000010:
00010110
AND
00000010
=
00000010
The result is non-zero, so the low-oxygen flag is set.
LDD SENSOR_STATE
AND B00000010
CMP #0
JPE OXYGEN_OK
When the result equals zero, JPE branches to
OXYGEN_OK. When it is not zero, execution continues to the code that starts
the aerator.
Switch an Actuator On with OR
The aerator is controlled by output bit 0. To make this bit 1 without changing the other
actuator states, use OR with B00000001.
00001000
OR
00000001
=
00001001
LDD CONTROL_STATE
OR B00000001
STO CONTROL_STATE
The warning beacon at bit 3 remains on, and the aerator at bit 0 is also switched on.
Switch an Actuator Off with an AND Clear Mask
When the next sensor reading shows that oxygen is no longer low, output bit 0 should be cleared. The mask contains 0 at bit 0 and 1 everywhere else:
00001001
AND
11111110
=
00001000
LDD CONTROL_STATE
AND B11111110
STO CONTROL_STATE
The aerator is switched off while the warning beacon remains unchanged.
AND B00000001 would preserve only bit 0 and
clear every other output bit. The correct clear mask uses 1s in all positions that must
remain unchanged.
Complete Sensor-to-Actuator Response
The following original fragment checks the low-oxygen input and updates the aerator output. It is written to show the decision clearly rather than to imitate a particular processor implementation.
CHECK_OXYGEN:
LDD SENSOR_STATE
AND B00000010
CMP #0
JPE OXYGEN_NORMAL
OXYGEN_LOW:
LDD CONTROL_STATE
OR B00000001
STO CONTROL_STATE
JMP CHECK_LEVEL
OXYGEN_NORMAL:
LDD CONTROL_STATE
AND B11111110
STO CONTROL_STATE
CHECK_LEVEL:
LDD SENSOR_STATE
AND B00000100
CMP #0
JPE LEVEL_NORMAL
LEVEL_LOW:
LDD CONTROL_STATE
OR B00000010
STO CONTROL_STATE
JMP READ_AGAIN
LEVEL_NORMAL:
LDD CONTROL_STATE
AND B11111101
STO CONTROL_STATE
READ_AGAIN:
JMP CHECK_OXYGEN
| Input condition | Input mask | Output action | Output mask |
|---|---|---|---|
| Oxygen low at bit 1 | B00000010 |
Set or clear aerator bit 0 | B00000001 / B11111110 |
| Water level low at bit 2 | B00000100 |
Set or clear refill-pump bit 1 | B00000010 / B11111101 |
Deterministic Control Is Usually Safer Than Toggling
XOR is useful when the intention is genuinely to reverse a state. However, a safety-related actuator should usually be set or cleared explicitly.
| Operation | Effect | Suitability for a required actuator state |
|---|---|---|
| OR with a set mask | Makes the selected bit 1. | Suitable when the output must definitely be on. |
| AND with a clear mask | Makes the selected bit 0. | Suitable when the output must definitely be off. |
| XOR with a toggle mask | Reverses the selected bit. | Use only when reversal is the intended behaviour and the current state is known. |
Interactive: Device Register Simulator
Toggle sensor conditions, choose one response, and step through the program's read–mask–decide–update sequence. The existing widget identifier and asset paths have been retained.
Common Mistakes and Misconceptions
- Counting bit positions from the left instead of placing bit 0 on the right.
- Confusing the input/status byte with the output/control byte.
- Using OR to test a sensor bit and accidentally changing ACC.
- Using a single-1 AND mask to clear an actuator bit, which clears the other outputs.
- Forgetting to reload the control byte after AND has replaced ACC with a masked sensor result.
- Forgetting to store the modified output byte back to the control location.
- Claiming that the sensor decides or acts. The sensor reports; the program decides; the actuator acts.
- Using XOR repeatedly for an output that must remain steadily on or off.
- Assuming feedback means the output command rather than a later measurement of the system.
Exam Tips
Separate input testing from output changing
First state which sensor bit is isolated. Then state which actuator bit is set or cleared.
Explain why the other bits are preserved
OR uses 0s in unaffected positions. An AND clear mask uses 1s in unaffected positions.
Trace ACC carefully
After an input mask, ACC contains the masked result—not the original sensor byte and not the output byte. Reload the correct byte before modifying an actuator command.
Practice
Interpret the registers
Use the aquaculture mappings above.
- Explain the active sensor conditions in
SENSOR_STATE = 00011101. - Explain the active actuators in
CONTROL_STATE = 00010110. - Construct a mask to test whether the service hatch is open.
- Construct a mask to switch on the cooling fan.
- Construct a mask to switch off the refill pump while preserving every other output.
Write assembly fragments
- Test bit 3 of
SENSOR_STATEand jump toFILTER_OKwhen clear. - Set warning-beacon bit 3 in
CONTROL_STATE. - Clear cooling-fan bit 2 in
CONTROL_STATE. - Test whether either oxygen-low bit 1 or water-low bit 2 is active using one mask.
Design a new device map
Create an 8-bit input map and an 8-bit output map for an automated art-storage room. Include at least three sensors and three actuators. Then write one test mask, one set mask and one clear mask.
Review
| Task | Typical operation | Essential explanation |
|---|---|---|
| Read a sensor flag | AND with a single-1 mask | Non-zero means the selected input is active. |
| Switch an actuator on | OR with a single-1 mask | The selected output becomes 1; other outputs remain unchanged. |
| Switch an actuator off | AND with a single-0 clear mask | The selected output becomes 0; other outputs remain unchanged. |
| Respond continuously | Repeat read–test–update | Later sensor readings provide feedback for the next decision. |