led 第一个实验
module led_flicker(sys_clk,
sys_rstn,led) ;
input sys_clk;
input sys_rstn;
output [7:0] led;
reg [7:0] led;
reg [25:0] delay_cnt;
always@(posedge sys_clk or negedge sys_rstn )
begin
if (!sys_rstn)
delay_cnt<=26'd0;
else
begin
if(delay_cnt==26'd49999999)
delay_cnt<=26'd0;
else
delay_cnt<=delay_cnt+1'b1;
end
end
always@(posedge sys_clk or negedge sys_rstn)
begin
if (!sys_rstn)
led<=8'b11111111;
else
begin
if(delay_cnt==26'd49999999)
led<=~led;
else
led<=led;
end
end
endmodule
8个LED 量1秒 灭一秒
参考51写的程序 只是把时间延长一倍,就能得到。 稍微不留神就会出错,看来51也是用心良苦啊。第一个实验就提醒我们避免以后犯错。
module led_flicker(sys_clk,
sys_rstn,
led) ;
input sys_clk;
input sys_rstn;
output [7:0] led;
reg [7:0] led;
reg [24:0] delay_cnt;
always@(posedge sys_clk or negedge sys_rstn )
begin
if (!sys_rstn)
delay_cnt<=25'd0;
else
begin
if(delay_cnt==26'd24999999)
delay_cnt<=25'd0;
else
delay_cnt<=delay_cnt+1'b1;
end
end
always@(posedge sys_clk or negedge sys_rstn)
begin
if (!sys_rstn)
led<=8'b11111111;
else
begin
if(delay_cnt==25'd24999999)
begin
led<=led>>1;
if(led==8'b00000000)
led<=8'b11111111;
end
else led<=led;
end
end
endmodule
sys_rstn,
led) ;
input sys_clk;
input sys_rstn;
output [7:0] led;
reg [7:0] led;
reg [24:0] delay_cnt;
always@(posedge sys_clk or negedge sys_rstn )
begin
if (!sys_rstn)
delay_cnt<=25'd0;
else
begin
if(delay_cnt==26'd24999999)
delay_cnt<=25'd0;
else
delay_cnt<=delay_cnt+1'b1;
end
end
always@(posedge sys_clk or negedge sys_rstn)
begin
if (!sys_rstn)
led<=8'b11111111;
else
begin
if(delay_cnt==25'd24999999)
begin
led<=led>>1;
if(led==8'b00000000)
led<=8'b11111111;
end
else led<=led;
end
end
endmodule
向左流水 只需稍改下代码
led第三个作业 向左流水
module led_flicker(sys_clk,
sys_rstn,led) ;
input sys_clk;
input sys_rstn;
output [7:0] led;
reg [7:0] led;
reg [24:0] delay_cnt;
always@(posedge sys_clk or negedge sys_rstn )
begin
if (!sys_rstn)
delay_cnt<=25'd0;
else
begin
if(delay_cnt==26'd24999999)
delay_cnt<=25'd0;
else
delay_cnt<=delay_cnt+1'b1;
end
end
always@(posedge sys_clk or negedge sys_rstn)
begin
if (!sys_rstn)
led<=8'b01111111;
else
begin
if(delay_cnt==25'd24999999)
led<={led[0],led[7:1]};
else led<=led;
end
end
endmodule
module led_display(sys_clk,
sys_rstn,
sm_seg,
sm_bit);
//输入输出信号
input sys_clk;
input sys_rstn;
output [7:0] sm_seg;
output [7:0] sm_bit;
//寄存器定义
reg [7:0] sm_seg;
wire [7:0] sm_bit;
reg [3:0] disp_dat;
reg [25:0] delay_cnt;
//逻辑部分
always@(posedge sys_clk or negedge sys_rstn)
begin
if(!sys_rstn)
delay_cnt<=26'd0;
else
begin
if(delay_cnt==26'd49999999)
delay_cnt<=26'd0;
else
delay_cnt<=delay_cnt+1'd1;
end
end
always@(posedge sys_clk or negedge sys_rstn)
begin
if(!sys_rstn)
disp_dat<=4'd0;
else
begin
if(delay_cnt==26'd49999999)
disp_dat<=disp_dat+1'b1;
else
disp_dat<=disp_dat;
end
end
always@(disp_dat)
begin
case (disp_dat)
4'h0:sm_seg=8'hc0;
4'h1:sm_seg=8'hf9;
4'h2:sm_seg=8'ha4;
4'h3:sm_seg=8'hb0;
4'h4:sm_seg=8'h99;
4'h5:sm_seg=8'h92;
4'h6:sm_seg=8'h82;
4'h7:sm_seg=8'hf8;
4'h8:sm_seg=8'h80;
4'h9:sm_seg=8'h90;
4'ha:sm_seg=8'h88;
4'hb:sm_seg=8'h83;
4'hc:sm_seg=8'hc6;
4'hd:sm_seg=8'ha1;
4'he:sm_seg=8'h86;
4'hf:sm_seg=8'h8e;
endcase
end
assign sm_bit=8'b01010101;
endmodule
sys_rstn,
sm_seg,
sm_bit);
//输入输出信号
input sys_clk;
input sys_rstn;
output [7:0] sm_seg;
output [7:0] sm_bit;
//寄存器定义
reg [7:0] sm_seg;
wire [7:0] sm_bit;
reg [3:0] disp_dat;
reg [25:0] delay_cnt;
//逻辑部分
always@(posedge sys_clk or negedge sys_rstn)
begin
if(!sys_rstn)
delay_cnt<=26'd0;
else
begin
if(delay_cnt==26'd49999999)
delay_cnt<=26'd0;
else
delay_cnt<=delay_cnt+1'd1;
end
end
always@(posedge sys_clk or negedge sys_rstn)
begin
if(!sys_rstn)
disp_dat<=4'd0;
else
begin
if(delay_cnt==26'd49999999)
disp_dat<=disp_dat+1'b1;
else
disp_dat<=disp_dat;
end
end
always@(disp_dat)
begin
case (disp_dat)
4'h0:sm_seg=8'hc0;
4'h1:sm_seg=8'hf9;
4'h2:sm_seg=8'ha4;
4'h3:sm_seg=8'hb0;
4'h4:sm_seg=8'h99;
4'h5:sm_seg=8'h92;
4'h6:sm_seg=8'h82;
4'h7:sm_seg=8'hf8;
4'h8:sm_seg=8'h80;
4'h9:sm_seg=8'h90;
4'ha:sm_seg=8'h88;
4'hb:sm_seg=8'h83;
4'hc:sm_seg=8'hc6;
4'hd:sm_seg=8'ha1;
4'he:sm_seg=8'h86;
4'hf:sm_seg=8'h8e;
endcase
end
assign sm_bit=8'b01010101;
endmodule
提供的代码 照着这个代码修改为下面的代码
module led_display( sys_clk,
sys_rstn,
sm_seg,
sm_bit,
key
);
//输入输出信号
input sys_clk;
input sys_rstn;
input key;
output [7:0] sm_seg;
output [7:0] sm_bit;
//寄存器定义
reg [7:0] sm_seg;
reg [7:0] sm_bit;
//reg [3:0] disp_dat;
reg [25:0] delay_cnt;
wire [7:0] key ;
//逻辑部分
always@(posedge sys_clk or negedge sys_rstn)
begin
if(!sys_rstn)
begin
delay_cnt<=26'd0;
//sm_bit=8'b11111111;
end
else
begin
if(delay_cnt==26'd49999999)
delay_cnt<=26'd0;
else
delay_cnt<=delay_cnt+1'd1;
end
end
always@(key)
begin
case(key)
8'b00000000:
begin
sm_bit=8'b11111111;
sm_seg=8'hc0;
end
8'b00000001:
begin
sm_bit=8'b11111110;
sm_seg=8'hf9;
end
8'b00000010:
begin
sm_bit=8'b11111101;
sm_seg=8'ha4;
end
8'b00000100:
begin
sm_bit=8'b11111011;
sm_seg=8'hb0;
end
8'b00001000:
begin
sm_bit=8'b11110111;
sm_seg=8'h99;
end
8'b00010000:
begin
sm_bit=8'b11101111;
sm_seg=8'h92;
end
8'b00100000:
begin
sm_bit=8'b11011111;
sm_seg=8'h82;
end
8'b01000000:
begin
sm_bit=8'b10111111;
sm_seg=8'hf8;
end
8'b10000000:
begin
sm_bit=8'b01111111;
sm_seg=8'h80;
end
default:
sm_bit=8'b11111111;
endcase
end
endmodule
sys_rstn,
sm_seg,
sm_bit,
key
);
//输入输出信号
input sys_clk;
input sys_rstn;
input key;
output [7:0] sm_seg;
output [7:0] sm_bit;
//寄存器定义
reg [7:0] sm_seg;
reg [7:0] sm_bit;
//reg [3:0] disp_dat;
reg [25:0] delay_cnt;
wire [7:0] key ;
//逻辑部分
always@(posedge sys_clk or negedge sys_rstn)
begin
if(!sys_rstn)
begin
delay_cnt<=26'd0;
//sm_bit=8'b11111111;
end
else
begin
if(delay_cnt==26'd49999999)
delay_cnt<=26'd0;
else
delay_cnt<=delay_cnt+1'd1;
end
end
always@(key)
begin
case(key)
8'b00000000:
begin
sm_bit=8'b11111111;
sm_seg=8'hc0;
end
8'b00000001:
begin
sm_bit=8'b11111110;
sm_seg=8'hf9;
end
8'b00000010:
begin
sm_bit=8'b11111101;
sm_seg=8'ha4;
end
8'b00000100:
begin
sm_bit=8'b11111011;
sm_seg=8'hb0;
end
8'b00001000:
begin
sm_bit=8'b11110111;
sm_seg=8'h99;
end
8'b00010000:
begin
sm_bit=8'b11101111;
sm_seg=8'h92;
end
8'b00100000:
begin
sm_bit=8'b11011111;
sm_seg=8'h82;
end
8'b01000000:
begin
sm_bit=8'b10111111;
sm_seg=8'hf8;
end
8'b10000000:
begin
sm_bit=8'b01111111;
sm_seg=8'h80;
end
default:
sm_bit=8'b11111111;
endcase
end
endmodule
数码管第一个实验
module led_display(sys_clk ,
sys_rstn,
sm_seg,
sm_bit);
input sys_clk;
input sys_rstn;
output [7:0] sm_seg;
output [7:0] sm_bit;
reg [7:0] sm_seg ;
reg [7:0] sm_bit ;
reg [3:0] gewei ;
reg [3:0] shiwei ;
reg [25:0] delay_cnt;
reg [4:0] dataout_buf;
reg [4:0] dataout2_buf;
always@(posedge sys_clk or negedge sys_rstn)
begin
if(!sys_rstn)
delay_cnt<=26'd0;
else
begin
if(delay_cnt==26'd2999999)
delay_cnt<=26'd0;
else
delay_cnt<=delay_cnt+1'b1;
end
end
always@(posedge sys_clk or negedge sys_rstn)
begin
if (!sys_rstn)
gewei<=4'd0;
else
begin
if(delay_cnt==26'd2999999)
gewei<=gewei+1'd1;
else if(gewei==4'd10)
gewei=4'd0;
else
gewei<=gewei;
end
end
always@(posedge sys_clk or negedge sys_rstn)
begin
if (!sys_rstn)
shiwei<=4'd0;
else
begin
if(gewei==4'd10)
shiwei<=shiwei+1'd1;
else if (shiwei==4'd7)
shiwei<=4'd0;
else
shiwei<=shiwei;
end
end
always@(posedge sys_clk or negedge sys_rstn )
begin
case(delay_cnt)
4'd0:
begin
sm_bit=8'b11111100;
dataout_buf=shiwei;
end
4'd1:
begin
sm_bit=8'b11111100;
dataout_buf=shiwei;
end
4'd2:
begin
sm_bit=8'b11111100;
dataout_buf=shiwei;
end
4'd3:
begin
sm_bit=8'b11111100;
dataout_buf=shiwei;
end
4'd4:
begin
sm_bit=8'b11111100;
dataout_buf=shiwei;
end
4'd5:
begin
sm_bit=8'b11111100;
dataout_buf=shiwei;
end
4'd6:
begin
sm_bit=8'b11111100;
dataout_buf=shiwei;
end
4'd7:
begin
sm_bit=8'b11111100;
dataout_buf=shiwei;
end
4'd8:
begin
sm_bit=8'b11111100;
dataout_buf=shiwei;
end
4'd9:
begin
sm_bit=8'b11111100;
dataout_buf=shiwei;
end
default:
begin
sm_bit=8'b11111100;
dataout_buf=shiwei;
end
endcase
end
always@(dataout_buf)
begin
case(dataout_buf)
4'h0:
sm_seg=8'hc0;
4'h1:
sm_seg=8'hf9;
4'h2:
sm_seg=8'ha4;
4'h3:
sm_seg=8'hb0;
4'h4:
sm_seg=8'h99;
4'h5:
sm_seg=8'h92;
4'h6:
sm_seg=8'h82;
4'h7:
sm_seg=8'hf8;
4'h8:
sm_seg=8'h80;
4'h9:
sm_seg=8'h90;
4'ha:
sm_seg=8'h88;
4'hb:
sm_seg=8'h83;
4'hc:
sm_seg=8'hc6;
4'hd:
sm_seg=8'ha1;
4'he:
sm_seg=8'h86;
4'hf:
sm_seg=8'h8e;
default:
sm_seg=8'hc0;
endcase
end
endmodule
sys_rstn,
sm_seg,
sm_bit);
input sys_clk;
input sys_rstn;
output [7:0] sm_seg;
output [7:0] sm_bit;
reg [7:0] sm_seg ;
reg [7:0] sm_bit ;
reg [3:0] gewei ;
reg [3:0] shiwei ;
reg [25:0] delay_cnt;
reg [4:0] dataout_buf;
reg [4:0] dataout2_buf;
always@(posedge sys_clk or negedge sys_rstn)
begin
if(!sys_rstn)
delay_cnt<=26'd0;
else
begin
if(delay_cnt==26'd2999999)
delay_cnt<=26'd0;
else
delay_cnt<=delay_cnt+1'b1;
end
end
always@(posedge sys_clk or negedge sys_rstn)
begin
if (!sys_rstn)
gewei<=4'd0;
else
begin
if(delay_cnt==26'd2999999)
gewei<=gewei+1'd1;
else if(gewei==4'd10)
gewei=4'd0;
else
gewei<=gewei;
end
end
always@(posedge sys_clk or negedge sys_rstn)
begin
if (!sys_rstn)
shiwei<=4'd0;
else
begin
if(gewei==4'd10)
shiwei<=shiwei+1'd1;
else if (shiwei==4'd7)
shiwei<=4'd0;
else
shiwei<=shiwei;
end
end
always@(posedge sys_clk or negedge sys_rstn )
begin
case(delay_cnt)
4'd0:
begin
sm_bit=8'b11111100;
dataout_buf=shiwei;
end
4'd1:
begin
sm_bit=8'b11111100;
dataout_buf=shiwei;
end
4'd2:
begin
sm_bit=8'b11111100;
dataout_buf=shiwei;
end
4'd3:
begin
sm_bit=8'b11111100;
dataout_buf=shiwei;
end
4'd4:
begin
sm_bit=8'b11111100;
dataout_buf=shiwei;
end
4'd5:
begin
sm_bit=8'b11111100;
dataout_buf=shiwei;
end
4'd6:
begin
sm_bit=8'b11111100;
dataout_buf=shiwei;
end
4'd7:
begin
sm_bit=8'b11111100;
dataout_buf=shiwei;
end
4'd8:
begin
sm_bit=8'b11111100;
dataout_buf=shiwei;
end
4'd9:
begin
sm_bit=8'b11111100;
dataout_buf=shiwei;
end
default:
begin
sm_bit=8'b11111100;
dataout_buf=shiwei;
end
endcase
end
always@(dataout_buf)
begin
case(dataout_buf)
4'h0:
sm_seg=8'hc0;
4'h1:
sm_seg=8'hf9;
4'h2:
sm_seg=8'ha4;
4'h3:
sm_seg=8'hb0;
4'h4:
sm_seg=8'h99;
4'h5:
sm_seg=8'h92;
4'h6:
sm_seg=8'h82;
4'h7:
sm_seg=8'hf8;
4'h8:
sm_seg=8'h80;
4'h9:
sm_seg=8'h90;
4'ha:
sm_seg=8'h88;
4'hb:
sm_seg=8'h83;
4'hc:
sm_seg=8'hc6;
4'hd:
sm_seg=8'ha1;
4'he:
sm_seg=8'h86;
4'hf:
sm_seg=8'h8e;
default:
sm_seg=8'hc0;
endcase
end
endmodule
module led_display(sys_clk ,
sys_rstn,
sm_cs,
sm_db
);
input sys_clk;
input sys_rstn;
output [3:0] sm_cs;
output [6:0] sm_db;
reg [3:0] gewei ;
reg [3:0] shiwei ;
reg [25:0] delay_cnt;
reg [25:0] delay_cnt1;
reg [3:0] num;
reg [6:0] sm_dbr;
reg sm_cs1_r;
reg sm_cs2_r;
parameter
seg0 = 7'hc0,
seg1 = 7'hf9,
seg2 = 7'ha4,
seg3 = 7'hb0,
seg4 = 7'h99,
seg5 = 7'h92,
seg6 = 7'h82,
seg7 = 7'hf8,
seg8 = 7'h80,
seg9 = 7'h90;
always@(posedge sys_clk or negedge sys_rstn)
begin
if(!sys_rstn)
delay_cnt1<=26'd0;
else
begin
if(delay_cnt1==26'd4999)
delay_cnt1<=26'd0;
else
delay_cnt1<=delay_cnt1+1'd1;
end
end
always@(posedge sys_clk or negedge sys_rstn)
begin
if(!sys_rstn)
delay_cnt<=26'd0;
else
begin
if(delay_cnt==26'd4999999)
delay_cnt<=26'd0;
else
delay_cnt<=delay_cnt+1'b1;
end
end
always@(posedge sys_clk or negedge sys_rstn)
begin
if (!sys_rstn)
gewei<=4'd0;
else
begin
if(delay_cnt==26'd4999999)
gewei<=gewei+1'd1;
else if(gewei==4'd10)
gewei=4'd0;
else
gewei<=gewei;
end
end
always@(posedge sys_clk or negedge sys_rstn)
begin
if (!sys_rstn)
shiwei<=4'd0;
else
begin
if(gewei==4'd10)
shiwei<=shiwei+1'd1;
else if (shiwei==4'd6)
shiwei<=4'd0;
else
shiwei<=shiwei;
end
end
always @(num)
case(num)
4'h0:sm_dbr<=seg0;
4'h1:sm_dbr<=seg1;
4'h2:sm_dbr<=seg2;
4'h3:sm_dbr<=seg3;
4'h4:sm_dbr<=seg4;
4'h5:sm_dbr<=seg5;
4'h6:sm_dbr<=seg6;
4'h7:sm_dbr<=seg7;
4'h8:sm_dbr<=seg8;
4'h9:sm_dbr<=seg9;
default:;
endcase
always @(sys_clk or shiwei or gewei)
begin
if(delay_cnt1<26'd2500)
begin
sm_cs1_r = 0;
sm_cs2_r = 1;
num = shiwei;
end
else
begin
sm_cs1_r = 1;
sm_cs2_r = 0;
num = gewei;
end
end
assign sm_db = sm_dbr;
assign sm_cs[1:0] = 2'b11;
assign sm_cs[3] = sm_cs1_r;
assign sm_cs[2] = sm_cs2_r;
sys_rstn,
sm_cs,
sm_db
);
input sys_clk;
input sys_rstn;
output [3:0] sm_cs;
output [6:0] sm_db;
reg [3:0] gewei ;
reg [3:0] shiwei ;
reg [25:0] delay_cnt;
reg [25:0] delay_cnt1;
reg [3:0] num;
reg [6:0] sm_dbr;
reg sm_cs1_r;
reg sm_cs2_r;
parameter
seg0 = 7'hc0,
seg1 = 7'hf9,
seg2 = 7'ha4,
seg3 = 7'hb0,
seg4 = 7'h99,
seg5 = 7'h92,
seg6 = 7'h82,
seg7 = 7'hf8,
seg8 = 7'h80,
seg9 = 7'h90;
always@(posedge sys_clk or negedge sys_rstn)
begin
if(!sys_rstn)
delay_cnt1<=26'd0;
else
begin
if(delay_cnt1==26'd4999)
delay_cnt1<=26'd0;
else
delay_cnt1<=delay_cnt1+1'd1;
end
end
always@(posedge sys_clk or negedge sys_rstn)
begin
if(!sys_rstn)
delay_cnt<=26'd0;
else
begin
if(delay_cnt==26'd4999999)
delay_cnt<=26'd0;
else
delay_cnt<=delay_cnt+1'b1;
end
end
always@(posedge sys_clk or negedge sys_rstn)
begin
if (!sys_rstn)
gewei<=4'd0;
else
begin
if(delay_cnt==26'd4999999)
gewei<=gewei+1'd1;
else if(gewei==4'd10)
gewei=4'd0;
else
gewei<=gewei;
end
end
always@(posedge sys_clk or negedge sys_rstn)
begin
if (!sys_rstn)
shiwei<=4'd0;
else
begin
if(gewei==4'd10)
shiwei<=shiwei+1'd1;
else if (shiwei==4'd6)
shiwei<=4'd0;
else
shiwei<=shiwei;
end
end
always @(num)
case(num)
4'h0:sm_dbr<=seg0;
4'h1:sm_dbr<=seg1;
4'h2:sm_dbr<=seg2;
4'h3:sm_dbr<=seg3;
4'h4:sm_dbr<=seg4;
4'h5:sm_dbr<=seg5;
4'h6:sm_dbr<=seg6;
4'h7:sm_dbr<=seg7;
4'h8:sm_dbr<=seg8;
4'h9:sm_dbr<=seg9;
default:;
endcase
always @(sys_clk or shiwei or gewei)
begin
if(delay_cnt1<26'd2500)
begin
sm_cs1_r = 0;
sm_cs2_r = 1;
num = shiwei;
end
else
begin
sm_cs1_r = 1;
sm_cs2_r = 0;
num = gewei;
end
end
assign sm_db = sm_dbr;
assign sm_cs[1:0] = 2'b11;
assign sm_cs[3] = sm_cs1_r;
assign sm_cs[2] = sm_cs2_r;
endmodule
数码管第二个实验 从0计数到60. 模为60的计数器。 这个实验花了不少时间,主要还是编程的思想没有改变。
回复
有奖活动 | |
---|---|
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |
打赏帖 | |
---|---|
【笔记】生成报错synthdesignERROR被打赏50分 | |
【STM32H7S78-DK评测】LTDC+DMA2D驱动RGBLCD屏幕被打赏50分 | |
【STM32H7S78-DK评测】Coremark基准测试被打赏50分 | |
【STM32H7S78-DK评测】浮点数计算性能测试被打赏50分 | |
【STM32H7S78-DK评测】Execute in place(XIP)模式学习笔记被打赏50分 | |
每周了解几个硬件知识+buckboost电路(五)被打赏10分 | |
【换取逻辑分析仪】RA8 PMU 模块功能寄存器功能说明被打赏20分 | |
野火启明6M5适配SPI被打赏20分 | |
NUCLEO-U083RC学习历程2-串口输出测试被打赏20分 | |
【笔记】STM32CUBEIDE的Noruletomaketarget编译问题被打赏50分 |