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