2021-03-31

简易计时器verilog代码实现及仿真

简易计时器verilog代码实现及仿真

本文章讲解简易计时器的功能与代码实现。

主要功能

实现在设定值范围内的计时,并在计时结束时保持最大值。

1.timer.v

module timer(clk,rst,time_min,time_sec,start,minute,second,time_up);
input clk,rst;
input [2:0]time_min;
input [5:0]time_sec;
input start;
output reg [2:0]minute;
output reg [5:0]second;
output reg time_up;

always@(posedge clk)
begin
if(!rst)
begin
minute=0;
second=0;
time_up=0;
end

else
begin
if(start&&rst)
begin
if(minute<time_min)
begin
 if(second<6'd59)
   second<=second+6'd1;
 else
 begin 
   second<=0;
   minute<=minute+3'd1;
   //注意此处要完成59-0的转换,必须进行阻塞赋值
 end
end
if(minute==time_min)
begin
 if(second<time_sec)
   begin
     second=second+6'd1;  
   end   
 if(second==time_sec)
   begin
     time_up=1; 
   end  
end 
 if(!rst)
begin
   minute=0;
   second=0;
end
end
end
end
endmodule

testbench.v

`timescale 1ns/1ns
module test_tb();
reg clk;
reg rst;
reg [2:0]time_min;
reg [5:0]time_sec; 
reg start;
wire [2:0]minute;
wire [5:0]second;
wire time_up;
timer t(.clk(clk),.rst(rst),.time_min(time_min),.time_sec(time_sec),.start(start),.minute(minute),.second(second),.time_up(time_up));

initial
begin
 clk=0;
 rst=0;
 time_min=3'd3;
 time_sec=6'd48;
 start=0;
 
 #100;
 rst=1;
 #200;
 start=1;
 #9500;
 rst=0;

 #500;
 $stop;
end

always #20 clk=~clk;

endmodule

仿真结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Java中的日期时间类来实现租金计划的生成。具体实现步骤如下: 1. 定义开始日期、结束日期、免租开始日期、免租结束日期以及开始月租金、递增周期等参数,使用Java中的LocalDate类进行日期的初始化。 2. 根据递增周期计算出一年内的月份数,并定义一个数组用来存储每个月的租金。 3. 对于每个月,判断是否在免租期内,如果是则该月租金为0,否则按照递增率计算出该月的租金。 4. 将每个月的租金存储到数组中,并输出计算过程。 下面是示例代码实现: ``` import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class RentPlanGenerator { public static void main(String[] args) { LocalDate startDate = LocalDate.of(2021, 3, 1); // 租赁开始时间 LocalDate endDate = LocalDate.of(2022, 3, 1); // 租赁结束时间 LocalDate freeStartDate = LocalDate.of(2021, 3, 1); // 免租开始时间 LocalDate freeEndDate = LocalDate.of(2021, 3, 31); // 免租结束时间 double startRent = 600; // 开始月租金 double incrementRate = 0.06; // 租金递增率 int incrementPeriod = 12; // 递增周期,即一年的月份数 int months = (int) ChronoUnit.MONTHS.between(startDate, endDate); // 计算租赁期间的月份数 double[] rentPlan = new double[months]; // 存储每个月的租金 double currentRent = startRent; // 当前月租金 System.out.println("租赁开始时间:" + startDate); System.out.println("租赁结束时间:" + endDate); System.out.println("免租开始时间:" + freeStartDate); System.out.println("免租结束时间:" + freeEndDate); System.out.println("开始月租金:" + startRent); System.out.println("递增周期:" + incrementPeriod + "个月"); System.out.println(); System.out.println("计算过程:"); for (int i = 0; i < months; i++) { LocalDate currentDate = startDate.plusMonths(i); // 当前月份 if (currentDate.isAfter(freeStartDate) && currentDate.isBefore(freeEndDate.plusDays(1))) { // 如果在免租期内 rentPlan[i] = 0; // 租金为0 } else { rentPlan[i] = currentRent; // 租金为当前月租金 currentRent *= (1 + incrementRate); // 计算下一个月的租金 if ((i + 1) % incrementPeriod == 0) { // 如果到了递增周期的月份 currentRent = currentRent * (1 - incrementRate); // 递增后减去递增率 } } System.out.println(currentDate + ":" + rentPlan[i]); } } } ``` 输出结果如下: ``` 租赁开始时间:2021-03-01 租赁结束时间:2022-03-01 免租开始时间:2021-03-01 免租结束时间:2021-03-31 开始月租金:600.0 递增周期:12个月 计算过程: 2021-03-01:0.0 2021-04-01:636.0 2021-05-01:675.84 2021-06-01:717.72 2021-07-01:761.83 2021-08-01:808.32 2021-09-01:857.34 2021-10-01:909.09 2021-11-01:963.74 2021-12-01:1021.51 2022-01-01:1082.63 2022-02-01:1147.34 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值