Você está na página 1de 15

GPS Receiver

Build and program your own GPS receiver.

Contents

• Overview
• Challenge 1: Understanding GPS Data
o Collecting Your Components
o Connecting the Receiver to Your Computer
o Setting up a Terminal Emulator
o Understanding GPS Data
• Challenge 2: Controlling the Receiver
o Collecting Your Components
o Connecting the Receiver to the Chip
o Displaying Raw GPS Data on the LCD
o Parsing the Raw GPS Data
o Selecting GPS Output Streams
o Displaying Specific GPS Data Elements
o Formatting GPS Data on Two Lines

Overview

In this project, you will build an interface to the GlobalSat EM-406A GPS Receiver. The
EM-406A is a 20-channel GPS receiver based on the SiRF StarIII chipset, and it features
a built-in antenna. After building the interface circuit, you will learn how to customize it
to display more than 30 standard GPS data types, including latitude, longitude, altitude,
velocity, heading, time, and date.

Figure 1. GlobalSat EM-406A GPS Receiver.


Like most GPS receivers, the EM-406A conforms to the standard NEMA SiRF interface
protocol, so the code you write in this project will work with most other GPS receivers.

Challenge 1: Understanding GPS Data

In order to better understand the GPS data format, you will first connect your GPS
receiver directly to your computer through the Machine Science
programming cable. GPS receivers are capable of transmitting a large
volume of data, which can be difficult to see on a small LCD screen. By
viewing the data on a computer monitor, you will be able to learn about the
different GPS data streams and data types.

Collecting Your Components

In order to connect your GPS receiver to your computer, you will need the following
components:

Part Quantity Description


A 1 GlobalSat EM-406A GPS Engine
B 1 Connector harness (6-pin)
C 1 Bent header (6-pin)
D 1 Bent header (4-pin)

Connecting the Receiver to Your Computer

Using Figure 2 as a guide, connect the GPS receiver to the programming board, which
will link it to your computer. To avoid intefering with the microcontroller, build this
circuit in an area of the board away from the chip. Figure 3 shows one way to lay out this
circuit. To orient the GPS unit properly, note the position of the grey wire in Figure 3.
Figure 2. Connecting the GPS receiver to the computer (schematic).

Figure 3. Connecting the GPS receiver to the computer (photo).

Setting up a Terminal Emulator


Connected as shown in Figure 2, the GPS receiver will send satellite data to your
computer’s COM port. To view the data, you will need to run a terminal emulator--a
program that emulates an older-style text-only computer terminal. Depending on your
operating system, your computer may already have a terminal emulator, or you may need
to download one from the Internet:

• Windows (Vista): TeraTerm Pro Web is available here:


http://www.ayera.com/teraterm/
• Windows (pre-Vista): HyperTerminal is available in the Start menu under
Programs > Accessories > Communication.
• Macintosh: ZTerm comes pre-installed with OSX and above.
• Linux: CuteCom can be found at: http://cutecom.sourceforge.net/

The instructions in this section are for installing TeraTerm Pro Web, but should give you
enough information to help you set up any of the other programs listed above.

1. Determine which COM port your Machine Science programming board is using
by viewing the Options menu in the Programming Window.
2. Download TeraTerm Pro Web from http://www.ayera.com/teraterm/
3. Unzip the .zip file to a convenient location on your hard drive.
4. Open the folder and double click on the file ttermpro.exe. You will see a dialog
box like the one shown in Figure 4.

Figure 4. New connection dialog box.

5. Select Serial and the COM port being used by your Machine Science
programming board. You should now see unintelligible characters scrolling across
and down your screen as shown in Figure 5.
Figure 5. TeraTerm Window.

6. The reason you see a jumble of characters is that you need to set the baud rate in
baud rate in your terminal program to match the baud rate of the GPS receiver.
Select “Serial port...” from the Setup menu. You should now see a dialog box like
the one shown in Figure 6.

Figure 6. Serial port setup.

7. Select 4800 for the baud rate. You should now see lines of GPS data being
updated every second on your screen, as shown in Figure 7.
Figure 7. Terminal emulator.

