找回密码
 注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

巢课
电巢直播8月计划
查看: 1321|回复: 0
打印 上一主题 下一主题

SPI程序问题

[复制链接]

3

主题

37

帖子

-8929

积分

未知游客(0)

积分
-8929
跳转到指定楼层
1#
发表于 2011-3-19 05:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

EDA365欢迎您!

您需要 登录 才可以下载或查看,没有帐号?注册

x
library IEEE;
+ _/ [4 P9 o* X  c5 R( [1 }/ kuse IEEE.STD_LOGIC_1164.ALL;7 o  s! L7 \/ C3 V9 ?0 x+ j0 X0 ?, v
use IEEE.STD_LOGIC_ARITH.ALL;
" D6 B9 r- i, W4 v/ v: a  r$ [use IEEE.STD_LOGIC_UNSIGNED.ALL;, u2 O4 q' K$ e% @" k5 G
entity spi is 0 }/ J2 |' R# I; ?6 L: P
port
6 |0 u3 @6 I6 g7 ^  (: r6 L& a( {' g
   reset       : in std_logic;  --global reset signal
, u9 P) m1 W/ G/ r" t   sysclk      : in std_logic;  -- systerm clock3 ], o1 U7 Q" z3 s; p
   data_in     : in std_logic_vector(13 downto 0);/ _/ H# Q# G: ?8 [
   spi_o       : out std_logic;
# `' z, E+ L* c) E' U1 W   sck_out     : out std_logic;
" f4 n; f: ^( Q   ss_n        : out std_logic_vector(1 downto 0)3 {. P$ ^, f8 s  v' I
  );
7 H& B: I. P2 v- e5 c2 j2 ]7 Q7 rend spi;9 o+ C/ G9 I; D
architecture b of spi is! J' n) l8 j1 R: L5 ~* k) y2 Z3 ^7 \2 W+ w
  type state_type is (idle,shift,stop);  -- data type define- a% y5 \, l) l1 b- V* X
  signal state             : state_type;3 x* {% t/ _  P# T: k' J
  signal out_reg           : std_logic_vector(13 downto 0):=(others=>'0');0 ^$ h* p5 n4 b& c  Y0 o
  signal clkdiv_cnt        : std_logic_vector(3 downto 0) :=(others=>'0');) T% g0 n8 V2 o$ Q! S( N0 f6 y
  signal bit_cnt           : std_logic_vector(3 downto 0) :=(others=>'0');  k3 O' l3 V/ N, U9 L
  signal sck_o             : std_logic;0 Z+ o) Z4 ]* B1 w1 N
  signal full     : std_logic;
