-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutputShifter.v
96 lines (77 loc) · 1.67 KB
/
outputShifter.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
module outputShifter(input clk,output outputPin,input [7:0]datain,input statex,output done);
localparam pulses9600=5208;
localparam pulses9600With5=7812;
localparam pulses9600With52=2500; //Wait after the stop bit
localparam pulses115200=434;
localparam pulses115200With5=651;
localparam pulses115200With52=100; //Wait after the stop bit
reg[3:0]state;
localparam waiting=4'd0,data=4'd2,start=4'd1,stop=4'd3;
reg[31:0]timeForNextBit;
reg[7:0]dataBitCounter;
reg lastState;
assign debug=datain;
reg outputData;
assign outputPin=(state===waiting || state===stop)?1'b1:outputData;
assign done=(state==waiting);
always@(posedge clk)
begin
case (state)
waiting:
begin
if(lastState!=statex)
begin
lastState=statex;
state=start;
timeForNextBit=pulses115200;
dataBitCounter=0;
outputData=0;
end
end
start:
begin
outputData=0;
if(timeForNextBit<=0)
begin
state=data;
end
else
begin
timeForNextBit=timeForNextBit-32'd1;
end
end
data:
begin
if(timeForNextBit<=0)
begin
if(dataBitCounter<=16'd7)
begin
outputData=datain[dataBitCounter];
dataBitCounter=dataBitCounter+1;
timeForNextBit=pulses115200;
end
else if(dataBitCounter==16'd8)
begin
state=stop;
timeForNextBit=pulses115200;
end
end
else
begin
timeForNextBit=timeForNextBit-32'd1;
end
end
stop:
begin
if(timeForNextBit<=0)
begin
state=waiting;
end
else
begin
timeForNextBit=timeForNextBit-32'd1;
end
end
endcase
end
endmodule