03_led_horse_run_v1 跑马灯

03_led_horse_run_v1

在Verilog中实现跑马灯通常涉及到使用一个计数器来控制LED灯的亮灭顺序。

跑马灯是一种常见的电子显示方式,它通过控制多个LED灯的顺序点亮,形成一种动态的视觉效果,看起来就像灯在"跑"一样。

更新

使用dip开关控制跑马灯的速度和方向

知识点:

  • 循环移位寄存器的使用
  • 两种不同的组合逻辑赋值风格(assign, always(*))
  • 灵活利用参数设计可复用模块(模块级参数,局部参数)
cpp 复制代码
module led_horse_run #(
    parameter LED_ON  = 1'b0,  //led active low
    parameter CLK_FREQ  = 50*1000*1000 //frequency of input clock
)(
    input  clk,
    input  rst_n,

    input wire [5:0] dip_u6,
   output reg [5:0] led
);

//
//Local parameter, same as const in c/c++
//For 50Mhz clock, 
//one second count to 50*1000*1000 
//one millisecond count to 50*1000 
//

localparam ONE_SECOND = CLK_FREQ;
localparam HALF_SECOND = ONE_SECOND / 2;
localparam ONE_MSECOND = ONE_SECOND / 1000;

//-------------------------------------------
//control the running speed and direction 
//according to the status of dip keys
wire [1:0] speed = dip_u6[1:0];
wire direct = dip_u6[5];

//First style of assignment for combinational logic, (not for sequential logic)
//Note: left value must be wire type, here 'wire [31:0] count_max;'
wire [31:0] count_max;  
assign count_max =  (speed == 0) ? ONE_MSECOND * 2000 :
                    (speed == 1) ? ONE_MSECOND * 1000 :
                    (speed == 2) ? ONE_MSECOND * 500 : ONE_MSECOND * 200;
/*
//another style of assignment of combinational logic
//Note: left value must be reg type, here 'reg [31:0] count_max;'
reg [31:0] count_max;  
always @(*) begin
    count_max =     (speed == 0) ? ONE_MSECOND * 2000 :
                    (speed == 1) ? ONE_MSECOND * 1000 :
                    (speed == 2) ? ONE_MSECOND * 500  : ONE_MSECOND * 200;
end
*/

//---------------------------------------------------------
reg [31:0] count; 
wire time_on = (count == count_max-1);

always @(posedge clk) begin
    if(~rst_n) count <= 0;
    else if(time_on) count <= 0;
    else count <= count + 1;
end

wire [5:0] init_led_status = (LED_ON == 1'b1) ? 6'b000001 : 6'b111110;

always @(posedge clk) begin
    if(~rst_n) led <= init_led_status;
    else if(time_on) begin
        if(direct == 1'b1) led <= {led[4:0], led[5]} ;  //shift left
        else led <= {led[0],led[5:1]} ;                 //shift right
    end
    //else led <= led;
end

endmodule

扩展思维

适了解流水灯和跑马灯的区别,适当修改上面的跑马灯代码实现流水灯的效果。

相关推荐
坏孩子的诺亚方舟13 天前
FPGA系统架构设计实践15_高云Arora V系列时钟体系
fpga开发·系统架构
FPGA小徐14 天前
入门 CNN 结构全解析|从流程图理论到 FPGA Verilog 硬件实现(含习题带讲解)
fpga开发
FPGA小徐14 天前
FPGA 数字信号处理:并行 FIR 与串行滤波器设计原理、对比与完整 Verilog 实现
fpga开发
Saniffer_SH14 天前
【高清视频】Gen6 服务器还没到,Gen6 SSD 怎么测?Emily 现场演示三种测试环境
人工智能·驱动开发·测试工具·缓存·fpga开发·计算机外设·压力测试
zlinear数据采集卡15 天前
双核架构深度解析:ARM+FPGA如何让数据采集卡实现500Ksps高性能?
arm开发·fpga开发·架构
9527华安15 天前
FPGA实现GTH Transceivers Wizard传输2路视频,基于aurora 8b10b编解码架构,提供4套工程源码和技术支持
fpga开发·gth·aurora 8b10b·transceivers
FPGA小徐15 天前
FPGA 数字信号处理(二):并行 FIR 滤波器的 Verilog 全流程设计与实现
fpga开发
国科安芯16 天前
基于AS32S601ZIT2型抗辐照MCU的商业航天卫星姿态确定与控制系统研究
单片机·嵌入式硬件·安全·fpga开发·架构·risc-v
ALINX技术博客16 天前
【黑金云课堂】FPGA技术教程FPGA基础:I2C 总线通信技术
fpga开发·i2c
Hello-FPGA16 天前
Xilinx KU040 FPGA Camera Link 图像采集
c++·fpga开发