zoukankan      html  css  js  c++  java
  • 02 LED翻转与计数器使用

    一、  设计定义:

    计数器设计与验证

    LED,每500ms,状态翻转一次也就是亮灭。

    第一步:

    系统时钟频率为50M,对应为T= =20ns

    计数周期或者时间是500ms,计数次数的计算:

    计数值=(500ms= 500_000_000ns)/20=25_000_000

    第二步:

    对计数值位宽的计算:

    打开电脑的计数器,输入计算值,可以得到计数值的十六进制为17D_7840.

    由于最高位为1,用一个二进制位1表示即可,其他六位十六进制用四个二进制位表示。

    位宽=1+6*4=25(位宽等于:最高位的二进制位数+剩余的位数*4)

     

    二设计输入(代码)

    这个设计分为两个部分:一是500ms的计数器。二是LED控制,每500MS翻转一次。

    module counter_led

    (

        clk50M,

        rst_n,

        led

    );

        input clk50M;   //the frequence system clock

        input rst_n;    //global reset,active low level

        output led;     //the output IO/port

       

        parameter T500Ms = 25'd24_999_999;

        reg [24:0]cnt;  //the shortening of counter,define the counter register

       

        //the counting process of 500Ms counter

        always@(posedge clk50M or negedge rst_n)

            if(!rst_n)

                cnt<=25'd0;

            else if(cnt==T500Ms)

                cnt<=25'd0;

            else

                cnt<=cnt+1'b1;

               

            //  the concrl of the output port of led

            reg rled;

            always@(posedge clk50M or negedge rst_n)

            if(!rst_n)

                rled<=1'b1;

            else if(cnt==T500Ms)

                rled<=~rled;

            else

                rled<=rled;

               

        assign led = rled;

       

    endmodule

    三 仿真

    //设计的Testbench文件:

    `timescale 1ns/1ns

    module counter_led_tb;

        reg clk;

        reg rst;

        wire rled;

        counter_led counter_led_m0

    (

        .clk50M(clk),

        .rst_n(rst),

        .led(rled)

    );

        initial clk=1;

        always begin

            #10 clk=~clk;

        end

        initial begin

            rst=0;#2000;

            rst=1;#200000;

            rst=0;#500_000_000;

            rst=1;#600_000_000;

            $stop;

        end

    endmodule

    仿真波形如下:

     

    仿真波形正确,led在1000Ms时翻转一次,实现了功能。

    今天是我做的一个小实验,LED每500MS翻转一次,很顺利就实现了功能。虽然实验简单,我也学到了知识。一是计数器的使用。计时就是计数。二是LED灯的控制,如翻转和亮灭。三是写文档可以用notepad++和word,排版轻松。我会继续加油!

  • 相关阅读:
    攻防一体 暴力攻击
    新的亮眼的但不彻底的交互
    利用物联网或智能化区分产品
    Character Sets, Collation, Unicode :: utf8_unicode_ci vs utf8_general_ci
    容灾 RPO RTO
    微信找人代付 下单账号 支付账号
    微信公众号 openId 支付 php中file_get_contents与curl性能比较分析
    t
    accesstoken 中控服务器 并发刷新 加并发锁
    a
  • 原文地址:https://www.cnblogs.com/Xwangzi66/p/12580340.html
Copyright © 2011-2022 走看看