-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUART.v
81 lines (61 loc) · 1.39 KB
/
UART.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
module UART (output reg[7:0]outputData,output reg newData,input RXiN,input clk);
localparam pulses115200=434;
localparam pulses115200With5=651;
localparam pulses115200With52=100; //Wait after the stop bit
localparam pulses9600=5208;
localparam pulses9600With5=7812;
localparam pulses9600With52=2500; //Wait after the stop bit
reg[3:0]state;
localparam waiting=4'd0,data=4'd1;
reg[15:0]timeForNextBit;
reg[7:0]dataBitCounter;
reg[7:0]buffer;
wire RX;
assign RX=RXiN;
//SMALLDebouncer deb(clk,RXiN,RX);
always@(posedge clk)
begin
//outputData=state;
case (state)
waiting: //On each tick we look for the incoming signal
begin
if(RX==1'b0)
begin
state=data;
timeForNextBit=pulses115200With5;
dataBitCounter=0;
end
end
data:
begin
if(timeForNextBit<=0)
begin
if(dataBitCounter<=16'd7)
begin
buffer[dataBitCounter]=RX;
dataBitCounter=dataBitCounter+1;
timeForNextBit=pulses115200;
end
else if(dataBitCounter==16'd8)
begin
if(RX==1'b1) //Successful stop
begin
outputData=buffer;
newData=!newData;
end
dataBitCounter=dataBitCounter+1;
timeForNextBit=pulses115200With52;
end
else
begin
state=waiting;
end
end
else
begin
timeForNextBit=timeForNextBit-16'd1;
end
end
endcase
end
endmodule