+ All Categories
Home > Documents > Fpga Lab Manual_student

Fpga Lab Manual_student

Date post: 03-Jan-2016
Category:
Upload: lesterpece
View: 144 times
Download: 2 times
Share this document with a friend
Popular Tags:
61
BUGHAW ELECTRONIC SOLUTIONS AND TECHNOLOGIES, INC. CPLD/FPGA Laboratory Manual Using the MAX II Starter Kit
Transcript
Page 1: Fpga Lab Manual_student

BUGHAW ELECTRONIC SOLUTIONS AND TECHNOLOGIES, INC.

CPLD/FPGA Laboratory Manual

Using the MAX II Starter Kit

Page 2: Fpga Lab Manual_student

Preface

This laboratory manual is aimed at undergraduate students taking up Computer Engineering, Computer Science, Electronic and Communications Engineering or other related courses. However, this manual may also be used by instructors as reference.

The users of this manual are assumed to have basic knowledge of Verilog Hardware Description Language or Verilog HDL. However, references and on-line tutorials for Verilog HDL are widely available. Thus, learning Verilog HDL will be quite easy.

Exercises in this laboratory manual are built on from previous exercises. Thus, before proceeding on to the next exercise, students must be familiar with the previous exercises. Also, the level of complexity of the exercises increases as one proceeds through the laboratory manual.

In each exercise, there are questions that need to be answered first before proceeding with the exercise. However, answers to these questions are located at the back of the laboratory manual.

Moreover, the complete Verilog HDL source codes for all the exercises as well as pictures or videos of the expected results are included in the CD, which will be given to the instructor.

2

©2008 BEST, Inc. All Rights Reserved

Page 3: Fpga Lab Manual_student

Contents

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

Minimum System Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

GFEC Max II Starter KitComponent view . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4Set up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

Notations and Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

Altera Quartus II 7.0 Web Edition Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

Exercise 1 – Introduction to the Max II Starter Kit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

Exercise 2 – Controlled LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

Exercise 3 – Blinking LEDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

Exercise 4 – LED Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

Exercise 5 – LED Operation Using a Push Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

Exercise 6 – 7-segment LED Display (I) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

Exercise 7 – 7-segment LED Display (II) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

Exercise 8 – Counter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

Exercise 9 – Counter With Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

Exercise 10 – PC to Max II Via RS232 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

Additional Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

Answers to questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50

Appendix A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

3

©2008 BEST, Inc. All Rights Reserved

Page 4: Fpga Lab Manual_student

Appendix B . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60

Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64

Introduction

The Max II Starter Kit emulation board is specifically designed for Altera Max II device family. It can be used by designers interested in simulating and synthesizing programmable logic circuits in their applications. The emulation board is based on the EPM1270T144C5E5 CPLD device. It provides 1270 logic elements (LEs), 116 I/O lines, and 8-Kbit user Flash Memory. Moreover, it also provides a few peripherals for your digital-logic designs.

Minimum System Requirements

Pentium III (400 MHz or faster) based computer, running one of the following operating systems: Microsoft Windows 2000 or Microsoft Windows XP

1.5 GB free hard disk space 256 MB RAM parallel and serial ports LAN card

GFEC Max II Starter Kit

A. Component view

4

©2008 BEST, Inc. All Rights Reserved

1

2

4

5

67

8

9

10

3

Page 5: Fpga Lab Manual_student

1. 4 Digital seven segment LED Displayers (DS1)2. 8-bit DIP Switches (SW6)3. 4 Push Buttons (SW1 – SW4)4. 8 LEDs (D1 – D8)5. 16 MHz oscillator6. UART Connector (J8)7. 4 Extension I/O Connectors (J1 / J2 / J3 / J4)8. DC Input Connector9. JTAG 5x2 Header Connector10. 44 Pin PLCC Socket for MPU (8051)

Note: Please refer to Appendix A for a detailed explanation of each component.

Warning: Please note that the extension I/O voltage on this emulation board can only receive up to 3.3V. If by mistake you end up burning the EPM1270T144C5 CPLD, you can buy a new device and re-solder the new CPLD onto the board.

B. Set up

1. Connect one side of the download cable to the JTAG 5x2 header connector on the emulation board and the other side to the parallel port of the CPU.

2.

5

©2008 BEST, Inc. All Rights Reserved

3

Page 6: Fpga Lab Manual_student

2. Connect the AC adapter to the DC input connector on the emulation board.

Notations and Conventions

This section provides an explanation of the notations that will be used throughout this manual.

Codes to be written are in Courier New font. Folder directories and file names are also written in Courier New font. Boldface font may indicate a series of buttons and menus to access or some notes and

hints. Words enclosed by the less than and greater than sign (< >) are keys in your keyboard

(e.g. <Tab>) while words enclosed in double quotation marks (“ “) may be an option that needs to be selected.

Boldface greater than sign (>) indicates a series of buttons or menus to access. The file naming conventions in this module consists of the word “exercise”

concatenated with the exercise number (e.g. exercise1 for the first exercise). Most of the exercises in this manual build on from the previous ones thus; it is good practice to save each exercise in different file names.

Altera Quartus II 7.0 Web Edition Installation

