You are on page 1of 12

ECE349/GECE551 Rapid Prototyping with FPGA

Electrical and Computer Engineering Department

4
Gannon University LabReport

GANNON UNIVERSITY
Cyber Engineering 237: Introduction to Cyber-Physical
Systems

Lab
Wire & Wireless System
Interface
1. Design Objective

1
ECE349/GECE551 Rapid Prototyping with FPGA
Electrical and Computer Engineering Department
Gannon University
Establish a wireless communication channel between a sensor system and an
FPGA module to display the distance measured by an ultrasound sensor module on a
VGA monitor via VGA interface. The wireless communication channel is built by
employing a pair of XBee modules.

1.1. Design Specification


Design a wireless sensor system and an interface module to display the sensor
signal processing results to the VGA monitor connected to an FPGA. Establish a
wireless communication channel by configuring a pair of XBee modules. Program the
microcontroller board to process the sensor signals received and convert the sensor
signals to meaningful data to display on the monitor. A VHDL model should be
developed for receiving the converted sensor signals via the wireless device, XBee and
implement a VGA display standard to display the measure data on the monitor. Figure 1
shows a block diagram to describe the system partitioning.

Figure 1. Diagram of the Project System

1.2. PING Ultrasonic Sensor


The ping ultrasonic sensor is a low power sensor that is commonly used in
applications where a relatively accurate displacement measurement is required. Figure 2
shows how the sensor can be configured and used.

Figure 2. PING Ultrasonic Sensor

A short pulse is sent from the Arduino to the sensor to trigger a 40 kHz “click”
event. The click is transmitted from the sensor and an “echo” is returned from any objects
that were in the click’s path. The sensor will return an echo time pulse, which will be high
until the echo returns. Since this distance
2
ECE349/GECE551 Rapid Prototyping with FPGA
Electrical and Computer Engineering Department
Gannon University
is equal to the echo time pulse duration times the speed of sound (in desired units) it can be
observed that this device is truly only limited to the precision of the microcontrollers
clock.

1.3. Arduino Microcontroller


An Arduino Uno was used to interface with the PING ultrasonic sensor and the
ZigBee wireless transmitter. The microcontroller was used to send a trigger to the PING
sensor. The microcontroller was then used to measure the duration of the echo time pulse
duration and subsequently calculate the displacement. The displacement was then packaged
into a string and send serially to the ZigBee transmitter. This microcontroller provided a
serial interface which could easily communicate with the ZigBee transmitter and had a fast
enough clock speed which it could accurately measure the PING’s echo time pulse.

1.4. ZigBee
ZigBee is a common wireless protocol that is used in various applications. Our device
transmitted at 2.4 GHz and can achieve data rates up to 250 kbit/second. This is excessive
given our small displacement message was a fixed length of 12 bytes. On the transmitter, the
ZigBee RX was connected to the TX of the Arduino. On the receiver, the ZigBee TX was
connected to the RX of the FPGA.

1.5. FPGA
Figure 3 shows the layout of the FPGA code layout.

Figure 3. FPGA Code Layout

The top level (dist_view) is used to instantiate each of the subcomponents. The
inputs into this module are the clock for synchronization, a key reset and the data passed to
the uart. The data passed to the uart is the displacement message passed from the ZigBee
receiver. The outputs of this top level module are everything required to display to the VGA
monitor: red, green and blue output (red_out, blue_out, and green_out) for a given pixel and
the horizontal and vertical sync signals (hsync and vsync).

There are three main subcomponents to the distance display which are the uart,
vga_sync and pixel_gen_dist. The vga_sync was provided in the lab 8 manual and was not

3
ECE349/GECE551 Rapid Prototyping with FPGA
Electrical and Computer Engineering Department
Gannon University
modified. A semi functioning uart was also provided but was modified to meet our design
requirements.

1.6. UART
The purpose of the uart is to receive the serial message from the ZigBee which
contains the displacement message. Once the data is received it separates the data buffer
into the three digits and the unit characters (“ cm”). The digits and unit characters are stored
in the variables dig_0 through dig_5.

1.7. VGA SYNC


The vga_sync is used to synchronize the output of VGA monitor with the desired
displayed value. Vga_sync will use clock, and reset as inputs and then return the pixel tick
(a clock used in other module), horizontal and vertical sync signal, a signal to display if
currently in a viewable region, and current coordinates pixel x and y.

