Transmitter Design
Part of the Fundamentals section of Coddy's Verilog journey — lesson 89 of 90.
Challenge
In this lesson, you will complete the UART transmitter by adding the shift register to send any byte, not just the fixed letter 'A'.
The shift register loads the full frame (stop bit + 8 data bits + start bit) and shifts it out one bit at a time.
Frame Format
| Stop (1) | Data (8 bits) | Start (0) |
|---|---|---|
| 1 | data_in | 0 |
For example, if data_in = 8'b01000001 (letter 'A'), the shift register becomes: 1 01000001 0
Your Task
You are given the state machine from the previous lesson (transmits fixed byte). You need to modify it to send any byte from the data_in input.
What to do:
- Add an
input [7:0]calleddata_into the port list (inside the parentheses) - Add a 10-bit
regcalledshift_regoutside the parentheses (inside the module body, because it is an internal signal) - When
cnt == 0andstart == 1:- Load
shift_regwith{1'b1, data_in, 1'b0}
- Load
- When
cntis between 1 and 8:- Send
tx <= shift_reg[0] - Shift right:
shift_reg <= shift_reg >> 1
- Send
- When
cnt == 9:- Send
tx <= shift_reg[0] - Shift right:
shift_reg <= shift_reg >> 1
- Send
Try it yourself
module uart_tx (
input clk,
input start, // NEW: start signal to begin transmission
output reg tx, // NEW: serial output line
output reg [3:0] cnt // Keep as output for testing
);
initial begin
cnt = 0;
tx = 1; // NEW: set tx HIGH (idle state)
end
always @(posedge clk) begin
// NEW: Counter logic with start condition
if (cnt == 0 && start) begin // NEW: start transmitting
cnt <= 1;
end
else if (cnt > 0 && cnt < 9) begin // NEW: count while transmitting
cnt <= cnt + 1;
end
else if (cnt == 9) begin // NEW: reset after last bit
cnt <= 0;
end
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