verilog rs232串口模块

前面发了个发送模块,这次补齐,完整。
串口计数器,波特率适配
uart_clk.v

module uart_clk(input wire clk,input wire rst_n,input wire tx_clk_en,input wire rx_clk_en,input wire[1:0] baud_sel,output wire tx_clk,output wire rx_clk
);
localparam OSC = 50_000_000;
reg[16:0] bauds[0:3] ;reg[16:0] tx_cnt;
reg[16:0] rx_cnt;initial beginbauds[0] <= OSC/19200 - 1;bauds[1] <= OSC/38400 - 1;bauds[2] <= OSC/57600 - 1;bauds[3] <= OSC/115200 - 1;
endalways @(posedge clk or negedge rst_n ) beginif (!rst_n )tx_cnt <= 0;else if (!tx_clk_en || tx_cnt === bauds[baud_sel])tx_cnt <= 0;elsetx_cnt <= tx_cnt + 1;
endassign tx_clk = (tx_cnt  === bauds[baud_sel]) ? 1'b1 : 1'b0;always @(posedge clk or negedge rst_n) beginif (!rst_n)rx_cnt <= 0;else if (!rx_clk_en || rx_cnt === bauds[baud_sel])rx_cnt <= 0;elserx_cnt <= rx_cnt + 1;
endassign  rx_clk = (rx_cnt === (bauds[baud_sel] >>1)) ? 1'b1 : 1'b0;endmodule

发送模块
tx.v

module tx (input wire clk,input wire rst_n,input wire tx_en,input wire tx_clk,input wire[7:0] tx_data,output reg tx_clk_en,output reg txd,output reg busy,output reg done
);reg [1:0] stage;
reg [2:0] curbit;
reg [7:0] tx_rdata;always @(posedge clk or negedge rst_n) beginif (!rst_n) beginbusy <= 1'b0;end else if (tx_en && !busy) beginbusy <= 1'b1;tx_rdata <= tx_data;end else if (done)busy <= 1'b0;
endalways @(busy) beginif(busy)tx_clk_en <= 1'b1;else tx_clk_en <= 1'b0;
endalways @(posedge clk or negedge rst_n) beginif (!rst_n) begintxd <= 1'b1;done <= 1'b0;stage <= 2'd0;end else if (busy) beginif (tx_clk) beginif(stage === 2'd0) begintxd <= 1'b0;done <= 1'b0;curbit <= 3'd0;stage <= 2'b1;end else if (stage === 2'b1) begintxd <= tx_rdata[curbit];if(curbit == 3'd7)stage <= 2'd2;elsecurbit <= curbit + 1;end else if (stage == 2'd2) begintxd <= 1'b1;stage <= stage + 1;end else if (stage == 2'd3) begintxd <= 1'b1;done <= 1'b1;stage <= 2'd0;endendend else begintxd <= 1'b1;done <= 1'b0;stage <= 2'd0;end
endendmodule

接收模块

rx.v

module rx (input wire clk,input wire rst_n,input wire rx_en,input wire rx_clk,input wire rxd,output reg rx_clk_en,output reg[7:0] rx_data,output reg busy,output reg done
);reg[7:0] tmp;
reg[3:0] tmpres;reg [ 1:0] stage;
reg [2:0] curbit;
reg [7:0] rx_rdata;wire rxd_negedge;
always @(posedge clk or negedge rst_n) beginif (!rst_n)tmp <= 8'b0;elsetmp <= (tmp << 1) | rxd;tmpres <= {tmp[7:6], tmp[1:0]};
end
assign rxd_negedge = ( tmpres === 4'b1100) ? 1'b1 : 1'b0;always @(posedge clk or negedge rst_n) beginif (!rst_n) beginbusy <= 1'b0;end else if (rx_en && rxd_negedge && !busy) beginbusy <= 1'b1;end else if (done)busy <= 1'b0;
endalways @(busy) beginif (busy)rx_clk_en <= 1'b1;elserx_clk_en <= 1'b0;
endalways @(posedge clk or negedge rst_n) beginif (!rst_n) begindone <= 1'b0;stage <= 2'd0;rx_rdata <= 8'd0;end else if (busy) beginif (rx_clk) beginif (stage === 2'd0) beginrx_rdata <= 8'd0;done <= 1'b0;curbit <= 3'd0;stage <= 2'b1;end else if (stage === 2'b1) beginrx_rdata[curbit] <= rxd;if (curbit == 3'd7)stage <= 2'd2;elsecurbit <= curbit + 1;end else if (stage === 2'd2) beginrx_data <= rx_rdata;done <= 1'b1;stage <= 2'd0;end elsestage <= 2'd0;endend else beginrx_rdata <= 8'd0;done <= 1'b0;stage <= 2'd0;end
endendmodule

组合
uart.v