Understanding GPS Data


GPS receivers are capable of transmitting eight types of GPS data streams, shown below:

Option Description
GGA Time and position data
GLL Latitude, longitude, time and status
GSA GPS receiver operating mode, satellites used in the position solution,
and DOP values
GSV The number of GPS satellites in view, satellite ID numbers, elevation,
and azimuth
MSS Signal-to-noise ratio, signal strength, frequency, and bit rate from a
radio-beacon, receiver
RMC Time, date, position, course and speed data
VTG Course and speed information relative to the ground
ZDA Pulse Per Second (PPS) timing message

Referring to Figure 7 (or your terminal emulator, if it is still running on your computer),
you should see four types of GPS data streams: GGA, RMC, GSV, and GSA. These are
the four default data streams that are transmitted by the GPS receiver. Each stream is
transmitted on a single line. An example of a GGA data stream is shown below.

$GPGGA,161229.487,3723.2475,N,12158.3416,W,1,07,1.0,9.0,M, , , ,0000*18
Each GPS data stream can have up to sixteen GPS data fields. The data fields are
separated by commas, as shown in the sample above. Table 3 below details the data fields
contained in the GGA data stream.

Name Example Description


Message ID $GPGGA GGA protocol header
UTC Time 161229.487 hhmmss.sss (h=hours,
m=minutes, s=seconds)
Latitude 3723.2474 ddm.mmmm (d=degrees,
m=minutes)
N/S Indicator N N=north or S=south
Longitude 12158.3416 dddmm.mmmm (d=degrees,
m=minutes)
E/W Indicator W E=east or W=west
Position Fix Indicator 1 0=fix not valid, 1,2, or 3= fix
valid
Satellites Used 07 Range 0 to 12
HDOP 1.0 Horizontal dilution of
precision
MSL Altitude 9.0 Meters above sea level
Units M Meters
Geoid Separation Meters
Units Meters
Age of Diff. Corr. Seconds
Diff. Ref. Station ID 0000
Checksum *18 Check for valid data
<CR><LF> Carriage return and line feed
indicating end of message

Don't worry about understanding all of the GPS data types shown in this table. The ones
of greatest interest to you will probably be time, latitude, longitude, and altitude. In the
next challenge, you will learn how to display this information in a meaningful way on
your LCD.

Challenge 2: Controlling the Receiver

In this challenge, you will construct a circuit connecting the EM-406A to


the Atmega Board and program the chip to display readable GPS data on
the LCD.
Collecting Your Components
In order to build the circuit, you will need the following components:

Part Quantity Description


A 1 GlobalSat EM-406A
B 1 Connector harness (6-pin)
C 1 Bent header (6-pin)
D 1 Atmega Board

Connecting the Receiver to the Chip


Using the schematic in Figure 8 as a guide, add your components to the breadboard.
Figure 9 shows an example of a completed board for this circuit. Since the leads on the
GPS receiver are not labeled, you may need to examine the picture to make the correct
connections.

Figure 8. Connecting GPS receiver to the Atmega168 (schematic).


Figure 9. Connecting GPS receiver to the Atmega168 (photo).

Displaying Raw GPS Data on the LCD


The following code will allow you to begin receiving data from your GPS receiver and
displaying the information on your LCD. In this first step, you will not be parsing the
data from your receiver. As you learned in the previous challenge, GPS receivers can
produce a lot of data, so your tiny LCD will quickly fill up with a lot of numbers, letters,
and symbols. While you will not be able to read the data, it will indicate that your circuit
is working properly. (It also looks kind of cool!)

IMPORTANT NOTE: The GPS receiver and the Machine Science programming board
both use the same pin for transmitting data to the Atmega168. You must disconnect the
wire linking the GPS pin 4 to the Port D0 of the Atmega168 before downloading your
code. You can immediately reconnect this wire once the code has been downloaded.

