目录
一、项目功能
二、设计思路
按键实现:
数码管
蜂鸣器
LED灯
三、流程图
四、代码实现
1、按键消抖 key_debounce.v
2、LED状态选择 led_drive.v
3、蜂鸣器模块 beep_drive.v
4、数码管位选信号选择 sel_drive.v
5、数码管段选信号选择 seg_drive.v
6、售货机按键选择处理 machine_drive.v
7、音符彩灯实现 lanterns.v
8、音符选择 freq_select.v
9、顶层文件 top_vending_machine
五、测试
RTL门级电路查看编辑
硬件测试:
六、总结
一、项目功能
1.默认只接收0.5元、1元投币。
⒉货物有4种可以选择,价格分别为0.5,1.5,2.4,3元。
3.满足当前选择的商品价格后自动出货,出货动作用4个LED做跑马灯(闪烁2s)表示。
4.超过前选择的商品价格之后,自动出货并找零,动作用4个LED做跑马灯表示(同样闪烁2s)。
5.显示当前投币的总额、当前选择的商品的价格以及找零的数目。
6.复位时播放音乐并显示彩灯。
7.投币不足目标价格时可以取消,动作用灯闪烁表示(2s)。
二、设计思路
按键按下,KEY输出值为低电平,否则为高电平。
按键实现:
每个按键对应相应的功能:
key0:退货
key1:选择不同的价格(0.5;1.5;2.4,3.0)
key2:投币,按一次个位增加5,即为0.5
key3:投币,按一次十位增加1,即增加1
数码管
数码管通过位选信号控制指定数码管,给0信号值,表示选中此数码管,给1信号值,表示未选中此应数码管,段选信号控制数码管上对应的led灯,数码管的LED灯共阳极,给低电平亮。
数码管显示数字真值表
数码管实现:
数码管6位,从左往右,依次是两位的投币输入金额,两位的商品价格,以及两位的找零金额。初始显示1位,两位,1位,位选总共六种显示状态,段选10种状态
蜂鸣器
不同的音符振动频率不同,周期T=1/频率f
根据上图可以计算出音符振动的周期,单位微秒。Cyclone IV开发板的晶振是50MHz,振动一次是20纳秒,使用周期时间除以20纳秒得出音符振动的次数 。比如高音的DO计算方式如下公式所示。
DO(高)=955*1000/20=47750
按下key0后,蜂鸣器播放5秒的音乐,(音符+每个音符的空白时间),每个音符对应一个外接彩灯闪烁灯
按下key1,key2,key3中任意一个,蜂鸣器会有0.2s的提示音
LED灯
LDE灯对应8种状态:全灭;全亮;只亮led[0];只亮led[1];只亮led[2];只亮led[3];流水灯;闪烁
三、流程图
四、代码实现
1、按键消抖 key_debounce.v
module key_debounce(input wire clk,input wire rst_n,input wire key,output reg flag,// 0抖动, 1抖动结束output reg key_value//key抖动结束后的值
);parameter MAX_NUM = 20'd1_000_000;reg [19:0] delay_cnt;//1_000_000reg key_reg;//key上一次的值always @(posedge clk or negedge rst_n) beginif(!rst_n) beginkey_reg <= 1;delay_cnt <= 0;endelse beginkey_reg <= key;//当key为1 key 为0 表示按下抖动,开始计时if(key_reg != key ) begin delay_cnt <= MAX_NUM ;endelse beginif(delay_cnt > 0)delay_cnt <= delay_cnt -1;elsedelay_cnt <= 0;endend
end//当计时完成,获取key的值
always @(posedge clk or negedge rst_n) beginif(!rst_n) beginflag <= 0;key_value <= 1;endelse begin// 计时完成 处于稳定状态,进行赋值if(delay_cnt == 1) beginflag <= 1;key_value <= key;endelse beginflag <= 0;key_value <= key_value;endend
endendmodule
2、LED状态选择 led_drive.v
module led_drive (input wire clk ,//时钟信号input wire rst_n,//复位信号input wire [3:0] value,//LED显示状态output reg [3:0] led //4个LED输出
);
/*value 效果0 全灭1 全亮2 只亮led[0]3 只亮led[1]4 只亮led[2]5 只亮led[3]6 流水灯7 闪烁
*/
parameter MAX_TIME_RUNNING = 28'd4_000_000; //流水灯频率0.08s
parameter MAX_TIME_FLASH = 28'd10_000_000; //闪烁频率0.2sreg [27:0] cnt_time_running ; //流水灯计时器
reg [27:0] cnt_time_flash; //闪烁灯计时器reg [7:0] led_running; //流水灯状态寄存器
reg [3:0] led_flash; //闪烁灯状态寄存器//流水灯计数器0.08s
always @(posedge clk or negedge rst_n) beginif(!rst_n)cnt_time_running <=28'd1;else if(value == 4'd6) beginif(cnt_time_running == MAX_TIME_RUNNING)cnt_time_running <=28'd1;elsecnt_time_running <= cnt_time_running+28'd1;endelse cnt_time_running <= 28'd1;
end//闪烁灯计数器0.2s
always @(posedge clk or negedge rst_n) beginif(!rst_n)cnt_time_flash <=28'd1;else if (value == 4'd7) beginif(cnt_time_flash == MAX_TIME_FLASH )cnt_time_flash <=28'd1;elsecnt_time_flash <= cnt_time_flash+28'd1;endelse cnt_time_flash <= 28'd1;
end//流水灯状态切换 间隔0.08s
always @(posedge clk or negedge rst_n) beginif(!rst_n)led_running <= 8'b00001111;else if(cnt_time_running == MAX_TIME_RUNNING)beginled_running <= {led_running[0],led_running[7:1]};endelse led_running <=led_running;end//闪烁状态切换 间隔0.2s
always @(posedge clk or negedge rst_n) beginif(!rst_n)led_flash <= 4'b0000;else if(cnt_time_flash == MAX_TIME_FLASH)beginled_flash <= ~led_flash;endelse led_flash <=led_flash;end//根据value值输出对应灯效果
always @(*) begincase(value)4'd0: beginled = 4'b0000;//默认状态LED全灭end4'd1:beginled = 4'b1111;//end4'd2:beginled = 4'b0001;//选择第一种商品end4'd3:beginled = 4'b0010;//选择第二种商品end4'd4:beginled = 4'b0100;//选择第三种商品end4'd5:beginled = 4'b1000;//选择第四种商品end4'd6: beginled = led_running[3:0];//购买成功找零不找零,流水灯end4'd7:beginled = led_flash;//取消订单,闪烁enddefault : led = 4'b0000;endcase
end
endmodule
3、蜂鸣器模块 beep_drive.v
module beep_drive (input wire clk,input wire rst_n,input wire flag, //蜂鸣器开始鸣叫input wire status,output reg beep
);parameter MAX_TIME = 24'd10_000_000; //鸣叫时间
parameter MAX_TIME_MUSIC = 28'd250_000_000; //音乐播放时间reg [23:0] cnt_time; //计时
reg [27:0] cnt_time_music; //音乐播放计时器
reg flag_beep_time_out; // 计时是否结束//音乐播放计时
always @(posedge clk or negedge rst_n) beginif(!rst_n) begincnt_time_music <= 28'd0;endelse if(cnt_time_music < MAX_TIME_MUSIC) begincnt_time_music <= cnt_time_music + 28'd1;endelse cnt_time_music <= cnt_time_music;
end//蜂鸣器输出
always @(posedge clk or negedge rst_n) beginif(!rst_n) begincnt_time <= 0;beep <= 1;flag_beep_time_out <= 1;endelse if(!status && cnt_time_music < MAX_TIME_MUSIC) beginbeep <= 0;endelse if(status && cnt_time_music < MAX_TIME_MUSIC) beginbeep <= 1;endelse if(flag && flag_beep_time_out) begin //开始鸣叫cnt_time <= MAX_TIME;flag_beep_time_out <= 0;endelse if(cnt_time >=1 && !flag_beep_time_out) begincnt_time <= cnt_time -24'd1;beep <= 0;endelse if(cnt_time == 0) begin//计时结束beep <= 1;flag_beep_time_out <= 1;endelse begincnt_time <= cnt_time ;beep <= beep;flag_beep_time_out <= flag_beep_time_out;end
endendmodule
4、数码管位选信号选择 sel_drive.v
总共有6个数码管,分成了3部分,每一部分包含整数和小数部分,左边一位显示带小数点的整数,右边一位显示小数部分。左边两位显示当前投币数,中间两位显示当前商品价格,右边两位显示当前找零数。
运用三段式状态机写法,六个数码管对应六种状态,每20ms状态切换一次,三段式的好处是可以避免冒险和竞争产生的毛刺。每一种状态数码管是否显示和对应的数值大小有关,当数值小于1时,只显示小数部分那个数码管。
module sel_drive(input wire clk ,input wire rst_n ,input wire [6:0] price_put , //投入的钱input wire [6:0] price_need , //商品的价格input wire [6:0] price_out , //找零的钱output reg [5:0] sel //数码管位选
);
//状态
localparam state0 = 3'd0;
localparam state1 = 3'd1;
localparam state2 = 3'd2;
localparam state3 = 3'd3;
localparam state4 = 3'd4;
localparam state5 = 3'd5;parameter MAX_NUM = 1_000;//计数器最大计数值 刷新频率20微秒reg [2 :0] current_state;
reg [2 :0] next_state;
reg [20:0] cnt; //时钟分频计数器
reg flag;//计数器
always @(posedge clk or negedge rst_n)beginif(!rst_n)beginflag <= 1'b0;cnt <= 0;endelse if(cnt == 0)begin//一轮计数完毕flag <= 1'b1;cnt <= 1;endelse begin flag <= 1'b0;cnt <= (cnt + 1'b1) % MAX_NUM;//循环+1end
end
// 状态跳转
always @(posedge clk or negedge rst_n) beginif(!rst_n) begincurrent_state <= state0;endelse if(flag) begincurrent_state <= next_state;endelse current_state <= current_state;
end//状态判断
always @(*) beginif(!rst_n) beginnext_state <= state0;endelse if(flag) begincase(current_state)state0: beginnext_state <= state1;endstate1: beginnext_state <= state2;endstate2: beginnext_state <= state3;endstate3: beginnext_state <= state4;endstate4: beginnext_state <= state5;endstate5: beginnext_state <= state0;enddefault:beginnext_state <= state0;endendcaseendelse beginnext_state <= next_state;end
end//根据value的值确定需要亮几位
always@(current_state) begincase (current_state)state0: begin //右第一位数码管显示sel <= 6'b011111;endstate1: begin //右第二位数码管显示if (price_out >=1) beginsel <= 6'b101111;endelse beginsel <= 6'b111111;endendstate2: begin //右第三位数码管显示if (price_need >=0) beginsel <= 6'b110111;endelse beginsel <= 6'b111111;endendstate3: begin //右第四位数码管显示if (price_need >=1) beginsel <= 6'b111011;endelse beginsel <= 6'b111111;endendstate4: begin //右第五位数码管显示if (price_put >=0) beginsel <= 6'b111101;endelse beginsel <= 6'b111111;endendstate5: begin //右第五位数码管显示if (price_put >=1) beginsel <= 6'b111110;endelse beginsel <= 6'b111111;endenddefault:beginsel <= 6'b111111;endendcase
end
endmodule
5、数码管段选信号选择 seg_drive.v
该部分数码管显示具体数值,售货机按键处理模块将用户投币的数据处理后传递给该模块,不同位选信号,也就是不同的数码管对应显示的相应值的位置是固定的,将收到的用户投币数,商品价格数,找零数取出十位和个位,在数码管上显示,同时在三部分的整数部分数码管显示小数点,也就是段选信号的左边第一位为0,其余部分根据真值表显示。
module seg_drive(input wire clk ,input wire rst_n , //复位input wire [5:0] sel , //数码管位选input wire [6:0] price_put , //投入的钱input wire [6:0] price_need , //商品的价格input wire [6:0] price_out , //找零的钱output reg [7:0] seg //数码管段选);reg [3:0] num;
always@(*) begincase(sel)//投入的钱6'b111_110: num = (price_put % 100) / 10; //十位 6'b111_101: num = price_put % 10; //个位//需要的钱6'b111_011: num = (price_need % 100) / 10; //十位6'b110_111: num = price_need % 10; //个位//找回的钱6'b101_111: num = (price_out % 100) / 10; //十位6'b011_111: num = price_out % 10; //个位default:num = 4'd0;endcase
endalways @ (*) begin//需要显示小数点if(!sel[1] || !sel[3] || !sel[5]) begincase(num)4'd0: seg = 8'b1100_0000; //匹配到后参考共阳极真值表4'd1: seg = 8'b1111_1001;4'd2: seg = 8'b1010_0100;4'd3: seg = 8'b1011_0000;4'd4: seg = 8'b1001_1001;4'd5: seg = 8'b1001_0010;4'd6: seg = 8'b1000_0010;4'd7: seg = 8'b1111_1000;4'd8: seg = 8'b1000_0000;4'd9: seg = 8'b1001_0000;default : seg = 8'b1100_0000;endcaseendelse begincase(num)4'd0: seg = 8'b0100_0000; //匹配到后参考共阳极真值表4'd1: seg = 8'b0111_1001;4'd2: seg = 8'b0010_0100;4'd3: seg = 8'b0011_0000;4'd4: seg = 8'b0001_1001;4'd5: seg = 8'b0001_0010;4'd6: seg = 8'b0000_0010;4'd7: seg = 8'b0111_1000;4'd8: seg = 8'b0000_0000;4'd9: seg = 8'b0001_0000;default : seg = 8'b0100_0000;endcaseendend
endmodule
6、售货机按键选择处理 machine_drive.v
该部分使用三个变量用于存储用户投币数,商品价格,找零。
通过按键可操作标志使处于售货机处理状态下按键不能重复操作影响正常功能。在按键可操作情况下,KEY4键投币加1,KEY3键投币加0.5,在用户投币为零的情况下,按键KEY2用于取消订单,退还投币,在投币非零的情况下,按键KEY2用于修改商品价格,用了一个取余操作,这里设置了4种商品价格,每次按下都加1再取余,数值只会在0-3之间循环。
module machine_drive(input wire clk , //时钟信号input wire rst_n , //复位信号input wire [2:0] key , //3个按键 KEY4-key[2]:1元 KEY3-key[1]:0.5元 KEY2-key[0]:更换商品价格output reg [3:0] led_value , //对应led显示效果的类型output reg [6:0] price_put , //用户投入的钱output reg [6:0] price_need , //商品价格output reg [6:0] price_out //找零
);//四种商品对应价格
parameter P1 = 7'd5 ;
parameter P2 = 7'd15;
parameter P3 = 7'd24;
parameter P4 = 7'd30;parameter MAX_TIME = 28'd100_000_000; //退款过程持续时间reg [1 :0] price_tmp ; //当前商品价格
reg [27:0] cnt_time ; //用作退款过程计时
reg flag_can_operation ; //可以继续投币和切换商品
reg flag_is_retreat_end ; //结算完毕wire flag_is_retreat ; //开始结算
wire flag_price_is_enough ; //可以买下商品
reg [6:0] price_put_last ; //结算前的投币数//结算倒计时模块
always @(posedge clk,negedge rst_n) beginif(!rst_n)begincnt_time <= 28'd0; //初始设置计数器为0flag_can_operation <= 1'b1 ;//设置KEY键可操作flag_is_retreat_end <= 1'b0;//设置结算完毕标志为0endelse beginif (flag_is_retreat) begin //开始结算cnt_time <= MAX_TIME; //计数器设置最大值flag_can_operation <= 1'b0 ;//当前处于结算状态,设置KEY键不可用flag_is_retreat_end <= 1'b0;//设置结算未完毕endelse if(cnt_time > 28'd1) begin //计数器数值大于1cnt_time <= cnt_time - 28'd1;//倒计时flag_can_operation <= 1'b0; //当前处于结算状态,设置KEY键不可操作flag_is_retreat_end <= 1'b0; //设置结算未完毕endelse if(cnt_time == 28'd1) begin//计数器倒计时数到1cnt_time <= 28'd0; //计数器清零flag_can_operation <= 1'b1; //结算完毕,设置KEY键可操作flag_is_retreat_end <= 1'b1; //设置结算完毕endelse begincnt_time <= cnt_time;flag_can_operation <= flag_can_operation;flag_is_retreat_end <= 1'b0;endendend//按下KEY2-key[0]切换商品价格
always@(posedge clk,negedge rst_n)beginif(!rst_n)beginprice_tmp<= 2'd0; //初始设置商品价格为1号商品endelse if(flag_can_operation) begin//当前按键可操作// 当没有投币的时候按下 KEY2-key[0] 为切换商品 if(key[0] && ! price_put) beginprice_tmp <= (price_tmp + 2'd1) % 4;//%操作,循环切换endelse beginprice_tmp <= price_tmp; //没有按下切换按键,当前商品价格保持endendelse beginprice_tmp <= price_tmp; end
end// 切换商品价格
always@(posedge clk,negedge rst_n)beginif(!rst_n)price_need<=P1;elsecase(price_tmp)2'b00 : price_need<=P1;2'b01 : price_need<=P2;2'b10 : price_need<=P3;2'b11 : price_need<=P4;default:price_need<=P1;endcase
end//切换商品价格时led灯光效果
always @(posedge clk,negedge rst_n) beginif(!rst_n)led_value <= 4'd1;//退款的时候判断是 补差价 或者 全额退款//补差价为流水灯效果//全额退款为闪烁效果 else if(flag_is_retreat)begin//开始结算led_value <= price_put_last >= price_need ? 4'd6 : 4'd7 ;//当前投币数大于等于商品价格,LED状态为6,否则状态为7end//正常操作状态根据当前选择商品亮起对应商品ledelse if(flag_can_operation)begincase(price_tmp)2'b00 : led_value <= 4'd2;2'b01 : led_value <= 4'd3;2'b10 : led_value <= 4'd4;2'b11 : led_value <= 4'd5;default:led_value <= 4'd2;endcaseendelse led_value <= led_value;
end//用户通过按键进行投币
always@(posedge clk,negedge rst_n)beginif(!rst_n)beginprice_put_last<=7'd0;//累计投币数初始化为0endelse if(flag_can_operation) begin//当前按键可操作//超过100 或者 if(price_put_last>=7'd100 || flag_is_retreat) begin//当前投币大于10超过可显示数字或开始结算,投币清零price_put_last<=7'd0;end/*按下 key[2] 投币 + 10按下 key[1] 投币 + 5*/ else beginif(key[2])//KEY4-key[2]投币1元price_put_last<=price_put_last+7'd10;else if(key[1])//KEY3-key[1]投币0.5元price_put_last<=price_put_last+7'd5;else price_put_last<=price_put_last;endendelse beginprice_put<=price_put;end
end//投币数码显示保持2s
always @(posedge clk,negedge rst_n) beginif(!rst_n)beginprice_put<=7'd0;endelse if(!flag_can_operation)//按键不可操作price_put <= price_put;//输出投币保持else price_put <= price_put_last;//否则将当前投币值赋值给输出投币寄存器
end// 输出找零
always @(posedge clk,negedge rst_n) beginif(!rst_n)beginprice_out <= 7'd0;//初始找零0元end//结算完毕,归零else if(flag_is_retreat_end) beginprice_out =7'd0 ;end //当退款标志到来,计算退款金额为 补差价 或者 全额退款else if(flag_is_retreat) begin//开始结算price_out <= price_put_last >= price_need ? price_put_last - price_need : price_put_last ;//当前投币大于商品价格则找零差值,否则找零为0endelse beginprice_out <= price_out;end
end//当投币可以买下商品
assign flag_price_is_enough = price_put_last >= price_need;//当前投币数大于等于商品价格,投币足够标志置1
//为了保证在结算前得到最后一次投币数量
// assign price_put_last = price_put;
// 当币足够 或者 在投币过程选择切换商品则开始退款
assign flag_is_retreat = flag_price_is_enough || (price_put && key[0]);//投币足够或退款,结算处理标志置1
endmodule
7、音符彩灯实现 lanterns.v
module lanterns(input wire clk,input wire rst_n,input wire [2:0] spec_flag,//音符output wire [6:0] lan_led //彩灯
);reg [6:0] led_r;//led工作
always@(posedge clk,negedge rst_n)beginif(!rst_n)led_r<=7'b000_0000;elsecase(spec_flag)3'd1: led_r<=7'b000_0001;3'd2: led_r<=7'b000_0010;3'd3: led_r<=7'b000_0100;3'd4: led_r<=7'b000_1000;3'd5: led_r<=7'b001_0000;3'd6: led_r<=7'b010_0000;3'd7: led_r<=7'b100_0000;default:led_r<=7'b000_0000;endcaseend
assign lan_led=led_r;endmodule
8、音符选择 freq_select.v
module freq_select
(input wire clk,input wire rst_n,output reg status, //蜂鸣器1/0output reg [2:0] spec_flag//音符);parameter NOTE_NUM=6'd50; //50个音符
//中
parameter DO = 20'd95600 ;//1
parameter RE = 20'd83150 ;//2
parameter MI = 20'd75850 ;//3
parameter FA = 20'd71600 ;//4
parameter SO = 20'd63750 ;//5
parameter LA = 20'd56800 ;//6
parameter XI = 20'd50600 ;//7
//高
parameter HDO = 16'd47750 ;//1
parameter HRE = 16'd42250 ;//2
parameter HMI = 16'd37900 ;//3
parameter HFA = 16'd37550 ;//4
parameter HSO = 16'd31850 ;//5
parameter HLA = 16'd28400 ;//6
parameter HXI = 16'd25400 ;//7
//低
parameter LDO = 20'd190800 ;//1
parameter LRE = 20'd170050 ;//2
parameter LMI = 20'd151500 ;//3
parameter LFA = 20'd143250 ;//4
parameter LSO = 20'd127550 ;//5
parameter LLA = 20'd113600 ;//6
parameter LXI = 20'd101200 ;//7reg [25:0] inte_cnt; //300ms,间隔
reg [19:0] note_cnt; //音符持续时间计时
reg [5:0] spec_cnt; //音谱个数计数
reg [19:0] spec_data; //音符频率
reg [25:0] continue_time; //持续时间
reg [27:0] blank_time; //空白时间 wire[18:0] duty_data; //占空比数据
wire end_note; //音符结束时间
wire end_spectrum; //音谱结束时间//音符之间间隔时间计数
always@(posedge clk,negedge rst_n)beginif(!rst_n)inte_cnt<=26'b0;else if(inte_cnt==continue_time+blank_time)inte_cnt<=26'b0;else begininte_cnt<=inte_cnt+1'b1;end
end//单个音符频率计数
always@(posedge clk or negedge rst_n)beginif(!rst_n)beginnote_cnt <= 20'd0;//20end else if(end_note)beginnote_cnt <= 20'd0;end else beginnote_cnt <= note_cnt + 1'd1;end
end//音符数计时
always@(posedge clk or negedge rst_n)beginif(!rst_n)beginspec_cnt <= 6'd0;end else if(end_spectrum)beginspec_cnt <= 6'd0;end else if(inte_cnt == continue_time+blank_time)beginspec_cnt <= spec_cnt + 1'd1;end else beginspec_cnt <= spec_cnt;end
end always@(posedge clk or negedge rst_n)begincase(spec_cnt)6'd0: continue_time<=26'd10_000_000;//你爱我6'd1: continue_time<=26'd_000_000;6'd2: continue_time<=26'd10_000_000; 6'd3: continue_time<=26'd20_000_000;6'd4: continue_time<=26'd10_000_000;//我爱你蜜雪 6'd5: continue_time<=26'd10_000_000; 6'd6: continue_time<=26'd20_000_000; 6'd7: continue_time<=26'd10_000_000;6'd8: continue_time<=26'd10_000_000;6'd9: continue_time<=26'd10_000_000;//冰城甜蜜 6'd10:continue_time<=26'd15_000_000;6'd11:continue_time<=26'd10_000_000;6'd12:continue_time<=26'd9_000_000;6'd13:continue_time<=26'd25_000_000;//蜜6'd14:continue_time<=26'd10_000_000;//你爱我6'd15:continue_time<=26'd10_000_000;6'd16:continue_time<=26'd10_000_000;6'd17:continue_time<=26'd20_000_000;6'd18:continue_time<=26'd10_000_000;//我爱你蜜雪6'd19:continue_time<=26'd10_000_000;6'd20:continue_time<=26'd20_000_000;6'd21:continue_time<=26'd10_000_000;6'd22:continue_time<=26'd10_000_000;6'd23:continue_time<=26'd10_000_000;//冰城甜蜜6'd24:continue_time<=26'd15_000_000;6'd25:continue_time<=26'd10_000_000;6'd26:continue_time<=26'd9_000_000;6'd27:continue_time<=26'd25_000_000;//蜜6'd28:continue_time<=26'd20_000_000;//你爱6'd29:continue_time<=26'd20_000_000;6'd30:continue_time<=26'd20_000_000;//我呀6'd31:continue_time<=26'd10_000_000;6'd32:continue_time<=26'd10_000_000;6'd33:continue_time<=26'd20_000_000;//我爱6'd34:continue_time<=26'd10_000_000;6'd35:continue_time<=26'd10_000_000;6'd36:continue_time<=26'd50_000_000;//你6'd37:continue_time<=26'd10_000_000;//你爱我6'd38:continue_time<=26'd10_000_000;6'd39:continue_time<=26'd10_000_000; 6'd40:continue_time<=26'd20_000_000;6'd41:continue_time<=26'd10_000_000;//我爱你蜜雪 6'd42:continue_time<=26'd10_000_000; 6'd43:continue_time<=26'd20_000_000; 6'd44:continue_time<=26'd10_000_000;6'd45:continue_time<=26'd10_000_000;6'd46:continue_time<=26'd10_000_000;//冰城甜蜜 6'd47:continue_time<=26'd25_000_000;6'd48:continue_time<=26'd10_000_000;6'd49:continue_time<=26'd9_000_000;6'd50:continue_time<=26'd25_000_000;//蜜default: continue_time<=26'd24_000_000;endcase
end
//空白时间
always@(spec_cnt)begincase(spec_cnt)6'd0: blank_time<=26'd2_000_000;//你爱我6'd1: blank_time<=26'd2_000_000;6'd2: blank_time<=26'd2_000_000; 6'd3: blank_time<=26'd5_000_000;6'd4: blank_time<=26'd2_000_000; //我爱你蜜雪 6'd5: blank_time<=26'd2_000_000; 6'd6: blank_time<=26'd5_000_000; 6'd7: blank_time<=26'd2_000_000;6'd8: blank_time<=26'd2_000_000; 6'd9: blank_time<=26'd2_000_000; //冰城甜蜜 6'd10:blank_time<=26'd5_000_000;6'd11:blank_time<=26'd2_000_000;6'd12:blank_time<=26'd2_000_000;6'd13:blank_time<=26'd5_000_000;//蜜6'd14:blank_time<=26'd2_000_000;//你爱我6'd15:blank_time<=26'd2_000_000;6'd16:blank_time<=26'd2_000_000;6'd17:blank_time<=26'd2_000_000;6'd18:blank_time<=26'd2_000_000;//我爱你蜜雪6'd19:blank_time<=26'd2_000_000;6'd20:blank_time<=26'd5_000_000;6'd21:blank_time<=26'd2_000_000;6'd22:blank_time<=26'd2_000_000;6'd23:blank_time<=26'd2_000_000;//冰城甜蜜6'd24:blank_time<=26'd5_000_000;6'd25:blank_time<=26'd2_000_000;6'd26:blank_time<=26'd2_000_000;6'd27:blank_time<=26'd5_000_000;//蜜6'd28:blank_time<=26'd2_000_000;//你爱6'd29:blank_time<=26'd5_000_000;6'd30:blank_time<=26'd2_000_000;//我呀6'd31:blank_time<=26'd2_000_000;6'd32:blank_time<=26'd5_000_000;6'd33:blank_time<=26'd2_000_000;//我爱6'd34:blank_time<=26'd2_000_000;6'd35:blank_time<=26'd5_000_000;6'd36:blank_time<=26'd10_000_000;//你6'd37:blank_time<=26'd2_000_000;//你爱我6'd38:blank_time<=26'd2_000_000;6'd49:blank_time<=26'd2_000_000; 6'd40:blank_time<=26'd5_000_000;6'd41:blank_time<=26'd2_000_000; //我爱你蜜雪 6'd42:blank_time<=26'd2_000_000; 6'd43:blank_time<=26'd5_000_000; 6'd44:blank_time<=26'd2_000_000;6'd45:blank_time<=26'd2_000_000; 6'd46:blank_time<=26'd2_000_000; //冰城甜蜜 6'd47:blank_time<=26'd5_000_000;6'd48:blank_time<=26'd2_000_000;6'd49:blank_time<=26'd2_000_000;6'd50:blank_time<=26'd5_000_000;//蜜default:blank_time<=26'd1_000_000;endcase
end
always@(posedge clk,negedge rst_n)beginif(!rst_n)spec_data<=DO;elsecase(spec_cnt)6'd0: spec_data <= MI;//你爱我6'd1: spec_data <= SO;6'd2: spec_data <= SO; 6'd3: spec_data <= LA;6'd4: spec_data <= SO; //我爱你蜜雪 6'd5: spec_data <= MI; 6'd6: spec_data <= DO; 6'd7: spec_data <= DO;6'd8: spec_data <= RE;6'd9: spec_data <= MI; //冰城甜蜜 6'd10:spec_data <= MI;6'd11:spec_data <= RE;6'd12:spec_data <= DO;6'd13:spec_data <= RE; //蜜6'd14:spec_data <= MI; //你爱我6'd15:spec_data <= SO;6'd16:spec_data <= SO;6'd17:spec_data <= LA;6'd18:spec_data <= SO; //我爱你蜜雪 6'd19:spec_data <= MI;6'd20:spec_data <= DO;6'd21:spec_data <= DO;6'd22:spec_data <= RE;6'd23:spec_data <= MI; //冰城甜蜜6'd24:spec_data <= MI;6'd25:spec_data <= RE;6'd26:spec_data <= RE;6'd27:spec_data <= DO; //蜜6'd28:spec_data <= FA; //你爱6'd29:spec_data <= FA;6'd30:spec_data <= FA; //我呀6'd31:spec_data <= LA;6'd32:spec_data <= LA;6'd33:spec_data <= SO; //我爱6'd34:spec_data <= SO;6'd35:spec_data <= MI;6'd36:spec_data <= RE; //你6'd37:spec_data <= MI; //你爱我6'd38:spec_data <= SO;6'd39:spec_data <= SO;6'd40:spec_data <= LA;6'd41:spec_data <= SO; //我爱你蜜雪 6'd42:spec_data <= MI;6'd43:spec_data <= DO;6'd44:spec_data <= DO;6'd45:spec_data <= RE;6'd46:spec_data <= MI; //冰城甜蜜6'd47:spec_data <= MI;6'd48:spec_data <= RE;6'd49:spec_data <= RE;6'd50:spec_data <= DO; //蜜default:spec_data <= DO;endcase
end
//当前音符spec_flag
always@(posedge clk,negedge rst_n)beginif(!rst_n)spec_flag<=3'd0;elsecase(spec_data)DO:spec_flag<=3'd1;RE:spec_flag<=3'd2;MI:spec_flag<=3'd3;FA:spec_flag<=3'd4;SO:spec_flag<=3'd5;LA:spec_flag<=3'd6;XI:spec_flag<=3'd7;default:spec_flag<=3'd0;endcase
endassign duty_data = spec_data >> 4;assign end_note = note_cnt== spec_data; //spec_dara对音谱计数
assign end_spectrum = spec_cnt == NOTE_NUM && inte_cnt == continue_time;//pwm信号产生模块
always@(posedge clk or negedge rst_n)beginif(!rst_n)beginstatus <= 1'b0;end else status <= (note_cnt >= duty_data) ? 1'b1 : 1'b0;
end endmodule
9、顶层文件 top_vending_machine
module top_vending_machine(input wire clk , //时钟 50Minput wire rst_n , //复位input wire [2:0] key , //按键output wire beep , //蜂鸣器output wire [3:0] led , //售货机状态灯效output wire [5:0] sel , //数码管位选output wire [7:0] seg , //数码管段选output wire [6:0] lan_led //音乐播放灯效);wire status; //音乐播放驱使蜂鸣器标志
wire [2:0] key_flag; //按键消抖完成标志
wire [2:0] key_value; //按键消抖完成后的按键值
wire [4:0] led_value; //售货机驱使led模块效果的值
wire [6:0] price_put; //售货机输出到数码管的投币值
wire [6:0] price_need; //售货机输出到数码管的商品价格
wire [6:0] price_out; //售货机输出到数码管的退款
wire [2:0] spec_flag; //音乐模块输出的音符,用于音乐灯效//数码管位选模块
sel_drive inst_sel_drive(
.clk (clk) ,
.rst_n (rst_n) ,
.price_put (price_put) ,
.price_need (price_need) ,
.price_out (price_out) ,
.sel (sel)
);//数码管段选模块
seg_drive inst_seg_drive(
.clk (clk) ,
.rst_n (rst_n) ,.price_put (price_put) ,
.price_need (price_need) ,
.price_out (price_out) ,
.sel (sel) ,
.seg (seg));//售货机模块
machine_drive inst_machine_drive(
.clk (clk) ,
.rst_n (rst_n) ,
.key ({key_value[2] && key_flag[2], key_value[1] && key_flag[1], key_value[0] && key_flag[0] }),.led_value (led_value) ,
.price_put (price_put) ,
.price_need (price_need) ,
.price_out (price_out)
);//led模块
led_drive inst_led(
.clk (clk) ,
.rst_n (rst_n) ,
.value (led_value) ,.led (led)
);//音乐模块
freq_select inst_freq_select
(
.clk (clk ) ,
.rst_n (rst_n ) ,.status (status) ,
.spec_flag (spec_flag)
);//音乐灯效模块
lanterns inst_lanterns(
.clk (clk ) ,
.rst_n (rst_n ) ,
.spec_flag (spec_flag) ,.lan_led (lan_led)
);//蜂鸣器
beep_drive inst_beep_drive(
.clk (clk) ,
.rst_n (rst_n) ,
.flag ((key_value[2] && key_flag[2]) || ( key_value[1] && key_flag[1]) || (key_value[0] && key_flag[0])),
.status (status) ,
.beep (beep)
);//按键消抖
key_debounce inst_key_debounce_key0(
.clk (clk) ,
.rst_n (rst_n) ,
.key (key[0]) ,.flag (key_flag[0]) ,
.key_value (key_value[0])
);key_debounce inst_key_debounce_key1(
.clk (clk) ,
.rst_n (rst_n) ,
.key (key[1]) ,.flag (key_flag[1]) ,
.key_value (key_value[1])
);key_debounce inst_key_debounce_key2(
.clk (clk) ,
.rst_n (rst_n) ,
.key (key[2]) ,.flag (key_flag[2]) ,
.key_value (key_value[2])
);endmodule
五、测试
RTL门级电路查看
硬件测试:
在黑金AX301 FPGA开发板上可看到现象
六、总结
以后牛客网刷题可能会遇到自动售货机的题目,可继续完善