CPLD VHDL intro 2: Toggle a LED with a button
In tutorial 2 we extend the first CPLD design by controlling the LED with a push button.
- Press the button to turn off the LED
- Release the button to turn on the LED
|Button PB||LED D1|
The development boards have a simple push button (PB) connected to pin P18 of the CPLD.
|NOT pressed||HIGH (1)|
A pull-up resistor (R1) holds P18 high when the button is NOT pressed. When pressed, the button connects P18 to ground and produces a low/0 state. See the truth table above.
The XC2C64A CPLD has internal pullup resistors R1 is unpopulated on the XC2C64A development board
In this demo we'll use the input on pin P18 to control the LED D1 connected to pin P39. Due to the inverse logic of the button, the LED will turn off when the button is pressed.
library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_ARITH.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL; entity light_LED_PB is Port (BUTTON: in STD_LOGIC:='1'; LED : out STD_LOGIC:='0'); end light_LED_PB; architecture Behavioral of light_LED_PB is begin LED <= BUTTON; end Behavioral;
This is a simple variant on tutorial 1. Instead of connecting the LED directly to the power supply, we connect it to the input from the button
- BUTTON is an input. This is where the push button input will enter the CPLD. We assign this to a specific pin number in the UCF file below
- LED is an output. This is where the CPLD will output to the LED. We assign this to a specific pin in the UCF file
#PIN MAP OF DANGEROUSPROTOTYPES.COM CPLD BREAKOUT BOARDS #lICENSE: CC-0 (CREATIVE COMMONS 0) #http://dangerousprototypes.com/docs/XC9500XL_CPLD_breakout_board #http://dangerousprototypes.com/docs/CoolRunner-II_CPLD_breakout_board NET "LED" LOC = "P39"; NET "LED_INV" LOC = "P38"; NET "BUTTON" LOC = "P18";
Input and outputs in the module are mapped to actual CPLD pin numbers in the UCF file. This is an example UCF file that defines the three IO connections on the development boards.
- The Pxx numbers are the actual pin number on the CPLD. Easy.
We mapped the BUTTON input market to pin 18 and the LED output marker to pin 39.
#PIN MAP OF DANGEROUSPROTOTYPES.COM CPLD BREAKOUT BOARDS #lICENSE: CC-0 (CREATIVE COMMONS 0) #http://dangerousprototypes.com/docs/XC9500XL_CPLD_breakout_board #http://dangerousprototypes.com/docs/CoolRunner-II_CPLD_breakout_board NET "LED" LOC = "P39"; NET "LED_INV" LOC = "P38"; NET "BUTTON" LOC = "P18"; NET "BUTTON" PULLUP;
The XC2C64A UCF is slightly different than the XC9572XL version. The CoolRunner-II family (XC2C) is newer and has some advanced features like configurable pull-up resistors.
The XC2C64A development board takes advantage of the CoolRunner-II internal resistors and omits external resistor R1. This doesn't save a lot of money, but it provides a nice contrast between the devices.
NET "BUTTON" PULLUP;
To use the pullups in Verilog, we can follow several methods, but the easiest is to make a minor modification to the UCF file as shown.
TIP: To enable the in internal pull-up resistors in a new project go to fit and change default pin type to float or pull-up. Keeper and pull-ups are mutually exclusive in a single design.