This will guide you through the installation process of the Integrated Development Environment (IDE) that will be used throughout the exercises.

1. Open the CD that comes with this manual and double-click on the file named 70_quartus_free.exe (the icon is shown in the figure below). The InstallShield Wizard should pop up and it will direct you to the installation menu.

6

©2008 BEST, Inc. All Rights Reserved

Page 7: Fpga Lab Manual_student

2. Click Next to continue installation

3. Select the “I accept the terms of the license agreement” option and click Next.

4. Type in your name and press <Tab> on the keyboard. Next, type in the name of your school or company.

7

©2008 BEST, Inc. All Rights Reserved

Page 8: Fpga Lab Manual_student

5. After entering the needed information, click Next.

6. This is the default location where the program will be installed. Click Next.

8

©2008 BEST, Inc. All Rights Reserved

Page 9: Fpga Lab Manual_student

7. This is the default folder name where all the needed files will be saved. Click Next.

8. Select the “Complete” option and click Next.

9

©2008 BEST, Inc. All Rights Reserved

Page 10: Fpga Lab Manual_student

9. This shows the complete setting of the installation. Click Next.

10. Wait for the installation to end. If this dialog box appears, click Yes.

10

©2008 BEST, Inc. All Rights Reserved

Page 11: Fpga Lab Manual_student

11. Installation has completed. You have the option to view the readme.txt or to launch Quartus immediately. This is done by clicking the button beside the option. After selecting the desired option/s, click Finish.

12. After installing Quartus II 7.0, apply for a license from Altera. Open your internet browser (e.g. Internet Explorer, Mozilla Firefox, etc.) and go to http://www.altera.com/support/licensing/lic-index.html .

13. Click Get Licenses > Quartus II Web Edition Software License.

11

©2008 BEST, Inc. All Rights Reserved

Page 12: Fpga Lab Manual_student

14. Fill in the needed information and click Sign in.

12

©2008 BEST, Inc. All Rights Reserved

Page 13: Fpga Lab Manual_student

15. Click Start (as shown in the left figure below). Click Run and enter <cmd>. Then, click OK.

16. Type in <ipconfig /all> and copy the Network Interface Card (NIC) number

17. Enter your Network Interface Card (NIC) number that you have copied in the previous 13

©2008 BEST, Inc. All Rights Reserved

Page 14: Fpga Lab Manual_student

step. Choose the “Yes” and “Academic” options. Then, click Continue.

18. Open your email account and download the license file to C:\altera\70\quartus and rename it to license.dat.

19. Navigate to the Desktop and double-click on the Quartus icon.

20. Upon opening of Quartus, a dialog box appears that asks you to enter your license. Select “Specify valid license file” option and click OK.

21. In the main menu, click on Tools > License Set up.

14

©2008 BEST, Inc. All Rights Reserved

Page 15: Fpga Lab Manual_student

22. Click on the drop down menu located at the right and navigate to the directory C:\altera\70\quartus. Select the license file license.dat and click Open.

23. Click OK to finish the set up and installation of Quartus II 7.0 Web Edition.15

©2008 BEST, Inc. All Rights Reserved

Page 16: Fpga Lab Manual_student

16

©2008 BEST, Inc. All Rights Reserved

Page 17: Fpga Lab Manual_student

Exercise 1 – Introduction to the Max II Starter Kit

A. Objectives

This exercise aims to familiarize the student with the basic operation of the Max II Starter Kit (M2SK) and the Quartus II 7.0 Web Edition Integrated Development Environment (IDE). By the end of this exercise, the student should be able to know the basic functionalities of the IDE and he/she will already be able to design Verilog programs that can control the different peripherals of the M2SK.

B. Instructions

Creating A Project

1. Navigate to the Desktop and double-click the “Quartus II 7.0 Web Edition” icon. This will open up the IDE.

17

©2008 BEST, Inc. All Rights Reserved

Page 18: Fpga Lab Manual_student

2. Create a new project by selecting File > New Project Wizard. This will guide you in creating your project.

3. A dialog box will pop up, click Next.

4. Another dialog box will pop up asking for your working directory. The working directory is where all of your projects will be saved. Type C:\FPGAprojects\exercise1 for the working directory and exercise1 for both the project name and the top-level design entity. Click Next.

Note: The module name should exactly match the name of the top-level design entity. It is case sensitive.

18

©2008 BEST, Inc. All Rights Reserved

Page 19: Fpga Lab Manual_student

5. A warning dialog box will then appear stating that the directory you typed does not exist. Click Yes. The directory that you have specified will automatically be created for you. Click Next.

6. Select the device to be used from the drop down list. Click Max II.

7. From the list of devices, choose EPM1270T144C5. Click Next.

8. Click Next.

9. Click Finish to complete the creation of the new project.

19

©2008 BEST, Inc. All Rights Reserved

Page 20: Fpga Lab Manual_student

Creating A Program File

10. Select File > New or press <Ctrl+N> to create a new blank file. A dialog box will pop up asking you the type of file you want to create. Select Verilog HDL file. Click OK.

11. Save (File > Save As...) this new file as exercise1.v. Make sure that the “Add File To Project” check box below is selected.

