- 收藏
- 点赞
- 分享
- 举报
fpga的uart协议问题,贴有代码
可以正常收到信息,并将收到的值减1发回。
但是led在发送接收过程中始终不为1;且在rx.v中还有一点小问题
已在代码中用“错误:xxx“标出,大佬帮忙看下。
以下是顶层模块uart.v
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 19:59:34 05/16/2020
// Design Name:
// Module Name: uart
// Project Name:
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module uart(clk,tx,rx,rst,key1,led
);
input rst,rx,clk;
output tx;
reg [7:0] datatx;
wire [7:0] datarx;
reg [7:0] data;
reg[5:0] counttest;
reg flagtx;
wire flagrx;
wire sendover;
reg readover;
input key1;
output reg led;
reg [1:0] state,nextstate;
parameter idle=0 , read=1 ,send=2,buffer=3;
tx#(.clkrate(50),.baudrate(9600)) tx1(clk,rst,datatx,flagtx,tx,sendover);
rx#(.clkrate(50),.baudrate(9600)) rx1(clk,rst,datarx,flagrx,rx,readover);
always @ (*)
begin
if(!rst)
begin
nextstate=idle;
readover=0;
end
case(state)
idle:
begin
if(flagrx==1)
begin
nextstate=read;
readover=0;
end
else
begin
nextstate=idle;
readover=0;
end
end
read:
begin
if(!flagrx)
begin
nextstate=send;
readover=0;
end
else
begin
data=datarx-1;
readover=1;
nextstate=read;
end
end
send:
begin
datatx=data;
readover=0;
if(sendover)
nextstate=idle;
else
nextstate=send;
end
default:
begin
nextstate=idle;
readover=0;
end
endcase
end
always @ (*)
begin
if(!rst)
flagtx=0;
else if (nextstate==send && state!=nextstate)
flagtx=1;
else if(state!=send)
flagtx=0;
end
always @ (posedge clk,negedge rst)
begin
if(!rst)
state<=idle;
else
state<=nextstate;
end
always @ (*)
begin
if(!rst)
led=0;
else if (state!=idle)
led=1; //问题:灯不亮
else
led=led;
end
always @(posedge clk ,negedge rst)
if(!rst)
counttest<=0;
else if (state==read)
counttest<=counttest+1;
endmodule
以下是接收模块rx.v
//以下是接收模块rx.v
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 20:18:30 05/16/2020
// Design Name:
// Module Name: rx
// Project Name:
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module rx #(parameter clkrate=50,parameter baudrate=9600
)(clk,rst,datarec,flag,rxport,readover);
input clk,rst;
output reg[7:0]datarec;
output reg flag;
input rxport;
input readover;
parameter counter_counts=clkrate*1000000/baudrate;
parameter idle=0 , start=1 , receive=2 , stop=3;
reg[1:0]state;
reg[1:0]nextstate;
reg [20:0] counter;
reg [2:0] rec_bit;
always @ (*)
begin
if(!rst)
nextstate=idle;
case(state)
idle:
begin
if(rxport==0)
nextstate=start;
else
nextstate=idle;
end
start:
begin
if(counter>=counter_counts-1)
nextstate=receive;
else
nextstate=start;
end
receive:
begin
if(rec_bit==7 && counter>=counter_counts-1)
nextstate=stop;
else
nextstate=receive;
end
stop:
begin
if(counter>=counter_counts-1)
nextstate=idle;
else
nextstate=stop;
end
default:
nextstate=idle;
endcase
end
always @ (posedge clk,negedge rst)
begin
if(!rst)
state<=idle;
else
state<=nextstate;
end
always @ (posedge clk,negedge rst)
begin
if(!rst)
counter<=0;
else if (nextstate!=state || (state==receive && counter>=counter_counts-1))
counter<=0;
else
counter<=counter+1;
end
always @ (posedge clk,negedge rst)
begin
if(!rst)
datarec=0;
else if(state==receive && counter==counter_counts>>1) //问题:改成counter>5 && counter<counter_counts-5则读不出来数据,读取乱码
begin
if (rec_bit==0)
datarec[0]=rxport;
else if(rec_bit==1)
datarec[1]=rxport;
else if(rec_bit==2)
datarec[2]=rxport;
else if(rec_bit==3)
datarec[3]=rxport;
else if(rec_bit==4)
datarec[4]=rxport;
else if(rec_bit==5)
datarec[5]=rxport;
else if(rec_bit==6)
datarec[6]=rxport;
else if(rec_bit==7)
datarec[7]=rxport;
end
end
always @ (posedge clk,negedge rst)
begin
if(!rst)
rec_bit<=0;
else if(state!=receive)
rec_bit<=0;
else if (counter>=counter_counts-1)
rec_bit<=rec_bit+1;
else
rec_bit<=rec_bit;
end
always @ (*)
begin
if(!rst)
flag=0;
else if(state==stop && counter==counter_counts-1)
flag=1;
else if (readover )
flag=0;
end
endmodule
以下是发送模块tx.v
\以下是发送模块tx.v
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 20:18:21 05/16/2020
// Design Name:
// Module Name: tx
// Project Name:
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module tx #(parameter clkrate=50,parameter baudrate=9600
)
(clk,rst,datat,flag,txport,sendover);
input clk,rst;
input [7:0] datat;
output reg txport;
input flag;
output reg sendover;
reg [1:0] state;
reg [1:0] nextstate;
reg [20:0] counter;
reg [2:0] sending_bit;
parameter idle=2’d0;
parameter start=2’d1;//qihiwei
parameter send=2’d2;//shuju
parameter stop=2’d3;//jieshuwei
parameter counter_counts=clkrate*1000000/baudrate;
always @ (posedge clk,negedge rst)
begin
if(!rst)
state<=idle;
//flag=’b0;
else
state<=nextstate;
end
always @ (*)
begin
case (state)
idle:
begin
if(!rst)
nextstate=idle;
else if(flag)
nextstate=start;
else
nextstate=idle;
end
start:
begin
if (counter>=counter_counts-1)
nextstate=send;
else
nextstate=start;
end
send:
begin
if (counter>=counter_counts-1 && sending_bit==7)
nextstate=stop;
else
nextstate=send;
end
stop:
begin
if (counter>=counter_counts-1)
nextstate=idle;
else
nextstate=stop;
end
endcase
end
always @ (posedge clk,negedge rst)
begin
if(!rst)
counter<=0;
else if (nextstate!=state || (state==send && counter>=counter_counts-1))
counter<=0;
else
counter<=counter+1;
end
always @ (posedge clk or negedge rst)
begin
if(!rst)
sending_bit=0;
else if(state!=send)
sending_bit=0;
else if (counter>=counter_counts-1)
sending_bit=sending_bit+1;
else
sending_bit=sending_bit;
end
always @ (posedge clk or negedge rst)
begin
if(!rst)
txport<=1;
else
case(state)
idle,stop:
txport<=1;
start:
txport<=0;
send:
begin
if (sending_bit==0)
txport<=datat[0];
else if (sending_bit==1)
txport<=datat[1];
else if (sending_bit==2)
txport<=datat[2];
else if (sending_bit==3)
txport<=datat[3];
else if (sending_bit==4)
txport<=datat[4];
else if (sending_bit==5)
txport<=datat[5];
else if (sending_bit==6)
txport<=datat[6];
else if (sending_bit==7)
txport<=datat[7];
end
endcase
end
always @ (*)
begin
if(!rst)
sendover=0;
else if (state==stop && counter==counter_counts-1)
sendover=1;
else if(state== idle && flag==1)
sendover=0;
else if (state== idle && flag==0)
sendover=1;
end
endmodule
Markdown 语法
- 加粗**内容**
- 斜体*内容*
- 删除线~~内容~~
- 引用> 引用内容
- 代码`代码`
- 代码块```编程语言↵代码```
- 链接[链接标题](url)
- 无序列表- 内容
- 有序列表1. 内容
- 缩进内容
- 图片![alt](url)
-
2016-01-12 14:03:53
-
2019-01-18 15:48:46
-
2019-11-15 14:53:22
-
2017-10-17 08:26:33
-
2008-10-02 20:14:02
-
2013-11-20 19:02:17
-
2019-11-13 09:32:18
-
2015-11-04 15:39:07
-
2020-04-25 10:49:27
-
2016-07-20 13:54:05
-
2021-01-06 13:51:27
-
2016-09-01 14:09:58
-
2016-10-27 17:50:12
-
2023-10-24 11:28:14
-
2016-08-16 15:11:59
-
12013-03-22 09:42:41
-
2016-09-14 14:39:53
-
2016-06-21 16:13:19
-
2015-06-09 18:54:38
-
5Hi3516CV610 如何使用SD卡升级固件
-
5cat /dev/logmpp 报错 <3>[ vi] [func]:vi_send_frame_node [line]:99 [info]:vi pic queue is full!
-
50如何获取vpss chn的图像修改后发送至vo
-
5FPGA通过Bt1120传YUV422数据过来,vi接收不到数据——3516dv500
-
50SS928 运行PQtools 拼接 推到设备里有一半画面会异常
-
53536AV100的sample_vdec输出到CVBS显示
-
10海思板子mpp怎么在vi阶段改变视频数据尺寸
-
10HI3559AV100 多摄像头同步模式
-
9海思ss928单路摄像头vio中加入opencv处理并显示
-
10EB-RV1126-BC-191板子运行自己编码的程序
举报类型
- 内容涉黄/赌/毒
- 内容侵权/抄袭
- 政治相关
- 涉嫌广告
- 侮辱谩骂
- 其他
详细说明