完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
本帖最后由 zefengx68 于 2015-12-10 14:43 编辑
昨天在论坛上看到一位朋友想用按键来切换LED显示的状态,把代码写出来,下载到自己的板子试了一下,可以使用,缺少了对按键的滤波处理。把代码贴出来给大家看看,可以改进的请留言。本人新手,会的请教下按键滤波这段怎么写,感谢。 module led_model(clk,rst_n,key,led); input clk; input rst_n; input[3:0]key; //0代表按下 output[7:0]led; //0代表亮 reg[7:0] led; reg[7:0] led_temp; reg[2:0] current_state; //当前状态 reg[2:0] next_state; //下一状态 parameter[2:0] RESET = 3'b000, STOP = 3'b001, S1 = 3'b010, //8盏灯1S交替亮灭 S2 = 3'b011, //单盏灯从左往右,再从右往左亮 S3 = 3'b100; //4盏灯1S交替两灭 //第一个进程,同步时序always模块,格式化描述次态寄存器迁移到现态寄存器 always@(posedge clk or negedge rst_n) begin if(!rst_n) begin current_state <= RESET; end else begin current_state <= next_state; //非阻塞赋值 end end //第二个进程,组合逻辑always模块,描述状态转移条件判断 always@(*) begin //电平触发 case(current_state) //复位状态 RESET: begin if(key == 4'b1101)//只有key1按下 begin next_state = S1; end else if(key == 4'b1011)//只有key2按下 begin next_state = S2; end else if(key == 4'b0111)//只有key3按下 begin next_state = S3; end else begin next_state = RESET; end end //暂停状态 STOP: begin if(key == 4'b1110)//只有key0按下 begin next_state = RESET; end else if(key == 4'b1101)//只有key1按下 begin next_state = S1; end else if(key == 4'b1011)//只有key2按下 begin next_state = S2; end else if(key == 4'b0111)//只有key3按下 begin next_state = S3; end else begin next_state = STOP; end end //S1状态 S1: begin if(key == 4'b1110) begin //只有key0按下 next_state = RESET; end else if(key == 4'b1101) begin //只有key1按下 next_state = STOP; end else if(key == 4'b1011) begin //只有key2按下 next_state = S2; end else if(key == 4'b0111) begin //只有key3按下 next_state = S3; end else begin next_state = S1; end end //S2状态 S2: begin if(key == 4'b1110) begin //只有key0按下 next_state = RESET; end else if(key == 4'b1101) begin //只有key1按下 next_state = S1; end else if(key == 4'b1011) begin //只有key2按下 next_state = STOP; end else if(key == 4'b0111) begin //只有key3按下 next_state = S3; end else begin next_state = S2; end end //S3状态 S3: begin if(key == 4'b1110) begin //只有key0按下 next_state = RESET; end else if(key == 4'b1101) begin //只有key1按下 next_state = S1; end else if(key == 4'b1011) begin //只有key2按下 next_state = S2; end else if(key == 4'b0111) begin //只有key3按下 next_state = STOP; end else begin next_state = S3; end end endcase end //第三个进程,同步时序always模块,格式化描述寄存器输出 always@(posedge clk or negedge rst_n) begin if(!rst_n) begin led <= 8'b1111_1111; end else if(current_state == RESET) begin led <= 8'b1111_1111;; //非阻塞赋值 end else if(current_state == STOP) begin led <= led_temp; end else if(current_state == S1) begin led <= led_temp; end else if(current_state == S2) begin led <= led_temp; end else if(current_state == S3) begin led <= led_temp; end end //led_temp always@(posedge clk or negedge rst_n) begin if(!rst_n) begin led_temp <= 8'b1111_1111; end else if(key == 4'b1101) //只按key1 begin if((current_state != S1) && (current_state != STOP)) begin led_temp <= 8'b1111_1111; end end else if(key == 4'b1011) //只按key2 begin if((current_state != S2) && (current_state != STOP)) begin led_temp <= 8'b1111_1110; end end else if(key == 4'b0111) //只按key3 begin if((current_state != S3) && (current_state != STOP)) begin led_temp <= 8'b1010_1010; end end else if(current_state == S1) begin if(counter == 28'd49_999_999) begin led_temp <= ~led_temp; end end else if(current_state == S2) begin if(counter == 28'd49_999_999) //1111_1110 begin led_temp <= {led[6:0],led[7]}; end end else if(current_state == S3) begin if(counter == 28'd49_999_999) begin led_temp <= ~led_temp; end end end //1s计时,counter reg[27:0] counter; always@(posedge clk or negedge rst_n) begin if(!rst_n) begin counter <= 28'd0; end else if(current_state == RESET) begin counter <= 28'd0; end else if((key == 4'b1101) || (key == 4'b1011) || (key == 4'b0111)) begin counter <= 28'd0; end else if((current_state == S1) || (current_state == S2) || (current_state == S3)) if(counter == 28'd49_999_999) begin counter <= 28'd0; end else begin counter <= counter + 1'b1; end end endmodule |
|
相关推荐
|
|
1539 浏览 1 评论
助力AIoT应用:在米尔FPGA开发板上实现Tiny YOLO V4
1077 浏览 0 评论
2619 浏览 1 评论
2306 浏览 0 评论
矩阵4x4个按键,如何把识别结果按编号01-16(十进制)显示在两个七段数码管上?
2578 浏览 0 评论
2037 浏览 55 评论
6038 浏览 113 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-29 10:17 , Processed in 0.711630 second(s), Total 64, Slave 47 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号