Sei sulla pagina 1di 63

Departement Technische Wetenschappen

Gegradueerde in elektriciteit
Optie elektronica

Sending a GPS location with a cell phone

CAMPUS
Geel

Maarten Frederix

Academiejaar 2005-2006

De houder van dit diploma is gerechtigd tot het voeren van de titel van Bachelor
Vaasa Polytechnic KHK Geel 1

FOREWORD
First I would like to thank everyone who helped me to complete my practical training
and my paper. As a senior year student the paper is very important to obtain a
certificate of Bachelor in ICT. Although my project mainly involves electronics, it has
very instructive.

I would like to thank Vaasa Polytechnic and KHK Geel for giving me the opportunity to
do my practical training in Finland. I would like to thank Mrs. Lena Dilliën and the
international office in Vaasa. This practical training has been a special experience.

I would like to thank my supervisor Mr. Jukka Matila for the work placement and all the
help I got from him. I also had lots of help from the laboratory engineer Jani Ahvonen.
I would like to thank him for that. Also I would like to thank the other student that
found time to help me in my assignment.

For my paper, I would like to thank my supervisor Mr. Luc Friant for the advice and the
help I had from him.

Finally I would like to thank my parents for the support and the opportunity to study at
KHK Geel and allowing me to go abroad for my training.

Maarten Frederix

June 2006

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 2

INTRODUCTION
My project took place in Technobotnia in Finland. I was there from the 18th of August
until the 14th of November.

During this period, I worked with microcontrollers. My assignment is using an Atmel


ATTINY2313 microcontroller. The microcontroller needs a serial connection with the
GPS-receiver of the Wavecom QUICK Q2501 chipset. The task of the microcontroller is
to filter the data. Only the position of the GPS-receiver must remain. This data is than
send to the GSM of the Wavecom QUICK Q2501 chipset.

Applications like these are already in use. But the Wavecom QUICK Q2501 is the first
chip with a GPS-receiver and a GSM-module combined on one chip.

In this project, I used a SMS to show that the program works. But practical it is
possible to use any service provided by the GSM. This application is useful in navigation
of ships, planes, etc. It can send its current position to a radar or something similar. It
is already in use to guide ships to the requested position.

In this paper, the first chapters are the theoretical background of the application. To
understand the program it is important to understand how a microcontroller works.

The middle chapters are an explanation of the programs that I used. These programs
are Codevision AVR and WinAVR.

The final chapter of my paper is the explanation of the project I made. This is explained
by flowcharts and the code is explained with comments.

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 3

TABLE OF CONTENTS
1 INTRODUCTION TO FINLAND ............................................................... 5
1.1 Finland ................................................................................................. 5

2 WAVECOM WISMO QUICK Q2501 ......................................................... 7


2.1 Geographical location .......................................................................... 8
2.2 GPS ...................................................................................................... 8
2.3 NMEA-0183 ........................................................................................ 10
2.4 Use of the Q2501 GPS-receiver .......................................................... 11

3 GSM ................................................................................................... 12
3.1 GSM network...................................................................................... 12
3.2 SMS.................................................................................................... 13
3.2.1 Use of the Q2501 GSM with SMS.............................................................. 13

4 ATMEL ATTINY2313 ........................................................................... 14


4.1 Features of the ATTINY2313 .............................................................. 15
4.2 Block diagram of the ATTINY2313 ..................................................... 16
4.2.1 System Clock ........................................................................................ 17
4.2.2 External connections .............................................................................. 17
4.3 Programming the ATTINY2313 .......................................................... 21
4.4 Interrupts .......................................................................................... 22
4.5 USART ................................................................................................ 22

5 CONNECTING THE MODULE WITH THE MICROCONTROLLER ............... 26

6 CODEVISION AVR............................................................................... 27
6.1 Advantages and disadvantages .......................................................... 27
6.2 Programming ..................................................................................... 27
6.3 Uploading the code ............................................................................ 29

7 WINAVR ............................................................................................. 31
7.1 Advantages and disadvantages .......................................................... 31
7.2 Programming with WinAVR ................................................................ 31

8 EXPLAINING THE CODE ...................................................................... 33


8.1 Flowchart ........................................................................................... 33
8.2 The code ............................................................................................ 37

CONCLUSION.................................................................................................... 43

9 APPENDIX .......................................................................................... 44
9.1 Appendix A: NMEA-0183 standard ..................................................... 44
9.1.1 $GPRMB................................................................................................44
9.1.2 $GPRMC................................................................................................44
9.1.3 $GPGGA................................................................................................45
9.1.4 $GPVTG ................................................................................................45
9.1.5 $GPRMA................................................................................................46
9.1.6 $GPGSA ................................................................................................46
9.1.7 $GPGSV ................................................................................................47

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 4

9.2 Appendix B: The makefile................................................................... 47


9.3 Appendix C: The program................................................................... 56

SOURCES .......................................................................................................... 62

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 5

1 INTRODUCTION TO FINLAND
For my practical training, I worked in Technobotnia. This is a Research centre of
Vaasan Ammattikorkeakoulu,Vaasan Yliopisto and Svenska Yrkeshögskolan. Vaasa is situated in
the middle of Finland.

1.1 Finland

Map of Finland

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 6

Finland is a Scandinavian country. It has borders with Norway, Sweden and Russia. A third of the
country is situated north of the Arctic Circle. It extends over a length of 1200 km from the north to the
south and about 550 from the west to the east. Different kind of geography can be found over the
entire country. In the North of Finland is a part of Lapland. A famous city in Lapland is Rovaniemi.
This is the city of Santa Claus. In Finland there are about 55000 lakes. Most of them are in the East
of Finland. All the lakes together cover about 10% of the country. A Big city in the east is Juväskylä.
This is also a big city for students. The north of Finland is known for its archipelagos. It exists of
more than thousands of islands. Most of the population in situated in these areas. Big cities are
Tampere, Turku, Oulu en Helsinki. Helsinki is the capital of Finland and has a population of about
500000 inhabitants. This is much compared to all the rest of Finland. The entire population is
5000000 people. The mother tongue in Finland is Finnish and Swedish. About 6% of the people
speak Swedish. Small minorities in the north speak Sami. This is the language of the Sami
civilisation which is still present in Lapland.

Vaasa is a city in the north of Finland. It is about 600km from Helsinki. It was the capital of Finland in
1917. Now it is a city for students. Over 10 000 people come to study in one of the schools every
year. In Vaasa about 25% of the people speak Swedish on a population of 57 000 people.

The school I worked for is Vaasan Ammattikorkeakoulo. Vaasa Polytechnic is a multi-disciplinary,


multilingual and international institution providing higher education and research services within
Technology and Communication, Business Economics and Tourism as well as Health Care and
Social Services. Vaasa Polytechnic has approximately 3500 students enrolled and a staff of over
240 members.

Technobotnia Vaasa

The laboratory of the Faculty of Technology and Communication is in Technobotnia. It is


a former Cotton factory which is renovated in 1996. It was awarded the price of
Building of the Year in 1996 by the State Real Estate Department. In Technobotnia
there are three departments working. These departments are an English, a Swedish
and a Finnish department. I worked for the Finnish department. My supervisor was
Jukka Matila. The laboratory Engineer is Jani Ahvonen.

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 7

2 WAVECOM WISMO QUICK Q2501

The Wavecom WISMO Q2501 is a GPS-receiver and a GSM-controller. I used this


chipset because it is combined on one print. Both parts have there own serial
connection to send and receive data. The chip was delivered with the glyn EVBQUICK
testing board. This board is shown in the picture below.

Glyn EVBQUIK-GPS testing board

1 = Serial connector for the GSM (RS232)

2 = Serial connector for the GPS (RS232)

3 = SIM-card socket

4 = Status-LED’s

GPS: LED blinks if a valid GPS signal is received

GSM: LED blinks if a SIM-card is connected to a network

Power: LED is on if power is on. If the LED is off, no position is

available

5 = Connector for the antenna for the GSM

6 = Connector for the antenna for the GPS

7 = Connectors for every pin on the Q2501-chip

8 = Power-connector (the board requires 9-12V DC, 1A)

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 8

2.1 Geographical location

