Digital Adventures

Notes on programmable logic, tabletop games & other stuff

Switchy

| Comments

In this week’s article we’ll be using the DIP switches located between the FPGA and the USB connector on the Microboard. This guide should be pretty short as I won’t be going into nearly as much detail about setting up the project, creating files, and so on.

Setup

As always, start a new project in Project Navigator. I called my project ‘switchy’, but you’re free to pick any name you wish. Hopefully the new project wizard remembered what FPGA you’re using, so creating the project once you’ve entered the name should just be a matter of clicking next a few times. Create a new Verilog file, calling it ‘adder.v’

For all your two-bit adding needs

This adder module adds the binary value of the two left switches to the binary value of the two right switches. Simple enough, right? The reason for the inversion (the ~) is because we want the switch in the down position to represent a zero, not a one. The reason for not having the switches on the sensitivity list (only the clock edge) stems back to a horrendous issue I encountered in my very first hardware design class. That case was slightly different, however, as it involved switch inputs on the sensitivity list of a Mealey state machine, but I digress.

adder.v
1
2
3
4
5
6
7
8
9
10
11
12
`timescale 1ns / 1ps

module adder(
    input clk,
    input [3:0] switches,
    output reg [2:0] leds
    );

    always @(posedge clk) begin
        leds = ~switches[3:2] + ~switches[1:0];
    end
endmodule

What to do with the constraints file should be straightforward by now. The use of the “PULLDOWN” flag is specified in the Microboard user’s guide

adder.ucf
1
2
3
4
5
6
7
8
9
NET "clk" LOC = C10;
NET "leds[1]" LOC = P4;
NET "leds[0]" LOC = L6;
NET "leds[2]" LOC = F5;

NET "switches[0]" LOC = B3 | PULLDOWN;
NET "switches[1]" LOC = A3 | PULLDOWN;
NET "switches[2]" LOC = B4 | PULLDOWN;
NET "switches[3]" LOC = A4 | PULLDOWN;

Program it!

Much the same as last time, go ahead and run “Generate Programming File”, wait a few minutes, and upload the programming file to your Microboard. Play around with the switches (when the USB connector is on the left, down = 0, up = 1) and watch the sum be displayed on the LEDs. Cool, huh?

Next time

Now that you’ve mastered the basics (I hope?), it’s time for a fun project. Implementing SPI communication sounds like fun, yeah?

Comments