当前位置:网站首页>数字IC手撕代码--交通灯
数字IC手撕代码--交通灯
2022-06-29 13:32:00 【FPGA硅农】
题目
描述
要求实现一个交通红绿灯,具有红黄绿三个小指示灯和一个行人按钮,正常情况下,机动车道指示灯按照60时钟周期绿灯,5个时钟周期黄灯,10个时钟周期红灯循环。当行人按钮按下,如果剩余绿灯时间大于10个时钟,则缩短为10个时钟,小于10个时钟则保持不变。
模块的信号接口图如下:
请使用VerilogHDL语言实现,并编写testbench验证功能。
代码
设计
`timescale 1ns/1ns
module triffic_light
(
input rst_n, //异位复位信号,低电平有效
input clk, //时钟信号
input pass_request,
output reg [7:0]clock,
output reg red,
output reg yellow,
output reg green
);
parameter RED=0;
parameter GREEN=1;
parameter YELLOW=2;
reg [1:0] state,next_state;
//60 clk green
//5 clk yellow
//10 clk red
[email protected](posedge clk,negedge rst_n)
if(~rst_n)
state<=RED;
else
state<=next_state;
[email protected](*)
begin
case(state)
RED:if(clock==1)
next_state=GREEN;
else
next_state=RED;
YELLOW:if(clock==1)
next_state=RED;
else
next_state=YELLOW;
GREEN:if(clock==1)
next_state=YELLOW;
else
next_state=GREEN;
default:next_state=RED;
endcase
end
//led
[email protected](*)
case(state)
RED:{
red,yellow,green}=3'b100;
YELLOW:{
red,yellow,green}=3'b010;
GREEN:{
red,yellow,green}=3'b001;
default:{
red,yellow,green}=3'b000;
endcase
//clock
[email protected](posedge clk,negedge rst_n)
if(~rst_n)
clock<=10; //red
else
case(state)
RED:if(clock==1)
clock<=60;
else
clock<=clock-1;
YELLOW:if(clock==1)
clock<=10;
else
clock<=clock-1;
GREEN:
if(pass_request)
if(clock>10)
clock<=10;
else
clock<=clock-1;
else if(clock==1)
clock<=5;
else
clock<=clock-1;
default:clock<=10;
endcase
endmodule
测试平台
module test;
reg clk;
reg rst_n;
reg pass_request;
wire [7:0] clock;
wire red;
wire yellow;
wire green;
//clk
initial
begin
clk=0;
forever
#5 clk=~clk;
end
//rst_n
initial
begin
rst_n=0;
#100
rst_n=1;
end
//
initial
begin
pass_request=0;
//
repeat(20)
@(posedge clk);
wait(green&&clock==18);
pass_request<=1;
@(posedge clk);
pass_request<=0;
//
repeat(40)
@(posedge clk);
wait(green&&clock==9);
pass_request<=1;
@(posedge clk)
pass_request<=0;
end
//inst
triffic_light U
(.*
/* input rst_n, //异位复位信号,低电平有效 input clk, //时钟信号 input pass_request, output reg [7:0]clock, output reg red, output reg yellow, output reg green */
);
endmodule
结果
正常情况下,绿灯60个周期,黄灯5个周期,红灯10个周期
绿灯且大于10个周期的时候,行人按下
绿灯且小于等于10个周期的时候,行人按下
边栏推荐
- 文物数字藏品,开启文化传承的新方式
- Cicd introduction [easy to understand]
- Application of ansvc reactive power compensation device in a shopping mall in Hebei
- vmware虚拟机的作用
- Stable currency risk profile: are usdt and usdc safe?
- [network bandwidth] Mbps & Mbps
- 揭秘百度智能测试在测试自动执行领域实践
- 深度学习的坎坷六十年
- June training (day 29) - divide and rule
- [dark horse morning post] the market value of China public education has evaporated by more than 200billion; New Oriental has more than 20million live fans; HM closes its first store in China; Vanke Y
猜你喜欢

灵感收集·创意写作软件评测:Flomo、Obsidian Memo、Napkin、FlowUs

喜迎市科协“十大”•致敬科技工作者 | 卢毅:守护电网传输安全的探索者

Korean AI team plagiarizes the shock academic world! One tutor with 51 students, or plagiarism recidivist

中康控股开启招股:拟募资净额3.95亿港元,预计7月12日上市
![[document translation] camouflaged object detection](/img/30/73a927c05173a95cc5a5d51e182e3b.png)
[document translation] camouflaged object detection

Wechat applet: new and exclusive cloud development wechat group contacts

Sixty years of deep learning

嵌入式开发:硬件在环测试

Teach you how to install the latest version of mysql8.0 database on windows, nanny level teaching

“死掉”的诺基亚,一年躺赚1500亿
随机推荐
Stable currency risk profile: are usdt and usdc safe?
Summary of binary tree exercises
Wechat applet: new and exclusive cloud development wechat group contacts
MySQL数据库:使用show profile命令分析性能
Shell——文本处理命令
Tiktok's global short video dominance may be reversed by YouTube
嵌入式开发:硬件在环测试
Crazy digital collections, the next myth of making wealth?
Problems in replacing RESNET convolution of mmdet with ghost convolution group
Open source machine learning platform
逆向调试入门-PE文件节表与区块03/07
win10安装Monggodb的基本使用教程
MySQL数据库:存储引擎
Recruiting talents and seeking development | Jincang of the National People's Congress won the "best employer school recruitment case Award"
How to install MySQL 8.0 on rocky Linux and almalinux
Characteristics of human immaturity
分布式唯一 ID 生成方案浅谈
"Dead" Nokia makes 150billion a year
高校女生穿旗袍答辩!网友:导师说论文要是和旗袍一样漂亮就好了
Turbulent intermediary business, restless renters