1.8. Pixel Generation


The pixel_gen_dist is responsible for determining how to display each character of
the displacement message. It looks at the current position from the vga_sync and determines
if that pixel makes up part of the screen that the code defines as a boundary for a character.
The inputs to this module are a reset and clock for synchronization, a signal to determine if
in a currently viewable region (display_on), the current coordinates pixel x and y, and then
each of the 6 characters that make up the displacement message. The outputs are the red,
green, and blue for the given pixel.

Pixel_gen_dist determines the corresponding ASCII letter and how to display that
letter within its allocated space by doing a lookup within the font_rom. Font_rom contains
an 8x16 binary array for each ASCII character. The ones are pixels that should be
illuminated to properly display the ASCII character.

1.9. Design Entry


The ZigBee module passes a serial message to the FPGA that contains the
displacement information. The PING sensor had a very wide range (from a single cm to
triple digit precision) that it could measure. This range variability could have driven a range
of string length, so a design decision was made to force the string length to 6 characters (3
integer digits and 3 characters for the units). The format string function (sprintf) was used to
force the numeric length to 3 digits, which would guarantee a string length of 6 characters.
How the message is displayed on the screen is shown in Figure 4.

Vga_sync returns the current x and y pixel that is being updated. The FPGA code
does a look up to determine if the current position is within bounds of a digit. Figure 1
shows that each digit starts at y_start in the y axis and then ends at y_start + length of array
representing ASCII characters(16) * scale factor (to increase size of font). If the y position is
outside of these bounds then the red, green, and blue are set to zeros so that black is
displayed. The current x position can be compared to each digit’s boundary to determine
which digit should be evaluated in the font rom.

4
Figure 4. Monitor Display Layout

Once it is determined which digit is current being updated the code does a look up on the ASCII
table (font rom) for the digits ASCII code. The displacement message is always comprised of three
numeric characters (‘0’ through ‘9’), and then a space and then followed by ‘c’ and ‘m’. Table 1 shows
the ASCII code associated with the desired characters. The font_rom represents each ASCII character in
an 8x16 array that is concatenated together for every ASCII character. So to determine the starting row of
a character, the ASCII code was multiplied by 16 to return the corrected offset in the table.

Table 1. ASCII Table

Code Hex Char


48 30 0
49 31 1
50 32 2
51 33 3
52 34 4
53 35 5
54 36 6
55 37 7
56 38 8
57 39 9
32 20 Spac

5
e
99 63 c
109 6D m

Within an individual character it is determined which part of the ASCII array corresponds to the
current x and y pixel. Within the boundary of the digit, it is divided into 16 parts in the y axis and 8 parts
in the 8 axis. As the vga_sync moved along the viewable region of the individual digit then it is
determined which individual bit of the font_rom should be used for display. Figure 5 shows how the
individual digit is divided and then how font from the array is mapped into the display. Figure 5 uses the
example of the character ‘8’ which is ASCII code 0x38.

Figure 5. Individual Digit Layout

If the ASCII table value was a zero, then red, green, and blue are set to zero so that the pixel
matches the black background. If the ASCII table value was one, then red, green and blue were also set to
one to make the font white.

The ZigBee module passes in a serial message of characters. Since the modules were only setup
to transmit from the Arduino side and receive from the FPGA there was no guarantee that the message
would be received in the correct order or that multiple transmissions were not mixed up. This
asynchronous transmission would sometimes shift the message such that instead of displaying the 3
numeric digits, a space and the unit labels we would display them out of order or only partially with other
garbage characters. To correct this asynchronous transmission, a design was put in place to compare the
characters as they were received. It was known that the last character would always be a ‘m’ of ‘ cm’. So,
each character is the message buffer was compared to the ASCII value for ‘m’. Once an ‘m’ was found,

6
then we knew the buffer was full and the transmission had finished. The display should only be updated
once the ‘m’ was received.

The following procedures can be used as references of your design and implementation on both of
the microcontroller and FPGA boards:
An implementation procedure of an XBee transmitter module to microcontroller board

