Verilog出租车计价器
时间: 2023-12-15 12:06:19 浏览: 53
根据提供的引用内容,出租车计价器需要实现以下功能:
1. 显示里程,精确到1公里;
2. 显示金额,精确到1元;
3. 根据行程计算价格,起步价设为5元,基本里程设为3公里,每多行一公里,在起步价上加2元;
4. 等待时计费,每等待一分钟加1元,不足一分钟的按一分钟计算;
5. 用脉冲信号模拟轮胎的转数,设每计一个脉冲汽车前进100米。
以下是一个Verilog出租车计价器的实现,其中包括计价器的各个模块以及顶层模块的代码:
```verilog
// 计价器顶层模块
module taxi_meter(
input clk, // 时钟信号
input rst, // 复位信号
input start, // 启动信号
input stop, // 停止信号
input wait, // 等待信号
input [15:0] pulse, // 脉冲信号
output reg [15:0] distance, // 里程
output reg [15:0] price // 价格
);
// 计价器状态定义
parameter IDLE = 2'b00; // 空闲状态
parameter RUNNING = 2'b01; // 行驶状态
parameter WAITING = 2'b10; // 等待状态
// 计价器参数定义
parameter START_PRICE = 16'd500; // 起步价,5元
parameter BASE_DISTANCE = 16'd3000; // 基本里程,3公里
parameter PRICE_PER_KM = 16'd200; // 每公里价格,2元
parameter WAITING_PRICE = 16'd100; // 等待价格,1元/分钟
// 计价器状态寄存器
reg [1:0] state = IDLE;
// 计价器计数器
reg [15:0] count = 16'd0;
// 计价器里程计数器
reg [15:0] distance_count = 16'd0;
// 计价器价格计数器
reg [15:0] price_count = START_PRICE;
// 计价器等待计数器
reg [15:0] wait_count = 16'd0;
// 计价器等待标志
reg waiting = 1'b0;
// 计价器启动信号处理
always @(posedge clk) begin
if (rst) begin
state <= IDLE;
count <= 16'd0;
distance_count <= 16'd0;
price_count <= START_PRICE;
wait_count <= 16'd0;
waiting <= 1'b0;
end else begin
case (state)
IDLE: begin
if (start) begin
state <= RUNNING;
count <= 16'd0;
distance_count <= 16'd0;
price_count <= START_PRICE;
wait_count <= 16'd0;
waiting <= 1'b0;
end
end
RUNNING: begin
if (stop) begin
state <= IDLE;
distance <= distance_count / 1000;
price <= price_count;
end else if (wait) begin
state <= WAITING;
waiting <= 1'b1;
end else begin
count <= count + 1;
if (count == 16'd5) begin
count <= 16'd0;
distance_count <= distance_count + pulse;
price_count <= price_count + PRICE_PER_KM;
end
end
end
WAITING: begin
if (!wait) begin
state <= RUNNING;
waiting <= 1'b0;
end else begin
wait_count <= wait_count + 1;
if (wait_count == 16'd60) begin
wait_count <= 16'd0;
price_count <= price_count + WAITING_PRICE;
end
end
end
endcase
end
end
endmodule
// 脉冲计数器模块
module pulse_counter(
input clk, // 时钟信号
input rst, // 复位信号
input pulse, // 脉冲信号
output reg [15:0] count // 脉冲计数器
);
always @(posedge clk) begin
if (rst) begin
count <= 16'd0;
end else begin
count <= count + pulse;
end
end
endmodule
// 顶层模块
module top(
input clk, // 时钟信号
input rst, // 复位信号
input start, // 启动信号
input stop, // 停止信号
input wait, // 等待信号
input [15:0] pulse, // 脉冲信号
output reg [15:0] distance, // 里程
output reg [15:0] price // 价格
);
wire [15:0] pulse_count;
pulse_counter pulse_counter_inst(
.clk(clk),
.rst(rst),
.pulse(pulse),
.count(pulse_count)
);
taxi_meter taxi_meter_inst(
.clk(clk),
.rst(rst),
.start(start),
.stop(stop),
.wait(wait),
.pulse(pulse_count),
.distance(distance),
.price(price)
);
endmodule
```