20

©2008 BEST, Inc. All Rights Reserved

Page 21: Fpga Lab Manual_student

12. Type the following code on the exercise1.v panel (ignore the numbers on the left side).

1 module exercise1 (clk, rst, led);23 input clk, rst;4 reg [7:0] state;5 output [7:0] led;67 assign led = state;89 always @ (posedge clk)10 if (rst == 0)11 state <= 8'b11111111;12 else13 state [0] <= 0;1415 endmodule

Code Explanations

Line No. Explanations

1 Words in blue are reserved words. Any Verilog program starts with the reserved word module <module_name>. In this example, the module is named exercise1. After declaring the module name, the port declaration follows. For this module, it has the following ports: clk, rst, led.

3 Input ports

4 Output ports data type (state: 8 bits)

5 Output ports

7 led will have the value of state

9-13 This block is executed for every positive edge of the clock. First, it checks whether rst is 0 or sw1 (pin 23 of the EPM1270T144C5) is pushed down. If yes, all the 8 bits of state is 1. As a result, all the 8 LEDs (pins 1-8 of the EPM1270T144C5) are turned off. If not, state [0] or the least significant bit (LSB) of state is set to 0. As a result, one of the LEDs lights up.

15 Signifies the end of the module

13. Save your file by pressing <Ctrl+S> or select File > Save in the main menu.

21

©2008 BEST, Inc. All Rights Reserved

Page 22: Fpga Lab Manual_student

Pin Assignments

Quartus II provides a few methods to specify pins. One of them is to use the menu option Assignments > Pins to specify the pins individually while the other is to use a Tcl script, wherein this will be the one used throughout the succeeding exercises.

14. Create a new Tcl script file, File > New. A dialog box will pop up asking you the type of file you want to create. Select Other files > Tcl Script. Click OK.

15. Save (File > Save As...) this new file as pin_assign.tcl.

16. Type the following code on the pin_assign.tcl panel (ignore the numbers on the left side).

Note: The port names written in the Tcl script file should match with the port names written in the program file. Moreover, see the appendix to read in more detail the pin assignments of the other peripherals.

17. Type source pin_assign.tcl in the TCL console box located inside Quartus. Press <Enter> to finish the pin assignments.

22

©2008 BEST, Inc. All Rights Reserved

port names

Page 23: Fpga Lab Manual_student

Program Compilation

18. Perform compilation by selecting in the main menu Processing > Start Compilation or clicking on the icon as shown below to compile your file.

19. Wait for the compilation to finish. When the compilation is successful, click OK.

23

©2008 BEST, Inc. All Rights Reserved

Page 24: Fpga Lab Manual_student

Program Download

20. Check if the Max II Starter Kit is turned on and connected to your PC.

21. Open Programmer by selecting in the main menu Tools > Programmer.

22. Select exercise1.pof and check Program/Configure. Click Start.

23. Once the program has finished downloading, you will be able to see the results immediately on your board.

24

©2008 BEST, Inc. All Rights Reserved

Page 25: Fpga Lab Manual_student

C. What To Expect

The D1 LED in the Max II Starter Kit (M2SK) should light up and when you press SW1, which is the reset (rst), the D1 LED in the M2SK would be turned off.

D. Summary

In this exercise, you have learned how to create Verilog HDL files and Tcl script files using the Quartus II 7.0 Web Edition IDE. Moreover, you have also learned to access and control the D1 LED on the Max II Starter Kit (M2SK).

25

©2008 BEST, Inc. All Rights Reserved

lights up

SW1

Page 26: Fpga Lab Manual_student

Exercise 2 – Controlled LED

A. Objectives

This exercise aims to design a Verilog program that controls the D1 LED with the use of a push button specifically, the SW2 push button. When SW2 is pressed and D1 LED is turned off, D1 LED will light up. However, when SW2 is pressed and D1 LED is lit up, D1 LED will be turned off.

Note: D1 LED is low-enabled.

B. Questions

What is the meaning if the LED is low-enabled? What pin in the Max II Starter Kit is designated to the SW2 push button?

C. Instructions

1. Create a new project and save it to the working directory C:\FPGAprojects\exercise2 and name your current project as exercise2.

2. Create a new Verilog HDL file and save it as exercise2.v.

3. Modify your exercise1.v code so that the D1 LED is controlled by a push button that is, when the push button is pressed and D1 LED is turned off, D1 LED will light up. However, when the push button is pressed and D1 LED is lit up, D1 LED will be turned off. Type in your code on the exercise2.v panel and save.

4. Compile your file and edit the Tcl source file (pin_assign.tcl) that you have written in the previous exercise to include the pin assignment for SW2. Copy the new Tcl file to the directory where your current project is saved.

5. Download your program to the M2SK and see what happens.

D. Summary

In this exercise, you have learned how to access and control the D1 LED of the Max II Starter Kit (M2SK) with the use of a push button.

26

©2008 BEST, Inc. All Rights Reserved

Page 27: Fpga Lab Manual_student

Exercise 3 – Blinking LEDs

A. Objectives

This exercise aims to design a Verilog program that controls the blinking of all the LEDs (D1 to D8) in the Max II Starter Kit (M2SK). The LEDs should blink every half a second or 0.5 seconds at the same time.

