3 State Machine Example: DHT22 Temperature and Humidity Sensor State Machine
- The controller pulls the data line low for 20 ms to initiate communication with the sensor and then releases the line.
- After 20 μs, the sensor pulls the data line low for 50 μs and then releases the data line.
- After another 50 μs, the sensor starts transmitting data.
The sensor transmits each bit by pulling the data line low for 50 μs and then releasing it. To
transmit a ‘0
’ the sensor releases the line for 28 μs before
transmitting the next bit. To transmit a ‘1
’, the sensor releases the
line for 70 μs before transmitting the next bit. Figure 3-1
illustrates the transmission process.
State | Output | Description | Binary Encoding |
---|---|---|---|
IDLE | Data line is released | Nothing is happening on the bus and the system is ready to begin a transaction | 000 |
START | Data line is pulled low by controller | The controller is initiating communication with the DHT22 sensor by pulling the data low for 20 ms | 001 |
W1 | Data line = 1 | Data line is released by controller, waiting to be pulled low by the DHT22 | 010 |
W2 | Data line = 0 | DHT22 is pulling the data line low for 80 μs before starting the transmission | 011 |
W3 | Data line = 1 | The data line is released by the DHT22 for 80 μs before transmission begins | 100 |
RX | Data line controlled by DHT22 | DHT22 is transmitting data bits | 101 |
Label | Source | Description |
---|---|---|
S | TU16B | This signal is driven by the internal universal timer. It is a 20 ms low pulse on the data line. This pulse acts as a start signal for the DHT22 protocol. |
D | I/O RD3 | This is the physical pin driven by the MCU to start communication and is subsequently driven by the sensor to transmit data |
F | TU16A | This signal will go high when 40 bits of sensor data have been clocked |
X2,X1,X0 | SRPORT RW0, RW1, RW2 | The state bits of the state machine |
Label | Source | Description |
---|---|---|
OUT | Output of SRPORT-based hardware state machine. Routed internally to SPI. | The output follows the DHT22 data line when it is transmitting
the data bits, otherwise it is ‘0' |
Figure 3-2 illustrates the state diagram for the DHT22 state machine
implemented as a Mealy Machine (Class 4 state machine). Inputs that are equal to
x
are “don’t care” conditions.
To start the transmission, Universal Timer TU16B is used in one-shot mode to generate a 20 ms
low pulse on the DHT22 data line. Once the line is released, the state machine will look
for a pattern of high, low, high, low to detect when the DHT22 begins transmitting data.
Once the state machine enters the RX state, the SPI is used in conjunction with Timer 2
and one CLC to read each bit. The CLC is used to produce a version of the DHT22 signal
without the start and wait sections. This signal can then be routed internally to the
data line of the SPI. Timer 2 can use this signal to produce a clock signal for the SPI
that will allow the SPI to clock in either a ‘1
’ or
‘0
’. TU16A counts the number of bits transmitted by the DHT22 and
generate the F signal once 40 bits have been detected. Figure 3-3
shows a timing diagram illustrating a complete transmission process.
A state transition table is made to implement the state machine based on the description above.
Another table is made to define the output logic for the OUT signal.
Now that the logic has been defined, each equation can be implemented using CLCs.
Once the state machine is configured, all that is needed to perform a transaction is to start the TU16B. When the state machine enters the RX state, the DMA is triggered by the SPI receive interrupt to move DHT22 data bytes into RAM. After all five bytes are received, the DMA message complete interrupt will notify the firmware that data is ready, and firmware can process the data accordingly.