Every position on earth can be pointed in the geographic coordinate system. This
system exists of a longitude and latitude.

The longitude describes the place of a location based on the Prime Meridian. The
meridian is in Greenwich (UK).It is represented by the vertical lines on the map. The
west side of the Prime Meridian is represented by a – sign in front of the position.

The latitude is the place of a location based on the distance from the equator. It is
represented by the horizontal lines on the map. The North Pole and the South Pole are
90 degree from the equator. The equator is the 0 degree line.

Map of Earth with longitude and latitude lines. The lines are actually circles around the
globe.

2.2 GPS

GPS stands for Global Positioning System. It is a worldwide navigation system that is
used to calculate the position of a receiver inside the geographic system. It is
developed by the US Department of Defence. The original name of the project was
NAVSTAR (Navigation System with Timing And Ranging). The GPS-system is a network
of 24 satellites. These satellites have 6 orbits around earth and are on an altitude of
20000 km. They move with a speed of 14000km/hour. Each position on earth can
receive a signal of at least four different satellites. Three satellites are needed to find
an exact position on earth. To calculate an altitude the fourth satellite is needed too.
The receiver calculates how far it is removed from the satellites and calculates his
position with this information. Here is shown why three satellites are necessary to
calculate one point.

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 9

Only one satellite is available. The distance from the satellite to the receiver is the
same over the entire circle.

In this case two satellites are available. The second satellite will narrow the position
down to two points. These two points are the intersections between the two circles.

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 10

A third satellite is available. The intersection between the three satellites is always on
one point only. This is the location of the receiver. Also other systems work the same
way as GPS. Other similar systems are GLONASS and EGNOS which are developed by
Russia and Europe. By 2008 the EGNOS program should be replaced by GALILEO.

The signal that is send by GPS-satellites is compiled according to the NMEA-0183


standard. The standard is explained below.

2.3 NMEA-0183

NMEA is National Marine Electronics Association. The NMEA standard defines the
protocol used for data communication between the GPS-receiver and an external
interface. All the data is organised in sentences with a different sentenceID. A list of the
different sentenceID that are used by the Q2501 is listed the appendix. In this program
only the $GPRMC is used to filter the data. The $GPRMC is the Recommended minimum
of data that a receiver should send through its serial output. The following data is sent
in this sentenceID.

Fix :
Time of fix (UTC-time)

Status :
The navigation receiver warning which displays
an ‘A’ for a valid position and a ‘V’ for an invalid
position. A position is valid if more than three
satellites are detected.

Latitude, N/S :
Latitude of the current position

Longitude, E/W :
Longitude of the current position

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 11

Speed over ground :


Horizontal speed of the receiver (in knots)

Course over ground :


Direction of the movement (in degrees)

UTC date :
Date of fix (UTC-date)

Magnetic variation :
It is the magnetic variation. If the magnetic
variation is incorrect, this will cause a directional
bias in the GPS.

Magnetic variation E/W

Checksum

In the receiver is a list of all the satellites. This list is updated regularly so the
information stays correct at any moment. Because the receiver knows the position it
can determine the time between the sending of the data by the satellite and the
receiving of the data by the receiver. Because the radio waves have a known speed,
the receiver can than calculate the position based on this time. This position is
translated in the longitude and the latitude of the receiver. By saving this position and
comparing it with the next calculation it is possible to determine the speed. The full
NMEA-0183 standard is included in appendix A.

2.4 Use of the Q2501 GPS-receiver

To communicate between a PC and the GPS-receiver, the serial port of the GSM module
is used and connected to the serial port of the computer. On the computer, the
HyperTerminal can be used to program the module. The default settings of the GPS are
9600bps, 8 data bits, no parity and hardware flow control. A list of the commands for
the GPS receiver is in the appendix.

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 12

3 GSM
GSM is the Global System for Mobile communications. It is developed in Europe in the early 1980s.
Each country developed its own standard. Because these systems were incompatible, the
Conference of European Posts and Telegraphs formed the study group Groupe Spécial Mobile to
develop and study a mobile system for Europe. The criteria for the system were:
- good subjective speech quality
- low terminal and service cost
- support for international roaming
- ability to support handheld terminals
- spectral efficiency
- ISDN compatible

3.1 GSM network


The GSM network consists of:
- Base Station Subsystem
- Network and Switching subsystem
- Operation and Support subsystem

The GSM network is build with a cellular structure. Each cell of the network has its own Base Station
Transceiver. The Base Station Controller provides the connection between several Transceivers. In
smaller areas, Multiple Transceivers can be connected to one Controller.

Cellular structure of the GSM network

The Network and Switching subsystem is used to manage the communications between cell phones
and the other telephone networks.

The Operation and Support subsystem oversees the proper operation and setup of the network.

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 13

3.2 SMS

SMS is the abbreviation of Short Message Service. It can send short alphanumeric
messages between mobile phones, PDA, etc

3.2.1 Use of the Q2501 GSM with SMS

The GSM chip works on a default value of 115200bps. For my project, I had to use
4800bps because this is the speed of the GPS and the microcontroller. To set the speed
in the GSM use the following commands

AT+IPR = 4800 (changes the current baud rate to 4800 bps)


AT&W (saves the data to the EEPROM)
AT+CFUN = 1 (restart the chip software)

If the speed of the GSM is the same as the speed of the microcontroller, it is possible to
use the GSM. The SMS service can be used with following commands.

AT+CMGS="(phone number)" <ENTER> (with the quotes)


(message) <CTRL><Z> (<CTRL><Z> can be send by a ASCII character 26dec)

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 14

4 ATMEL ATTINY2313
To connect the GPS and the GSM I use a microcontroller. For this project this
microcontroller is an ATMEL ATTINY2313. The ATTINY2313 works on a Tietomyrsky Oy
EXB2313 programming and testing board.

Tietomyrsky EXB2313 testing board

1 = Microcontroller ATTINY2313

2 = serial connector. The connection with the microcontroller is made


on PORTD.0 and PORTD.1

3 = The LED’s are used to see that data is send. These are connected to
PORTB

4 = Connector for a parallel programming cable.

5 = Power connector (12V DC)

6 = Connectors for every pin on the microcontroller.

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 15

4.1 Features of the ATTINY2313

The microcontroller uses AVR architecture.

32 x 8 General Purpose Working Registers

2KBytes of In-System Self Programmable Flash

128 Bytes of In-System Programmable EEPROM

128 Bytes of Internal SRAM

One 8-bit Timer/Counter with Prescaler and Compare Mode

One 16-bit Time/Counter with Prescaler, Compare and Capture Mode

Full Duplex USART

2,7V - 5,5V for Operating Voltage

Up to 20MHz clock frequency

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 16

4.2 Block diagram of the ATTINY2313

Block Diagram of the ATTINY2313

A microcontroller is a small computer build in a chip. It is very useful for on-board


programming and to save space on a print. It exists of a CPU, RAM memory, ROM
memory and I/O.
The CPU consists of the Arithmetic Logical Unit, registers and a control unit. In this
microcontroller, the CPU uses AVR architecture.
The ALU is directly connected with the 32 General Purpose Registers. These Registers
are uses for data and control information. For fast access, it is directly connected to the
ALU with 2 data buses.
In the RAM memory it is possible to store data.

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 17

The ROM memory stores the system program. The difference with RAM memory is that
it can only be read and not be written. The I/O is the bus to transfer all the data
between the different parts.

The ATMEL AVR is part of the ATMEL RISC family. RISC is an instruction set for the
computers, microcontrollers, etc. It is the abbreviation of RISC is Reduced Instruction
Set Computer. It is the successor of the CISC instruction set. CISC is Complex
Instruction Set Computer. The advantages of RISC are easy to program, pipelining, all
the instruction use the same numbers of bits, only few instructions and modes and
more than 1 register for each functions.
Pipelining is a technique where every process is divided in multiple smaller sub
processes. Each sub process can be send to a unit that is designed for the instruction.
The advantage of this technique is that multiple units can work at the same time.
Because it is an AVR microcontroller, it uses Harvard architecture to connect the
several components to each other. This means that one 8-bit data bus is uses for the
communication between the different parts of the microcontroller. This architecture is
faster that the Von Neumann architecture. In the Von Neumann architecture, all the
data and the program are put in the same memory. A disadvantage of this architecture
is that all the instruction and data can only be read in multiple cycles.