Note: The system clock (clk), pin 18 of the EPM1270T144C5, is 16 MHz. There should be a reset (rst) assigned to SW1 or pin 23 of the EPM1270T144C5. The switch is low when pushed down but at default, it is normally high. Also, the D1-D8 LEDs are low-enabled.

B. Questions

What pins in the Max II Starter Kit are designated to D1-D8 LEDs? How are you going to set the blinking interval for 0.5 seconds (i.e. the LED blinks for

every 0.5 seconds)?

C. Sample Code

// This code lets the D1 LED blink for every 0.5 seconds that is, D1 LED is on for 0.5 seconds and off for the next 0.5 seconds.1 module exercise3 (clk, rst, led);23 input clk, rst;4 reg [7:0] state;5 reg [23:0] counter;6 output [7:0] led;78 assign led = state;910 always @ (posedge clk)11 if (rst == 0)12 counter <= 0;13 else if (counter == 8000000)14 counter <= 0;15 else16 counter <= counter+1;1718 always @ (posedge clk)19 if (rst == 0)20 state <= 8'b11111111;21 else if (counter == 8000000)22 state [0] = ~state [0];23

27

©2008 BEST, Inc. All Rights Reserved

Page 28: Fpga Lab Manual_student

24 endmoduleD. Code Explanations

Line No. Explanations

1 Words in blue are reserved words. Any Verilog program starts with the reserved word module <module_name>. In this example, the module is named exercise3. After declaring the module name, the port declaration follows. For this module, it has the following ports: clk, rst, led.

3 Input ports

4-5 Output ports data type (state: 8 bits, counter: 24 bits)

6 Output ports

8 led will have the value of state

10-16 This block is executed for every positive edge of the clock. First, it checks whether rst is 0 or sw1 is pushed down. If yes, counter is set to 0, if not, the value of counter is checked. If the condition is satisfied that is, if counter is equal to 8000000, then, counter is set to 0. If not, counter is incremented.

18-22 This block is also executed for every positive edge of the clock. First, it checks whether rst is 0 or sw1 is pushed down. If yes, all the bits of state have the value of 1, as a result, all the 8 LEDs are turned off. If not, the value of counter is checked. If the condition is satisfied that is, if counter is equal to 8000000, state [0] or the least significant bit (LSB) of state is set to the inverse of the previous vale of state [0]. As a result, the value of state [0] changes every 0.5 seconds and the D1 LED lights up for 0.5 seconds and then, it is turned off for 0.5 seconds.

Note: Why 8000000? The frequency of the oscillator is 16MHz that is, 16000000 cycles per second. If each transition should only take 0.5 s then, the counter should be set at 16000000/2 or 8000000 before the next transition.

24 Signifies the end of the module

E. Instructions

1. Create a new project and save it to the working directory C:\FPGAprojects\exercise3 and name your current project as exercise3.

2. Create a new Verilog HDL file and save it as exercise3.v.

28

©2008 BEST, Inc. All Rights Reserved

Page 29: Fpga Lab Manual_student

3. Modify the sample code given above so that not only the D1 LED lights up but all the 8 LEDs and also, all the LEDs blink every 0.5 seconds. Type in your code on the exercise3.v panel and save.

Hint: Not only the least significant bit (LSB) of state changes its value but all the 8 bits of state.

4. Compile your file and edit the Tcl source file (pin_assign.tcl) that you have written in the previous exercise to include the pin assignments for D1-D8 LEDs. Copy the new Tcl file to the directory where your current project is saved.

5. Download your program to the M2SK and see what happens.

F. Summary

In this exercise, you have learned how to access and control the D1-D8 LEDs of the M2SK. In the succeeding exercises, you will be asked to output a certain pattern of the LEDs in the M2SK.

29

©2008 BEST, Inc. All Rights Reserved

Page 30: Fpga Lab Manual_student

Exercise 4 – LED Pattern

A. Objectives

This exercise aims to design a Verilog program that outputs the pattern below using the LEDs.

D1 – D4 D5 - D8

The transition interval for each set is 0.5 seconds. A colored dot means that the corresponding LED lights up.

Note: The system clock (clk), pin 18 of the EPM1270T144C5, is 16 MHz. There should be a reset (rst) assigned to SW1 or pin 23 of the EPM1270T144C5. The switch is low when pushed down but at default, it is normally high. Also, the D1-D8 LEDs are low-enabled.

B. Questions

What are the uses of case statements in Verilog? Is it necessary to provide a default in case statements?

30

©2008 BEST, Inc. All Rights Reserved

Page 31: Fpga Lab Manual_student

C. Instructions

1. Create a new project and save it to the working directory C:\FPGAprojects\exercise4 and name your current project as exercise4.

2. Create a new Verilog HDL file and save it as exercise4.v.

3. Modify your exercise3.v code so that the 8 LEDs will output a certain pattern as described above. Type in your code on the exercise4.v panel and save.

Hint: Use case statements to output the sequential LED pattern.

4. Compile your file and copy the Tcl source file (pin_assign.tcl) that you have written in the previous exercise to the directory where your current project is saved.

