Sunday, April 13, 2014

Buttons and Switches on the ZYBO

Controlling the LEDs with switches and buttons

This is an embellishment of the previous post on programming the FPGA on the ZYBO board to control the LEDs. This time I programmed the first LED to merely blink at 1 Hz with an enable switch and reset button. The other three LEDs are controlled by a single logic gate (AND, OR and XOR) between the corresponding switch and button. Here is the code - all steps in my previous post apply, though all the buttons and switched will have to be uncommented in the XDC file in addition to the LEDs.

`timescale 1ns / 1ps

module switches_and_buttons(
    output reg [3:0] led,
    input [3:0] btn,
    input [3:0] sw,
    input clk
    );

reg [25:0] counter;
reg one_sec_clk;

wire rst;
wire en;

assign rst = btn[0];
assign en = sw[0];

always @ (posedge clk) begin
    if (rst) begin
        counter <= 0;
        one_sec_clk <= 0;
    end else if (en) begin
        if (counter == 26'd62500000) begin
            counter <= 0;
            one_sec_clk <= ~one_sec_clk;
        end else begin
            counter <= counter + 1;
        end
    end
end

always @ (one_sec_clk, btn, sw) begin
    led[0] = one_sec_clk;
    led[1] = btn[1] || sw[1];
    led[2] = btn[2] ^  sw[2];
    led[3] = btn[3] && sw[3];
end

endmodule

As you can see, I have a counter running to 62.5M, cutting the 125 MHz clock in half. The reset (rst) is attached to the first button (btn[0]) and the enable (en) is attached to the first switch (sw[0]). The other button/switch pairs are used to set the remaining LEDs in the second (combinational) always block and are operating rather independently of the first blinking LED.

No comments:

Post a Comment