4.2.1 System Clock

To make the microcontroller work, a certain frequency is given to all of the


components. The maximum frequency of this microcontroller is 20MHz. In the
microcontroller is a clock present. This clock can be set for 4MHz or 8MHz. It uses an
RC Oscillator. An external oscillator is also possible. A Crystal Oscillator can be
connected to the XTAL1 and XTAL2 pins of the microcontroller. This is pin 4 and pin 5 of
the microcontroller.

Crystal Oscillator

4.2.2 External connections

The microcontroller can be connected to other devices with its ports. In this
microcontroller there are three ports available. The ports are named PORTA, PORTB
and PORTD. Each port exists of a number of pins which can be used simultaneously.
The pin map of this microcontroller is given below. It is a 20 pin s PDIP package.

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 18

Pin map of the ATTINY2313

Pins:

1 PA2 20 VCC

2 PD0 19 PB7

3 PD1 18 PB6

4 PA1 17 PB5

5 PA0 16 PB4

6 PD2 15 PB3

7 PD3 14 PB2

8 PD4 13 PB1

9 PD5 12 PB0

10 GND 11 PD6

All the ports in the microcontroller are bidirectional input/output ports. This means that
they can be set as input or output. The settings can be adjusted in the register DDxn,
PORTxn and PINxn. PINxn is the current value of the pin. The DDxn register sets the
pin as an output or input. If the corresponding pin is input, the value of DDxn is 0. The
PORTxn register can set a pull-up resistor to choose a default value. The pull-up
resistor creates a default value for each pin. When the default value of an input or
output supposed to be high, then the pull-up resistor has to be set. The pull-up resistor
is a transistor. When it is enabled, it will pass the 5V from the source to the min. This
setting for the pull-up resistor happens in the PORT register. The value of PORTx should
be 0x00 to enable the pull-up resistor of every port in that array. This is because the
resistor is enabled if no value is given. To disable it, PORTx should have the value 0xFF.

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 19

4.2.2.1 PORT A

PORTA is a 3-bit input/output port

PORTA.2

RESET:
dW: debug Wire On-chip Debug System

PORTA.1

XTAL2: PIN2 of an external clock

PORTA.0

XTAL1: PIN1 of an external clock

4.2.2.2 PORTB

Port B is an 8-bit input/output port.

PORTB.7

USCK: Three-wire mode Serial Interface Clock


SCL: Two-wire Serial Clock for USI communication
PCINT7: Pin Change Interrupt source 7.

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 20

PORTB.6

DO: Three-wire mode Serial Interface Data Output


PCINT6: Pin Change Interrupt source 6

PORTB.5

DI: Three-wire mode Serial Interface Data Input


SDA: Two-wire mode Serial Interface Data
PCINT5: Pin change Interrupt source 5

PORTB.4

OC1B: Output Compare Match B output


PCINT4: Pin change Interrupt source 4

PORTB.3

OC1A: Output Compare Match A output


PCINT3: Pin change Interrupt source 3

PORTB.2

OC0A: Output Compare Match A output


PCINT2: Pin change Interrupt source 2

PORTB.1

AIN1: Analogue Comparator Negative Input


PCINT1: Pin change Interrupt source 1

PORTB.0

AIN0: Analogue Comparator Positive Input


PCINT0: Pin change Interrupt source 0

4.2.2.3 PORTD

Port D is a 7-bit input/output port.

PORTD.6

ICP: Time/Counter1 Input Capture Pin

PORTD.5

OC0B: Output Compare Match B output


T1: Timer/Counter1 External Counter Clock input

PORTD.4

T0: Timer/Counter0 External Counter Clock input

PORTD.3

INT1: External Interrupt source 1

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 21

PORTD.2

INT0: External Interrupt source 0


XCK: USART Transfer Clock used for synchronous Transfer mode
CKOUT: System Clock Out

PORTD.1

TXD: USART Data Transmitter

PORTD.0

RXD: USART Data Receiver

4.3 Programming the ATTINY2313

Connection for programming the ATTINY2313

There is a connection for programming on the EXB2313 testing board. The picture
above shows the connection between the cable and the microcontroller. The cable is
connected with a parallel plug onto the computer and with a RJ45 jack on the testing
board. Pin 2 is used as the RESET for the microcontroller. When a program is loaded in
the microcontroller, the registers have to be set to their initial values. This happens
when a reset occurs. Pin 4, 5 and 6 are the programming pins.

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 22

4.4 Interrupts

The use of an interrupt is to stop the normal procedure of a program and start another
procedure. There are 2 kinds of interrupts. Namely external and internal interrupt. The
internal interrupt are timers/counter and to store data. The external interrupt are
changes on one of the port. In this case, I use an interrupt to stop the program from
running and then read the signal on the serial receive port. The advantage of interrupt
is that the program does not have to listen to the port all the time. In the mean time, it
can run a normal procedure to by example edit the data. A problem that can occur is
that an interrupt gets interrupted. Therefore a General Interrupt Enable bit (GIE) can
block all further interrupt while the interrupt is running. This kind of interrupt is called a
signal.

4.5 USART

A USART is a Universal Synchronous / Asynchronous Receiver / Transmitter. Both


receiver and transmitter require a USART for serial communication. The USART of the
ATTINY2313 is on PORTD.0 for receiving data and PORTD.1 for sending data.

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 23

Schematic of the USART in the ATTINY2313

The USART is build up by three main parts. The first part is the clock generator. It
consists of a Synchronization Logic and a Baud Rate Generator. This pin is only used for
synchronous data transfer. With an asynchronous transfer, an internal clock is used.
This clock has a preset Baud Rate and Frame Size.

The second part is the Transmitter (TxD). It consists of a Write Buffer, a Shift Register,
Parity Generator and a Control Logic. The data byte that has to be sent is loaded in the
Write Buffer. When the Complete byte is in the buffer, it will be shifted to the
connection line. The shifting is done by the Shifting Register. When all the data is send,
the Parity Generator sends the parity bit. The receiver can test the data with the parity
bit.

The third part is the Receiver (RxD). It consists of a recovery unit for the clock and for
the data, a Parity Checker, Control Logic, a Shift Register and a Receiver Buffer (UDR).
When a start bit is received, the USART will send an interrupt. It read the data bit by
bit and sends them to the Receive Buffer. To receive a byte, the data has to be shifted
in the Shifting Register. The data is then checked in the Parity Checker who will
generate its own parity bit and compare it with the received one.

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 24

The settings for the USART are saved in the UCSRA and UCSRB register. This registers
consists of flag bits to communicate properly. They indicate if the Transmit and Receive
buffer are full, errors, interrupts, size of the frame, etc

//*****************************************************************//
// TRANSMIT //
//*****************************************************************//

void USART_Transmit(unsigned char data)


{
while( ! ( UCSRA & ( 1<<UDRE ) ) ); // Wait for empty transmit buffer
UDR = data; //Put data into buffer, sends the data
}
//*****************************************************************//

For transmitting a frame, the UDRE is important. This flag bit indicates if the transmit
buffer is ready to receive new data.

//*****************************************************************//
// RECEIVE //
//*****************************************************************//

SIGNAL ( SIG_USART0_RX )
{
IncomingData = UDR; // Store received byte
PORTB = IncomingData; // This shows the data with LEDS on PORTB
}
//******************************************************************//

USI is also a way for serial communication. The USI is the Universal Serial Interface.
There are two possibilities for communication. It is also called SPI. In two-wire
communication only a transmitter and receiver are available. A clock frequency can be
send along in three-wire communication. The incoming and outgoing data is saved in
the 8-bit Shift Register (USIDR). This is for both the receiver and transmitter the same
register. To set the register for input or output, the D-Latch makes the connection with
the right pin. The data has to be written to a memory place immediately after
receiving. There is no buffer available to store the data.

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 25

Schematic of the USI in the ATTINY2313

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 26

5 CONNECTING THE MODULE WITH THE