1. First Part include interface XBee with an adapter (Chosen adapter is XBee Adapter Kit
V1.1).
2. Connect Power from Arduino to 5V. XBee adapter in which XBee module is mounted on.
3. Connect GND from Arduino to GND in XBee adapter in which XBee module is mounted
on.
4. Connect Tx pin from Arduino to Rx pin in XBee adapter in which XBee module is
mounted on.
5. Connect Rx pin from Arduino to Tx pin in XBee adapter in which XBee module is
mounted on.

Controlling Logic has been done based on the received sensor signal and with the use of AD
pins, 0, 1, 3, and 4, wireless commands have been issued from Arduino board and sent via XBee
module wirelessly to trigger an action according to those pins status ( High or Low).

Appendix 1 include a sample of Arduino code sent to set pin AD4 High and Low.

2. An implementation procedure of an XBee receiver module to FPGA


board
1. Like the sender module receiver XBee module has to be interfaced with an XBee adapter
(Chosen adapter is XBee Adapter Kit V1.1).

2. For the receiver module AD pins on XBee adapter (AD0, AD1, AD3, AD4) has been
soldered with junction wires for FPGA interfacing purposes.

3. J4 and J2 header extensions on FPGA Spartan 3E has been used to interface XBee receiver
module with the board since those headers have an IO pins in which can swiftly be used for
our design.

4. J4 VCC is connected to 3.3 V of the XBee module to power the XBee module .

5. J4 GND is connected to the ground of the XBee module .

6. IO9 (D7) of J4 header extension is connected to AD0 of the XBee module .

7. IO10 (C7) of J4 header extension is connected to AD1 of the XBee module.

8. IO12 (E8) of J4 header extension is connected to AD4 of the XBee module.

7
9. IO8 (F7) of J2 header extension is connected to AD3 of the XBee module .

Logic received wirelessly through XBee can be used to apply action on FPGA board as
indicated by Appendix 2 code and Appendix 3 which represents UCF file for that code.

3. Test Plan
While sitting on a table (so that it is stationary), place the PING sensor x distance away
from a solid wall. Use table 2 below to determine the desired distance, x, for each test case.
Measure the distance from the sensor to the wall with a tape measure.

Table 2: Test Plan

Test True Distance


Case (x)
1 2.54 cm (1”)
2 15.24 cm (6”)
3 30.48 cm (1’)
4 60.96 cm (2’)
5 91.44 cm (3’)
6 152.4 cm (5’)
7 304.8 cm (10’)

4. Report
The Design and verification report should be a written report including all the detailed aspects of the lab
activities.

5. Appendix 1
/* Ping Sensor (3-pin Ultrasonic sensor module) The
circuit:
* +V connection of the PING attached to +5V
* GND connection of the PING attached to ground
* SIG connection of the PING attached to digital pin 7
*/
// this constant won't change. It's the pin number of the sensor's output:
const int pingPin = 7;

8
long interval = 100; long
previousMillis = 0; void setup() {
// initialize serial communication:
Serial.begin(9600);
}

void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
unsigned long currentMillis = millis(); long
duration, inches, cm;
char str[7];

previousMillis = currentMillis;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2); digitalWrite(pingPin,
HIGH); delayMicroseconds(5);
digitalWrite(pingPin, LOW);

// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

// convert the time into a distance


//inches = microsecondsToInches(duration); cm =
microsecondsToCentimeters(duration);
//Serial.print(inches);
//Serial.print("in, ");
//Serial.print(cm); sprintf(str, "%3d
cm",cm); Serial.print(str);
Serial.println(); delay(100);
}

long microsecondsToInches(long microseconds)


{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
return microseconds / 74 / 2;

9
}

long microsecondsToCentimeters(long microseconds)


{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled. return
microseconds / 29 / 2;
}

void xBeeSignalHigh()
{
Serial.write(0x7E);//Start byte, represents the start of data frame
Serial.write(0x00);//Length of the frame (MSB)
Serial.write(0x10);//Length of the frame (LSB)
Serial.write(0x17);//Frame ID represents AT command
Serial.write(0x00);//No reply for AT command
Serial.write(0x00);//Start of the MAC address of the receiver XBee
Serial.write(0x00);// MAC address set to 00 means broadcast .
Serial.write(0x00);
Serial.write(0x00);
Serial.write(0x00);
Serial.write(0x00);
Serial.write(0x00);
Serial.write(0x00);//End of the MAC address of the receiver XBee
Serial.write(0xFF);//Destination Network ID (Broadcast)
Serial.write(0xFE);//Second byte of the Destination Network ID
Serial.write(0x02);//For Payload to be applied (accept settings)
Serial.write(0x44);//Represents Digital Pin number (first byte)
Serial.write(0x34); //Represents Digital Pin number (second byte)
Serial.write(0x05); // Represents Value to be assigned for the pin
Serial.write(0xFA); // Checksum Value
}