module uart(input wire clk,input wire rst_n,input wire rx_en,input wire rxd,input wire tx_en,output wire[7:0] rx_data,input wire[7:0] tx_data,input  wire[1:0] baud_sel,output wire txd,output wire rbusy,output wire rdone,output wire tbusy,output wire tdone
);wire rx_clk,rx_clk_en;
wire tx_clk, tx_clk_en;uart_clk uclk1(clk,rst_n,tx_clk_en,rx_clk_en,baud_sel,tx_clk,rx_clk);
tx tx1(clk,rst_n,tx_en,tx_clk,tx_data,tx_clk_en,txd,tbusy,tdone);
rx rx1(clk,rst_n,rx_en,rx_clk,rxd,rx_clk_en,rx_data,rbusy,rdone);endmodule

测试
uart_test.v

module uart_test (input wire clk,input wire rst_n,input wire rxd,output wire txd
);
reg rx_en = 1;
reg tx_en;
wire rbusy,tbusy;
wire rdone,tdone;
reg[7:0] tx_data = 0;
wire[7:0] rx_data ;
// reg received = 1'b0;
reg sent = 1'b0;
reg[1:0] baud_sel = 0;always @(posedge clk or negedge rst_n) beginif (!rst_n) beginrx_en = 0;tx_en = 0;end else  if(!tbusy && !rbusy && !sent) begintx_data <= tx_data+1 ;tx_en <= 1'b1;sent <= 1;$display("sent %d",tx_data);end else if (rdone) beginsent <= 0;$display("received %d",rx_data);end else begintx_en <= 1'b0;rx_en <= 1'b1;end
enduart uart1(clk, rst_n, rx_en,rxd,tx_en,rx_data, tx_data,baud_sel,txd,rbusy,rdone,tbusy,tdone);endmodule

testbench

module test;reg rst_n=1 ;initial begin$dumpfile("test.vcd");$dumpvars(0, test);#10 rst_n = !rst_n;#30 rst_n = !rst_n;#500000 $finish;
endreg clk = 0;
always #1 clk = !clk;wire txd;
wire rxd;
uart_test uart1(clk,rst_n,rxd,txd);
// always @(posedge clk)
//    rxd <= txd;
assign rxd = txd;endmodule

gtkwave 波形
在这里插入图片描述
方便模拟测试,发送转接收,对比结果。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/582485.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Kubernetes快速实战与核心原理剖析

K8S 概览 K8S 是什么 K8S 官网文档&#xff1a;https://kubernetes.io/zh/docs/home/ K8S 是 Kubernetes 的全称&#xff0c;源于希腊语&#xff0c;意为“舵手”或“飞行员”。Kubernetes 是用于自动部署、扩缩和管理容器化应用程序的开源系统。 Kubernetes 源自 Google 15 年…

知识笔记(六十三)———JavaScript 工具库 | PrefixFree给CSS自动添加浏览器前缀

为了解决这个问题&#xff0c;国外的牛人开发了了一个 -Prefix-free 的插件&#xff0c;能够自动给我们添加这些前缀&#xff0c;我们仅仅需要编写一次代码&#xff0c;无需在考虑是否兼容其他浏览器&#xff0c;而且如果后面浏览器支持这个属性了&#xff0c;我们只需要移除 -…

EDKII:第一个Helloworld

目录 0 说明 1 步骤 1.1 简介 1.2 创建新文件 1.3 创建printhelloworld.c、printhelloworld.inf&#xff1a; 1.4 修改MdeModulePkg\MdeModulePkg.dsc 1.5 修改EmulatorPkg\EmulatorPkg.dsc 1.6 运行 0 说明 上篇文章记录了如何安装UEFI环境&#xff0c;在这里将会写下…

c++ / day03

1. 定义一个Person类&#xff0c;包含私有成员&#xff0c;int *age&#xff0c;string &name&#xff0c;一个Stu类&#xff0c;包含私有成员double *score&#xff0c;Person p1&#xff0c;写出Person类和Stu类的特殊成员函数&#xff0c;并写一个Stu的show函数&#xf…

CodeWhisperer——轻松使用一个超级强大的工具

CodeWhisperer 简介 CodeWhisperer是亚⻢逊云科技出品的一款基于机器学习的通用代码生成器&#xff0c;可实时提供代码建议。 CodeWhisperer有以下几个主要用途&#xff1a; 解决编程问题&#xff0c;提供代码建议&#xff0c;学习编程知识等等&#xff0c;并且CodeWhisper…

基于人工势场法的航线规划

MATLAB2016b可以运行 基于人工势场法的航线规划资源-CSDN文库

JavaSE学习笔记 2023-12-21 --流

十九、流 上一篇 个人整理非商业用途&#xff0c;欢迎探讨与指正&#xff01;&#xff01; 文章目录 十九、流19.1流的概念19.2File类19.2.1File对象的创建19.2.2Java中的路径表示19.2.3File中的常用方法19.2.4FileNameFilter接口 19.3IO流19.3.1流的划分19.3.2字节流[重点]…

常用的 linux 命令