MICROCONTROLLER
The GPS is connected to the microcontroller by a serial cable and the GSM too. Because
the USART of the microcontroller has both pins on the serial connection, a special type
of cable is used to connect all three devices to each other. This serial connection is a
Full Duplex Espionage monitor cable. The connections of the cable are shown in
following picture. It shows that the receive and the transmit cable of a normal serial
cable is connected to another connector which is connected to the GSM. The baud rate
for all the connections has to be the same. In this case I used a baud rate of 4800bps.
To test the code in the microcontroller, I used a HyperTerminal connection instead of
the GSM-module. In the HyperTerminal, the data can be seen as plain text.

Full Duplex Espionage Monitor Cable

The connections of a serial port are:

1. Carrier detect
2. Rx ( Receive)
3. Tx (Transmit)
4. Data Terminal Ready
5. Ground
6. Data Set Ready
7. Request To Send
8. Clear To Send
9. Ring Indicator

For the transmission, the GPS sends its data to the microcontroller. This transmission
needs 2 connections. The first connection is from the Tx of the GPS to the Rx of the
microcontroller. The second connects the 2 ground with each other. The
microcontroller handles the data so that the GSM can send an SMS. To do this, there
are also 2 connections needed. The first from the Tx of the microcontroller to the Rx of
the GSM. The second also connects the ground with each other.

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 27

6 CODEVISION AVR

6.1 Advantages and disadvantages

It is an easy to use programming environment. A lot of the code is generated by a


wizard. When the code is finished, it can be automatically compiled by this program

The disadvantages are that the program is not free. For my project, I could only use an
evaluation version. In this version are not all the features enabled. By example the
USART is locked.

6.2 Programming

Codevision AVR is specially designed for the Atmel AVR microcontrollers. The program
has a programmer and compiler on board. An Evaluation version can be downloaded at

http://www.hpinfotech.ro/html/download.htm

Codevision AVR

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 28

Some hardware functions are code generator by a wizard. To make a program, select
create new project. The Wizard is very useful if you are not used to program a lot. The
first thing to do is selecting the chip, which is a ATtiny2313. Then the ports can be set
to input or output. To work with USART, pin PD0 has to be input with a pull-up
transistor and pin PD1 has to be output with 0. These are the pins used for the USART
signal.

Codevision AVR wizard

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 29

6.3 Uploading the code

Codevision – save and generate

Project - Configure

When the code is written, it can be programmed into the microcontroller. To do this,
first the code has to be compiled. The compiler is the make option in the Project menu.
If the code is compiled correctly, the Program button is shown on the bottom of the
window.

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 30

Project - Make

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 31

7 WINAVR

7.1 Advantages and disadvantages

WinAVR is a freeware and open-source program. It allows companies to change code of


the program and help develop this program.

The disadvantages are that it is very hard to learn this program. A makefile needs to be
made to compile code. The program doesn’t compile the code. Another program needs
to be use to compile and upload the code to the microcontroller.

7.2 Programming with WinAVR

Layout of WinAVR

WinAVR is a set of tools for microcontrollers. The tool for programming the
microcontroller is Programmers Notepad 2. This is an open source text editor with extra
features for coding. It supports several languages. For this project I chose for c-
language because all of the experience I had with microcontrollers was in c. A project
exists of a c-file and a makefile. Both are saved in the same directory. The c-file
contains the code for the program. A makefile tells the compiler what commands he
has to run and what files to compile. There is a makefile included in WinAVR. By default
installation it can be found in c:\winavr\sample. For this project, some data has to be

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 32

change to make a working makefile. The first thing to do is choose a microcontroller


and a name for the project. This data is put in the first lines of the makefile:

MCU = attiny2313
TARGET = project_GPS
SRC = $(TARGET).c
F_CPU = 4000000
FORMAT = ihex

To determine the programming protocol in the makefile the following data has to be
adjusted to:

AVRDUDE_PROGRAMMER = dt006
#AVRDUDE_PORT = com1 # programmer connected to serial device
AVRDUDE_PORT = lpt1 # programmer connected to parallel device

To work with the LPT-port, the file c:\winavr\bin\install_giveio.bat has to be installed


manually first. This can be done in the command browser.

An example of the used makefile is in appendix B. It is also possible it make the


makefile by the tool Mfile. This tool is also included in WinAVR.

When a file is compiled, a .hex-file and a .cof-file are created. The .hex-file is the file
that will be uploaded in the FLASH memory of the microcontroller of the
microcontroller.

The official WinAVR website is http://winavr.sourceforge.net/ and the program can be


downloaded at http://sourceforge.net/projects/winavr/.

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 33

8 EXPLAINING THE CODE

8.1 Flowchart

Flowchart of the entire program

The flowchart shows how the program must run to complete his task. The
microcontroller reads the data from the GPS, filters the data and transmits the good
data to the GSM. The actions of the microcontroller are explained in the next
flowcharts. When the microcontroller receives the data, it is filtered immediately for the
right ID sentence. For this data, we use the GPRMC data. Because the first two signals
are always the same, it is possible to filter the data on the three signals that follow the
GP. If the data is correct, the reading can begin. Because the data is separated by
commas, it is only possible to count the commas to determine the data we need. This
data is stored in the memory of the microcontroller. To send the data with the cell
phone, the AT+CMGS= command is put in from together with the phone number of the
receiver. A counter is put on the transmitter, so it won’t send every time it receives the
GPS data. The different stages are explained in the following flowcharts.

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 34

Flowchart of Receive the data

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 35

PORTD has to be set to use the USART in the program. This USART will generate an
interrupt when data is inserted. When there is an interrupt, the data needs to be
checked for the GPRMC sentenceID. If the RMC is matched, the microcontroller can
start reading data.

Flowchart of saving the data

If the data is checked, the microcontroller will read the data from the GPS. The right
data starts on the third place in the sentenceID and is separated by “,”. When the
counter that counts the commas reaches seven, it can send the data.

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 36

Counter to send = 10
No

Yes

Send AT+CMGS=”(phone
number)”

Send message

Counter = 0

Increment Counter

Flowchart of sending the data

Sending data with a microcontroller is also done with the USART. In my project, I use a
counter. Because of this counter the data is not send every time a location is known. To
send a message with a GSM, it first need the data “AT+CMGS=”(phone number)”. Then
the message can be send with at the end char(26) which is CTRL – Z.

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 37

8.2 The code

All the libraries that are needed to create a program are included on the top of the
code. The variables that not change during the program can be created here too. In the
middle of the program, the different functions are created. The program always starts
at the line int main( ). From this class other classes can be called. These are by
example Transmit and Receive functions.

#include <avr/io.h>
#include <stdio.h>
#include <inttypes.h>
#include <avr/signal.h> //include file for signal interrupts
#include <avr/interrupt.h> //include files for interrupts
#define DEF_UART 0 //value for UBRR
#define BAUD 51 //the baud is the speed at which the microcontroller
should check the USART port. This is calculated by

fosc
−1
16( BAUDRATE )