5. Download your program to the M2SK and see what happens.

D. Summary

In this exercise, you have learned how to output a certain pattern of the LEDs in the M2SK.

31

©2008 BEST, Inc. All Rights Reserved

Page 32: Fpga Lab Manual_student

Exercise 5 – LED Operation Using a Push Button

A. Objectives

This exercise aims to design a Verilog program that outputs the pattern below using the LEDs.

D1 – D4 D5 - D8

The transition from one set to another is controlled by a push button SW2 (pin 24 of the EPM1270T144C5). A colored dot means that the corresponding LED lights up.

Note: The system clock (clk), pin 18 of the EPM1270T144C5, is 16 MHz. There should be a reset (rst) assigned to SW1 or pin 23 of the EPM1270T144C5. The switch is low when pushed down but at default, it is normally high. Also, the D1-D8 LEDs are low-enabled.

B. Questions

What is a switch bounce? What are possible ways to remedy this problem?

32

©2008 BEST, Inc. All Rights Reserved

Page 33: Fpga Lab Manual_student

C. Instructions

1. Create a new project and save it to the working directory C:\FPGAprojects\exercise5 and name your current project as exercise5.

2. Create a new Verilog HDL file and save it as exercise5.v.

3. Modify your exercise4.v code so that the LED transition is controlled by a push button. Type your code on the exercise4.v panel and save.

4. Compile your file and copy the Tcl source file (pin_assign.tcl) that you have written in the previous exercise to the directory where your current project is saved.

5. Download your program to the M2SK and see what happens.

C. Summary

In this exercise, you have learned how to access and control the LEDs using a push button. In the succeeding exercises, you will be asked to output data into the digital display or the 7-segment LEDs.

33

©2008 BEST, Inc. All Rights Reserved

Page 34: Fpga Lab Manual_student

Exercise 6 – 7-Segment LED Display (I)

A. Objectives

This exercise aims to design a Verilog program that outputs the letter “F” to the seven segment display in the Max II Starter Kit (M2SK).

B. Questions

Is the segment pin in the 7-segment LED display low-enabled or high-enabled? What does it mean?

What pins in the Max II Starter Kit are designated to the 4 7-segment LEDs?

C. Sample Code

// This code outputs the number 4 to one of the digital displays of the M2SK.

1 module exercise6 (clk, rst, m1, m2, m3, m4, a, b, c, d, e, f, g, dp);23 input clk, rst;4 reg [3:0] state;5 reg [6:0] number;6 output m1, m2, m3, m4, a, b, c, d, e, f, g, dp;78 parameter number4 = 7'b1001100;910 assign {m1, m2, m3, m4} = state;11 assign {a, b, c, d, e, f, g} = number;12 assign dp = 1;1314 always @ (posedge clk)15 if (rst == 0)16 state <= 4'b0000;17 else 18 state <= 4'b0001;1920 always @ (posedge clk)21 if (rst == 0)22 number <= 7'h7f;

34

©2008 BEST, Inc. All Rights Reserved

Page 35: Fpga Lab Manual_student

23 else 24 number <= number4;2526 endmodule

D. Code Explanations

Line No. Explanations

1 Words in blue are reserved words. Any Verilog program starts with the reserved word module <module_name>. In this example, the module is named exercise6. After declaring the module name, the port declaration follows. For this module, it has the following ports: clk, rst, m1, m2, m3, m4, a, b, c, d, e, f, g, dp

Note: m1 – m4 (four digital displays) a, b, c, d, e, f, g, dp (each LED in the 7- segment display

including the dot point)

3 Input ports

4-5 Output ports data type (state: 4 bits, number: 7 bits)

6 Output ports

8 Assigns the 7-segment display of the numbers

10 Assigns the values of the 4 bits of state to m1-m4 respectively

11 Assigns the values of the 7 bits of number to a, b, c, d, e, f, g, dp respectively

12 Assigns the value of 1 to dpNote: Since the LEDs are low-enabled, assigning the value of 1 to dp means that this specific LED is turned off all the time

14-18 This block is executed for every positive edge of the clock. First, it checks whether rst is 0 or sw1 is pushed down. If yes, all the bits of state have the value of 0, as a result, the 4 digital displays are inactive. If not, the LSB of state is set or its value is equal to 1. As a result, one of the digital displays is active.

35

©2008 BEST, Inc. All Rights Reserved

Page 36: Fpga Lab Manual_student

20-24 This block is also executed for every positive edge of the clock. First, it checks whether rst is 0 or sw1 is pushed down. If yes, all the bits of number have the value of 1, as a result, all the LEDs in the 7-segment display are turned off. If not, number4 is assigned to number thus; the number 4 is displayed to the active digital display.

26 Signifies the end of the module

E. Instructions

1. Create a new project and save it to the working directory C:\FPGAprojects\exercise6 and name your current project as exercise6.

2. Create a new Verilog HDL file and save it as exercise6.v.

3. Modify the given sample code above wherein instead of displaying the number 4, the letter F will be displayed. Type in your code on the exercise6.v panel and save.

