|
EDA365欢迎您!
您需要 登录 才可以下载或查看,没有帐号?注册
x
library IEEE;
/ I) ^* q$ n: x( R5 tuse IEEE.STD_LOGIC_1164.ALL;
* E8 E) }$ q, K3 V8 i' l7 Zuse IEEE.STD_LOGIC_ARITH.ALL;1 h( X9 T' @6 i3 I% w
use IEEE.STD_LOGIC_UNSIGNED.ALL;
. w( @" K( J0 G$ W( L0 W3 j3 B9 ventity spi is
* U1 [6 K: Y2 |port $ X+ L9 t$ T7 \% B+ D) l- t
(
8 G& ^( |* ?) ]! h reset : in std_logic; --global reset signal
3 b+ Q/ e8 u# U: |5 F9 b sysclk : in std_logic; -- systerm clock1 Y7 o+ v3 ~$ J" T+ S: s J: |- Z5 J
data_in : in std_logic_vector(13 downto 0);
c7 A# ?, p* V. r spi_o : out std_logic;
- O8 L: V1 l2 B% ^9 y8 ~+ t9 {0 v sck_out : out std_logic;$ G, N6 K! j2 G5 e& e- p& J
ss_n : out std_logic_vector(1 downto 0)
5 c9 ]: L" T6 C- T; C: A! m );
# o/ F- ~( V2 hend spi;
, l1 h5 \5 s, t7 P3 @( b+ X) Earchitecture b of spi is
4 z' u8 h1 I3 |" x: n' Z4 Q! b type state_type is (idle,shift,stop); -- data type define
/ C4 o1 r" v- {. ~4 B1 q4 t( } signal state : state_type;
1 k9 {( w6 O: V2 ?! e9 z signal out_reg : std_logic_vector(13 downto 0):=(others=>'0');- H5 s3 ^! ?$ R* Q
signal clkdiv_cnt : std_logic_vector(3 downto 0) :=(others=>'0');' y* D) R% b: t6 V c) x- B
signal bit_cnt : std_logic_vector(3 downto 0) :=(others=>'0');
1 W: l# h+ j% k signal sck_o : std_logic;
0 }6 o6 {% o t/ r signal full : std_logic;
( a5 y7 y2 z8 e. }; N, r, g# P
) {" o# b5 w- w, r, T3 wbegin
& R0 `8 x7 X& C# M/ n sck_out <= sck_o;
+ n+ [" m3 t8 Y( p process(sysclk)
8 [5 ? W, ^! D! S3 E3 {$ g% [ begin
" b2 D* f* t) t& X; n/ o if (sysclk'event and sysclk = '1') then --reset
/ k" E1 v- }) K h k) [0 K if (reset = '1') then
. s# k1 D8 `' j Y$ L4 `5 e ss_n <= (others=>'1'); --AD5553 idle CS =1 V& [0 Q. n+ b) K! N
out_reg <= (others=>'0');
% g9 f' V, [# p& a7 H0 a9 b clkdiv_cnt <= (others=>'0');/ i9 u7 r- X6 q; v8 X3 k
bit_cnt <= (others=>'0');
+ A$ c# o+ l8 ~* ~- R) w# M1 c spi_o <= '1';
/ w$ B. J' j# `+ s0 I( J. ? sck_o <= '0'; -- AD5553 SCK idle is 0! |; \( z# U; j/ a* o# E" m
state <= idle;( B4 n4 W1 ]! k# S5 r. i" s+ s& M
full <= '0';& f" c: o$ S+ @) w7 c
else 5 y$ ?& u1 r8 @6 W. O
if(full = '0') then
, j4 g3 O P# `. `8 N9 ^0 V( U: {0 S out_reg <= data_in ;
: V9 J! K7 T& w/ Z7 j/ D& t$ e" \) e full <= '1';
& E3 t. ~; a p. L5 m4 ^& R end if;. [. b( j) V1 R+ ~# Q2 G! L
5 h( E1 e; V* g( o9 B3 ]
case state is
- P" N( T( {7 k2 l- q7 B when idle =>
" l7 j- n% ]: ^7 U" K ( F6 W# c2 R; \' R3 \& N
state <= shift;, F7 N" q; n% Q K9 O
spi_o <= out_reg(13);: h" A2 |2 W/ C5 E$ y
out_reg <= out_reg(12 downto 0) & '0';
4 i3 O) M$ w3 h8 G! b sck_o <= '0'; % j9 E( N7 z9 Z5 F% H9 D
when shift =>
3 `1 L( i$ j: d clkdiv_cnt <= clkdiv_cnt + '1';
5 w% X/ h& {+ X9 M2 l8 } if (clkdiv_cnt(2 downto 0)="111") then
/ }% m; k7 t2 V0 J% R sck_o <= not sck_o;
. k1 ^/ i; j2 V* i) e end if;
3 j) U( s" H1 m& m1 r4 Z1 l" }) { . y/ x( D( X2 X: o# C
if (clkdiv_cnt = "1111") then2 |" y& Y& {8 P5 X* x: R
spi_o <= out_reg(13);+ ^0 B$ @1 a( f' A& k; J
out_reg <= out_reg(12 downto 0) & '0';
; P% `5 T" j" z: Q+ | bit_cnt <= bit_cnt + '1';
& v1 D |3 k" ]7 Y- @ end if;
" ^! e9 q6 I* w
$ g, T2 T- N3 J, H7 g' [1 \8 _( F if (bit_cnt="1110" and clkdiv_cnt = "1111") then; v9 \3 U0 P9 v+ J" A
state <= stop;% Z/ ?5 w- J$ h& l: f$ g
sck_o <= '0';
& f( y! o5 y% n4 N \- g+ Q spi_o <= '1';/ W* \9 q/ R: }1 I0 I$ P! ? c
end if;! o# a( C* Y) f6 l# s
2 J) L- b9 _6 r% }' ~ when stop => N! Q/ q3 c, M2 ?( C3 {, K2 r
state <= idle;# k$ V V/ a/ }
sck_o <= '0';
+ M+ t$ L) r1 z- I& O% U spi_o <= '1';2 y L8 \7 X+ [ E7 R5 H4 Z
clkdiv_cnt <= (others=>'0');) o J* ^ u3 w' }, w7 D1 w
bit_cnt <= (others=>'0');& @* A& n" O& x6 k# g
full <= '0';
. J: x7 C( G/ _ when others =>
" {) d2 b, Y, d2 A/ P0 [! } state <= idle;
- q6 t0 M, x" @6 E- L ?* f end case;
6 [5 c' ^9 s( |! i( O end if;
1 @: W+ L) e" \ end if;
8 a" ~# }+ h, N* [! P end process;
: ~. v5 q' \8 z3 S1 y Qend b;1 B# f; P8 X" D* w
* Y! v$ D1 T6 o# [$ [: l
7 n+ s7 p+ j0 ?: i* K# B( w! {2 l( D其中out_reg 一直是0,在idle状态赋不上值,大家看是怎么回事
) g; w' B2 Q2 V5 e: p |
|