The clock frequency of the oscillator is 4 MHz and the Baud Rate (speed of the USART is
4800. (4000000/(16*4800))-1 = 51,0833

#define COMMA3 3
#define COMMA4 4
#define COMMA5 5
#define COMMA6 6
#define CR 0x0D //CR and LF are use to send ENTER
#define LF 0x0A
#define CTRLZ 0x1A //The hexadecimal value of CTRL-Z is 0x1A
#define SMSCOMMAND 20

unsigned char IncomingData; //global variable


unsigned int check; //this variable is used for a timing function.
If there is an interrupt, the checking of the data
should begin.

//*************************************************//
// TRANSMIT //
//*************************************************//

void USART_Transmit(unsigned char data)


{
while( ! ( UCSRA & ( 1<<UDRE ) ) ); // Wait for empty transmit buffer
UDR = data; //Put data into buffer, sends the data
}

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 38

//*************************************************//
// MAIN PROGRAM //
//*************************************************//

int main ( )
{
//SET USART
UBRRH = DEF_UART; // 4800 bps at 4 MHz
UBRRL = BAUD;
UCSRB = _BV(RXEN) | _BV(TXEN) | _BV(RXCIE); // Rx Complete Interrupt &
Enable Rx/Tx
UCSRC = _BV(UCSZ1) | _BV(UCSZ0); // 8N1

//SET PORTS: PORTB is not needed, but is used to send to the LEDs
//PORTD uses 2 bits. PD0 for RXD and PD1 for TXD

DDRB=0xFF; //B port output


PORTB=0x00; //B port high
DDRD=0x02; //port0 on pull up and port1 on 0

asm("sei"); //enables the interrupts

//*****************************//
//reserve and set variables//
//*****************************//

/*
Read is variable to check the data
n is variable to count the commas that have past the check with reading
m is used to count the number of bytes needed to store he data
Longitude, Latitude, DirNs and DirEW are used to save the data from the GPS
LenLon and LenLat are used to see how long the array for Longitude and Latitude is
the counter is used to send the data at certain times
*/

int Read , Write , n;

/*
maximum 9 characters are possible for writing the data to latitude and longitude
because the last character of an array is a Carriage Return
*/

char Longitude[10] , Latitude[10] , DirNS , DirEW;


int LenLon, LenLat;
int counter;
int command[20];
IncomingData = 0;
check = 0;
Read = 0;
Write = 0;
n = 0;
LenLon = 0;
LenLat = 0;
counter = 0;

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 39

/*set variables of the array command. This array consists of the command to send an
SMS. This is AT+CMGS=“<phone number>”
*/

command[0] = 'A';
command[1] = 'T';
command[2] = '+';
command[3] = 'C';
command[4] = 'M';
command[5] = 'G';
command[6] = 'S';
command[7] = '=';
command[8] = '"';
command[9] = '0';
command[10] = '4';
command[11] = '4';
command[12] = '1';
command[13] = '2';
command[14] = '3';
command[15] = '4';
command[16] = '5';
command[17] = '6';
command[18] = '7';
command[19] = '"';

while(1) //this makes an infinite loop


{
//*************************************//
// CHECKING OF DATA //
//*************************************//

/*
In this program 3 characters are used to test: RMC
This is the signal to start the reading of the right data
*/

if ( check == 1 ) //check is 1 after every interrupt


//and 0 when action is done
{
if ( Read == 0 )
{
if( IncomingData == 'R' ) // check if received char is R
{
Read = 1; //If character is R then check
//the next character
}
check = 0;
}
}

if ( check == 1 )
{
if ( Read == 1 )
{

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 40

if( IncomingData == 'M' ) // check if char is M


{
Read = 2;
}
if( IncomingData != 'M' )
{
Read = 0;
}
check = 0;
}
}

if( check == 1 )
{
if( Read == 2 )
{
if( IncomingData == 'C' ) // check if char is C
{
Read = 3;
LenLon = 0;
LenLat = 0;
n = 0;
}
if( IncomingData != 'C' )
{
Read = 0;
}
check = 0;
}
}

//*************************************//
// READ THE RIGHT DATA //
//*************************************//

/*
All 3 characters are checked and OK
The reading of the right data begins
Again if check = 1 it should do an action
The data is divided in 4 parts which are read separately
*/

if( check == 1 )
{
if( Read == 3 )
{
/*
data is shown in form of ,longitude, lattide, N-S, E-W,...
the fist sign is a comma
*/

if( IncomingData == ',' ) //if the char is a comma


//then don't read the char, but increment n
{

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 41

n = n + 1;
}

if( IncomingData != ',' )


{
if( n == COMMA3 )
{
Latitude[LenLat] = IncomingData;
LenLat = LenLat + 1;
}

if( n == COMMA4 )
{
DirNS = IncomingData;
}

if( n == COMMA5 )
{
Longitude[LenLon] = IncomingData;
LenLon = LenLon + 1;
}

if( n == COMMA6 )
{
DirEW = IncomingData;
Read = 0;
Write = 1;
counter = counter + 1;
}

check = 0;
}

//*************************************//
// SENDING THE DATA //
//*************************************//

/*
The data is send in this part
Every certain time it has to send a message to another phone
The message exists of AT+CMGS="TELEPHONE NUMBER" <CR>
The message is send after this with CTRL-Z at the end.
*/

if( Write == 1 )
{
if( counter == 10 )
{

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 42

/*
To initiate the SMS service. Using a array gives an error on the last char of that array.
The service command is AT+CMGS= " (phone number) " <ENTER> message
<CTRL><Z>. AT+CMGS is written in the array command[]. On the GSM, the message
will be like this: <Latitude N or S , Longitude E or W> and the message will be send
after CTRL-Z
*/

for( int t = 0 ; t == SMSCOMMAND-1 ; t++ )


{
USART_Transmit( command[t] );
}

USART_Transmit( CR ); //CR
USART_Transmit( LF ); //LF
for( int v = 0 ; v <= LenLat-1 ; v++ )
{
USART_Transmit( Latitude[v] );
}

USART_Transmit( DirNS );
USART_Transmit( ',' );

for( int u = 0 ; u <= LenLon-1 ; u++ )


{
USART_Transmit( Longitude[u] );
}

USART_Transmit( DirEW );
USART_Transmit( CTRLZ );
counter = 0;
}

} //END WHILE LOOP

return 1;

} //END MAIN

//*************************************************//
// RECEIVE //
//*************************************************//

SIGNAL ( SIG_USART0_RX )
{
IncomingData = UDR; //Store received byte
check = 1;
PORTB = IncomingData; //Show byte on PORTB. This is only to check if
//data is received and transmitted
}

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 43

CONCLUSION
During my practical training, I learned to work with GPS, Atmel microcontrollers and
GSM. Not only the hardware is important but also software is important to make
application. Codevision AVR is easy to learn and can be a step to the more powerful
WinAVR.

For the GPS it was important to connect it and have insight in the data that is
transmitted over the serial line. This data is build by the NMEA-0183 standard. This
standard is the same for every receiver and therefore easy to use.

The microcontroller is an Atmel ATTINY2313. It is one of the smallest and cheapest


microcontrollers of the Atmel series. The AVR technology that Atmel uses causes the
microcontroller to work with c-language instructions. Compare to assemble it allows the
user to write better programs that are also better comprehendible for people who don’t
work with microcontrollers in everyday life. An introduction in the world of
microcontrollers is still an advantage. I had an introduction from my school KHK Geel.

In the beginning, my biggest concern was to use the GSM-module. After reading the
datasheet and testing some examples, the use was not so hard. It is also connected
with a serial cable and uses simple commands. Special combinations of letters can be
found in ASCII tables. By example <CTRL> + Z is char 26.

The choice of the program is very important for an application. WinAVR is a powerful
program that can be used as a notepad. Is contains an identifier for several
programming languages like Java, C, etc.

These years, GPS is used in many applications. In the future, the use of GPS will rise
rapidly. But GPS is still a military project. The European Galileo project is being made to
break the American monopoly in this field. This commercial product has much support
from other countries and will probably compete with GPS in the following years.
Advantages as higher accuracy and service integrity are used to convince companies
and people.

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 44

9 APPENDIX

9.1 Appendix A: NMEA-0183 standard

9.1.1 $GPRMB

RMB = Recommended Minimum Navigation Information

1 = Data Status (V=navigation receiver warning)

2 = Crosstrack error in nautical miles

3 = Direction to steer (L or R) to correct error

4 = Origin waypoint ID#

5 = Destination waypoint ID#

6 = Destination waypoint latitude

7 = N or S

8 = Destination waypoint longitude

9 = E or W

10 = Range to destination in nautical miles

11 = Bearing to destination, degrees True

12 = Destination closing velocity in knots

13 = Arrival status; (A=entered or perpendicular passed)

14 = Checksum

9.1.2 $GPRMC

RMC = Recommended Minimum Specific GPS/TRANSIT Data

1 = UTC of position fix

2 = Data status (V=navigation receiver warning)

3 = Latitude of fix

4 = N or S

5 = Longitude of fix

6 = E or W

7 = Speed over ground in knots

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 45

8 = Track made good in degrees True

9 = UT date

10 = Magnetic variation degrees (Easterly var. subtracts from true course)

11 = E or W

12 = Checksum

9.1.3 $GPGGA

GGA = Global Positioning System Fix Data

1 = UTC of Position

2 = Latitude

3 = N or S

4 = Longitude

5 = E or W

6 = GPS quality indicator (0=invalid; 1=GPS fix; 2=Diff. GPS fix)

7 = Number of satellites in use [not those in view]

8 = Horizontal dilution of position

9 = Antenna altitude above/below mean sea level (geoids)

10 = Meters (Antenna height unit)

11 = Geoidal separation (Diff. between WGS-84 earth ellipsoid and

mean sea level. -=geoid is below WGS-84 ellipsoid)

12 = Meters (Units of geoidal separation)

13 = Age in seconds since last update from diff. reference station

14 = Diff. reference station ID#

15 = Checksum

9.1.4 $GPVTG

VTG = Actual track made good and speed over ground

1 = Track made good

2 = Fixed text 'T' indicates that track made good is relative to true north

3 = not used

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 46

4 = not used

5 = Speed over ground in knots

6 = Fixed text 'N' indicates that speed over ground in knots

7 = Speed over ground in kilometres/hour

8 = Fixed text 'K' indicates that speed over ground is in kilometres/hour

9 = Checksum

9.1.5 $GPRMA

RMA = Navigation data from present position

1 = Data status

2 = Latitude

3 = N/S

4 = longitude

5 = W/E

6 = not used

7 = not used

8 = Speed over ground in knots

9 = Course over ground

10 = Variation

11 = Direction of variation E/W

12 = Checksum

9.1.6 $GPGSA

GSA = GPS receiver operating mode, SVs used for navigation, and DOP values.

1 = Mode:

M=Manual, forced to operate in 2D or 3D

A=Automatic, 3D/2D

2 = Mode:

1=Fix not available

2=2D

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 47

3=3D

3-14 = IDs of SVs used in position fix (null for unused fields)

15 = PDOP

16 = HDOP

17 = VDOP

9.1.7 $GPGSV

GSV = Number of SVs in view, PRN numbers, elevation, azimuth & SNR values.

1 = Total number of messages of this type in this cycle

2 = Message number

3 = Total number of SVs in view

4 = SV PRN number

5 = Elevation in degrees, 90 maximum

6 = Azimuth, degrees from true north, 000 to 359

7 = SNR, 00-99 dB (null when not tracking)

8-11 = Information about second SV, same as field 4-7

12-15= Information about third SV, same as field 4-7

16-19= Information about fourth SV, same as field 4-7

9.2 Appendix B: The makefile

#----------------------------------------------------------------------------
# WinAVR Makefile Template written by Eric B. Weddington, Jörg Wunsch, et al.
#
# Released to the Public Domain
#
#----------------------------------------------------------------------------
# On command line:
#
# make all = Make software.
#
# make clean = Clean out built project files.
#
# make coff = Convert ELF to AVR COFF.
#
# make extcoff = Convert ELF to AVR Extended COFF.
#
# make program = Download the hex file to the device, using avrdude.
# Please customize the avrdude settings below first!
#

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 48

# make debug = Start either simulavr or avarice as specified for debugging,


# with avr-gdb or avr-insight as the front end for debugging.
#
# make filename.s = Just compile filename.c into the assembler code only.
#
# make filename.i = Create a preprocessed source file for use in submitting
# bug reports to the GCC project.
#
# To rebuild project do "make clean" then "make all".
#----------------------------------------------------------------------------

MCU = attiny2313
TARGET = gps_data
SRC = $(TARGET).c

# Processor frequency.
# This will define a symbol, F_CPU, in all source code files equal to the
# processor frequency. You can then use this symbol in your source code to
# calculate timings. Do NOT tack on a 'UL' at the end, this will be done
# automatically to create a 32-bit value in your source code.
F_CPU = 4000000

# Output format. (can be srec, ihex, binary)


FORMAT = ihex

# Target file name (without extension).


# List C source files here. (C dependencies are automatically generated.)
# List Assembler source files here.

# Make them always end in a capital .S. Files ending in a lowercase .s


# will not be considered source files but generated files (assembler
# output from the compiler), and will be deleted upon "make clean"!
# Even though the DOS/Win* filesystem matches both .s and .S the same,
# it will preserve the spelling of the filenames, and gcc itself does
# care about how the name is spelled on its command-line.

ASRC =

# Optimization level, can be [0, 1, 2, 3, s].


# 0 = turn off optimization. s = optimize for size.
# (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
OPT = s

# Debugging format.
# Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.
# AVR Studio 4.10 requires dwarf-2.
# AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.
DEBUG = dwarf-2

# List any extra directories to look for include files here.


# Each directory must be seperated by a space.
# Use forward slashes for directory separators.
# For a directory that has spaces, enclose it in quotes.
EXTRAINCDIRS =

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 49

# Compiler flag to set the C Standard level.


# c89 = "ANSI" C
# gnu89 = c89 plus GCC extensions
# c99 = ISO C99 standard (not yet fully implemented)
# gnu99 = c99 plus GCC extensions
CSTANDARD = -std=gnu99

# Place -D or -U options here


CDEFS = -DF_CPU=$(F_CPU)UL

# Place -I options here


CINCS =

#---------------- Compiler Options ----------------


# -g*: generate debugging information
# -O*: optimization level
# -f...: tuning, see GCC manual and avr-libc documentation
# -Wall...: warning level
# -Wa,...: tell GCC to pass this to the assembler.
# -adhlns...: create assembler listing
CFLAGS = -g$(DEBUG)
CFLAGS += $(CDEFS) $(CINCS)
CFLAGS += -O$(OPT)
CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
CFLAGS += -Wall -Wstrict-prototypes
CFLAGS += -Wa,-adhlns=$(<:.c=.lst)
CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
CFLAGS += $(CSTANDARD)

#---------------- Assembler Options ----------------


# -Wa,...: tell GCC to pass this to the assembler.
# -ahlms: create listing
# -gstabs: have the assembler create line number information; note that
# for use in COFF files, additional information about filenames
# and function names needs to be present in the assembler source
# files -- see avr-libc docs [FIXME: not yet described there]

ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs

#---------------- Library Options ----------------


# Minimalistic printf version
PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min

# Floating point printf version (requires MATH_LIB = -lm below)


PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt

# If this is left blank, then it will use the Standard printf version.
PRINTF_LIB =

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 50

#PRINTF_LIB = $(PRINTF_LIB_MIN)
#PRINTF_LIB = $(PRINTF_LIB_FLOAT)

# Minimalistic scanf version


SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min

# Floating point + %[ scanf version (requires MATH_LIB = -lm below)


SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt

# If this is left blank, then it will use the Standard scanf version.
SCANF_LIB =
#SCANF_LIB = $(SCANF_LIB_MIN)
#SCANF_LIB = $(SCANF_LIB_FLOAT)

MATH_LIB = -lm

#---------------- External Memory Options ----------------


# 64 KB of external RAM, starting after internal RAM (ATmega128!),
# used for variables (.data/.bss) and heap (malloc()).
#EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
# 64 KB of external RAM, starting after internal RAM (ATmega128!),
# only used for heap (malloc()).
#EXTMEMOPTS = -Wl,--defsym=__heap_start=0x801100,--
#defsym=__heap_end=0x80ffff
EXTMEMOPTS =

#---------------- Linker Options ----------------


# -Wl,...: tell GCC to pass this to linker.
# -Map: create map file
# --cref: add cross reference to map file
LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
LDFLAGS += $(EXTMEMOPTS)
LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)

#---------------- Programming Options (avrdude) ----------------


# Programming hardware: alf avr910 avrisp bascom bsd
# dt006 pavr picoweb pony-stk200 sp12 stk200 stk500
#
# Type: avrdude -c ?
# to get a full listing.
#
#AVRDUDE_PROGRAMMER = stk500
AVRDUDE_PROGRAMMER = dt006

# com1 = serial port. Use lpt1 to connect to parallel port.


#AVRDUDE_PORT = com1 # programmer connected to serial device
AVRDUDE_PORT = lpt1 # programmer connected to serial device

#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 51

# Uncomment the following if you want avrdude's erase cycle counter.


# Note that this counter needs to be initialized first using -Yn,
# see avrdude manual.
#AVRDUDE_ERASE_COUNTER = -y

# Uncomment the following if you do /not/ wish a verification to be


# performed after programming the device.
#AVRDUDE_NO_VERIFY = -V

# Increase verbosity level. Please use this when submitting bug


# reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
# to submit bug reports.
#AVRDUDE_VERBOSE = -v –v

AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) –c


$(AVRDUDE_PROGRAMMER)
AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)

#---------------- Debugging Options ----------------


# For simulavr only - target MCU frequency.
DEBUG_MFREQ = $(F_CPU)

# Set the DEBUG_UI to either gdb or insight.


# DEBUG_UI = gdb
DEBUG_UI = insight

# Set the debugging back-end to either avarice, simulavr.


DEBUG_BACKEND = avarice
#DEBUG_BACKEND = simulavr

# GDB Init Filename.


GDBINIT_FILE = __avr_gdbinit

# When using avarice settings for the JTAG


JTAG_DEV = /dev/com1

# Debugging port used to communicate between GDB / avarice / simulavr.


DEBUG_PORT = 4242

# Debugging host used to communicate between GDB / avarice / simulavr, normally

# just set to localhost unless doing some sort of crazy debugging when

# avarice is running on a different computer.

DEBUG_HOST = localhost

#=====================================================
# Define programs and commands.
SHELL = sh

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 52

CC = avr-gcc
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
SIZE = avr-size
NM = avr-nm
AVRDUDE = avrdude
REMOVE = rm –f
COPY = cp
WINSHELL = cmd

# Define Messages
# English

MSG_ERRORS_NONE = Errors: none


MSG_BEGIN = -------- begin --------
MSG_END = -------- end --------
MSG_SIZE_BEFORE = Size before:
MSG_SIZE_AFTER = Size after:
MSG_COFF = Converting to AVR COFF:
MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
MSG_FLASH = Creating load file for Flash:
MSG_EEPROM = Creating load file for EEPROM:
MSG_EXTENDED_LISTING = Creating Extended Listing:
MSG_SYMBOL_TABLE = Creating Symbol Table:
MSG_LINKING = Linking:
MSG_COMPILING = Compiling:
MSG_ASSEMBLING = Assembling:
MSG_CLEANING = Cleaning project:

# Define all object files.


OBJ = $(SRC:.c=.o) $(ASRC:.S=.o)

# Define all listing files.


LST = $(SRC:.c=.lst) $(ASRC:.S=.lst)

# Compiler flags to generate dependency files.


GENDEPFLAGS = -MD -MP -MF .dep/$(@F).d

# Combine all necessary flags and optional flags.


# Add target processor to flags.
ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)
ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)

