|
EDA365欢迎您!
您需要 登录 才可以下载或查看,没有帐号?注册
x
library IEEE;
# I6 `, ]5 L3 E4 X huse IEEE.STD_LOGIC_1164.ALL;
1 |: R* [, n/ d2 x, T9 g% H! buse IEEE.STD_LOGIC_ARITH.ALL;
* H4 n6 c: h! I7 Tuse IEEE.STD_LOGIC_UNSIGNED.ALL;
3 Q& ?8 v3 b+ ~" ]3 {$ P% S" P! Lentity spi is : z/ n1 W; p' t& ^) `5 S8 G$ |
port
- S# _6 T' C" i e( D% I0 J7 J. [) u (0 l( e6 _1 H1 p; K" M; ?) y
reset : in std_logic; --global reset signal
1 a- c5 `* R$ v& A% Y. b0 p sysclk : in std_logic; -- systerm clock7 b: r6 M/ w F7 T( x7 {; @9 A* n! `
data_in : in std_logic_vector(13 downto 0);6 p0 F q3 P8 w1 v% T: J L8 c
spi_o : out std_logic;
7 `7 [' A/ r$ F) z/ M0 y sck_out : out std_logic;1 Y D. b7 p3 |& c/ ]/ S2 i- f
ss_n : out std_logic_vector(1 downto 0)
+ U O+ A- Y, y- Q+ y/ \4 g: P );3 [. `. ?. |5 M: S3 m2 Z
end spi;( F( \6 C R+ z& J0 Q. c: p* g
architecture b of spi is
+ A4 I ] V' S' G type state_type is (idle,shift,stop); -- data type define
" _/ X9 I4 ~2 p3 q* g signal state : state_type;0 K0 u+ q [& ]) H* o) U
signal out_reg : std_logic_vector(13 downto 0):=(others=>'0');3 ~8 P" A# {4 s7 N' p1 R+ z( s, G8 h
signal clkdiv_cnt : std_logic_vector(3 downto 0) :=(others=>'0');# X( E h0 h, U6 W( G0 J" Q' `
signal bit_cnt : std_logic_vector(3 downto 0) :=(others=>'0');
+ k2 n3 W6 {0 A" f signal sck_o : std_logic;2 U3 J( n- E( r: L% f" N3 h% N
signal full : std_logic;
9 l7 u% C# \9 l% `% a: ^# ~
9 q( y. u5 S. f( h1 E& Ubegin
' N5 m+ A) x& G6 A! E" [: j6 G% L sck_out <= sck_o;
1 L8 i' ~1 H: i& s0 M process(sysclk)
) G* N, P& V+ ~ begin0 k0 C. \* o; H# V7 j
if (sysclk'event and sysclk = '1') then --reset
* b/ |' @& x% G. f6 X+ O if (reset = '1') then
7 C; c7 g2 n9 G1 ?- o, n3 e, |- U ss_n <= (others=>'1'); --AD5553 idle CS =1: M3 A8 g' a) t; A0 ?: \- L8 c
out_reg <= (others=>'0');# V8 v" m9 D$ e% W6 S
clkdiv_cnt <= (others=>'0');1 J: D2 N8 Y' x/ P! G- K; F
bit_cnt <= (others=>'0');* n" Z2 f& g2 j7 { b
spi_o <= '1';
+ [7 T5 I" u7 g7 l9 c: R sck_o <= '0'; -- AD5553 SCK idle is 0
( n# x: ?/ d* j state <= idle;! h9 T3 D* H0 E0 o2 r- z
full <= '0';
) H1 B% `7 f( }+ ]$ S8 ^& S* k else
+ p1 p5 h( B/ J$ N5 v+ c) d7 N$ x if(full = '0') then( Y1 s: f" V6 U
out_reg <= data_in ;
. G& _# {' M0 y1 ] full <= '1';
4 D$ S7 {6 a7 v9 q! r/ |5 o) I end if;" B& f+ U. q# T4 u& g& V/ b
- R2 W" Q. e6 A
case state is
' x+ y' ~ T" h+ J P when idle =>
/ m: O* m% E8 l' w7 h" V) F7 O 3 m: f J1 e& l8 [" N) r- a# l
state <= shift;6 @+ h. _$ P7 ~) K! K3 h, W
spi_o <= out_reg(13);
& C6 [) e( L# F2 E' ` out_reg <= out_reg(12 downto 0) & '0';& P- s; s0 J, i9 F; ^! {
sck_o <= '0'; $ ?; S" D% z) |* x- D* \: a( ?
when shift =>% a4 O6 `+ C* K1 z t
clkdiv_cnt <= clkdiv_cnt + '1';
( p4 F8 d6 n& i0 P- q if (clkdiv_cnt(2 downto 0)="111") then
; |7 O& U8 q: o/ ?* L0 \$ P sck_o <= not sck_o;
- O9 L2 L9 c' @; k4 }% d8 D: X; X end if;
" V1 D U! X3 q8 A4 j, I # x) w9 Z0 e! @7 {% Q
if (clkdiv_cnt = "1111") then
+ g/ H, f/ S/ S0 f: \. q spi_o <= out_reg(13);
% T' z6 A' C( \& d0 p% W! m* r out_reg <= out_reg(12 downto 0) & '0';
$ l, s1 B2 P; s* t% ^ bit_cnt <= bit_cnt + '1';
( z- X0 g; f# r( ^; D ~. |: w2 X end if;5 x m) Y) }# g
- K1 ~9 E* A4 _1 F/ g* |7 M2 [ if (bit_cnt="1110" and clkdiv_cnt = "1111") then
/ P2 L. ^! z4 j state <= stop;
1 b7 C0 N4 ~$ D9 r* N sck_o <= '0';, d- `7 ?) K |
spi_o <= '1';
# m- B. b. e6 E! T M end if;$ I I1 k9 V' C2 e, e- a$ P; Z! r3 e
3 z8 X% s0 G/ e2 J8 J" n
when stop =>
, q0 L c! E0 V" A state <= idle;
7 b2 Q. Y9 X- x3 r) \9 ^, y# Q sck_o <= '0';! m8 ^4 C* K8 s- U" X, v, c3 d
spi_o <= '1';
1 C/ Y6 C9 j# K! a0 P# D clkdiv_cnt <= (others=>'0');/ |, H- q5 r8 N7 k8 f/ H
bit_cnt <= (others=>'0');
! B. P: I# p8 Z i- C+ E$ n8 M full <= '0';
- f% n! {) W& j when others =>
V) z, s: }# K! G2 z* X% Z state <= idle;
. C9 p- ?2 o# m8 d/ k8 p; C end case;
3 C2 @% U- ~+ V: s& ` end if;; T8 Q1 H$ H* c# F$ e P
end if;
8 i0 T' b e, h9 ?" B# h c end process;
5 ]3 F6 N1 I Q# pend b;
: T9 n. B& ~! c* e2 h
+ v* o v9 B& P: a" |( \" |( x
$ \' o3 ` q' Z其中out_reg 一直是0,在idle状态赋不上值,大家看是怎么回事
; [" F5 h s9 D2 U$ n+ P7 A% A& O; r |
|