1 | // Serial port demo program
|
2 | //
|
3 | // Assumptions: 50Mhz clock rate
|
4 |
|
5 | module SerDemo(input clk, output ser);
|
6 |
|
7 |
|
8 | // Start signal tells it to start sending bits
|
9 | reg start;
|
10 |
|
11 | //The bits of data to send
|
12 | reg [7:0] data;
|
13 |
|
14 | /////////////////////////////////////////////////////////////////////////////
|
15 | // Serial port clock generator
|
16 | // Generate a 9600 baud clock signal for the serial port by dividing the
|
17 | // 50Mhz clock by 5208
|
18 |
|
19 | reg [14:0] clockdiv;
|
20 |
|
21 | // Count from 0..5207 then reset back to zero
|
22 | always @(posedge clk)
|
23 | begin
|
24 | if (clockdiv == 5207)
|
25 | clockdiv <= 0;
|
26 | else
|
27 | clockdiv <= clockdiv + 1;
|
28 | end
|
29 |
|
30 | // The serclock is a short pulse each time we are reset
|
31 | wire serclock = (clockdiv == 0);
|
32 |
|
33 | /////////////////////////////////////////////////////////////////////////////
|
34 | // Serial port state machine
|
35 | // Only start the state machine when "start" is set. Only advance to the
|
36 | // next state when serclock is set.
|
37 |
|
38 | reg [3:0] state;
|
39 |
|
40 | always @(posedge clk)
|
41 | begin
|
42 | case (state)
|
43 | 4'b0000: if (start) state <= 4'b0001;
|
44 | 4'b0001: if (serclock) state <= 4'b0010; // Start bit
|
45 | 4'b0010: if (serclock) state <= 4'b0011; // Bit 0
|
46 | 4'b0011: if (serclock) state <= 4'b0100; // Bit 1
|
47 | 4'b0100: if (serclock) state <= 4'b0101; // Bit 2
|
48 | 4'b0101: if (serclock) state <= 4'b0110; // Bit 3
|
49 | 4'b0110: if (serclock) state <= 4'b0111; // Bit 4
|
50 | 4'b0111: if (serclock) state <= 4'b1000; // Bit 5
|
51 | 4'b1000: if (serclock) state <= 4'b1001; // Bit 6
|
52 | 4'b1001: if (serclock) state <= 4'b1010; // Bit 7
|
53 | 4'b1010: if (serclock) state <= 4'b0000; // Stop bit
|
54 | default: state <= 4'b0000; // Undefined, skip to stop
|
55 | endcase
|
56 | end
|
57 |
|
58 |
|
59 | ///////////////////////////////////////////////////////////////////////////////
|
60 | // Serial port data
|
61 | // Ensure that the serial port has the correct data on it in each state
|
62 |
|
63 | reg outbit;
|
64 |
|
65 | always @(posedge clk)
|
66 | begin
|
67 | case (state)
|
68 | 4'b0000: outbit <= 1; // idle
|
69 | 4'b0001: outbit <= 0; // Start bit
|
70 | 4'b0010: outbit <= data[0]; // Bit 0
|
71 | 4'b0011: outbit <= data[1]; // Bit 1
|
72 | 4'b0100: outbit <= data[2]; // Bit 2
|
73 | 4'b0101: outbit <= data[3]; // Bit 3
|
74 | 4'b0110: outbit <= data[4]; // Bit 4
|
75 | 4'b0111: outbit <= data[5]; // Bit 5
|
76 | 4'b1000: outbit <= data[6]; // Bit 6
|
77 | 4'b1001: outbit <= data[7]; // Bit 7
|
78 | 4'b1010: outbit <= 0; // Stop bit
|
79 | default: outbit <= 1; // Bad state output idle
|
80 | endcase
|
81 | end
|
82 |
|
83 | // Output register to pin
|
84 | assign ser = outbit;
|
85 |
|
86 | ///////////////////////////////////////////////////////////////////////////////
|
87 | // Test by outputting a letter 'd'
|
88 |
|
89 | always @(posedge clk)
|
90 | begin
|
91 | data = 100;
|
92 | start = 1;
|
93 | end
|
94 |
|
95 | endmodule
|