3 State Machine Example: DHT22 Temperature and Humidity Sensor State Machine

An additional example of a hardware state machine that can be implemented using SR Port and CIPs is a DHT22 temperature and humidity sensor driver. The DHT22 is a temperature/humidity sensor that uses a custom open-drain single-wire protocol for data transmission. Getting data from the sensor consists of three steps:
  1. The controller pulls the data line low for 20 ms to initiate communication with the sensor and then releases the line.
  2. After 20 μs, the sensor pulls the data line low for 50 μs and then releases the data line.
  3. 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.

Figure 3-1. DHT22 Waveform
The sensor transmits 40 bits of data: two bytes for the temperature data, two bytes for the humidity data, and one checksum byte. Using the SR Port and CIPs, reading the sensor data can be done entirely in hardware. For this state machine, there will be six states.
Table 3-1. DHT22 State Machine State Table
StateOutputDescriptionBinary Encoding
IDLEData line is releasedNothing is happening on the bus and the system is ready to begin a transaction000
STARTData line is pulled low by controllerThe controller is initiating communication with the DHT22 sensor by pulling the data low for 20 ms001
W1Data line = 1Data line is released by controller, waiting to be pulled low by the DHT22010
W2Data line = 0DHT22 is pulling the data line low for 80 μs before starting the transmission011
W3Data line = 1The data line is released by the DHT22 for 80 μs before transmission begins100
RXData line controlled by DHT22DHT22 is transmitting data bits101
The inputs to the state machine include the start signal, which initially pulls the line low, the data line, and a transmission finished signal.
Table 3-2. DHT22 State Machine Input Table
LabelSourceDescription
STU16BThis 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.
DI/O RD3 This is the physical pin driven by the MCU to start communication and is subsequently driven by the sensor to transmit data
FTU16AThis signal will go high when 40 bits of sensor data have been clocked
X2,X1,X0SRPORT RW0, RW1, RW2The state bits of the state machine
The output of the system will be the DHT22 waveform without the start pulses. This output can be routed to the input of the on-chip Serial Peripheral Interface (SPI) for automatic decoding of the DHT22 data.
Table 3-3. DHT22 State Machine Output Table
LabelSourceDescription
OUTOutput 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.

Figure 3-2. DHT22 State Diagram

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.

Figure 3-3. DHT22 Timing Diagram

A state transition table is made to implement the state machine based on the description above.

Figure 3-4. DHT22 State Transition Table

Another table is made to define the output logic for the OUT signal.

Figure 3-5. DHT22 Output Logic Table
Now that the state transition table has been defined, an SOP expression for each of the state bits is created. Terms of each equation can be grouped to maximize the use of each CLC. The logic for X0, X1, and X2 will be implemented by CLC1, CLC2, and CLC3, respectively.
Equation 3-1. SOP Expressions for Next-State Logic
X0=S¯ X¯2X¯1X¯0CLC4+D¯ X¯2X¯1X0+D¯ X¯2X1X¯0+D¯ X¯2X1X0+D¯ X2X¯1X¯0CLC5+F¯ X2X¯1X0CLC6
X1=D X¯2X¯1X0+D X¯2X1X¯0+D¯ X¯2X1X¯0+D¯ X¯2X1X0
X2=D X¯2X1X0+D X2X¯1X¯0+D¯ X2X¯1X¯0CLC7+F¯ X2X¯1X0CLC6
Since X0 and X2 share a common term, this common term can be implemented using one CLC to reduce the number of CLCs needed. Because each CLC has a 4-input AND configuration, it will be easier to implement the equations for X1, CLC5, and CLC6 if they are written as POS equations rather than SOP equations.
Equation 3-2. POS Expressions for Next-State Logic
X1=(D¯+X2+X1+X¯0)(D¯+X2+X¯1+X0)(D+X2+X¯1+X0)(D+X2+X¯1+X¯0)¯
CLC5=(D+X2+X1+X¯0)(D+X2+X¯1+X0)(D+X2+X¯1+X¯0)(D+X¯2+X1+X0)¯
CLC7 =(D¯+X2+X¯1+X¯0)(D¯+X¯2+X1+X0)(D+X¯2+X1+X0)¯

Now that the logic has been defined, each equation can be implemented using CLCs.

Figure 3-6. DHT22 Logic Implementation

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.