Giter Club home page Giter Club logo

dutycounter's Introduction

DutyCounter

This is a simple Verilog project to measure the duty of a PWM wave.
这是一个检测PWM波形占空比的Verilog项目。

To begin with

程序利用Artix-7开发板上自带的100MHz时钟,使用两个计数器分别记录PWM波形输入高电平和低电平的时间,并由此算出周期。 程序将会在FPGA开发板(本样例中使用Basys3)的JA1针脚读取输入的波形,并在JB1针脚输出高电平时间,在JB2针脚输出周期。
通过以上的两个输出值即可计算出占空比,占空比计算公式为 Duty
项目包含四个主要的源文件,为top.vout.vpara2serial.vPWM_cnt.v,同时包含一个仿真文件DutyCounter_tb.v.

About top.v

该文件为项目的顶层文件,输入为CLK(连接到板上时钟)和PWM_in(连接到外部输入波形),输出为High_timeCycle_time,即高电平时间和总周期。

About PWM_cnt.v

该文件为核心的计数代码,由于最长的周期为1s,最大的占空比为90%,因此,最长的计数应当为90M个时钟周期,对应二进制‭0101_0101_1101_0100_1010_1000_0000‬, 因此需要27位计数器,最长的周期为100M个时钟周期,对应‭0101_1111_0101_1110_0001_0000_0000‬,也需要27位寄存器来存储。
当输入波形发生跳变时,开始对应状态的计时器,并且将另一状态的计时结果送入缓冲区,然后清零。输出的计数值与缓冲区的值同步。

About para2serial.v

计数器输出的值为并行输出,输出高达27位,而如果要同时传输高电平时间和总周期需要54位并行传输,这是不现实的,由于最大要求能够辨识的频率也只要5MHz,所以 完全可以选择使用串行的方式输出。
该文件为将并行信号转为串行信号的模块,输出开始的信号为load,将由下一个文件进行定义。。
本文件可以自定义输入并行信号的位宽,默认为27位。闲置时,输出恒为低电平,当输出开始时,输出一个高电平作为输出开始的标志,之后的N位为输出内容。

About out.v

该文件负责控制开始串行转换并传输输出信号的时机。
由于High_timeCycle_time的输出彼此之间是并行的,因此传输完一次需要28个时钟周期。所以选择每50个时钟周期周期发送一次。 如果要将输出改为全串行,则发送周期最小为100个时钟周期。
当计时器计满50后,在下一个周期将flag_reg置为1,同时与之同步的flag也会产生一个脉冲,控制串行转换模块开始工作。

About DutyCounter_tb.v (Test Bench)

仿真环境中,将CLK设置为#5 CLK = ~CLK; 即时钟周期为10ns。
输入的PWM波形为一个周期为2000ns,占空比为75%的PWM波。
DutyCounter_tb_result
观察下图示波器图像,可以看出输出的串行传输的值和输出的周期均符合要求。
DutyCounter_tb_scope
通过下图变量窗口可以看到电路内部并行传输的中间值,其中高电平时间为150个时钟周期,低电平时间为50个时钟周期,总周期为200个时钟周期,与输入波形相符。
DutyCounter_tb_variables
综上所述,本项目基本满足要求。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.