Skip to content

Commit 4879e78

Browse files
committed
stage-1: count rising edges on PMOD pin 1 using onboard LEDs
1 parent 0fbee2f commit 4879e78

File tree

6 files changed

+166
-0
lines changed

6 files changed

+166
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vscode/

constraints/clock-constraint.sdc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
create_clock -period 40.00 -name {i_clk} [get_ports {i_clk}]

constraints/pin-constraints.pcf

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Go Board Pin Constraints File
2+
# Family & Device: iCE40HX1K
3+
# Package: VQ100
4+
5+
# Main FPGA Clock
6+
set_io i_clk 15
7+
8+
# LED Pins:
9+
set_io o_led_1 56
10+
set_io o_led_2 57
11+
set_io o_led_3 59
12+
set_io o_led_4 60
13+
14+
# Push-Button Switches
15+
set_io i_switch_1 53
16+
set_io i_switch_2 51
17+
set_io i_switch_3 54
18+
set_io i_switch_4 52
19+
20+
# 7 Segment Outputs
21+
set_io o_segment1_a 3
22+
set_io o_segment1_b 4
23+
set_io o_segment1_c 93
24+
set_io o_segment1_d 91
25+
set_io o_segment1_e 90
26+
set_io o_segment1_f 1
27+
set_io o_segment1_g 2
28+
set_io o_segment2_a 100
29+
set_io o_segment2_b 99
30+
set_io o_segment2_c 97
31+
set_io o_segment2_d 95
32+
set_io o_segment2_e 94
33+
set_io o_segment2_f 8
34+
set_io o_segment2_g 96
35+
36+
# UART Outputs
37+
set_io i_uart_rx 73
38+
set_io o_uart_tx 74
39+
40+
# PMOD Signals
41+
set_io io_pmod_1 65
42+
set_io io_pmod_2 64
43+
set_io io_pmod_3 63
44+
set_io io_pmod_4 62
45+
set_io io_pmod_7 78
46+
set_io io_pmod_8 79
47+
set_io io_pmod_9 80
48+
set_io io_pmod_10 81

src/led-driver.vhd

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
library ieee;
2+
use ieee.std_logic_1164.all;
3+
4+
entity LedDriver is
5+
port (
6+
i_sel0 : in std_logic;
7+
i_sel1 : in std_logic;
8+
i_sel2 : in std_logic;
9+
i_sel3 : in std_logic;
10+
o_data0 : out std_logic;
11+
o_data1 : out std_logic;
12+
o_data2 : out std_logic;
13+
o_data3 : out std_logic);
14+
end entity;
15+
16+
architecture RTL of LedDriver is
17+
begin
18+
o_data0 <= '1' when i_sel0 = '1' else '0';
19+
o_data1 <= '1' when i_sel1 = '1' else '0';
20+
o_data2 <= '1' when i_sel2 = '1' else '0';
21+
o_data3 <= '1' when i_sel3 = '1' else '0';
22+
end architecture;

src/main.vhd

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
-- PROJECT TOP.
2+
-- Counts the number of rising edges on pin 1 of the PMOD connector.
3+
-- With each rising edge, it turns on the next onboard LED in sequence,
4+
-- continuously cycling from D1 to D4.
5+
6+
library ieee;
7+
use ieee.std_logic_1164.all;
8+
use ieee.numeric_std.all;
9+
10+
entity LedCounter is
11+
-- Inputs/Outputs for the top module.
12+
port (
13+
io_pmod_1 : in std_logic; -- Clock signal from PMOD pin 1
14+
o_led_1 : out std_logic; -- Output to onboard LED 1
15+
o_led_2 : out std_logic; -- Output to onboard LED 2
16+
o_led_3 : out std_logic; -- Output to onboard LED 3
17+
o_led_4 : out std_logic); -- Output to onboard LED 4
18+
end entity;
19+
20+
architecture RTL of LedCounter is
21+
22+
-- Count up to 4 (the number of onboard LEDs).
23+
constant COUNT_LIMIT : natural := 4;
24+
25+
-- Wires connecting the two modules RisingEdgeCounter and LedDriver.
26+
signal w_sel0 : std_logic;
27+
signal w_sel1 : std_logic;
28+
signal w_sel2 : std_logic;
29+
signal w_sel3 : std_logic;
30+
31+
begin
32+
-- Module tracking the number of rising edges on pmod_1 in a local
33+
-- register, and activating the corresponding output signals.
34+
RisingEdgeCounterInstance: entity work.RisingEdgeCounter
35+
generic map (COUNT_LIMIT => COUNT_LIMIT)
36+
port map (
37+
i_clk => io_pmod_1,
38+
o_sel0 => w_sel0,
39+
o_sel1 => w_sel1,
40+
o_sel2 => w_sel2,
41+
o_sel3 => w_sel3);
42+
43+
-- Module activating the correct LED based on the control signals.
44+
-- A module is overkill here, but I wanted to experiment with wires.
45+
LedDriverInstance: entity work.LedDriver
46+
port map (
47+
i_sel0 => w_sel0,
48+
i_sel1 => w_sel1,
49+
i_sel2 => w_sel2,
50+
i_sel3 => w_sel3,
51+
o_data0 => o_led_1,
52+
o_data1 => o_led_2,
53+
o_data2 => o_led_3,
54+
o_data3 => o_led_4);
55+
56+
end architecture;

src/rising-edge-counter.vhd

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
library ieee;
2+
use ieee.std_logic_1164.all;
3+
use ieee.numeric_std.all;
4+
5+
entity RisingEdgeCounter is
6+
generic (COUNT_LIMIT : natural);
7+
port (
8+
i_clk : in std_logic;
9+
o_sel0 : out std_logic;
10+
o_sel1 : out std_logic;
11+
o_sel2 : out std_logic;
12+
o_sel3 : out std_logic);
13+
end entity;
14+
15+
architecture RTL of RisingEdgeCounter is
16+
17+
-- Register storing the number of rising edges,
18+
-- from 0 to (<onboard-leds-count> - 1).
19+
signal r_count : natural range 0 to COUNT_LIMIT - 1;
20+
21+
begin
22+
process (i_clk) is
23+
begin
24+
if rising_edge(i_clk) then
25+
if r_count = COUNT_LIMIT - 1 then
26+
r_count <= 0;
27+
else
28+
r_count <= r_count + 1;
29+
end if;
30+
end if;
31+
end process;
32+
33+
o_sel0 <= '1' when r_count = 0 else '0';
34+
o_sel1 <= '1' when r_count = 1 else '0';
35+
o_sel2 <= '1' when r_count = 2 else '0';
36+
o_sel3 <= '1' when r_count = 3 else '0';
37+
38+
end architecture;

0 commit comments

Comments
 (0)