也许我们刚开始用到开发板的时候都会去做跑马灯的程序,后来给我们的要求是,如果硬件接口有限制,只有一个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. python10min系列之面试题解析:python实现tail -f功能

    同步发布在github上,跪求star 这篇文章最初是因为reboot的群里,有人去面试,笔试题有这个题,不知道怎么做,什么思路,就发群里大家讨论 我想了一下,简单说一下我的想法吧,当然,也有很好用的 ...

  2. JDK JRE先保存 后面再整理

    1. 定义 JRE(Java Runtime Enviroment)是Java的运行环境.面向Java程序的使用者,而不是开发者.如果你仅下载并安装了JRE,那么你的系统只能运行Java程 序.JRE ...

  3. 高质量程序设计指南C/C++语言——内存管理

    • free()和delete只是把指针所指的内容给释放掉,并没有把指针本身删掉.指针被free()或delete以后其地址仍然不变(不等于NULL),只是该地址对应的内存是垃圾——p成了野指针.如果 ...

  4. web api 开发之 filter

     1.使用filter之前应该知道的(不知道也无所谓,哈哈!) 谈到filter 不得不先了解下aop(Aspect Oriented Programming)面向切面的编程.(度娘上关于aop一大堆 ...

  5. 为什么Lisp没有流行起来

    很久以前,这种语言站在计算机科学研究的前沿,特别是人工智能的研究方面.现在,它很少被用到,这一切并不是因为古老,类似古老的语言却被广泛应用.其他类似的古老的语言有??FORTRAN. COBOL. L ...

  6. Access2007中如何运行SQL执行SQl语句

    1. 在“创建”选项卡中,单击“其他”组中的“查询设计”. 2. 单击“显示表”对话框中的“关闭”,而不添加任何表或查询. 3. 在“设计”选项卡中,单击“查询类型”工作组中的“联合”. 4. 单击“ ...

  7. 在C#中调用API获取网络信息和流量

    原文 在C#中调用API获取网络信息和流量 最近一项目中要求显示网络流量,而且必须使用C#. 事实上,调用 IpHlpApi.dll 的 GetIfTable API 可以轻易获得网络信息和网络流量. ...

  8. C++ Primer 学习笔记_75_模板与泛型编程 --模板定义

    模板与泛型编程 --模板定义 引言: 所谓泛型程序就是以独立于不论什么特定类型的方式编写代码.使用泛型程序时,我们须要提供详细程序实例所操作的类型或值. 模板是泛型编程的基础.使用模板时能够无须了解模 ...

  9. poj1077 Eight【爆搜+Hash(脸题-_-b)】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4298840.html   ---by 墨染之樱花 题目链接:http://poj.org/pr ...

  10. ADO.NET基础笔记

    ADO.NET 程序要和数据库交互要通过ADO.NET进行,通过ADO.Net就能在程序中执行SQL了. ADO.Net中提供了对各种不同的数据库的统一操作接口. 连接字符串: 程序通过连接字符串指定 ...