This is only a preview of the October 2020 issue of Practical Electronics. You can view 0 of the 72 pages in the full issue. Articles in this series:
|
Make it with Micromite
Phil Boyce – hands on with the mighty PIC-powered, BASIC microcontroller
Part 21: Exploring a GPS module with UART communication
T
his month we will explain
how to connect the Micromite
to external hardware that uses
an interface commonly known as
‘UART’. ‘UART’ stands for ‘Universal
Asynchronous Receiver-Transmitter’,
which in essence relates to data that is
transmitted (and received) between two
devices using a specific serial protocol.
There are many readily available
hardware modules that use this protocol,
and once you have an understanding
of the basic concepts it will be easy to
implement UART-based hardware in
future Micromite projects.
Throughout this series we have tried to
keep things simple by demonstrating new
topics with just a few lines of code, which
in turn interface with minimal hardware.
Here, we will again adopt this approach
by first covering the concept of UART
communication, and then introducing
the relevant MMBASIC commands. With
these two elements understood, we can
then explore how to communicate with
a simple UART module.
To make this fun, our aim will be to listen
to, and display, the serial data ‘sentences’
that a low-cost Global Positioning System
(GPS) receiver module continuously
outputs. We will show you how to use this
data in various ways, including plotting
your position on a map, and how to build
a simple, yet highly accurate clock based
upon the precise time information that is
contained within GPS data.
G PS antenna
M icro m ite
T x
R x
R x
T x
U A R T G P S m o d u le
Fig.1. Concept for connecting any UART
module (eg GPS) to the Micromite.
54
UART concepts
UART protocol: Baud-rate
Back in Part 11 (PE, December 2019)
we discussed the theory of serial data
communication. That included a brief
description of how asynchronous
communication works between two
devices. If you have access to Part 11,
then it is well worth rereading. However,
if you don’t have that issue, here’s a brief
summary of the concepts.
For two devices to communicate serially
with each other, the transmit pin (Tx) of
the first device needs to be connected
to the receive pin (Rx) of the second
device (and vice versa: the Tx on the
second device connected to Rx on the first
device). If we now consider the first device
as being a Micromite, and the second
device as a UART GPS module, then
this setup represents how the hardware
we’ll be using this month is required to
be connected – see Fig.1.
An important point to clarify here is that
the two ‘signal-lines’ shown in Fig.1 are
each their own dedicated communication
link and they work independently of each
other. Each communication link has a
transmitter at one end, and a receiver at
the other. Tx is the ‘source’ of the data,
and the Rx is the ‘destination’ of the
data; hence the green link is used when
the Micromite wants to ‘talk’ to the GPS
module (which in effect is ‘listening’
on its Rx pin); and the red link is used
when the GPS module wants to ‘talk’ to
the Micromite.
In our application here, we only need the
Micromite to listen to data being outputted
from the GPS module. Therefore, we only
need the red link shown in Fig.1.Thus,
the GPS module’s Tx pin will effectively
output serial data which is received at the
Micromite’s Rx ‘listening’ pin.
That covers the physical connections,
now let’s look at various elements of
the UART protocol itself – ie, how the
communication takes place.
Asynchronous communication requires
that both devices (at either end of a
communication link) operate at exactly
the same speed as each other. This is
because the transmitter needs to set the
communication link to either a logic
high or a logic low for a defined amount
of time, during which the receiver then
needs to sample the logic level present
on the communication link. Think of
this as a Tx-set/Rx-sample process
representing the communication of a
single bit of information. This process
must be repeated at regular (ie, agreed)
time intervals in order for each bit of
transmitted data to be received correctly
by the receiver and formed back into a
meaningful piece of data (comprising
a certain number of bits). If the two
devices are not operating at the same
speed, then very quickly the Tx-set
(and/or the Rx-sample) will occur at
the wrong time and hence corrupt data
will be received.
Consider the transmission of a single
byte of data, which effectively means the
transmission of 8-bits. In order for the
byte to be successfully communicated
asynchronously from the transmitting
device to the receiving device, the
Tx-set/Rx-sample process needs to be
repeated eight times at precise time
intervals. This time interval is defined
by what is known as the UART ‘baud
rate’. We discussed this in Parts 3 and
4 (PE, April and May 2019) when we
talked about how to set up your Terminal
application (we used TeraTerm) to
successfully communicate with your
Micromite code
The code in this article is available
for download from the PE website.
Practical Electronics | October | 2020
Packet
1
S tart
bit
5 to 9
Data bits
0 or 1
Parity
bit( s)
1 or 2
S top
bit( s)
Data f rame
Fig.2. UART communication involves
sending packets. The required data is
‘wrapped’ with a Start bit, a Parity bit,
and 1 or 2 Stop bits, as shown here.
MKC. Note that the baud rate needs to
be set ‘manually’ at both ends; and if set
to different values then communication
will simply not work correctly.
UART protocol: Start bit
There is one other important detail for
successful UART communication to
take place, and that is how does the
Tx device and Rx device know when
to start the Tx-set/Rx-sample process.
For example, if you were to power up
only the transmitter circuit and let it
begin the Tx-set part of the process,
then there would be nothing ‘listening’
on the other end of the communication
link to receive (ie, sample) the data. If
you now power up the receiver circuit,
then the Rx-sample process is potentially
occurring part way through a message
that is currently being transmitted.
Hence, in this scenario, the received data
would be incorrect and/or incomplete.
To solve this issue, the UART protocol
simply ‘wraps’ the data into ‘packets’,
with the first bit of a UART packet being
a ‘Start Bit’ indicator (see Fig.2). It is this
Start Bit that synchronises the receiver
with the transmitter so that the data is
received correctly.
In summary, a UART transmitter (Tx)
which is at one end of a communication
link, is responsible for sending the data
to the receiver (Rx) which is at the other
end of the communication link. The
sending/receiving of data is implemented
with the Tx-set/Rx-sample process on
the communication link. The Start Bit
in the ‘UART packet’ synchronises the
Rx with the Tx; and the transmitter and
receiver need to be set to operate at the
same speed (baud rate).
The UART packet
Referring to Fig.2, we can see that
there are four main sections to the
UART packet. To be able to use UART
communication in its simplest form,
we need a basic understanding of all
four sections.
1. The Start bit synchronises the Tx-set/
Rx-sample process. Think of it as Tx
telling Rx to start listening.
2. The Data frame contains the actual data
being transferred serially. It can comprise
5, 6, 7, 8, or 9 bits (8 is the most common
since it makes a byte of data).
Practical Electronics | October | 2020
3. The Parity bit provides a partial
method to determine if the data has
been received accurately. Three terms
associated with this include: no parity,
odd parity, and even parity. If parity
is used, then the total number of ‘1’
bits in the data frame are counted
by the Tx, and then the parity bit is
either set, or left unset by the Tx so
that there is either an odd number,
or even number of ‘1’ bits in total (in
the data frame and the parity bit). The
Rx then uses this parity bit to check
that it also receives an odd or even
number of ‘1’ bits. Note that parity
checking can only identify some dataerror scenarios, but not all.
4. Stop bits (either one or two) are sent by
the transmitter to signify to the receiver
that the transmission is complete.
The point of highlighting the above is
that there are several parameters that
need to be defined when using UART
communications. These are then set as
required in order to configure both devices
at either end of the communication:
Baud rate: defines the speed of data
transmission – here we will be using
9600 baud
Data bits: between 5 and 9 bits can be
used – here we will be using 8 data bits
Parity: N(o), O(dd), E(ven) – here we
will be using No parity
Stop bits: 1 or 2 – we will be using 1.
The above is often seen written as
something similar to: 8-N-1 (9600).
Without knowing (and setting) these basic
parameters, UART data communication
will not work correctly (if at all).
There are several more additional
elements to UART communication and
a quick search on Google will allow you
to read more if you’re interested. For
example, the signals can be logically
inverted; however, the point in this article
is to keep things to the bare minimum so
we can understand how to use MMBASIC
commands to allow communication with
a UART module.
Micromite UART pins
The 28-pin Micromite (as used in the
MKC) has a total of three hardware
UART ports. These are referred to as:
‘COM1’, ‘COM2’, and ‘COM3’. Each
has a Tx pin and an Rx pin. COM3
is specifically used as the console
connection – ie, to connect the MKC
to the Terminal App (or more specially,
COM3 is connected to the Development
Module). However, COM1 and COM2
are both available for you to connect to
UART hardware. Refer to Fig.3 to see
the relevant Micromite pin numbers. In
our application, we will be connecting
the GPS module to COM1 Rx.
1
28
2
27
3
26
4
25
5
24
6
23
7
8
22
M icro m ite
21
CO M2 Tx
9
20
CO M2 R x
10
19
CO M3 ( console) Tx
11
18
CO M3 ( console) R x
12
17
13
16
14
15
CO M1 R x
CO M1 Tx
Fig.3. The Micromite used in the MKC
has three UART (COM) Ports. COM3 is
reserved for the Console connection.
Now that we understand the concept
of UART communication, along with
knowing what certain UART terminology
refers to, we are almost ready to connect a
GPS UART module to our MKC. However,
we first need to select a suitable GPS
module from the many types available.
GPS module choice
A GPS receiver module comprises two
main parts: the antenna (that receives
the GPS signal), and the GPS receiver
itself. The GPS receiver contains all the
necessary electronics, and these will
typically be mounted on a small PCB.
The antenna may be part of the PCB, or
instead the PCB will allow for an external
antenna to be connected.
The PCB will also have either a row
of header pins, or allow for a small
connector/lead to be plugged in. This
is the physical connection to the GPS
module, allowing it to be powered and
use the communication link. Note that the
module may communicate over various
protocols, including UART, I2C and SPI.
A quick online search for ‘UART GPS
module’ will highlight that there are
many different types of GPS modules
available. They can start at a cost as low
as £3 (but we would advise against these)
and a high-end GPS module can cost £50
or more. You may even have a suitable
GPS module already in your collection
of ‘useful bits for a rainy day’.
We recommend a GPS module that
comes with an external antenna, and
ideally one that is based on a recognised
GPS receiver module such as the popular
U-Blox 6M, or SIM28. The one we
have used in this article is the Grove
GPS module (see Fig.4). It is a lowcost, high-performance module that is
readily available for around £20 from
many recognised suppliers such as
RS and Farnell (as well as from many
smaller online suppliers). If you do an
online search you will see potential
55
GPS circuit diagram
The circuit diagram for
connecting a GPS UART
module to the Micromite is
shown in Fig.6. As usual, it is
just a matter of connecting the
correct pins together between
the GPS module and the
MKC. This could hardly be
Fig.4. We recommend
any simpler since just three
this low-cost Grove
connections are required:
GPS module which is
two for supplying power to
readily available from
the module, and one for the
many online suppliers.
communications link.
Most GPS modules operate
from a 5V supply, but do be
careful to check your module’s spec, just in
suppliers that can send you one at a
case it is a 3.3V module. If so, then simply
very reasonable cost.
connect to the Micromite’s 3.3V output.
The issue with most of the cheaper
Due to the simplicity of the circuit, we
units typically available from popular
are not using stripboard; instead we will
auction websites is that they often use a
just use three male-male jumper wires,
sub-standard antenna and poor-quality
each plugged between the MKC and the
antenna lead. This results in a GPS receiver
supplied GPS connector lead.
that can’t actually receive the GPS signal.
If you have been following this series
Either that, or you have to position the
and have built the Bluetooth (BT) module,
module outdoors just to be able to receive
then we would advise using it. This
the GPS signal. The Grove GPS module on
allows the MKC (and BT module) to be
the other hand has been tried and tested
connected to the GPS module (see Fig.7)
indoors and works very well. However, do
and then everything positioned near to a
take care when handling the uFL antenna
window with a ‘view to the sky’, which
connector/lead – it is rather small and
allows better GPS reception.
delicate! (See Fig.5, left – by the way, we
However, if you have not built the
have no affiliation to the manufacturer of
BT module then simply plug the DM
this particular module.)
into your MKC, and connect the GPS as
Another point to consider is using a
shown in Fig.6.
uFL-to-SMA adaptor in order to use an
antenna that has a much longer lead –
see Fig.5, right. Antennae with longer
MMBASIC UART commands
leads typically have an SMA connector
Appendix A of the Micromite User Manual
and hence the need for the adaptor.
is dedicated to UART communication
Using such a setup allows you to have
and is worth reading (see this month’s
the GPS antenna near the window,
download). However, to keep things simple
which improves reception of the GPS
we will just highlight the single command
signal. It also avoids having to move
that correctly configures the specific UART
your computer!
port to which we have connected the GPS
G P S m o d u le
G ND
Vd d
R x
T x
NC
0V
+V
( see text )
22
MK C
CO M1: R x
Fig.6. The circuit diagram for connecting
a GPS module – only three connections
are required to be made.
module to. The command line is:
OPEN "COM1:9600" AS #1
This one line will configure pin 21 as
the Tx pin, and pin 22 as the Rx pin (the
two pins associated to COM1). However,
we are not actually using pin 21 in this
application since the Micromite is only
being used here to ‘listen’ to data sent
from the GPS module.
The 9600 figure defines the baud
rate. This value is chosen as it is the
rate which the GPS module uses (as
per it’s datasheet). We do not have to
concern ourselves about setting the 8-N-1
parameters discussed above since the GPS
module uses the same default values as
MMBASIC. If it did use different values,
then the relevant parameters would
also be set in the above configuration
command (refer to the Micromite User
Manual for the specific details).
The #1 is a reference to a file buffer
which is inside the Micromite. Any data
that is received from the GPS module will
reside in this buffer, as we will now see.
Data that is sent by the GPS module to
the Micromite temporarily sits in what is
referred to as the ‘UART receive buffer’.
It sits there until the buffer is either
full (at which point new data received
overwrites the ‘old’ data), or until the
data is read out from the buffer by using
the MMBASIC command:
data$=INPUT$(n,#1)
What the above command does is to
extract n character bytes from the UART
receive buffer and load them into the
string that we have named data$. With
the data bytes loaded into this string,
you can then perform various checks on
the data in order to make your program
code behave in certain ways – we will
see how to do this very shortly.
GPS sentences
Fig.5. If required, use a uFL-SMA adaptor to allow a long-lead antenna to be plugged
in instead of the smaller Grove antenna.
56
We have mentioned that our aim this
month is to receive and display the data
that is outputted from the GPS receiver.
Practical Electronics | October | 2020
The great thing about most GPS receivers (including the
Grove GPS module used here) is that they continuously
send out useful data such as current position co-ordinates,
date, time, altitude, speed, number of satellites seen
and so on. This data is typically sent every second
on the GPS module’s Tx pin. In fact, the various data
elements are sent out in clearly defined ‘sentences’, the
format of which was developed by the National Marine
Electronics Association (which is why these sentences
are better known as ‘NMEA sentences’).
Table 1 shows several of the different NMEA sentences
that exist, and Table 2 shows all the elements that are
contained in the specific sentence that we will be using
(the GPRMC sentence). As can be seen, the GPRMC
sentence contains coordinate information (longitude
and latitude), and also the current date and time.
Note that each data element in an NMEA sentence is
separated by a comma; and another bonus is that the
data is outputted in the standard ASCII format so it is
very easy to display the received data. Note too that
each sentence begins with a leading $ character (at the Fig.7. The Grove GPS module connected with jumper wires to the MKC
start of data element 1), and ends with a checksum data and the Bluetooth module. (This is a 5V-powered module.)
element that starts with a * character. We will be using these
start-up time is approximately 15 seconds, with coordinate
characters to help detect the start and the end of a sentence.
data starting within 30 seconds.
Referring to the screenshot in Fig.8, you will see that there
are four NMEA sentences displayed. The left-most (ie, first)
Viewing GPS Sentences
data element of each line shows the type of NMEA sentence
Now that we have the GPS module connected to the MKC,
(refer to Table 1). If you look at the last line, then you can see
and that we also have a basic understanding of the MMBASIC
that this is the GPRMC sentence, which is the one that contains
commands used to configure and extract data from the
all the data that we are interested in. It is shown in Fig.8 with
Micromite’s UART receive buffer, let’s load a short program
a total of 10 received data elements, the definition of which
that allows us to view the data from the GPS module. Go ahead
are outlined in Table 2. So from the screenshot shown in Fig.8,
and download GPS_Sentences.txt from the October 2020 page
and by referencing Table 2, we can extract the following useful
of the PE website and install it on your MKC. On running the
data from the GPRMC sentence:
program, you should see some text appear on your terminal
Time = 161231.000 = 16:12:31
screen similar to that shown in Fig.8. Note that when you first
Data is valid (Data element 3 = A)
apply power to your GPS module, there may be a delay of up
Latitude = 5130.0505 N
to two minutes before any data is displayed on the screen.
This is purely because a GPS module needs to see and
lock on to several GPS satellites before it can output Table 2: The format of the GPRMC sentence. It contains all the data
any meaningful data. With the Grove GPS module, the elements we require, so this is the sentence we use. Here, the example
GPRMC GPS sentence is:
$GPRMC, 161229.487, A, 3723.2475, N, 12158.3416, W, 0.13, 309.62, 120598, , A, *10
Table 1: Different types of NMEA sentences
NMEA
sentence
Meaning
Date element
Example
Description
GPGGA
Global positioning system fix data (time,
position, fix type data)
1. Message ID
$GPRMC
RMC Protocol Header
GPGLL
Geographic position, latitude, longitude
2. UTC time
161229.487
hhmmss.sss
GPVTG
Course and speed information relative to ground
3. Status
A
A = data valid or V = data not valid
4. Latitude
3723.2475
ddmm.mmmm
GPRMC
Time, date, position, course and speed data
5. N/S indicator
N
N = north or S = south
GPGSA
GPS receiver operating mode, satellites used in
the position solution, and DOP values
6. Longitude
12158.3416
dddmm.mmmm
GPGSV
The number of GPS satellite in view, ID number,
elevation, azimuth and SNR values
7. E/W indicator
W
E = east or W = west
8. Speed over ground
0.13
knots
GPMSS
Signal-to-noise ratio, signal strength, frequency,
and bit rate from a radio beacon receiver
9. Course over ground
309.62
degrees
GPTRF
Transit fix data
10. Date
120598
ddmmyy
GPSTN
Multiple data ID
GPXTE
Cross track error, measured
GPZDA
Date and time (PPS timing message,
synchronised to PPS)
Practical Electronics | October | 2020
11. Magnetic variation
Degrees (E = east or W = west)
12. Mode
A
13. Checksum
*10
14. <CR><LF>
A = autonomous, D = DGPS, E = DR
End of message termination
57
Fig.8. On running GPS_Sentences.txt you should see something similar to this, which indicates that GPS data is being received.
Longitude = 00008.5793 W
Date = 150820 = 15 August 2020
When the program is up and running,
you will see a continuous stream of
batches of NMEA sentences received
from the GPS module (typically once
every second). However, we are only
interested in the GPRMC sentence so now
make the modification to the program
code that is outlined in the detailed
comments. Once you have done this,
re-run the program and you will now
only see the GPRMC sentences. Note
that this makes it easier to see the time,
as the displayed GPRMC sentence is
effectively updated each second.
We will not repeat how the code works
here; instead, please do take the time
to study the detailed comments in the
program code.
GPS data conversion
Data from a GPS receiver contains
lots of information; however, some of
the data is in a ‘standardised’ format
and hence will need converting before
use (using simple calculations). For
example, the observant of you will have
noticed that the GPS time is not correct.
Instead it will be in a format referred to
as ‘Universal Time Coordinated’ (UTC),
so we will first need to add (or subtract)
an offset in order to convert it into the
true ‘local time’, not forgetting to take
into account any daylight saving. At
the time of writing, for UK readers this
simply means adding on one hour to
the GPS time.
The other data that will need converting
are the GPS longitude and latitude
coordinates. By converting them into a
‘decimal degrees’ format we will then be
able to use the converted data to plot a
location on a map (by manually entering
the decimal degree coordinates into a
website). Let’s do this next.
Where are you?
To check the accuracy of your GPS
receiver, we will now work through the
steps of how to plot your position on a
map using the GPS coordinates received
by your module. Be sure to follow these
steps precisely!
1. Run the existing program code to
check that the third data element is
shown as an A character (and not a
V). If you see an A then move to step
2, otherwise wait for an A to appear
(you may need to check the antenna
is inserted correctly, and/or relocate
nearer to a window to improve the
GPS reception)
2. Make a written note of data elements
4-7 (ie, 5130.0505 , N , 00008.5793 , W)
3. Convert data element 4 (ie, 5130.0505)
by removing the first two digits (51)
and dividing the remaining digits by
60 (ie, 30.0505/60 = 0.500841666).
Now add the first two digits back on
to the result to yield: 51.500841666
4. Convert data element 6 (ie, 00008.5793)
by removing the first three digits (000)
and dividing the remaining digits by
60 (ie, 08.5793/60 = 0.142988333).
Now add the first three digits back on
to the result, producing: 0.142988333
5. Now visit the website: https://www.
gps-coordinates.net/gps-coordinatesconverter and scroll down the page to
see the input boxes for DD (decimal
degrees), as shown in Fig.9.
6. Enter the converted latitude value
(51.500841666) and longitude value
(0.142988333) calculated above
7. Look at data element 5 (N or S) and
data element 7 (E or W) and ensure
you select matching letters in the DMS
section (underneath where you entered
the DD coordinates)
8. Now zoom in on the map and it should
show your current position with a very
high degree of accuracy. If you are
not seeing the correct location, then
simply check the above calculations
again, and ensure that the values have
all been correctly entered.
As a challenge, why not modify the
program code to automatically perform
the above conversion calculations on the
coordinates and display those converted
values on the terminal screen instead.
Hint: use the LEFT$, MID$ and VAL
commands to manipulate the data.
A simple GPS Clock
Next, we will download a short program
to display the time. There is nothing
complicated about this code – it simply
extracts the time (data element 2) from the
GPRMC sentence, and then it uses ESC
codes (see Part 8, PE, September 2019) to
format the time on your terminal screen
(ie, in the TeraTerm window).
Comments are present throughout the
program code, and the file you need to
download is GPS_Clock.txt. (from the
October 2020 page of the PE website). Do
be sure to look through the code to ensure
you can see how the program works.
As always, drop me an email if there is
something you don’t fully understand.
Next month
The topics covered in this article have
set us up nicely for next month, when
we will be showing you how to create
a modern-looking analogue-style clock
built from a ring of 60 digital RGB LEDs.
Until then, have fun coding!
Fig.9. Visit www.gps-coordinates.net/gps-coordinates-converter – then scroll down
the page and enter your Decimal Degrees values, along with selecting the N/S and E/W
options. It will then plot your exact location on the map.
58
Questions? Please email Phil at:
contactus<at>micromite.org
Practical Electronics | October | 2020
|