|
EDA365欢迎您!
您需要 登录 才可以下载或查看,没有帐号?注册
x
//偶数分频器,外部带入两个参数决定分频系数。默认是2分频 F: b6 J6 x% W/ Y" j' S
//divdFACTOR--分频系数,实际分频数为divdFACTOR*2/ s( t: n/ |" x! S% G4 m( i
//divdWIDTH--分频计数器的位宽,实际位宽为divdWIDTH+1,该位宽所能表达的最大值>divdFACTOR
+ F' X* B$ s' k. \" m. `% x5 T2 cmodule gen_divd(reset,clkin,clkout);
" M* I1 }, q- U+ ` input reset,clkin;1 y0 X) K; n9 n7 ?) M! Y
output clkout;
, b8 U8 ~. h1 x* n, R4 g% n parameter divdWIDTH=1;//分频计数器的位宽,实际位宽为divdWIDTH+1,该位宽所能表达的最大值>divdFACTOR6 Y) c2 j; O* h. ?# u/ a% [7 h
parameter divdFACTOR=1;//分频系数是divdFACTOR*2% g" J, P9 _, @* ^0 \
reg clkout;
- `; j7 b+ f8 c' }; I. x* X reg [divdWIDTH:0] cnt;( |5 q6 p, ~9 [& ^
) x* h* y+ j( Q
! Y5 Z; v* b" [; C9 G* Z% o
always @ (negedge reset or posedge clkin)) O, g( q4 Z; e" j8 V0 `
if(reset==1'b0)
2 K8 o' O- z: a# K begin4 o. B# Y$ z) c3 t) \0 {
cnt=0;* y% s) E( W4 V! m( g0 Y" J9 z' U; M
clkout=0;! o, y/ H7 C( ]8 l
end# K2 d5 Y' k5 L0 l$ A) I
else
5 c F' N' j& _; G' U8 C. n begin. `! u4 e. I0 q `) A' x, A% z, a$ n
cnt=cnt+1'b1;//常数1要指定位数,否则默认是32位的7 g3 e3 x9 e+ v: X
if(cnt>=divdFACTOR)$ ]7 i9 w2 J9 t
begin' Z3 J. j- t6 ^* B
cnt=0;* M, ?# L4 S& c5 k+ }) K
clkout=~clkout;
5 ` K8 t/ N4 E end
- o; ]( [. M! R' m end
" E* t/ A- p# W; x
& y* d$ S5 j1 a% A. nendmodule- c* K" W, R& K7 l) P7 D4 [
% n. @4 g9 H: \9 e/ d4 ]
`include "gen_divd.v"5 g9 j9 v; d) {0 z
' s% ]" }# P/ u$ }3 I- U
module led_w_dir
- p- F+ W* p' q+ E) r( d- s) u (
* B% b/ v; F! b" P ClkIn,
7 m7 ~! p+ O! v" b5 v O% V3 a KeyIn,
# W2 t8 ]- |8 g+ y/ o; {, i6 u# h- y Resetb,
* o+ K8 A% g c3 w) M6 X. J LedOut
. w, E- `' m f+ Y4 f );
8 O" [. ~$ k+ O! {2 w" \: c2 P input ClkIn,Resetb;
4 g+ }3 i: o1 q' r2 N input [2:0] KeyIn;
" O! D+ x/ O0 s3 }; t* A6 F5 o output [2:0] LedOut;
" Z$ Y! {9 o6 Q7 E 3 A9 S/ P' u5 P
reg [2:0] LedOut;
$ d, r8 @4 E V1 X* F. D; [ wire ClkLed;
9 [) b. ^( S- e5 K. U$ g7 H4 E/ m. @% r reg [2:0] Cnt;5 W; M7 w Q/ H& s/ r# K- a
" R3 s1 E0 Q f' ]. x defparam Gen_ClkLed.divdWIDTH=23,Gen_ClkLed.divdFACTOR=10000000;//10_000_000分频; q, A! T+ `/ m; M* P2 S" X
gen_divd Gen_ClkLed(.reset(Resetb),.clkin(ClkIn),.clkout(ClkLed)); //帮忙解释一下是什么意思啊。9 W# Y! _4 w$ t7 f9 ?- h
" ~, G5 }9 ]4 x, G9 e# K
always @ (posedge ClkLed or negedge Resetb)' `$ E. k7 V6 x
if(Resetb==1'b0)
3 A& [2 x- M4 b begin
5 C; \* g2 I) U LedOut<=3'b111;
: E& `7 d# h2 C/ E1 j. |3 ^ F Cnt<=3'd0;- B( H2 N9 C1 B5 y& e* [
end
2 u& l* {* e5 @ else
$ J" Q8 _4 i! L begin M9 r i! n4 Z6 G0 ?
if(KeyIn==3'b000)
; V6 G6 O7 ?/ v$ q8 w1 R A4 U6 F begin
5 R* R! i, y5 K' f' T# p if(Cnt<=3'd2)9 h3 y+ [9 v# F& l
begin& x5 ?2 J" J6 ~+ e6 L2 T$ d* x
Cnt<=Cnt+1'b1;
. u/ ~, l& y9 t# i/ v+ P! S1 ` LedOut<={LedOut[1:0],1'b0};//拼接1 g6 K: P5 ]1 u0 T0 y& }: I
end5 I. h+ F5 h* ?# G5 R
else
4 S% Z+ } _( R% E3 n9 m5 w begin' o4 f" I9 O9 M! L+ K! Y; | O. Q9 p
Cnt<=3'd0;2 t$ ^/ A3 j# [& B6 `- W# n# G
LedOut<=3'b001;
4 O0 o' F$ K @1 c' e end( u0 {3 Y8 k* k+ v3 H- W/ [
end
8 K* ]8 d* T3 N( u) a else if(KeyIn==3'b111)
/ l; N+ B, d1 y6 w begin
* d& ]: z; k( G9 a$ s if(Cnt<=3'd2)
1 t; a8 b& X0 l- s7 z5 `/ E" Y$ Y7 | begin, T) r6 p! h+ f3 E( t' V
Cnt<=Cnt+1'b1;
# [! L, T0 B7 y LedOut<={1'b0,LedOut[2:1]};
+ V/ z8 H7 m1 a4 a1 {1 `/ s end
- u: _6 {5 @) ^ else ! [0 r: H0 T: @+ M: F: {
begin
0 }, b( M+ b5 X* `9 e+ o& }3 f* b Cnt<=3'd0;+ y) s# K, k |: @
LedOut<=3'b100;- S5 }) @3 U8 q; }4 f3 ^; ^3 Z+ A
end! G( x7 U( w( t+ a
end$ V0 C9 x5 L& M7 M7 o
end
* B3 J* Y9 I0 n( @% \# ]endmodule |
|