7 {; _7 |$ g7 `2 D   6 U& s8 C5 \9 X3 C5 |) e
begin/ o. G8 x' [0 U& I0 H4 N: n9 @
    sck_out  <= sck_o;
( W3 f1 _9 f- O    process(sysclk)' ]$ Z6 D! p) ]( u
    begin
8 H7 z/ @7 m+ Q5 e) B% ^, f+ y1 y        if (sysclk'event and sysclk = '1') then  --reset
3 l) e6 K5 }, G7 M" H+ l- p  j  N          if (reset = '1') then1 n  p/ y  L0 x  n4 x! `7 z
              ss_n        <= (others=>'1');  --AD5553 idle CS =1( ^& H5 C2 F, u/ ~0 ?0 E
              out_reg     <= (others=>'0');, M$ @4 Z- c3 O0 y
              clkdiv_cnt  <= (others=>'0');0 y( h8 U- H. X% _/ u# i
              bit_cnt     <= (others=>'0');+ B5 ?) w; |, W6 V* s+ G9 M
              spi_o       <= '1';# c% J# U4 s$ C, t7 q$ S* N: K# H
              sck_o       <= '0';      -- AD5553 SCK idle is 08 E! @9 l% ~% x4 s1 p9 q/ C1 Z
              state       <= idle;1 z/ {  O8 K3 C. W0 A" y& G
      full     <= '0';$ N5 T  t* `: ^+ N
         else
4 s' T; M, D: S( [    if(full = '0') then2 J' F4 m6 K1 K! s$ O% f
     out_reg    <= data_in ;
- b, w9 Y% @; C7 \: j2 B     full <= '1';
" F$ F' Z9 Y3 V- |+ p. x: B    end if;7 ]/ N2 m- t, s# @; {- {; R
   
( q: |2 A  d- |  @% s" w: `5 E           case state is - r: A4 |" w5 J
             when idle =>. B' g. L# U( a$ e: \8 [* B2 G
       # R1 I' V3 E' o9 F/ T+ s' c
                     state      <= shift;
& G5 J- u! {' R7 v                     spi_o      <= out_reg(13);
6 v8 v* ~, p% I: ?                     out_reg    <= out_reg(12 downto 0) & '0';7 k: |$ h& n+ w& B' b. w+ ~6 V
                     sck_o      <= '0'; % s9 U! ~! A% j# G: x
             when shift =>) `7 c- [, u0 ~$ x. N* `1 [
                  clkdiv_cnt <= clkdiv_cnt + '1';
3 E. k" X* R$ `1 |7 z: y: @4 U                  if (clkdiv_cnt(2 downto 0)="111") then7 g  ?& z1 D$ C( m
                      sck_o      <= not sck_o;) N4 {; Q& D. ]
                  end if;
0 I5 N2 A3 {- f: m$ E+ o& h* J                  
+ k7 ]7 v; H8 |* y+ i      if (clkdiv_cnt = "1111") then' T  [2 u! ]2 G1 N0 Z
                      spi_o      <= out_reg(13);
7 k$ n  p7 |) l$ c                      out_reg    <= out_reg(12 downto 0) & '0';/ ?$ B, f- E9 O' |
                      bit_cnt    <= bit_cnt + '1';0 T& X$ q! _( v  E% z# t
                  end if;$ B$ g. O" A  v1 L
                  - H: y" C0 z8 ~
      if (bit_cnt="1110" and clkdiv_cnt = "1111") then
  a5 F, b, j3 b6 Y/ D1 v                      state      <= stop;, G9 o9 f7 C/ P
                      sck_o      <= '0';: n- J4 W5 B: O2 H
                      spi_o      <= '1';( m; |1 A9 O, V. B
                  end if;+ r4 c( o  P$ g1 [2 w$ ~5 [
             4 C& U2 h. J+ S' o; \9 w
     when stop =>7 `9 e3 X- V" G( J0 k  Z# G9 o7 ~3 O
                  state       <= idle;
! R7 F2 O; X* Y* K+ F9 n8 U! Y                  sck_o       <= '0';
8 `, S) I3 U  [: M0 G$ j: h# S                  spi_o       <= '1';
+ _5 r% D( R& v8 u1 O                  clkdiv_cnt  <= (others=>'0');
, I2 d( }, [8 a' U% Q                  bit_cnt     <= (others=>'0');
2 o4 j- m5 a! |: \! W      full     <= '0';
  g- v/ v8 E0 P             when others =>
" \) [3 e! q* n5 C1 n% x                  state      <= idle;, K7 s, V5 A5 o
             end case;) F: U; E) f2 c0 T. x- n3 Q
          end if;/ F( |0 U* d4 ^- U" V9 w* B& W
        end if;8 A6 x1 A$ r7 ~: A# Y
    end process;& P+ |  n: p) o3 m  R2 R; p/ Q
end b;; _' o; p% s' m, k4 n

! [, G1 F6 t* ~: f8 [: N
: m/ ]3 H0 \) p7 [+ z5 e& w其中out_reg     一直是0,在idle状态赋不上值,大家看是怎么回事9 O& k9 J9 c1 z: B8 I  O$ x
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
收藏收藏1 支持!支持! 反对!反对!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

推荐内容上一条 /1 下一条

巢课

技术风云榜

关于我们|手机版|EDA365 ( 粤ICP备18020198号 )

GMT+8, 2025-6-2 08:13 , Processed in 0.053985 second(s), 32 queries , Gzip On.

深圳市墨知创新科技有限公司

地址:深圳市南山区科技生态园2栋A座805 电话:19926409050

快速回复 返回顶部 返回列表