//数码管显示四位数字 max=8191
module smg4(
clk,rst_n,shuzi, //input
qian,bai,shi,ge //output
);
input clk;
input rst_n;
input [12:0] shuzi;
output [6:0] qian,bai,shi,ge;
reg [3:0] q,b,s,g;
// 加三移位法 13位的二进制 只需要16位来存其十进制数字 一共29位 28:0
reg [3:0] count;
reg [28:0] temp;
//---------------- 计数部分 -----------------
always@(posedge clk or negedge rst_n )
begin
if(!rst_n )
count<=0;
else if(count==14)
begin
count<=0;
q<=temp[28:25];
b<=temp[24:21];
s<=temp[20:17];
g<=temp[16:13];
end
else
count<=count+1;
end
//------------------加三移位法---------------
always@(posedge clk,negedge rst_n)
begin
if(!rst_n) temp=0;
else if(count==4'b0) temp={16'b0,shuzi};
else if(count<=13)
begin
if(temp[24:21]>4) temp[24:21]=temp[24:21]+3;
if(temp[20:17]>4) temp[20:17]=temp[20:17]+3;
if(temp[16:13]>4) temp[16:13]=temp[16:13]+3;
temp=temp<<1;
end
end
//------------------七段译码--------------------
assign qian=qiduan(q);
assign bai=qiduan(b);
assign shi=qiduan(s);
assign ge=qiduan(g);
function [6:0] qiduan;
input [3:0] s;
qiduan=(s==0)?7'b1000000:
(s==1)?7'b1111001:
(s==2)?7'b0100100:
(s==3)?7'b0110000:
(s==4)?7'b0011001:
(s==5)?7'b0010010:
(s==6)?7'b0000010:
(s==7)?7'b1111000:
(s==8)?7'b0000000:
7'b0010000;
endfunction
endmodule