Xilinx Chapter 19 – Basic Stamp 2 to FPGA

Written 02/25/99 by Andrew Fikes ()

To demonstrate how to interface the Basic Stamp 2 with the Xilinx FPGA, we will create a simple counter using both devices. The counter itself will be incremented within the BS2, transferred to a register in the Xilinx FPGA, and then displayed on a row of LEDs.

The first thing we must do is create the counter module within the BS2. The BS2 is programmed on the computer using a language called PBASIC. Information on PBASIC’s constructs can be found in the Basic Stamp Programming Manual. The programming manual provides in depth information on each construct, and also provides numerous examples of BS2 applications. A shorter reference guide that contains a brief description of the commands is also available from your instructor.


The following is the pinout for the BS2:

To set up the BS2:

  1. Power off the breadboard to prevent damaging the BS2.
  2. Connect PWR (pin 24) to the 5V power source on your breadboard.
  3. Connect GND (pin 23) to the ground on your breadboard.
  4. Using the serial cable and connectors provided by your instructor, hook pins 2-5 of the serial cable to pins 1-4 of the BS2. Make sure that the pins from the serial cable match up to the BS2 as follows:

Serial Cable / BS2
Pin 2 / Pin 1
Pin 3 / Pin 2
Pin 4 / Pin 3
Pin 5 / Pin 4

Once you have properly set up the BS2, start the stampw program and select the proper COM port for your serial cable. If your COM port is not listed, add it by typing its number in the COM # box. Once the stampw program has opened, you are ready to program the BS2. The following code should properly implement the counter in the BS2.

Basic Stamp II Code:

'******************************************************************

'File: bs2demo.bs2

'******************************************************************

'Name inputs

ENABLE con 07

'Declare input pins

input ENABLE

'Name outputs

CLK con 10

LED0 con 12

LED1 con 13

LED2 con 14

LED3 con 15

'Declare output pins

output CLK

output LED0

output LED1

output LED2

output LED3

'Variables

counter var nib

'Check for ENABLE signal

CHK_ENABLE:

if IN7>1 then CHK_ENABLE

'Pause to make LED change visible

pause 500

CHK_BIT0:

if counter.bit0=IN12 then CHK_BIT1

toggle LED0

CHK_BIT1:

if counter.bit1=IN13 then CHK_BIT2

toggle LED1

CHK_BIT2:

if counter.bit2=IN14 then CHK_BIT3

toggle LED2

CHK_BIT3:

if counter.bit3=IN15 then CHK_COUNT

toggle LED3

CHK_COUNT:

if counter>15 then INC_COUNT

RST_COUNT:

counter=0

high CLK

low CLK

goto CHK_ENABLE

INC_COUNT:

counter=counter+1

high CLK

low CLK

goto CHK_ENABLE

The implementation of the Xilinx module is extremely simple. The module has 5 inputs, 1 for the clock and 4 for the counter data. When the clock is asserted, the Xilinx should place the counter data from the BS2 into a register and display that data to the 4 LED’s. The following shows the schematic for the Xilinx module. Note that the register was created using the Xilinx LOGIBLOX tool, and the clock enable input to the register was removed.

Xilinx Schematic:


At this point, it is a good idea to test the modules separately to make sure they work as expected. Note that the IPADs in the Xilinx schematic are connected to switches. This was done so that the Xilinx module can be tested as a separate component. After you finish testing, however, make sure that you return the switches to the “0” position before hooking the modules together.

If you are confident that both modules work separately, you can hook the two modules together as follows:

  1. Make sure that both the BS2 and Xilinx are powered off and are hooked up properly.
  2. Both the BS2 and Xilinx have compatible source and sink currents, which means that we can hook them together without any intermediate buffering. Your instructor should have cute little cables with black ends that should make the whole process fairly simple. Using these cables, connect the BS2 and Xilinx together as follows:

BS2 / Xilinx
Pin 20 / Pin 24
Pin 19 / Pin 23
Pin 18 / Pin 20
Pin 17 / Pin 19
Pin 15 / Pin 25

  1. The only thing that remains is to hook up a switch to work the enable. The following diagram was taken from page 250 of the Basic Stamp Programming Manual and shows the proper way to hook up both active-high and active-low switches to the BS2.
  2. Now, make sure that the enable is set low, and download your modules to the BS2 and Xilinx. Set your enable high, and the LEDs should begin counting.