也许我们刚开始用到开发板的时候都会去做跑马灯的程序,后来给我们的要求是,如果硬件接口有限制,只有一个key 或者是button—— 我们的板子上是button,让你用一个button去控制这四个led,那么你应该怎么做呢? —— 之前的跑马灯都是一个key 对应一个led。 或许有其他的解决方案。

  这里的解决方案是计数法,按住其中一个button,led 会被循环点亮——每次只有一个是被点亮的。放开button,控制信号就定位在某一个led上。

the first one for the top

module   onekey_fourLED  (
clock ,
reset ,
i_key ,
o_led
); input clock ,reset ;
input i_key ;
output [:]o_led ; wire temp0,temp1 ;
key_edge u0 (
.clock(clock) ,
.reset (reset ),
.i_key (i_key),
.counter_en (temp0 )
); counter0 u1 (
.clock (clock ),
.reset (reset),
.i_key(i_key),
.counter_en(temp0),
.counter_full (temp1)
); led_sel u2(
.clock(clock),
.reset (reset ),
.en(temp0),
.cnt_full (temp1),
.o_led(o_led)
); endmodule

开关边沿检测模块

module  key_edge  (
clock ,
reset ,
i_key ,
counter_en
); input clock ,reset ;
input i_key ;
output reg counter_en ; reg r_key0 ,r_key1 ;
always @ (posedge clock )
begin
if(!reset )
begin
r_key0 <= 'b1 ;
r_key1 <= 'b1 ;
end
else
begin
r_key0 <= i_key ;
r_key1 <= r_key0 ; if((!r_key0) & (r_key1) ) //开关下降沿到来开始计数使能端打开
counter_en <= 'b1 ;
else if ((r_key0) & (!r_key1)) //开关上升沿到来,证明一次按下动作完毕
counter_en <= 'b0 ;
else ;
end
end endmodule

  

  对按下的button 时间进行计数

module  counter0  (
clock ,
reset ,
i_key,
counter_en,
counter_full
);
input clock ,reset ;
input i_key ;
input counter_en ; output reg counter_full; reg [:] cnt ;
always @ (posedge clock )
begin
if(!reset )
begin
cnt <= 'd0 ;
end
else
begin
if((!i_key) & (counter_en))
cnt <= cnt + 'd1;
else cnt <= 'd0 ;
end
end always @ (posedge clock )
begin
if(!reset )
counter_full <= 'd0 ;
else
begin
if(cnt == 'hff_ffff) counter_full <= 1'b1 ;
else counter_full <= 'b0 ;
end
end endmodule

  依据按键按下的时间选择led

module led_sel (
clock,
reset ,
en,
cnt_full ,
o_led
);
input clock ,reset ;
input en ,cnt_full;
output reg [:] o_led ; reg [:] o_led_cnt;
always @ (posedge clock )
begin
if(!reset )
o_led_cnt <= 'd0 ;
else if((en) & (cnt_full))
begin
if (o_led_cnt <= 'd3)o_led_cnt <= o_led_cnt + 2'd1 ;
else o_led_cnt <= 'd0 ;
end
end //reg [3:0] o_led_reg;
always @ (posedge clock )
begin
if(!reset )
o_led <= 'b1111 ;
else case (o_led_cnt)
'b00 : o_led <= 4'b1110;
'b01 : o_led <= 4'b1101;
'b10 : o_led <= 4'b1011;
'b11 : o_led <= 4'b0111;
default : o_led <= 'b1111 ;
endcase
end
// assign o_led = o_led_reg; endmodule

onekey_fourLED的更多相关文章

随机推荐

  1. 文件上传 PHP

    参考http://www.w3school.com.cn/php/php_file_upload.asp 文件上传实际上是一个文件复制的过程  当我们选中一个文件之后  php默认的tmp文件夹中就有 ...

  2. 模拟美萍加密狗--Rockey2虚拟狗(三)

    几经挣扎,我最终还是选择了虚拟设备的方法来模拟Rockey2加密狗.HID.DLL劫持+API劫持的办法技术上虽然简单些,但太繁琐了,不仅要转发大量的函数,还要Hook好几个API,向我这么懒的人可干 ...

  3. elaserch 查看节点是否是master

    http://192.168.32.81:9200/_cat/nodes 192.168.32.81 192.168.32.81 3 21 0.00 d * node02 192.168.32.80 ...

  4. 为什么webview.loadUrl("javascript:function() ")不执行?

    这几天搞webview 但是常常有时候会出现webview.loadurl 没有反映的情况对现在的分析如下: 情况一:webview.loadurl 的加载是在另一个线程中执行必须要在webview加 ...

  5. vs linq to db template

    linq to db template 支持sqlite. mysql .db2. accress. oracle. Firebird等多种数据库以linq方式操作数据. NuGet NuGet 是 ...

  6. hdu1395-2^x mod n = 1

    http://acm.hdu.edu.cn/showproblem.php?pid=1395 原理为 a ^ b % n == d ; >>>>>>  (( a % ...

  7. Runtime.exec使用错误导致延迟.md

    这篇文章是纪录了一个bug解决的过程,可是我还是没有可以真正地找出bug的缘由.希望大牛可以详解. 问题的发现 当接触的系统越来越大的时候,对于系统的性能越来越高的时候,找到表面问题的真正原因就慢慢地 ...

  8. 拿出来分享了!VIP珍藏!!!全网最齐全的 DEDECMS模板 全盘下载地址列表!没有你找不到的!

    拿出来分享了!VIP珍藏!!!全网最齐全的 DEDECMS模板 网盘地址!没有你找不到的! 模板类型最齐全: ----------------------优美的走起!------------ 一:DE ...

  9. 前端笔试面试中的常用知识点总结(CSS)

    1.CSS选择器的优先级!important  > 内联 > id选择器 > 类选择器 > 标签选择器多个类选择器叠加(256)之后的优先级大于一个id选择器!importan ...

  10. TortoiseSVN和Eclipse中subversion无法协同使用

    环境: Eclipse版本Luna, 第一次安装subversion插件时, 使用了http://download.eclipse.org/releases/luna中的Subversive, 版本为 ...