4. Compile your file and edit the Tcl source file (pin_assign.tcl) that you have written in the previous exercise to include the pin assignments for the four 7-segment LEDs. Copy the new Tcl file to the directory where your current project is saved.

5. Download your program to the M2SK and see what happens.

F. Summary

In this exercise, you have learned how to output numbers and letters to the digital display of the M2SK.

36

©2008 BEST, Inc. All Rights Reserved

m4m3m2m1

Page 37: Fpga Lab Manual_student

Exercise 7 – 7-Segment LED Display (II)

A. Objectives

This exercise aims to design a Verilog program that outputs the numbers letters “bESt” to the seven segment display in the Max II Starter Kit (M2SK).

B. Questions

Is the common pin in the 7-segment LED display low-enabled or high-enabled? What does it mean?

C. Sample Code

// This code outputs the numbers 1 to 4 in the 4 digital displays of the M2SK.

1 module exercise7 (clk, rst, m1, m2, m3, m4, a, b, c, d, e, f, g, dp);23 input clk, rst;4 reg [3:0] state;5 reg [23:0] counter;6 reg [6:0] number;7 output m1, m2, m3, m4, a, b, c, d, e, f, g, dp;89 parameter number1 = 7'b1001111;10 parameter number2 = 7'b0010010;11 parameter number3 = 7'b0000110;12 parameter number4 = 7'b1001100;1314 assign {m1, m2, m3, m4} = state;15 assign {a, b, c, d, e, f, g} = number;16 assign dp = 1;1718 always @ (posedge clk)19 if (rst == 0)20 counter <= 0;21 else if (counter == 100000)22 counter <= 0;23 else24 counter <= counter+1;2526 always @ (posedge clk)27 if (rst == 0)28 state <= 4'b0000;29 else if (counter == 100000)30 case (state)31 4’b0000: state <= 4'b0001;32 4’b0001: state <= 4'b0010;

37

©2008 BEST, Inc. All Rights Reserved

Page 38: Fpga Lab Manual_student

33 4’b0010: state <= 4'b0100;34 4’b0100: state <= 4'b1000;35 4’b1000: state <= 4'b0000;36 default: state <= 4'b0000;37 endcase3839 always @ (posedge clk)40 if (rst == 0)41 number <= 7'h7f;42 else 43 case (state)44 4’b0001: number <= number1;45 4’b0010: number <= number2;46 4’b0100: number <= number3;47 4’b1000: number <= number4;48 default: number <= 7'h7f;49 endcase5051 endmodule

D. Code Explanations

Line No. Explanations

1 Words in blue are reserved words. Any Verilog program starts with the reserved word module <module_name>. In this example, the module is named exercise7. After declaring the module name, the port declaration follows. For this module, it has the following ports: clk, rst, m1, m2, m3, m4, a, b, c, d, e, f, g, dp

Note: m1 – m4 (four digital displays) a, b, c, d, e, f, g, dp (each LED in the 7- segment display

including the dot point)

3 Input ports

4-6 Output ports data type (state: 4 bits, counter: 24 bits, number: 7 bits)

7 Output ports

9-12 Assigns the 7-segment display of the numbers 1-4

14 Assigns the values of the 4 bits of state to m1-m4 respectively

38

©2008 BEST, Inc. All Rights Reserved

Page 39: Fpga Lab Manual_student

15 Assigns the values of the 7 bits of number to a, b, c, d, e, f, g, dp respectively

16 Assigns the value of 1 to dpNote: Since the LEDs are low-enabled, assigning the value of 1 to dp means that this specific LED is turned off all the time

18-24 This block is executed for every positive edge of the clock. First, it checks whether rst is 0 or sw1 is pushed down. If yes, counter is set to 0, if not, the value of counter is checked. If the condition is satisfied that is, if counter is equal to 100000, then, counter is set to 0. If not, counter is incremented.

26-37 This block is also executed for every positive edge of the clock. First, it checks whether rst is 0 or sw1 is pushed down. If yes, all the bits of state have the value of 0, as a result, the 4 digital displays are inactive. If not, the value of counter is checked. If the condition is satisfied that is, if counter is equal to 100000, a case statement follows wherein the 4-bit value of state is checked and a new value of state is assigned. This controls what digital display is enabled that is, if state [0] is set to 1, the digital display assigned to m4 is active. If state [1] is set to 1, the digital display assigned to m3 is active. If state [2] is set to 1, the digital display assigned to m2 is active. If state [3] is set to 1, the digital display assigned to m1 is active.

39-49 This block is also executed for every positive edge of the clock. First, it checks whether rst is 0 or sw1 is pushed down. If yes, all the bits of number have the value of 1, as a result, all the LEDs in the 7-segment display are turned off. If not, a case statement follows wherein the 4-bit value of state is checked and the corresponding 7-segment display of a number is assigned that is, for the m4 display, the number 1 is assigned. For m3, the number 2 is assigned, for m2, the number 3 and for m1, the number 4 is assigned.

51 Signifies the end of the module

39

©2008 BEST, Inc. All Rights Reserved

m4 m3 m2 m1

Page 40: Fpga Lab Manual_student

E. Instructions

1. Create a new project and save it to the working directory C:\FPGAprojects\exercise7 and name your current project as exercise7.

