`timescale 1ns / 1ps
//
// Company:
// Engineer:
//
// Create Date: 2020/11/11 08:15:03
// Design Name:
// Module Name: tomato
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//
//16進制
module second(
input wire clk,
output reg sec);
reg [27:0]q1;
always @(posedge clk)
begin
if(q1==5000000)
begin
q1<=0;
sec<=~sec;
end
else
q1<=q1+1;
end
endmodule
module cnt24(
input wire clk,
output reg [3:0] cnt60_L,
output reg [3:0] cnt60_H,
output reg [3:0] cnt25_mL,
output reg [3:0] cnt25_mH,
output reg carry
);
initial begin
cnt60_L=9;
cnt60_H=5;
cnt25_mL=4;
cnt25_mH=2;
end
always @(posedge clk)
begin
carry<=0;
cnt60_L<=cnt60_L-1;
if(cnt60_L==0)
begin
cnt60_L<=9;
cnt60_H<=cnt60_H-1;
end
if(cnt60_H==0 && cnt60_L==0 )
begin
cnt60_L<=9;
cnt60_H<=5;
cnt25_mL<=cnt25_mL-1;
carry<=1;
end
if(cnt60_H==0 && cnt60_L==0 && cnt25_mL==0)
begin
cnt60_L<=9;
cnt60_H<=5;
cnt25_mL<=9;
cnt25_mH<=cnt25_mH-1;
end
if(cnt60_H==0 && cnt60_L==0 && cnt25_mL==0 && cnt25_mH==0)
begin
cnt60_L<=9;
cnt60_H<=5;
cnt25_mL<=4;
cnt25_mH<=2;
end
end
endmodule
module top(
input wire clk,
output wire [3:0] second_L,
output wire [3:0] second_H,
output wire [3:0] mL,
output wire [3:0] mH
);
wire jinwei;
second U0(
.clk(clk),
.sec(jinwei)
);
cnt24 U1(
.clk(jinwei),
.cnt60_L(second_L),
.cnt60_H(second_H),
.cnt25_mL(mL),
.cnt25_mH(mH)
);
endmodule
module tomato24(
input clk,
input rst_n,
//input [7:0] x, //等待顯示的BCD碼
output reg [6:0] a_to_g, //段信號
output reg [3:0] an //位選信號
);
wire [3:0]sL;
wire [3:0]sH;
wire [3:0]mL;
wire [3:0]mH;
top zhuogege(
.clk(clk),
.second_L(sL),
.second_H(sH),
.mL(mL),
.mH(mH)
);
wire [15:0]x={mH,mL,sH,sL};
//x={H,L};
//時鐘分頻 計數器
reg [20:0] clkdiv;
always @(posedge clk or negedge rst_n)
begin
if(!rst_n)
clkdiv<=21'd0;
else
clkdiv<=clkdiv+1;
end
/*利用計數器自動溢位時間,即就是clkdiv從0~11111111111111111111回圈計數,
則clk[19]會在0~1之間以5.24ms為時間間隔變化 2^19=524288
(即后19位全0到全1的計數時間)
*/
//bitcnt: 位掃描信號 0~1回圈變化 掃描周期 5.24ms 控制總掃描時間不超過10ms,單個數碼管顯示時間約為5ms
wire [1:0]bitcnt;
assign bitcnt=clkdiv[20:19];
//an:位選信號產生,高有效
always @(posedge clk or negedge rst_n)
begin
if(!rst_n)
an=4'd0;
else
case(bitcnt)
2'd0:an=4'b0001;
2'd1:an=4'b0010;
2'd2:an=4'b0100;
2'd3:an=4'b1000;
endcase
end
//digit 當前帶顯示的數字
reg [3:0]digit;
always @(posedge clk or negedge rst_n)
begin
if (!rst_n)
digit=4'd0;
else
case(bitcnt)
2'd0:digit=x[3:0];
2'd1:digit=x[7:4];
2'd2:digit=x[11:8];
2'd3:digit=x[15:12];
default:digit=4'd0;
endcase
end
//a_to_g: 段碼信號,共陰極數碼管,段碼高有效, 7段譯碼表
always @(posedge clk or negedge rst_n)
begin
if(!rst_n)
a_to_g=7'b1111111;
else
case(digit)
0:a_to_g=7'b1111110;//段碼位序由高到低為a-g
1:a_to_g=7'b0110000;
2:a_to_g=7'b1101101;
3:a_to_g=7'b1111001;
4:a_to_g=7'b0110011;
5:a_to_g=7'b1011011;
6:a_to_g=7'b1011111;
7:a_to_g=7'b1110000;
8:a_to_g=7'b1111111;
9:a_to_g=7'b1111011;
default:a_to_g=7'b1111110;
endcase
end
endmodule
備注:本代碼由西安交通大學電氣工程及其自動化專業教學使用,如有侵權,聯系作者洗掉, 本代碼為西安交通大學學生備忘而用,
如果有共同愛好者,可以一起學習
qq:2685783428
如果代碼有用,請盡情的點贊和打賞即可
白嫖雖然爽,但是違背公序良俗,不可取哦,親(づ ̄3 ̄)づ╭?~
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/214336.html
標籤:其他
上一篇:時序約束(二)
下一篇:大家是怎么處理傳感器資料的