常用的 linux 命令 1.从其他机器拷贝文件夹2.查看哪个程序在用特定端口3.实时监控日志文件内容4.查看指定用户拥有的进程5.查看磁盘空间使用情况6.文件搜索which&#xff08;whereis&#xff09; 显示系统命令所在目录find 查找任何文件或目录1&#xff09; 根据文件名称查找2)…

具身智能主流方法:模仿学习,和强化学习

1.区别 模仿学习&#xff1a;倾向于从优秀的个体展现出来的技能中快速学习&#xff0c;并获得泛化能力&#xff0c;但模仿学习目前学到的仅是相同技能的不用应用&#xff0c;比方说&#xff0c;“放苹果”泛化到“放梨”&#xff0c;“放牛奶”&#xff0c;都是“放”这个技能的…

磁盘——磁盘管理与文件系统

目录 一、在linux中使用硬盘分三步 1、分区 2、文件系统&#xff08;管理大小权限。日志恢复&#xff09; 3、挂载&#xff08;硬盘和系统文件做关联&#xff0c;使用文件夹使用系统&#xff09; 二、磁盘结构 三、MBR与GPT磁盘分区 1、分区的原因&#xff0c;为什么分区…

Ubuntu18.04安装GTSAM库并验证GTSAM是否安装成功(亲测可用)

在SLAM&#xff08;Simultaneous Localization and Mapping&#xff09;和SFM&#xff08;Structure from Motion&#xff09;这些复杂的估计问题中&#xff0c;因子图算法以其高效和灵活性而脱颖而出&#xff0c;成为图模型领域的核心技术。GTSAM&#xff08;Georgia Tech Smo…

Java八股文面试全套真题【含答案】- RocketMQ篇

以下是关于Java八股文面试全套真题- RocketMQ篇 1.RocketMQ 是什么&#xff1f;它的特点和优势是什么&#xff1f; RocketMQ 是一个开源的分布式消息中间件系统&#xff0c;具有高吞吐量、低延迟、可靠性强等特点。 特点和优势&#xff1a; 高吞吐量&#xff1a;支持每秒百万级…

Mybatis 动态 SQL - foreach

动态SQL的另一个常见需求是需要迭代一个集合&#xff0c;通常用于构建IN条件。例如&#xff1a; <select id"selectPostIn" resultType"domain.blog.Post">SELECT *FROM POST P<where><foreach item"item" index"index&quo…

Vue 3 中安装并使用 Axios 详细步骤+样例代码详解

axios详细步骤 在集成终端打开&#xff0c;使用 npm 或 yarn 安装 Axios&#xff1a; npm install axios或 yarn add axios这将在您的项目中安装 Axios。 在您的 Vue 3 项目中创建一个用于发送 HTTP 请求的模块或文件&#xff0c;比如 http.js。 在 http.js 文件中导入 Axios…

K8s实战-init容器

概念&#xff1a; 初始化容器的概念 比如一个容器A依赖其他容器&#xff0c;可以为A设置多个 依赖容易A1&#xff0c;A2&#xff0c;A3 A1,A2,A3要按照顺序启动&#xff0c;A1没有启动启动起来的 话&#xff0c;A2,A3是不会启动的&#xff0c;直到所有的静态容器全 部启动完毕…

Java并发编程(四)

ThreadLocal 1.ThreadLocal是什么 ThreadLocal类让每一个线程都拥有了自己的本地变量&#xff0c;这意味着每个线程都可以独立地、安全地操作这些变量&#xff0c;而不会影响其他线程。 ThreadLocal的常用API get()&#xff1a;获取当前线程中与ThreadLocal对象关联的变量副…

Java EasyExcel 导入代码

Java EasyExcel 导入代码 导入方法 /*** 仓库库位导入** param req* param res* param files* throws Exception*/RequestMapping(value {"/import/line_store_locs"}, method {RequestMethod.POST})ResponseBodypublic void importStoreLoc(HttpServletRequest …

MySQL 索引、事务与存储引擎

MySQL 索引 索引的概念 索引是一个排序的列表&#xff0c;在这个列表中存储着索引的值和包含这个值的数据所在行的物理地址&#xff08;类似于C语言的链表通过指针指向数据记录的内存地址&#xff09;。使用索引后可以不用扫描全表来定位某行的数据&#xff0c;而是先通过索引…

一种适合企业的大体量数据迁移方式

在企业进行数字化转型的过程中&#xff0c;数据迁移是一项至关重要的任务。无论是从旧系统到新系统、从本地数据中心到云端&#xff0c;还是在不同云服务提供商之间进行数据迁移&#xff0c;数据的顺利转移对业务的成功至关重要。 然而&#xff0c;随着数据体量的不断增加&…

[SWPUCTF 2021 新生赛]sql

[SWPUCTF 2021 新生赛]sql wp 输入 1 正常回显&#xff1a; ?wllm1 返回&#xff1a; Want Me? Cross the Waf Your Login name:xxx Your Password:yyy输入单引号引发报错&#xff1a; ?wllm1 返回&#xff1a; Want Me? Cross the Waf You have an error in your SQL s…