2. Create a new Verilog HDL file and save it as exercise7.v.

3. Modify the given sample code above wherein each digital display should output one letter as shown in the figure below instead of the numbers 1 to 4. Type in your code on the exercise7.v panel and save.

4. Compile your file and copy the Tcl source file (pin_assign.tcl) that you have written in the previous exercise to the directory where your current project is saved.

5. Download your program to the M2SK and see what happens.

F. Summary

In this exercise, you have learned how to access and control the four digital displays and its corresponding 7-segment LEDs. You also have learned how to output numbers and letters to the digital display of the M2SK.

40

©2008 BEST, Inc. All Rights Reserved

Page 41: Fpga Lab Manual_student

Exercise 8 – Counter

A. Objectives

This exercise aims to design a Verilog program that outputs 0000 to 9999 to the seven segment display in the Max II Starter Kit (M2SK) with a 0.1 second interval.

B. Questions

How are you going to set the counting interval at 0.1 seconds?

C. Sample Code

// This code outputs the counting of numbers from 0 to 9 at a 1 second interval in the digital display of the M2SK.

1 module exercise8 (clk, rst, m1, m2, m3, m4, a, b, c, d, e, f, g, dp);23 input clk, rst;4 output m1, m2, m3, m4;5 output a, b, c, d, e, f, g, dp;67 reg [23:0] counter;8 reg [19:0] state;9 reg [7:0] number;1011 assign {m1, m2, m3, m4} = state;12 assign {a, b, c, d, e, f, g} = number;13 assign dp = 1;1415 parameter number1 = 7'b1001111;16 parameter number2 = 7'b0010010;17 parameter number3 = 7'b0000110;18 parameter number4 = 7'b1001100;19 parameter number5 = 7'b0100100;20 parameter number6 = 7'b0100000;21 parameter number7 = 7'b0001111;22 parameter number8 = 7'b0000000;23 parameter number9 = 7'b0000100;24 parameter number0 = 7'b0000001;2526 always @ (posedge clk)27 if (rst == 0)28 counter <= 0;29 else if (counter == 16000000)30 counter <= 0;31 else32 counter <= counter+1;

41

©2008 BEST, Inc. All Rights Reserved

Page 42: Fpga Lab Manual_student

3334 always @ (posedge clk)35 if (rst == 0)36 state <= 4'b0000;37 else 38 state <= 4'b0001;3940 always @ (posedge clk)41 if (rst == 0)42 number <= number0;43 else if (counter == 16000000)44 case (number)45 number0: number <= number1;46 number1: number <= number2;47 number2: number <= number3;48 number3: number <= number4;49 number4: number <= number5;50 number5: number <= number6;51 number6: number <= number7;52 number7: number <= number8;53 number8: number <= number9;54 number9: number <= number0;55 default: number <= number0;56 endcase5758 endmodule

D. Code Explanations

Line No. Explanations

1 Words in blue are reserved words. Any Verilog program starts with the reserved word module <module_name>. In this example, the module is named exercise8. After declaring the module name, the port declaration follows. For this module, it has the following ports: clk, rst, m1, m2, m3, m4, a, b, c, d, e, f, g, dp

Note: m1 – m4 (four digital displays) a, b, c, d, e, f, g, dp (each LED in the 7- segment display

including the dot point)

3 Input ports

4-5 Output ports

7-9 Output ports data type (state: 4 bits, counter: 24 bits, number: 7 bits)

11 Assigns the values of the 4 bits of state to m1-m4 respectively

42

©2008 BEST, Inc. All Rights Reserved

Page 43: Fpga Lab Manual_student

12 Assigns the values of the 7 bits of number to a, b, c, d, e, f, g, dp respectively

13 Assigns the value of 1 to dpNote: Since the LEDs are low-enabled, assigning the value of 1 to dp means that this specific LED is turned off all the time

15-24 Assigns the 7-segment display of the numbers 0-9

26-32 This block is executed for every positive edge of the clock. First, it checks whether rst is 0 or sw1 is pushed down. If yes, counter is set to 0, if not, the value of counter is checked. If the condition is satisfied that is, if counter is equal to 16000000, then, counter is set to 0. If not, counter is incremented.

Note: Why 16000000? This is because the frequency of the oscillator is 16MHz that is, 16000000 cycles per second. If each transition should only take 1 s then, the counter should be set at 16000000.

34-38 This block is also executed for every positive edge of the clock. First, it checks whether rst is 0 or sw1 is pushed down. If yes, all the bits of state have the value of 0, as a result, the 4 digital displays are inactive. If not, the value of counter is checked. If the condition is satisfied that is, if counter is equal to 16000000, a new value of state is set, which enables one of the 7-segment LEDs to be active specifically, the m4 digital display.

40-56 This block is also executed for every positive edge of the clock. First, it checks whether rst is 0 or sw1 is pushed down. If yes, the number 0 is displayed. If not, the value of counter is checked. If the condition is satisfied that is, if counter is equal to 16000000, a case statement follows wherein the value of number is checked and a new value of number is assigned, which is displayed in the active 7-segment LED.

58 Signifies the end of the module

43

©2008 BEST, Inc. All Rights Reserved

