You are on page 1of 14

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. Build and program your own GPS receiver. Challenge 1: Understanding GPS Data Contents 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 Overview volume of data, which can be difficult to see on a small LCD screen. By Challenge 1: Understanding GPS Datamonitor, you will be able to learn about the viewing the data on a computer Collecting Your Components o different GPS data streams and data types. o Connecting the Receiver to Your Computer o Setting up a Terminal Emulator o Understanding GPS Data Challenge 2: Controlling Collecting Your Com ponents the Receiver o Collecting Your Components Connecting thereceiver Receiver the computer, Chip In order to o connect your GPS toto your you will need the following o Displaying Raw GPS Data on the LCD components: o Parsing the Raw GPS Data Selecting GPS Output Streams Part o Quantity Description o Displaying Specific GPS Data Elements A 1 GlobalSat EM-406A GPS Engine o Formatting GPS Data on Two Lines B 1 Connector harness (6-pin) C 1 Bent header (6-pin) D 1 Bent header (4-pin) Overview In this project, you will build an interface to the GlobalSat EM-406A GPS Receiver. The Connecting Receiver GPS to Your Computer EM-406A is the a 20-channel 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 Using Figure 2 as a guide, connect the data GPStypes, receiver to the programming board,altitude, which to display more than 30 standard GPS including latitude, longitude, 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. velocity, heading, time, and da te.

GPS Receiver

Figure 1. GlobalSat EM-406A GPS Receiver.

Figure 2. Connecting the GPS receiver to the computer (schematic).

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

Setting up a Terminal Em ulator Connected as shown in Figure 2, the GPS receiver will send satellite data to your computers 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. Term inal 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. Nam e 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 Units Age of Diff. Corr. Diff. Ref. Station ID 0000 Checksum *18 Check for valid data <CR><LF> Carriage return and line feed indicating end of message Meters Meters Seconds

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 Com ponents 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 (schem atic).

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 2. #include 3. #include

"mxapi.h" "usart.h" "lcd.h"

4. ( void ) 5. int main 6. { char gps_characters; 7. storing GPS characters //Declare a variable for

8. usart_init

( 4800 ) ;

//Initialize the USART to

4800 baud () ; //Initialize your LCD 9. lcd_init while ( 1 == 1 ) //Start an infinite loop 10. { 11. () ; //Read each 12. gps_characters=usart_read character from the GPS ( gps_characters ); //Display 13. lcd_character 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 2. #include 3. #include

"mxapi.h" "usart.h" "lcd.h" ( void )

4.
5. int main

6. { char gps_characters; //Declare a variable to store GPS characters ( 4800 ) ; //Initialize the USART to 8. usart_init 4800 baud () ; //Initialize the LCD 9. lcd_init while ( 1 == 1 ) //Start an infinite loop 10.
7.

{ 11. 12. gps_characters = usart_read

() ;

//Read

each character from the GPS ( gps_characters ); //Display 13. lcd_character each character on the LCD if ( gps_characters == 10 ) //Check for the 14. line feed (ASCII 10) character { 15. ( FIRST_LINE ); //Move 16. lcd_instruction 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. 2. 3. 4.

#include #include #include #include

"mxapi.h" "usart.h" "lcd.h" "gps.h" ( void )

5.
6. int main

7. { char gps_characters; //Declare a variable to store GPS character ( 4800 ) ; //Initialize the USART to 9. usart_init 4800 baud () ; //Initialize the LCD 10. lcd_init ( 2000 ) ; //Delay while GPS receiver 11. delay_ms warms up 12.
8.

/* Turn off all NMEA formats except the one you want to see. */ ( NMEA_GGA, 1 ) ; //Turn on GGA 14. gps_setdata format at 1 Hertz ( NMEA_GSA, 0 ) ; //Turn off GSA 15. gps_setdata format (0 Hertz) ( NMEA_GSV, 0 ) ; //Turn off GSV 16. gps_setdata format (0 Hertz) ( NMEA_RMC, 0 ) ; //Turn off RMC 17. gps_setdata format (0 Hertz) while ( 1 == 1 ) //Run the following code in an 18. infinite loop { 19. () ; //Read 20. gps_characters = usart_read each character from the GPS ( gps_characters ); //Display 21. lcd_character each character on the LCD if ( gps_characters== 10 ) //Check for the 22. line feed (ASCII 10) character { 23. ( FIRST_LINE ) ; //Move the 24. lcd_instruction cursor to the first line } 25. } 26. 27. }
13.

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 Elem ents 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 and minutes North/South N 3 4 Longitude 12158.3416 Degrees and minutes East/West W Altitude 9.0 Meters 9 Speed 0.2 Kilometers / 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. 2. 3. 4. dddmm.mmmm 4 5 ddmm.mmmm 2 3

56 7

#include #include #include #include

"mxapi.h" "usart.h" "lcd.h" "gps.h" ( void ) ( 4800 ) ; //Initialize the USART to

5.
6. int main

7. { 8. usart_init 4800 baud

9. usart_interrupt_rx

( ENABLE) ;

//Enable

interrupts for the USART () ; //Turn on interrupts 10. sei () ; //Initialize the LCD 11. lcd_init 12. while ( 1 == 1 ) //Start an infinite loop 13. { 14. ( FIRST_LINE ) ; //Move the 15. lcd_instruction cursor to the first line ( CLEAR) ; //Clear the LCD 16. lcd_instruction ( gps_data [ 2 ]) ; //Display the 3rd 17. lcd_text 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. 2. 3. 4.

#include #include #include #include

"mxapi.h" "usart.h" "lcd.h" "gps.h" ( void )

5.
6. int main

7. { ( 4800 ) ; //Initialize the 8. usart_init Atmega168s USART to 4800 baud ( ENABLE) ; //Enable 9. usart_interrupt_rx interrupts for the USART () ; //Turn on interrupts 10. sei () ; //Initialize your LCD 11. lcd_init 12. while ( 1 ) //Run the following code in an 13. infinite loop { 14. ( FIRST_LINE ) ; //Move the 15. lcd_instruction cursor to the first line ( gps_data [ 2 ]) ; //Display value 16. lcd_text of third data field ( ':' ) ; //Display ':' 17. lcd_character character

18. lcd_text

( gps_data

[ 3 ]) ;

//Display value of //Move the

fourth data field 19.


20. lcd_instruction

( SECOND_LINE ) ;

curse to the first line ( gps_data [ 4 ]) ; //Display value 21. lcd_text of fifth data field ( ':' ) ; //Display ':' 22. lcd_character character ( gps_data [ 5 ]) ; //Display value of 23. lcd_text sixth data field () ; //Send blanks to 24. lcdBlankLine clear the lines } 25. 26. }