The Signal Routing (SR) Port, is a peripheral that allows for flexible connections of other PIC® microcontroller peripherals without the need for dedicating Input/Output (I/O) pins and routing wires. The SR Port can be read and written from software just like a normal I/O port, and each pin in the port can optionally be configured to be clocked, allowing each pin to act as a flip-flop. The unique features of the SR Port make it possible to build state machines that can run without software intervention, which can greatly enhance the performance of the system. This code example demonstrates how the SR Port can be used together with other Core Independent Peripherals (CIP)s on PIC® microcontrollers to implement a state machine for communicating with a DHT22 sensor with minimal software intervention.
- MPLAB Code Configurator
- PIC18-Q24 Family Product Page
- Signal Routing (SR) Ports
- Using the Signal Routing Port Module on 8-bit PIC® Microcontrollers
- MPLABX® X IDE v6.20 or newer
- MPLABX® XC8 compiler v2.46 or newer
- MPLABX® Melody Library v2.8.0 or newer
- Microchip PIC18F-Q Series Device Support (DFP) v1.25.433 or newer
Finite State Machines (FSM)s are an important concept in the design of digital systems. A traditional FSM consists of three main components:
- Memory: The memory of the state machine hold the current state of the system
- Next State Logic: The Next State Logic dictates how the system will transistion from one state to the next based on the current state and the input
- Output Logic: The output logic controls the outputs of the system based on the current state in the case of a Moore Machine, or a combination of inputs and the current state, in the case of a Mealy Machine
When building embedded systems, FSMs are typically implemented in software. Using the SR PORT, Configurable Logic Cells (CLC)s, and other peripherals present on PIC microcontrollers, it is possible to build functional state machines that can operate with little or no CPU intervention. Implementing state machines this way frees up the CPU to perfrom other tasks, which can greatly improve the peformance of a system. The figure below illustrates how state machines can be implemented using the SR PORT and CLCs:
The DHT22 Sensor is a temperature/humidity sensor that uses a custom single wire protocol for transmitting data. The sensor transmits 40 bits of data: two bytes for the temperature data, two bytes for the humidity data, and one checksum byte. Getting data from the sensor consists of three steps:
- The controller pulls the data line low for 20 milliseconds to intiate communication with the sensor and then releases the line.
- After 20 microseconds, the sensor pulls the data line low for 50 microseconds and then releases the data line.
- After another 50 microseconds, the sensor starts transmitting data.
The sensor transmits each bit by pulling the data line low for 50 microseconds and then releasing the line. To transmit a `'0'` the sensor releases the line for 28 microseconds before transmitting the next bit. To transmit a `'1'`, the sensor releases the line for 70 microseconds before transmitting the next bit. The waveform below illustrates the transmission process:
Using the SRPORT and CIPs, getting data from the DHT22 sensor can be done entirely in hardware with no software intervention. The first step to designing a state machine to read the DHT22 sensor is to define the necessary system states. This state machine will have six states, each of which can be encoded using a three bit number:
DHT22 System States
State | Output | Binary Encoding |
---|---|---|
IDLE | Data line is released. No transaction on bus. | 000 |
START | Data line is pulled low by microcontroller | 001 |
W1 | Data line = 1 | 010 |
W2 | Data line = 0 | 011 |
W3 | Data line = 1 | 100 |
RX | DHT22 is transmitting data bits | 101 |
The state machine will have three inputs:
Input Label | Source | Description |
---|---|---|
U | Universal Timer B | Generates a 20 ms low pulse to start transmission |
D | I/O PORTD3 | The physical bi-directional data line |
F | Universal Timer A | Signals the end of the 40 bit transmission state |
The transitions between each state can be described using a state transistion diagram:
To start the transmission, 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 will begin to transmit 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. Using the SR PORT, 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’
. Universal Timer A is used to count the number of bits transmitted by the DHT22 and generate the F signal once 40 bits have been detected. A timing diagram illustrating a complete transmission process is shown below:
A state transistion table can be used to describe the state transistions based on the binary encoding of each of the states. The three-bit encoded state value will be labeled S2S1S0.
Once the state transitions have been determined, a Sum-of-Products (SOP) Boolean expression can be written for each of the next state bits. The terms of the equations can be grouped to maximize the use of each CLC. The logic for S0, S1, and S2 will be implemented by CLC1, CLC2, and CLC3, respectively.
Since S2 and S0 each have a common term, this 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 S1, CLC5, and CLC6 if they are written as Product-of-Sums (POS) equations rather than SOP equations.
Once the state machine logic has been defined, the system can be implemented using MPLAB Melody Code Configurator. Melody is a tool built in to MPLAB X that makes it easy to generate code for any of the on-chip peripherals present on PIC microcontrollers. Using Melody it is easy to implement the hardware state machine with just a few clicks. The following images show the configuration of the CLCs and SR PORT:
CLC1
CLC2
CLC3
CLC4
CLC5
CLC6
CLC7
CLC8
SR PORT
Once the peripherals are configured and code has been generated, all that is needed to start a transmission is to start Timer Counter B. Once Timer Counter B is started, the state machine in combination with the DMA and SPI modules will handle the reception of five bytes from the DHT22 sensor. An interrupt from the DMA alerts the application once all five bytes have been transferred. After the five bytes have been received, a checksum is performed in software to validate the data. If the checksum passes, the temperature and humidity readings are sent to the PC using one of the on-chip UARTs of the MCU and the Virtual Serial Port of the Curiosity HPC board.
Pin Mapping
Signal | Pin |
---|---|
DHT22 | RD3 |
UART Tx | RB0 |
Serial Port Output
The SR PORT allows for advanced interconnectivity between CIPs on PIC microcontrollers. It also has the ability to individually configure each bit as a flip-flop, which when enabled allows the SR PORT to act as a memory element in a hardware state machine. Using the SR PORT in combination with the CLCs makes it possible to build core-independent state machinces that can offload tasks from the CPU and greatly increase system performance. This code example shows how the SR PORT can be used to seamlessly connect the on-chip peripherals of PIC microcontrollers and use the flip-flop feature of the SR PORT to implement a hardware state machine that is useful in a real-world scenario.