m4 m3 m2 m1

Page 44: Fpga Lab Manual_student

C. Instructions

1. Create a new project and save it to the working directory C:\FPGAprojects\exercise8 and name your current project as exercise8.

2. Create a new Verilog HDL file and save it as exercise8.v.

3. Modify the sample code given above wherein the 4 digital displays will output the counting of numbers from 0000 to 9999. The 7-segment LED display of the different numbers is shown in the figure below. Type in your code on the exercise8.v panel and save.

4. Compile your file and copy the Tcl source file (pin_assign.tcl) that you have written in the previous exercise to the directory where your current project is saved.

5. Download your program to the M2SK and see what happens.

D. Summary

In this exercise, you have learned how to control the output of the four digital displays. In the succeeding exercises, you will be asked to control the output with the use of push buttons and a switch.

44

©2008 BEST, Inc. All Rights Reserved

Page 45: Fpga Lab Manual_student

Exercise 9 – Counter with Controls

A. Objectives

This exercise aims to design a Verilog program that outputs 0000 to 9999 to the seven segment display in the Max II Starter Kit (M2SK) with a 0.1 second interval when the DIP1 switch (pin 22 of the EPM1270T144C5) is off. When the DIP1 switch is on, the output number will increment by 1 if SW3 (pin 27 of the EPM1270T144C5) is pressed and it will decrement by 1 if SW4 (pin 28 of the EPM1270T144C5) is pressed.

B. Questions

How can you tell if the DIP1 switch on the M2SK is on or not? Where is the SW3 and SW4 of the M2SK located? What pins in the Max II Starter Kit are assigned to SW3 and SW4?

C. Instructions

1. Create a new project and save it to the working directory C:\FPGAprojects\exercise9 and name your current project as exercise9.

2. Create a new Verilog HDL file and save it as exercise9.v.

3. Modify your exercise8.v code so that when the DIP1 switch is off, the result is similar to that in the previous exercise. However, if the DIP1 switch is on, the output in the digital display will increment by 1 if SW3 is pressed and it will decrement by 1 if SW4 is pressed. Type in your code on the exercise9.v panel and save.

4. Compile your file and edit the Tcl source file (pin_assign.tcl) that you have written in the previous exercise to include the pin assignments for SW3 and SW4. Copy the new Tcl file to the directory where your current project is saved.

5. Download your program to the M2SK.

45

©2008 BEST, Inc. All Rights Reserved

Page 46: Fpga Lab Manual_student

D. Test your program

After you have downloaded the program, you will be able to see the start of the counting from 0000 to 9999 on the digital display. This is because at default, the DIP1 switch is off. Turning the switch on will stop the counting. Press SW3 and the count should increment. Press SW4 and the count should decrement.

E. Summary

In this exercise, you have learned how to control the output of the four digital displays with the use of push buttons and a switch.

46

©2008 BEST, Inc. All Rights Reserved

SW3

SW4

Page 47: Fpga Lab Manual_student

Exercise 10 – PC to MAX II via RS232

A. Objectives

This exercise aims to design a Verilog program that communicates to a personal computer (PC) via the RS232 protocol. You have the option to make a PC program or use the HyperTerminal to send data to the Max II kit. The Verilog program should display data or message serially using the four seven (7) segment display of the Max II Starter Kit (M2SK). Implement different baud rates selectable using the DIP switches.

B. Questions

What is the ASCII code? What are the hexadecimal values of 1? 2? 3? 4? 5? b? E? S? T? What pin in the Max II Starter Kit is assigned to receive data in the UART connector?

C. Instructions

1. Create a new project and save it to the working directory C:\FPGAprojects\exercise10 and name your current project as exercise10.

2. Create a new Verilog HDL file and save it as exercise10.v.

3. Type in your code on the exercise10.v panel and save. (refer to the sample code given in the Appendix B)

4. Compile your file and edit the Tcl source file (pin_assign.tcl) that you have written in the previous exercise to include the pin assignments for the UART connector. Copy the new Tcl file to the directory where your current project is saved.

5. Download your program to the M2SK.

47

©2008 BEST, Inc. All Rights Reserved

Page 48: Fpga Lab Manual_student

Hyperterminal Connection

6. Go to Start > All Programs > Accessories > Communication > HyperTerminal

7. Type in your name. Click OK.

8. Select COM1 connection. Click OK.

48

©2008 BEST, Inc. All Rights Reserved

Page 49: Fpga Lab Manual_student

9. Type in the COM1 properties. Choose None for Flow Control. Click OK.

10. Start typing in your keyboard and see the output in the digital display of the M2SK.

D. Test Your Program

Once you pressed any character in your keyboard, you will be able to see the hexadecimal value of the ASCII character that you have typed in the two rightmost digital displays of the M2SK. Once you entered in another character or a new character, the previous value would be shifted to the left and it will be shown in the two leftmost digital displays of the M2SK while the new hexadecimal value will be shown in the two rightmost digital displays of the M2SK.

E. Summary

In this exercise, you have learned how to display data in the four 7-segment displays of the Max II Starter Kit via RS232.

49

©2008 BEST, Inc. All Rights Reserved


Recommended