FPGA实验

半加器

  1. module half_qwer(a,b,cout,sum);
  2. //半加器设计
  3. input a,b;
  4. output cout,sum;
  5. assign sum = a^b;
  6. assign cout = a
  7. endmodule

全加器

  • 利用for循环进行级联设计 数据流
  1. `timescale 1ns / 1ps
  2. module qwe(a,b,cin,cout,sum);//一位
  3. input a,b,cin;
  4. output sum,cout;
  5. assign sum = a^b^cin;
  6. assign cout = (a&b)|(a&cin)|(b
  7. endmodule
  8. //四位全加器
  9. module asdfg(sum,cout,a,b,cin);
  10. input[3:0] a,b;
  11. input cin;
  12. output[3:0] sum;
  13. output cout;
  14. assign {cout,sum} = a+b+cin;
  15. endmodule
  16. ================四位全加器仿真===================
  17. initial begin
  18. // Initialize Inputs
  19. a = 0;b = 0;cin = 0;#100;
  20. // Add stimulus here
  21. repeat(10000) #100 {a,b,cin} = {a,b,cin}+1;
  22. end
  23. //八位全加器
  24. module qwer(a,b,cin,sum,cout);
  25. parameter SIZE = 8;
  26. input[SIZE-1:0]a,b;
  27. input cin;
  28. output[SIZE-1:0] sum;
  29. output cout;
  30. wire[SIZE:0] c;
  31. assign c[0] = cin;
  32. generate //for循环级联8位
  33. genvar i;
  34. for(i=0;i<SIZE;i=i+1)
  35. begin : add
  36. qwe fi(a[i],b[i],c[i],sum[i],c[i+1]);//模块调用
  37. end
  38. endgenerate
  39. assign cout = c[SIZE];
  40. endmodule

2-4译码器

  1. module fgvh(A,Q);
  2. input[1:0] A;//两位输入
  3. output reg[3:0] Q;//四位输出
  4. always @(A)
  5. begin
  6. case(A)
  7. 2'b00 : Q[3:0] =4'b1110;
  8. 2'b01 : Q[3:0] =4'b1101;
  9. 2'b10 : Q[3:0] =4'b1011;
  10. 2'b11 : Q[3:0] =4'b0111;
  11. endcase
  12. end
  13. endmodule
  14. =========================建议使用下面的方法=============================
  15. module qwe(A,Q);
  16. //2-4译码器 for循环
  17. input[1:0] A;
  18. output reg[3:0] Q;
  19. integer i;
  20. always @(A)
  21. begin
  22. Q = 4'b1111;
  23. for(i=0;i<=3;i=i+1)
  24. if(A==i)
  25. Q[i] = 0;
  26. else
  27. Q[i] = 1;
  28. end
  29. endmodule
  30. ============================================================
  31. //仿真激励代码
  32. initial begin
  33. // Initialize Inputs
  34. A = 0;
  35. #100;
  36. forever begin A=A+1;#100;end
  37. end

3-8译码器

  1. module sanba(A,Q);
  2. //3-8译码器 法一(不推荐)
  3. input[2:0] A;
  4. output reg[7:0] Q;
  5. always @(A)
  6. begin
  7. case(A)
  8. 3'b000:Q=8'b1111_1110;
  9. 3'b001:Q=8'b1111_1101;
  10. 3'b010:Q=8'b1111_1011;
  11. 3'b011:Q=8'b1111_0111;
  12. 3'b100:Q=8'b1110_1111;
  13. 3'b101:Q=8'b1101_1111;
  14. 3'b110:Q=8'b1011_1111;
  15. 3'b111:Q=8'b0111_1111;
  16. endcase
  17. end
  18. endmodule
  19. ======================推荐使用下面方法==========================
  20. module qwerttgy(A,Q);
  21. //3-8译码器 for循环 方法二(推荐)
  22. input[2:0] A;
  23. output reg[7:0] Q;
  24. integer i;
  25. always @(A)
  26. begin
  27. Q=8'b1111_1111;
  28. for(i=0;i<=7;i=i+1)
  29. if(A==i)
  30. Q[i] = 0;
  31. else
  32. Q[i] = 1;
  33. end
  34. endmodule

编码器

  1. module bvhk(A,Q);
  2. input[3:0] A;
  3. output reg[1:0] Q;
  4. always@(A)
  5. begin
  6. case(A)
  7. 4'b0001:begin Q=2'b00;end
  8. 4'b0010:begin Q=2'b01;end
  9. 4'b0100:begin Q=2'b10;end
  10. 4'b1000:begin Q=2'b11;end
  11. default:begin Q = 2'bx;end
  12. endcase
  13. end
  14. endmodule
  15. =============仿真==============
  16. A = 1;
  17. #100;
  18. forever begin A=A*2;#100;end

分频器

  1. module aerwyghz(fout,clk,res);
  2. //分频器
  3. input clk,res;
  4. output reg fout;
  5. reg[29:0] counter;
  6. always@(posedge clk)
  7. begin
  8. if(res)begin counter<=0;fout<=0; end
  9. else begin
  10. if(counter==24)//50分频,10分频counter==9
  11. begin
  12. counter<=0;fout<=~fout;
  13. end
  14. else begin
  15. counter <=counter+1;
  16. end
  17. end
  18. end
  19. endmodule
  20. ==================分频器仿真=======================
  21. clk = 0;res = 1;
  22. clk=1;#100;
  23. res=0;
  24. forever #10 clk = !clk;

PWM

  1. module ryuj(clk,res,pwm,fout);
  2. input clk,res;
  3. input[3:0]pwm;
  4. output reg fout;
  5. reg[30:0] j;
  6. always@(posedge clk or negedge res)
  7. begin
  8. if(res==0) begin j<=0;fout<=0;end
  9. else begin
  10. if(j==9) begin j<=0;end
  11. else j<=j+1;
  12. if(j<pwm)fout<=1;
  13. else fout<=0;
  14. end
  15. end
  16. endmodule
  17. ========================仿真========================
  18. initial begin
  19. // Initialize Inputs
  20. clk = 0;pwm = 0;res = 1;res = 0;#100;
  21. res = 1;
  22. pwm = 8;
  23. repeat(100) begin #100; clk = ~clk;end
  24. end

七段数码管译码器*

  1. `timescale 1ns / 1ps
  2. module rtyu(bcd_out,bit_S,indec,res);
  3. input [3:0]indec;
  4. input res;
  5. output reg[3:0] bit_s;
  6. output reg[6:0]bcd_out;
  7. always@(res,indec)
  8. begin
  9. if(!res)begin bcd_out=7'bx;bit_s=4'b1111;end
  10. else begin
  11. bit_s=4'b1110;
  12. case(indec)//共阳数码管 dp、g、f、e、d、c、b、a
  13. 0:bcd_out=7'hc0;//1100_0000
  14. 1:bcd_out=7'hf9;//1111_1001
  15. 2:bcd_out=7'ha4;//1010_0100
  16. 3:bcd_out=7'hb0;//1011_0000
  17. 4:bcd_out=7'h99;//1001_1001
  18. 5:bcd_out=7'h92;//1001_0010
  19. 6:bcd_out=7'h82;//1000_0010
  20. 7:bcd_out=7'hf8;//1111_1000
  21. 8:bcd_out=7'h80;//1000_0000
  22. 9:bcd_out=7'h90;//1001_0000
  23. endcase
  24. end
  25. end
  26. endmodule
  27. ============仿真代码================
  28. indec = 0;
  29. res = 1;#50;
  30. repeat(10)begin#100 indec=indec+1;end
  31. res=0;indec=0;
  32. repeat(10)begin#100 indec=indec+1;end

流水灯

  1. module jhfgx(clk,led,res);
  2. output reg[7:0]led;
  3. input clk,res;
  4. reg[29:0] counter;
  5. always@(posedge clk)
  6. begin
  7. if(res)begin counter<=0;led<=8'b0000_0001;end
  8. else begin
  9. if(led==0) led<=8'b0000_0001;
  10. if(counter>=25_000_00)
  11. begin counter<=0;
  12. led<=led<<1;
  13. end
  14. else counter<=counter+1;
  15. end
  16. end
  17. endmodule
  18. =====================仿真代码=====================
  19. initial begin
  20. // Initialize Inputs
  21. clk = 0;res = 1;clk=1;#100;
  22. res = 0;
  23. forever begin #25 clk=~clk;end
  24. end
本文原创,作者:二牛学FPGA,其版权均为FPGA线上课程平台|最全栈的FPGA学习平台|FPGA工程师认证培训所有。
如需转载,请注明出处:https://z.shaonianxue.cn/9554.html

"愿我的文字能带给您一丝美好"

还没有人赞赏,支持一下

评论

A 为本文作者,G 为游客总数:0
加载中…

提交评论

游客,您好,欢迎参与讨论。

我的购物车

购物车为空

优惠券

没有优惠券