1. #include "mxapi.h"
2. #include "usart.h"
3. #include "lcd.h"
4.
5. int main (void)
6. {
7. char gps_characters; //Declare a variable for
storing GPS characters
8. usart_init(4800); //Initialize the USART to
4800 baud
9. lcd_init(); //Initialize your LCD
10. while(1==1) //Start an infinite loop
11. {
12. gps_characters=usart_read(); //Read each
character from the GPS
13. lcd_character(gps_characters); //Display
each character on the LCD
14. }
15. }

Parsing the Raw GPS Data


In order to better view the data on your LCD, you will need to parse the raw data stream
from the GPS receiver. In Figure 7, you can see that the terminal emulator on your
computer displays each data stream on a separate line. It does this by checking for a
carriage return and line feed, the ASCII characters 13 and 10, at the end of each data
stream (as shown in the last rows of Table 3). When the terminal emulator finds these
two characters, it starts the next data set on a new line, making the data much easier to
read than if it were simply a continuous stream.

The microcontroller has no built-in ability to parse data. As you may have seen in the
previous exercise, the carriage return and line feeds at the end of each data string are
displayed on the LCD as more data. As a result, using the previous code, the GPS data
was displayed on the LCD in long stream without easily discernible breaks.

The next code sample introduces breaks after each line of data, just like the terminal
emulator did. It checks continuously for a line feed character (ASCII 10), and when it
gets one, it repositions LCD's cursor at the start of the first line before displaying the next
character. The data on the display remains somewhat difficult to read, since all of the
different GPS data streams are displayed, but it is easier to pick out patterns in the data.

1. #include "mxapi.h"
2. #include "usart.h"
3. #include "lcd.h"
4.
5. int main (void)
6. {
7. char gps_characters; //Declare a variable to
store GPS characters
8. usart_init(4800); //Initialize the USART to
4800 baud
9. lcd_init(); //Initialize the LCD
10. while(1==1) //Start an infinite loop
11. {
12. gps_characters = usart_read(); //Read
each character from the GPS
13. lcd_character(gps_characters); //Display
each character on the LCD
14. if (gps_characters == 10) //Check for the
line feed (ASCII 10) character
15. {
16. lcd_instruction( FIRST_LINE ); //Move
the cursor to the first line
17. }
18. }
19. }

Programming Challenge
Modify your code so that each line new line scrolls up in the same way that it displays on
your computer monitor. This is difficult!

Selecting GPS Output Streams


The following code turns on and off specific GPS data streams. Remember that the
receiver is capable of transmitting any or all of the eight different GPS streams listed in
Table 2 at a rate of up to 100 Hertz each. In order to display the data on the LCD, it is
best to turn on only one stream and turn off the rest, using a function called gps_setdata.
This function takes two arguments: the first argument selects the GPS stream that you
want to turn on or off; the second argument determines how frequently (in Hertz) the
GPS receiver transmits the stream. A value of 0 turns off the stream entirely, while a
value of 1 to 99 specifies a transmission frequency of 1 to 99 times a second.

1. #include "mxapi.h"
2. #include "usart.h"
3. #include "lcd.h"
4. #include "gps.h"
5.
6. int main (void)
7. {
8. char gps_characters; //Declare a variable to
store GPS character
9. usart_init(4800); //Initialize the USART to
4800 baud
10. lcd_init(); //Initialize the LCD
11. delay_ms(2000); //Delay while GPS receiver
warms up
12.
13. /* Turn off all NMEA formats except the one
you want to see. */
14. gps_setdata(NMEA_GGA, 1); //Turn on GGA
format at 1 Hertz
15. gps_setdata(NMEA_GSA, 0); //Turn off GSA
format (0 Hertz)
16. gps_setdata(NMEA_GSV, 0); //Turn off GSV
format (0 Hertz)
17. gps_setdata(NMEA_RMC, 0); //Turn off RMC
format (0 Hertz)
18. while(1==1) //Run the following code in an
infinite loop
19. {
20. gps_characters = usart_read(); //Read
each character from the GPS
21. lcd_character(gps_characters); //Display
each character on the LCD
22. if (gps_characters==10) //Check for the
line feed (ASCII 10) character
23. {
24. lcd_instruction(FIRST_LINE); //Move the
cursor to the first line
25. }
26. }
27. }

