Menu
Coddy logo textTech

State Machine

Part of the Fundamentals section of Coddy's Verilog journey — lesson 88 of 90.

challenge icon

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')

cnttx value
01
10
21
30
40
50
60
70
80
91
101

What to do

  1. Add an input called start
  2. Add an output reg called tx
  3. In the initial block, set tx = 1 (idle high)
  4. Change the counter logic:
    • When cnt == 0 and start == 1, set cnt <= 1 (start transmitting)
    • When cnt is between 1 and 9, increment: cnt <= cnt + 1
    • When cnt == 10, reset to 0

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

endmodule

All lessons in Fundamentals