void xBeeSignalLow()
{
Serial.write(0x7E);//Start byte, represents the start of data frame
Serial.write(0x00);//Length of the frame (MSB)
Serial.write(0x10);//Length of the frame (LSB)
Serial.write(0x17);//Frame ID represents AT command
Serial.write(0x00);//No reply for AT command
Serial.write(0x00);//Start of the MAC address of the receiver XBee
Serial.write(0x00); // MAC address set to 00 means broadcast .
Serial.write(0x00);
Serial.write(0x00);
Serial.write(0x00);

10
Serial.write(0x00);
Serial.write(0x00);
Serial.write(0x00);//End of the MAC address of the receiver XBee
Serial.write(0xFF);//Destination Network ID (Broadcast)
Serial.write(0xFE);//Second byte of the Destination Network ID
Serial.write(0x02);//For Payload to be applied (accept settings)
Serial.write(0x44);//Represents Digital Pin number (first byte)
Serial.write(0x34);//Represents Digital Pin number (second byte)
Serial.write(0x04);//Represents Value to be assigned for the pin
Serial.write(0xFB); //Checksum Value
}

6. Appendix 2
A wireless communication model for XBee in VHDL for an FPGA implementation

library IEEE;
use IEEE.STD_LOGIC_1164.ALL; use
IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;

ENTITY PWM_gen is
Port ( Clk : in STD_LOGIC; input1:
in std_logic; input2: in
std_logic; input3: in std_logic; xbeereset:
in std_logic;
led : out std_logic_vector(6 downto 0)); end PWM_gen;

ARCHITECTURE Behav of PWM_gen is


signal led_pattern : std_logic_vector(6 downto 0):= "1000000";

led_display: process(CLK) begin


if ( CLK'event and CLK='1' )then
if (input1 <='1' and input2 <= '0' and input3 <= '0') then
led_pattern <= "1000000"; led_pattern_en <= '1';
elsif (input1 <='0' and input2 <= '1' and input3 <= '0' ) then
led_pattern <= "1100000"; led_pattern_en <= '1';
elsif ( input1 <='1' and input2 <= '1' and input3 <= '0' ) then
led_pattern <= "1110000"; led_pattern_en <= '1';
elsif (input1 <='0' and input2 <= '0' and input3 <= '1' ) then
led_pattern <= "1111000"; led_pattern_en <= '1';
elsif ( input1 <='1' and input2 <= '0' and input3 <= '1' ) then
led_pattern <= "1111100"; led_pattern_en <= '1';
elsif ( input1 <='0' and input2 <= '1' and input3 <= '1' ) then
led_pattern <= "1111110"; led_pattern_en <= '1';

11
elsif ( input1 <='0' and input2 <= '0' and input3 <= '0' ) then
led_pattern <= "0000000"; led_pattern_en <='1'; end if;
led <= led_pattern; end if;
end process led_display;

7. Appendix 3

UCF file for the XBee wireless communication VHDL model


## Clock

NET"CLK" LOC ="C9"| IOSTANDARD = LVCMOS33;#in

# These four connections are shared with the J4 6-pin accessory header NET "input1" LOC = "D7" |
IOSTANDARD = LVCMOS33 | SLEW = FAST |DRIVE= 8

| CLOCK_DEDICATED_ROUTE = FALSE; #first pin

NET "input2" LOC = "C7" | IOSTANDARD = LVCMOS33 | SLEW = FAST |DRIVE =

8 ; #second pin

NET "input3" LOC = "E8" | IOSTANDARD = LVCMOS33 | SLEW = FAST |DRIVE =

8 ; #fourth pin # J2 last pin

NET "xbeereset" LOC = "F7" | IOSTANDARD = LVTTL | SLEW = SLOW |DRIVE=6;

12

You might also like