Wednesday, 20 June 2012

Binary Counter in Verilog | BASYS2

Now that we have the blinking LED in Verilog up and running, we can do an advanced version of the same project; a binary counter using the LED's.
Also this time I will be using the push button to increment the counter. So every time the push button is pressed the counter will be incremented by 1, the LED's will present the binary counter.

The code for the program is shown below:
module button_binary(
    input clock,
    input reset,
  input button,
    output led,
    output led2,
    output led3,
    output led4,
    output led5,
    output led6,
    output led7,
    output led8
    );

reg [7:0]count;

always @ (posedge button or posedge reset)
 begin
  if (reset)
   count <= 0;
  else if (button)
   count <= count + 1;
 end
 
assign led = count[0];
assign led2 = count[1];
assign led3 = count[2];
assign led4 = count[3];
assign led5 = count[4];
assign led6 = count[5];
assign led7 = count[6];
assign led8 = count[7];


endmodule

The above program is so simple it needs no commenting, the thing to note here though is the always block. In place of the usual clock I used the push button (always @ posedge button). This is not the best method, but if I use clock here without any debouncing mechanism the counter will not be accurate, i.e. every time the push button is pressed due to the bouncing problem related to push buttons the counter will be incremented many times for each time it is pressed.

I will design the de-bouncing circuit in a later post.

The .ucf file for the above design for the basys2 board is shown below:

# Pin assignment for LEDs
NET "led8" LOC = "g1" ; 
NET "led7" LOC = "p4" ; 
NET "led6" LOC = "n4" ; 
NET "led5" LOC = "n5" ; 
NET "led4" LOC = "p6" ; 
NET "led3" LOC = "p7" ; 
NET "led2" LOC = "m11" ; 
NET "led" LOC = "m5" ; 

# Pin assignment for pushbutton switches
NET "reset" LOC = "a7"; 
NET "button" LOC = "m4"; 

# Pin assignment for clock
NET "clock" LOC = "b8"; 

The working of the above code is shown below:

Tuesday, 12 June 2012

Code to Make the LED Blink

This is a very simple program, the desired target is to make the LED's on the BASYS2 board blink, and by blinking I mean a very visible turning on and off. This can be very easily achieved in verilog. The code can be found below:


module led_simple(
    input clock,
    input reset,
    output led, led2, led3, led4, led5
    );

reg [26:0] count; //A sizable 27 bit register so that the blink can be seen and is visible, too small a register will make the
      //register stay on as it will blink extremely fast.

always@ (posedge clock or posedge reset)
 begin
  if (reset)
   count <= 0;  //if reset button is pressed, initialize or reset the register
  else 
   count <= count + 1;  //otherwise increment the register
  end
  
assign led = count[26];   //MSB connected to output led. and the other outputes conncted as below
assign led2 = count[25];
assign led3 = count[24];
assign led4 = count[23];
assign led5 = count[22];
endmodule


The concept is very simple. I defined a 27 bit register to count from 27'b0 till 27'b1. Every time the MSB and the MSB-1, MSB-2... become 1 the LED connected to that bit will turn on, otherwise it will stay off.

The register can be of any size, but it should be large enough for it to take some time to activate the MSB thus making the blink nice and visible.

The above code has been tested and can be programed onto the BASYS2 board, using the BASYS2 .ucf file I posted. Ofcourse the file must be edited for it to work, only the required pins must be used. For example the original .ucf file for this code will be:


# Pin assignment for LEDs 
NET "led5" LOC = "n5" ; 
NET "led4" LOC = "p6" ; 
NET "led3" LOC = "p7" ; 
NET "led2" LOC = "m11" ; 
NET "led" LOC = "m5" ; 

# Pin assignment for pushbutton switches
NET "reset" LOC = "a7"; 

# Pin assignment for clock
NET "clock" LOC = "b8"; 

The above code can be seen working in the video here:

Monday, 11 June 2012

User Constraint File .UCF for BASYS2 Board

Here is the user constraint file (.ucf) file that shows every pin location on the board. This file will be used in every project here as without it programming the FPGA is impossible.



# Pin assignment for LEDs NET "ld<7>" LOC = "g1" ; NET "ld<6>" LOC = "p4" ; NET "ld<5>" LOC = "n4" ; NET "ld<4>" LOC = "n5" ; NET "ld<3>" LOC = "p6" ; NET "ld<2>" LOC = "p7" ; NET "ld<1>" LOC = "m11" ; NET "ld<0>" LOC = "m5" ; # Pin assignment for slide switches NET "sw<7>" LOC = "n3"; NET "sw<6>" LOC = "e2"; NET "sw<5>" LOC = "f3"; NET "sw<4>" LOC = "g3"; NET "sw<3>" LOC = "b4"; NET "sw<2>" LOC = "k3"; NET "sw<1>" LOC = "l3"; NET "sw<0>" LOC = "p11"; # Pin assignment for pushbutton switches NET "btn<3>" LOC = "a7"; NET "btn<2>" LOC = "m4"; NET "btn<1>" LOC = "c11"; NET "btn<0>" LOC = "g12"; # Pin assignment for 7-segment displays NET "a_to_g<6>" LOC = "l14" ; NET "a_to_g<5>" LOC = "h12" ; NET "a_to_g<4>" LOC = "n14" ; NET "a_to_g<3>" LOC = "n11" ; NET "a_to_g<2>" LOC = "p12" ; NET "a_to_g<1>" LOC = "l13" ; NET "a_to_g<0>" LOC = "m12" ; NET "dp" LOC = "n13" ; NET "an<3>" LOC = "k14"; NET "an<2>" LOC = "m13"; NET "an<1>" LOC = "j12"; NET "an<0>" LOC = "f12"; # Pin assignment for clock NET "clk" LOC = "b8";