本文主要是介绍基于Verilog语言和BASYS3开发板的移位寄存器实验,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
基于Verilog语言和BASYS3开发板的移位寄存器实验
闲来无事就想把之前做的实验整理一下,既是自己写博客的尝试,同时希望对后来的学弟学妹以及所有看到这个博客的人有所帮助。
首先是原理部分,想必做实验的时候各位已经知道寄存器的原理。简而言之就是触发器在收到时钟的脉冲时根据输入改变输出。在下一个改变的数据到来之前输出不会改变,就相当于是存储了数据。
代码部分
用的是参考书上的代码,如有雷同,那可能是因为用的同一个书吧。XD
module shift_register( //移位寄存器实现
input btn,
input [10:0] sw,
output [7:0] led //输出8位reg数据作为数码的输入
);
wire clk;
assign clk=btn; //设置初值
wire[7:0] data_in;//低8位用做数据输入
wire[1:0] select;//高两位用作功能选择
wire res; //设置开关
assign {res,select,data_in}=sw;
reg[7:0] q;
always@(posedge clk,posedge res)//设置初值
if(res) q<=data_in;
else begin
case(select)
0:q<={1’b0,q[7:1]};//逻辑右移
1:q<={q[6:0],1’b0};//逻辑左移
2:q<={q[0],q[7:1]};//循环右移
3:q<={q[6:0],q[7]};//循环左移
endcase
end
assign led=q;
Endmodule
然后是数码显示的代码
module segled(
input [7:0] q,
input clk,
output reg [6:0]a_to_g,
output dp,
output reg [3:0] an
);
reg count;
parameter update_interval = 50000000 / 80 - 1;
integer selcnt;
always @(posedge clk) //分频50Hz
begin
selcnt <= selcnt + 1;
if (selcnt == update_interval) begin selcnt <= 0; count <= count + 1; end
end
reg [3:0]digit;
always @(*) //选择位
case(count)0:begin digit=q[3:0];an=4'b1110;end1:begin digit=q[7:4];an=4'b1101;end
endcase
always @(*)//显示段数
case(digit)0:a_to_g=7'b0000001;1:a_to_g=7'b1001111;2:a_to_g=7'b0010010;3:a_to_g=7'b0000110;4:a_to_g=7'b1001100;5:a_to_g=7'b0100100;6:a_to_g=7'b0100000;7:a_to_g=7'b0001111;8:a_to_g=7'b0000000;9:a_to_g=7'b0000100;'hA:a_to_g=7'b0001000;'hB:a_to_g=7'b1100000;'hC:a_to_g=7'b0110001;'hD:a_to_g=7'b1000010;'hE:a_to_g=7'b0110000;'hF:a_to_g=7'b0111000;
default:a_to_g=7’b0000001;
endcase
endmodule
最后加一个top模块 把这两个模块整合在一起就好了。博客排版不太会用,等熟悉下再写下一个吧。
这篇关于基于Verilog语言和BASYS3开发板的移位寄存器实验的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!