State Machine
Part of the Fundamentals section of Coddy's Verilog journey — lesson 88 of 90.
Challenge
A state machine is a circuit that can be in one of several states. For UART, we have different states for each bit: idle, start, data bits 0-7, and stop. The bit counter (cnt) tells us which state we are in. Based on cnt, we decide what value to send on the tx line.
You have the bit counter from the previous lesson. You need to modify it to work as a UART transmitter.
Bit Values to Send (for letter 'A')
| cnt | tx value |
|---|---|
| 0 | 1 |
| 1 | 0 |
| 2 | 1 |
| 3 | 0 |
| 4 | 0 |
| 5 | 0 |
| 6 | 0 |
| 7 | 0 |
| 8 | 0 |
| 9 | 1 |
| 10 | 1 |
What to do
- Add an input called
start - Add an output reg called
tx - In the
initialblock, settx = 1(idle high) - Change the counter logic:
- When
cnt == 0andstart == 1, setcnt <= 1(start transmitting) - When
cntis between 1 and 9, increment:cnt <= cnt + 1 - When
cnt == 10, reset to0
- When
Try it yourself
module uart_tx (
input clk,
output reg [3:0] cnt
);
initial begin
cnt = 0;
end
always @(posedge clk) begin
cnt <= cnt + 1;
end
endmoduleAll lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorComparison OperatorsRecap - Simple MathBitwise Operators7Assign And Gates
Continuous AssignmentAssign With OperatorsBuilt In Gate PrimitivesAND OR NOT GatesXOR XNOR GatesRecap - Logic Gate Circuit10Decision Making
If StatementIf - ElseRecap - Simple ComparatorCase StatementCasex And CasezRecap - ALU Design5Operators Part 2
Logical OperatorsReduction OperatorsShift OperatorsConcatenation OperatorConditional OperatorRecap - Operator Challenge3Number System
Binary RepresentationSized NumbersUnsized NumbersNegative NumbersSpecial Values X And ZRecap - Number Formats6Modules
Module StructureInput And Output PortsInout PortsModule InstantiationPort Mapping By NamePort Mapping By OrderRecap - Build A Module9Procedural Blocks
Always BlockInitial BlockSensitivity ListBlocking AssignmentNon Blocking AssignmentRecap - Always vs Initial15Traffic Light Controller
Defining The StatesState Machine Logic