# Default target.
all: begin gccversion sizebefore build sizeafter end
build: elf hex eep lss sym

elf: $(TARGET).elf

hex: $(TARGET).hex

eep: $(TARGET).eep

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 53

lss: $(TARGET).lss

sym: $(TARGET).sym

# Eye candy.
# AVR Studio 3.x does not check make's exit code but relies on
# the following magic strings to be generated by the compile job.
begin:
@echo
@echo $(MSG_BEGIN)

end:
@echo $(MSG_END)
@echo

# Display size of file.


HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
ELFSIZE = $(SIZE) -A $(TARGET).elf
AVRMEM = avr-mem.sh $(TARGET).elf $(MCU)

sizebefore:
@if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE);
$(ELFSIZE); \
$(AVRMEM) 2>/dev/null; echo; fi

sizeafter:
@if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER);
$(ELFSIZE); \
$(AVRMEM) 2>/dev/null; echo; fi

# Display compiler version information.


gccversion :
@$(CC) –version

# Program the device.


program: $(TARGET).hex $(TARGET).eep
$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH)
$(AVRDUDE_WRITE_EEPROM)

# Generate avr-gdb config/init file which does the following:


# define the reset signal, load the target file, connect to target, and set
# a breakpoint at main().
gdb-config:
@$(REMOVE) $(GDBINIT_FILE)
@echo define reset >> $(GDBINIT_FILE)
@echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
@echo end >> $(GDBINIT_FILE)
@echo file $(TARGET).elf >> $(GDBINIT_FILE)
@echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >>
$(GDBINIT_FILE)

