搜索
bottom↓
回复: 2

我也小玩一下74HC595

[复制链接]

出0入0汤圆

发表于 2011-5-2 10:07:54 | 显示全部楼层 |阅读模式

http://www.cnblogs.com/yuphone/archive/2011/05/02/2034228.html

阿莫论坛20周年了!感谢大家的支持与爱护!!

知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)

出0入0汤圆

 楼主| 发表于 2011-5-2 10:10:05 | 显示全部楼层
顶层模块
module _74hc595_test
(
    // 全局时钟及复位信号
    input CLOCK_50,
    input nRST,
    // 74HC595接口
    output wire SER,
    output wire nG,
    output wire RCK,
    output wire SCK,
    output wire nCLR,
    // 将74HC595 QA~QH接至FPGA
    input [7:0] Q
);

function integer log2(input integer n);
    integer i;
    for(i=0; 2**i <=n; i=i+1) log2=i+1;
endfunction

localparam N_500ns=25;
reg [log2(N_500ns):1] cnt_500ns;
always@(posedge CLOCK_50, negedge nRST)
    if(!nRST) cnt_500ns <= 0;
    else if(cnt_500ns < N_500ns-1)
        cnt_500ns <= cnt_500ns + 1'b1;
    else cnt_500ns <= 0;
wire tick_500ns = (N_500ns-1 == cnt_500ns) ? 1 : 0;

reg [3:0] updata_cnt;
reg [7:0] tx_data;
reg _74hc595_enable;
always@(posedge CLOCK_50, negedge nRST)
    if(!nRST) updata_cnt <= 0;
    else if(tick_500ns) begin
        updata_cnt <= updata_cnt + 1'b1;
        case(updata_cnt)
            0: begin tx_data <= 8'h1; _74hc595_enable = 1; end
            2: begin tx_data <= 8'h2; _74hc595_enable = 1; end
            4: begin tx_data <= 8'h4; _74hc595_enable = 1; end
            6: begin tx_data <= 8'h8; _74hc595_enable = 1; end
            8: begin tx_data <= 8'h10; _74hc595_enable = 1; end
            10: begin tx_data <= 8'h20; _74hc595_enable = 1; end
            12: begin tx_data <= 8'h40; _74hc595_enable = 1; end
            14: begin tx_data <= 8'h80; _74hc595_enable = 1; end
            default : begin tx_data <= 8'h0; _74hc595_enable <= 0; end
        endcase
    end
      

_74hc595_driver _74hc595_driver_inst(
    .CLOCK_50(CLOCK_50),
    .nRST(nRST),
   
    ._74hc595_enable(_74hc595_enable),
    .output_enable(1'b1),
    .tx_data(tx_data),

    .SER(SER),
    .nG(nG),
    .RCK(RCK),
    .SCK(SCK),
    .nCLR(nCLR)
);

endmodule


74HC595驱动模块
module _74hc595_driver(
    // 输入时钟及异步复位(上电复位)信号
    input CLOCK_50,
    input nRST,
    // 74HC595控制及数据信号
    input _74hc595_enable,
    input output_enable,
    input [7:0] tx_data,
    // 74HC595接口
    output reg SER,
    output reg nG,
    output reg RCK,
    output reg SCK,
    output reg nCLR
);

function integer log2(input integer n);
    integer i;
    for(i=1'b0; 2**i <=n; i=i+1) log2=i+1'b1;
endfunction

reg [log2(17):1] cnt_20ns;
always@(posedge CLOCK_50, negedge nRST)
    if(!nRST)
        cnt_20ns <= 0;
    else if(_74hc595_enable) begin
        if(cnt_20ns < 16) cnt_20ns <= cnt_20ns + 1'b1;
        else cnt_20ns <= 0;
    end else cnt_20ns <= 0;
   
   
always@(posedge CLOCK_50, negedge nRST)
    if(!nRST) begin
        SER <= 0;
        nG <= 1;
        RCK <= 0;
        SCK <= 0;
        nCLR <= 0; // 低电平复位
    end else begin
        nCLR <= 1; // 解除复位
        
        if(output_enable) nG <= 0;
        else nG <= 1;
        
        if(_74hc595_enable) begin
        
            // 产生SCK信号
            case(cnt_20ns)
                0,2,4,6,8,10,12,14 : SCK <= 0;
                1,3,5,7,9,11,13,15: SCK <= 1;
                16 : SCK <= 0;
                default : ; // 缺省不操作
            endcase
            
            // 产生RCK信号
            case(cnt_20ns)
                16: RCK <= 1;
                default: RCK <= 0;            
            endcase
            
            // 送出串型数据
            case(cnt_20ns)
                0,1 : SER <= tx_data[7];
                2,3 : SER <= tx_data[6];
                4,5 : SER <= tx_data[5];
                6,7 : SER <= tx_data[4];
                8,9 : SER <= tx_data[3];
                10,11 : SER <= tx_data[2];
                12,13 : SER <= tx_data[1];
                14,15 : SER <= tx_data[0];
                default: SER <= 0;
            endcase
        end else begin
            SCK <= 0;
            RCK <= 0;
            SER <= 0;
        end        
    end

endmodule

出0入0汤圆

发表于 2011-5-2 16:18:34 | 显示全部楼层
mask
学习!!!!
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-7-24 15:29

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

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