You do not need to select a GPS data stream every time you program the device. Once a
stream is selected, the receiver displays only that stream until the gps_setdata function is
called again.

Programming Challenge
Try changing the GPS stream displayed by your GPS receiver. Try changing how often
the GPS stream is updated.

Displaying Specific GPS Data Elements


With the following code, you can display specific GPS data elements, such as time,
latitude, longitude, and altitude, from the selected data stream. To simplify matters, every
GPS data stream is parsed in the background by a function within the “gps.h” header file.
The parsed data is stored in an array called gps_data[ ], with each value in the array
representing a different GPS data element. You can reference any element in the
gps_data[ ] array in your code.
For example, with the GGA data stream, selected a line of data might look like this:

$GPGGA,161229.487,3723.2475,N,12158.3416,W,1,07,1.0,9.0,M, , , ,0000*18

In this instance, gps_data[0] would equal $GPGGA, gps_data[1] would equal


161229.487, gps_data[2] would equal N, and so on.

The following table shows the gps_data[] values for some of the more useful data
elements in the GGA, RMC, and VTC data streams.

GPS Data Example Units Format GGA RBC VTG


Message ID $GPGGA 0 0 0
Latitude 3723.2475 Degrees ddmm.mmmm 2 3
and
minutes
North/South N 3 4
Longitude 12158.3416 Degrees dddmm.mmmm 4 5
and
minutes
East/West W 5 6
Altitude 9.0 Meters 9
Speed 0.2 Kilometers 7
/ hour
Course 309.62 Degrees 8 1
Time 161229.487 hhmmss.ss 1 1
Date 120598 ddmmyy 9
Satellites 07 0 to 12 7

This program uses the chip's internal interrupts, which allow the microcontroller to
execute multiple tasks at the same time. A few new statements are added to enable and
initialize the interrupts.

1. #include "mxapi.h"
2. #include "usart.h"
3. #include "lcd.h"
4. #include "gps.h"
5.
6. int main (void)
7. {
8. usart_init(4800); //Initialize the USART to
4800 baud
9. usart_interrupt_rx(ENABLE); //Enable
interrupts for the USART
10. sei(); //Turn on interrupts
11. lcd_init(); //Initialize the LCD
12.
13. while(1==1) //Start an infinite loop
14. {
15. lcd_instruction(FIRST_LINE); //Move the
cursor to the first line
16. lcd_instruction(CLEAR); //Clear the LCD
17. lcd_text(gps_data[2]); //Display the 3rd
value in the selected data stream
18. }
19. }

Formatting GPS Data on Two Lines


The last code example in this project will display and format multiple GPS data fields on
both lines of the LCD. The added functions in this example should be familiar to you
from other Machine Science projects.

1. #include "mxapi.h"
2. #include "usart.h"
3. #include "lcd.h"
4. #include "gps.h"
5.
6. int main (void)
7. {
8. usart_init(4800); //Initialize the
Atmega168s USART to 4800 baud
9. usart_interrupt_rx(ENABLE); //Enable
interrupts for the USART
10. sei(); //Turn on interrupts
11. lcd_init(); //Initialize your LCD
12.
13. while(1) //Run the following code in an
infinite loop
14. {
15. lcd_instruction(FIRST_LINE); //Move the
cursor to the first line
16. lcd_text(gps_data[2]); //Display value
of third data field
17. lcd_character(':'); //Display ':'
character
18. lcd_text(gps_data[3]); //Display value of
fourth data field
19.
20. lcd_instruction(SECOND_LINE); //Move the
curse to the first line
21. lcd_text(gps_data[4]); //Display value
of fifth data field
22. lcd_character(':'); //Display ':'
character
23. lcd_text(gps_data[5]); //Display value of
sixth data field
24. lcdBlankLine(); //Send blanks to
clear the lines
25. }
26. }

Você também pode gostar