ifeq ($(DEBUG_BACKEND),simulavr)
@echo load >> $(GDBINIT_FILE)

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 54

endif
@echo break main >> $(GDBINIT_FILE)

debug: gdb-config $(TARGET).elf

ifeq ($(DEBUG_BACKEND), avarice)

@echo Starting AVaRICE - Press enter when "waiting to connect" message


displays.
@$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
$(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
@$(WINSHELL) /c pause

Else
@$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
$(DEBUG_MFREQ) --port $(DEBUG_PORT)

endif

@$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)

# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.

COFFCONVERT=$(OBJCOPY) --debugging \
--change-section-address .data-0x800000 \
--change-section-address .bss-0x800000 \
--change-section-address .noinit-0x800000 \
--change-section-address .eeprom-0x810000

coff: $(TARGET).elf
@echo
@echo $(MSG_COFF) $(TARGET).cof
$(COFFCONVERT) -O coff-avr $< $(TARGET).cof

extcoff: $(TARGET).elf
@echo
@echo $(MSG_EXTENDED_COFF) $(TARGET).cof
$(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof

# Create final output files (.hex, .eep) from ELF output file.

%.hex: %.elf
@echo
@echo $(MSG_FLASH) $@
$(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@

%.eep: %.elf
@echo
@echo $(MSG_EEPROM) $@
-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
--change-section-lma .eeprom=0 -O $(FORMAT) $< $@

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 55

# Create extended listing file from ELF output file.


%.lss: %.elf
@echo
@echo $(MSG_EXTENDED_LISTING) $@
$(OBJDUMP) -h -S $< > $@

# Create a symbol table from ELF output file.


%.sym: %.elf
@echo
@echo $(MSG_SYMBOL_TABLE) $@
$(NM) -n $< > $@

# Link: create ELF output file from object files.


.SECONDARY : $(TARGET).elf
.PRECIOUS : $(OBJ)

%.elf: $(OBJ)
@echo
@echo $(MSG_LINKING) $@
$(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)

# Compile: create object files from C source files.


%.o : %.c
@echo
@echo $(MSG_COMPILING) $<
$(CC) -c $(ALL_CFLAGS) $< -o $@

# Compile: create assembler files from C source files.


%.s : %.c
$(CC) -S $(ALL_CFLAGS) $< -o $@

# Assemble: create object files from assembler source files.


%.o : %.S
@echo
@echo $(MSG_ASSEMBLING) $<
$(CC) -c $(ALL_ASFLAGS) $< -o $@

# Create preprocessed source for use in sending a bug report.


%.i : %.c
$(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@

# Target: clean project.


clean: begin clean_list end
clean_list :
@echo
@echo $(MSG_CLEANING)
$(REMOVE) $(TARGET).hex
$(REMOVE) $(TARGET).eep
$(REMOVE) $(TARGET).cof
$(REMOVE) $(TARGET).elf
$(REMOVE) $(TARGET).map
$(REMOVE) $(TARGET).sym
$(REMOVE) $(TARGET).lss

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 56

$(REMOVE) $(OBJ)
$(REMOVE) $(LST)
$(REMOVE) $(SRC:.c=.s)
$(REMOVE) $(SRC:.c=.d)
$(REMOVE) .dep/*

# Include the dependency files.


-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)

# Listing of phony targets.


.PHONY : all begin finish end sizebefore sizeafter gccversion \
build elf hex eep lss sym coff extcoff \
clean clean_list program debug gdb-config

9.3 Appendix C: The program

#include <avr/io.h>
#include <stdio.h>
#include <inttypes.h>
#include <avr/signal.h> //include file for signal interrupts
#include <avr/interrupt.h> //include files for interrupts
#define DEF_UART 0 //value for UBRR
#define BAUD 51 //the baud is the speed at which the microcontroller
should check the USART port. This is calculated by

fosc
−1
16( BAUDRATE )

The clock frequency of the oscillator is 4 MHz and the Baud Rate (speed of the USART is
4800. (4000000/(16*4800))-1 = 51,0833

#define COMMA3 3
#define COMMA4 4
#define COMMA5 5
#define COMMA6 6
#define CR 0x0D //CR and LF are use to send ENTER
#define LF 0x0A
#define CTRLZ 0x1A //The hexadecimal value of CTRL-Z is 0x1A
#define SMSCOMMAND 20

unsigned char IncomingData; //global variable


unsigned int check; //this variable is used for a timing function.
If there is an interrupt, the checking of the data
should begin.

//*************************************************//
// TRANSMIT //
//*************************************************//

void USART_Transmit(unsigned char data)


{
while( ! ( UCSRA & ( 1<<UDRE ) ) ); // Wait for empty transmit buffer
UDR = data; //Put data into buffer, sends the data
}

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 57

//*************************************************//
// MAIN PROGRAM //
//*************************************************//

int main ( )
{
//SET USART
UBRRH = DEF_UART; // 4800 bps at 4 MHz
UBRRL = BAUD;
UCSRB = _BV(RXEN) | _BV(TXEN) | _BV(RXCIE); // Rx Complete Interrupt &
Enable Rx/Tx
UCSRC = _BV(UCSZ1) | _BV(UCSZ0); // 8N1

//SET PORTS: PORTB is not needed, but is used to send to the LEDs
//PORTD uses 2 bits. PD0 for RXD and PD1 for TXD

DDRB=0xFF; //B port output


PORTB=0x00; //B port high
DDRD=0x02; //port0 on pull up and port1 on 0

asm("sei"); //enables the interrupts

//*****************************//
//reserve and set variables//
//*****************************//

/*
Read is variable to check the data
n is variable to count the commas that have past the check with reading
m is used to count the number of bytes needed to store he data
Longitude, Latitude, DirNs and DirEW are used to save the data from the GPS
LenLon and LenLat are used to see how long the array for Longitude and Latitude is
the counter is used to send the data at certain times
*/

int Read , Write , n;

/*
maximum 9 characters are possible for writing the data to latitude and longitude
because the last character of an array is a Carriage Return
*/

char Longitude[10] , Latitude[10] , DirNS , DirEW;


int LenLon, LenLat;
int counter;
int command[20];
IncomingData = 0;
check = 0;
Read = 0;
Write = 0;
n = 0;
LenLon = 0;
LenLat = 0;

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 58

counter = 0;

/*set variables of the array command. This array consists of the command to send an
SMS. This is AT+CMGS=“<phone number>”
*/

command[0] = 'A';
command[1] = 'T';
command[2] = '+';
command[3] = 'C';
command[4] = 'M';
command[5] = 'G';
command[6] = 'S';
command[7] = '=';
command[8] = '"';
command[9] = '0';
command[10] = '4';
command[11] = '4';
command[12] = '1';
command[13] = '2';
command[14] = '3';
command[15] = '4';
command[16] = '5';
command[17] = '6';
command[18] = '7';
command[19] = '"';

while(1) //this makes an infinite loop


{
//*************************************//
// CHECKING OF DATA //
//*************************************//

/*
In this program 3 characters are used to test: RMC
This is the signal to start the reading of the right data
*/

if ( check == 1 ) //check is 1 after every interrupt


//and 0 when action is done
{
if ( Read == 0 )
{
if( IncomingData == 'R' ) // check if received char is R
{
Read = 1; //If character is R then check
//the next character
}
check = 0;
}
}

if ( check == 1 )
{
if ( Read == 1 )
{

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 59

if( IncomingData == 'M' ) // check if char is M


{
Read = 2;
}
if( IncomingData != 'M' )
{
Read = 0;
}
check = 0;
}
}

if( check == 1 )
{
if( Read == 2 )
{
if( IncomingData == 'C' ) // check if char is C
{
Read = 3;
LenLon = 0;
LenLat = 0;
n = 0;
}
if( IncomingData != 'C' )
{
Read = 0;
}
check = 0;
}
}

//*************************************//
// READ THE RIGHT DATA //
//*************************************//

/*
All 3 characters are checked and OK
The reading of the right data begins
Again if check = 1 it should do an action
The data is divided in 4 parts which are read separately
*/

if( check == 1 )
{
if( Read == 3 )
{
/*
data is shown in form of ,longitude, lattide, N-S, E-W,...
the fist sign is a comma
*/

if( IncomingData == ',' ) //if the char is a comma


//then don't read the char, but increment n
{

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 60

n = n + 1;
}

if( IncomingData != ',' )


{
if( n == COMMA3 )
{
Latitude[LenLat] = IncomingData;
LenLat = LenLat + 1;
}

if( n == COMMA4 )
{
DirNS = IncomingData;
}

if( n == COMMA5 )
{
Longitude[LenLon] = IncomingData;
LenLon = LenLon + 1;
}

if( n == COMMA6 )
{
DirEW = IncomingData;
Read = 0;
Write = 1;
counter = counter + 1;
}

check = 0;
}

//*************************************//
// SENDING THE DATA //
//*************************************//

/*
The data is send in this part
Every certain time it has to send a message to another phone
The message exists of AT+CMGS="TELEPHONE NUMBER" <CR>
The message is send after this with CTRL-Z at the end.
*/

if( Write == 1 )
{
if( counter == 10 )
{

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 61

/*
To initiate the SMS service. Using a array gives an error on the last char of that array.
The service command is AT+CMGS= " (phone number) " <ENTER> message
<CTRL><Z>. AT+CMGS is written in the array command[]. On the GSM, the message
will be like this: <Latitude N or S , Longitude E or W> and the message will be send
after CTRL-Z
*/

for( int t = 0 ; t == SMSCOMMAND-1 ; t++ )


{
USART_Transmit( command[t] );
}

USART_Transmit( CR ); //CR
USART_Transmit( LF ); //LF
for( int v = 0 ; v <= LenLat-1 ; v++ )
{
USART_Transmit( Latitude[v] );
}

USART_Transmit( DirNS );
USART_Transmit( ',' );

for( int u = 0 ; u <= LenLon-1 ; u++ )


{
USART_Transmit( Longitude[u] );
}

USART_Transmit( DirEW );
USART_Transmit( CTRLZ );
counter = 0;
}

} //END WHILE LOOP

return 1;

} //END MAIN

//*************************************************//
// RECEIVE //
//*************************************************//

SIGNAL ( SIG_USART0_RX )
{
IncomingData = UDR; //Store received byte
check = 1;
PORTB = IncomingData; //Show byte on PORTB. This is only to check if
//data is received and transmitted
}

Frederix Maarten June 2006


Vaasa Polytechnic KHK Geel 62

SOURCES

Books

Dhananjay V. Gadre (2000). Programming and Customizing the AVR microcontroller.


Hightstown: McGraw-Hill’s professional Book Grouup composition unit

Richard H. Barnett, Sarah Cox, Larry O’Cull (2002). Embedded C Programming and the
Atmel AVR. Florence: Thomson Delmar Learning

Internet

Ma Chao, Lin Ming (2003) GPS-GSM Mobile Navigator


Found 24th of August 2005 on the internet:
http://www.atmel.fi/journal/documents/issue1/gps.pdf

Atmel (2005) Datasheet Atmel ATTINY2313


Found 23th of August 2005 on the internet:
http://www.atmel.fi/dyn/products/product_card.asp?part_id=3229

Wavecom (2004) Datasheets of Wavecom QUICK Q2501


Found 23th of August 2005 on the internet:
http://www.wavecom.com/media/files/support/Hard_platforms/Modules/Q2501B/Firmw
are_release_v650A/AT_commands_manual/AT_Commands_Interface_Guide_for_revisi
on_X50a.pdf

Wavecom (2004) Datasheets of Wavecom QUICK Q2501 GPS commands


Found 23th of August 2005 on the internet:
http://www.wavecom.com/media/files/support/Hard_platforms/Modules/Q2501B/Firmw
are_release_v650A/AT_commands_manual/Q2501_Specific_GPS_AT_commands_Interf
ace_Guide.pdf

Dale DePriest Information about the NMEA-0186 standard.


Found 14th of September 2005 on the internet:
http://www.gpsinformation.org/dale/nmea.htm#nmea

John Scourias (1994) Information of the GSM-network infrastructure.


Found 14th of September 2005 on the internet: http://kbs.cs.tu-
berlin.de/~jutta/gsm/js-intro.html

Frederix Maarten June 2006

Potrebbero piacerti anche