French proposal to assign 2m-Band from Amateur Radio Service to Air Traffic Control

The 2m-Band is in danger! French authorities have proposed to take the frequencies from 144MHz to 146MHz from amateur radio service to aeronautical radio service.

What you can do:

  1. Get informed and download the CEPT papers here!
  2. Ask your national amateur association for assistance, provided you are a member!
  3. Sign the petition here!
  4. Donate! (Just done as well!)
  5. USE THE BAND!

Thanks de Peter (DK7IH)

Advertisements

A “lean design” SSB-Transceiver for 14 MHz

Preface

Currently I am revising older projects that are in my radio shelf, some of them not finished yet, postponed to a later date, some without a cabinet, some with severe problems with performance and so on. All the stuff that needs a “second chance” ;-). This project is one of this collection. The transmitter did not work correctly (severe parasetic oscillations occurred when the section was driven to power levels >1 watt).

By careful testing and examining  I found the reason: The grounding of the rf power amplifier stage was defective due to a connection that had not been soldered properly. After having cured that I found the output was 5 to 6 watts PEP output (very clean). Then, having the project on “GO!”,  I finished the design. Thus I got a nice little “vintage style” SSB QRP trsanceiver as a travel or hiking companion:

DK7IH - Simple SSB Transceiver for 14MHz (VFO controlled, 5W PEP)
DK7IH – Simple SSB Transceiver for 14MHz (VFO controlled, 5W PEP)

Basic concept

Frequent readers on my blog know that one thing I really enjoy is building radios based on a minimalist concept. The fewer components you need for a working transceiver, the better it is. At least in my point of view. Here is another one of these “very lean design” transceivers.

The radio originally was designed as a study for my “Old School Transceiver“. After having not built a “real” analog VFO for a number of years I wanted to find out if I still can set up a construction that is really stable concerning frequency. And because it is not very challenging to just watch the result on a frequency counter, a full transceiver had to be built along with the VFO. The VFO was OK, (see later text!) the power transmitter, as mentioned before, was not. Until I had revised it.

The design is another remake of the „Kajman Transceiver“ by  SQ7JHM. A design I absolutely love because of its simplicity. The radio basically has been designed for 80 meters (even when lot of websites quote it as a 20m rig) so it shows some weaknesses when adapted to 14MHz without any changes. Thus some improvements had to be made.

Improving performance of the SQ7JHM basic design

Some changes that were top of the agenda to meet my requirements:

  1. The receiver needed a preamplifier for bands where atmospheric noise is not that strong. A dual-gate MOSFET equipped radio frequency preamplifier improves noise figure significantly and can be put into the AGC chain to give more dynamic range and a more pleasant listening experience.
  2. An AGC (automatic gain control) is a good idea if you want to use the receiver in a more comfortable way without the need to lower the volume when strong stations appear. In addition the S-meter reading can be derived from the output of the AGC DC amplifier stage.
  3. A little bit more rf output power can be achieved by using a push-pull amplifier. Linearity also improves to a certain degree when using this design because AB mode combined with separated amplification of the half waves plus suppression of even-order harmonics.
  4. To enhance receiver gain a single stage interfrequency amplifier has been added that is only in use when on receive. It is also connected to the AGC chain.
  5. And, last, a microphone amplifier allows you to talk in a moderate way into the microphone which is good for me because I often have my QSOs when the rest of the family is asleep and not keen on listening to my strange “This is DK7IH/QRP, do you copy?” messages.

The schematic of my enhanced design:

DK7IH - Simple SSB Transceiver for 14MHz (VFO controlled, 5W PEP)
DK7IH – Simple SSB Transceiver for 14MHz (VFO controlled, 5W PEP) – REV1 – TNX to Paul, VK3HN for error report! (Full size picture)

Fascination originates from the fact that you only need a handful of components (OK your hand should not have the size of that of a new born baby!) to set up a working short wave SSB transceiver.

The VFO

Some thoughts on frequency stability

Careful design is the key for stable operation. This means component selection as well as setting it up on the veroboard.

The basic problem for every conventional free running VFO is temperature and its influence on the size of components. Due to the theory of thermodynamics all materials change their mechanical dimensions with temperature. This is caused by the kinetic energy of the molecules forming the crystals of a solid body. Thermal energy leads to enhanced oscillation of the molecules and therefore the need of larger spaces of each in individual molecule in a crystal. Because we have capacitors in a tuned circuit this will affect the values of all caps (wanted and unwanted ones) to a certain degree.

Something that helps the builder is called “temperature coefficient”. This means that electronic components increase OR decrease their respective value when they get warmer. The first is called “positive temperature coefficient”, the opposite is called “negative temperature coefficient”. So, you might guess, the fine art of radio building involves the knowledge of the characteristic behavior of components when heated.

I quote my findings about temperature behavior listed in the article referred to on the beginning of this text:

Capacitors:

  • Ceramic capacitors: —
  • Polystyrene capacitor: –
  • NP0 (C0G) capacitor: no measurable effect

Inductors:

  • Air coil on polystyrene coil former: +++
  • Coil wound on T50-6 yellow toroid: +

The more “+” or “-” signs, the more steep the function of T->dC or T->dL is. So you can see: The best choice are polystyrene capacitors combined with coil on a yellow toroid. This combination is likely to outbalance temperature effects. If extra capacity is needed, NP0 caps are recommended.

The circuit

From the existing principles of building a free running radio frequency oscillator I prefer the Hartley circuit. It uses a tapped coil (tap about 1/5 from the “lower” end) and saves capacitors compared to the Colpitts design. The tap achieves in-phase feedback. The lower you put the tap to the end the lower the amount of fed back energy will be. This leads to more frequency stability because the circuit does not heat up by excessive internal radio frequency. But be sure that oscillation is always strong enough and does not stop. The Hartley circuit is more simple and caps always inherit the risk of thermal problems when poorly selected.

The tuning is done with a Vernier drive and a homemade variable capacitor. For this a foil variable cap of an old AM radio has been dismantled an reassembled with air as dielectric. Lots of experiments were necessary to get the “frequency swing” correct and the basic capacitance to the right area.

Other measures that support frequency stability are :

  • Low DC power into the oscillator stage (avoids heating the device up by DC current),
  • Stabilizing voltage for the VFO stage by 2 consecutive steps,
  • Using a FET instead of bipolar transistor (no PN boundary layers in a FET),
  • Very loose coupling between oscillator and buffer stage reduce fed back of impedance changes by the output,
  • Low impedance output with emitter follower,
  • Avoid metal sheets (spec. Aluminum) close to the tuning elements! Aluminum sheet metal changes its size largely with even low temperature differences.

Practical results

This oscillator is stable. It needs 5 to 10 minutes to settle which is in the normal range of what can be expected. I then can have it tuned to one frequency and there is a maximum change in frequency < 50Hz for hours. And, to compare with synthesizer  technology: NO birdies at all. Really not. I love it! 😉

The mixers and filter section

NE602 and its derivatives have been used in legions of amateur transceivers. Basically designed for cell phones and small cordless phones radio amateurs quickly have found out that this mixer IC can be the universal mixer in lots of possible amateur radio designs. The main weakness is its low IMD3. But for a 14MHz rig the risk of appearance of strong out-of-band signals is not that likely. Besides, the selectivity of the receiver’s input section supports this. Strong in-band signals did not appear so far due to low band conditions. We’ll have to see how the receiver performs here.

On the other hand NE602 gives a good sensitivity which makes it ideal for radios on the higher bands where signal levels are not so high.

The NE602 has a balanced input AND a balanced output. This allows the designer to get two different signal sources to the input then subsequently mixed with the oscillator signal. As well the two outputs can be used to send the mixed signal to different paths.

This is what is the basic idea behind the design described here.

The mixer that is used together with the microphone to produce the DSB signal by mixing the audio signal with the local oscillator (LO) also serves as the product detector on receive by mixing the interfrequency with the LO. Correct signal path is set with the two relays depending on the fact you are either on transmit or receive mode.

The same principle is for the other mixer. It is transmit mixer or receive mixer, depending on the position of the relays.

The relays connect the SSB filter either to the input or the output of a distinct mixer. A graphical presentation should make it clear:

DK7IH - Simple SSB Transceiver for 14MHz (VFO controlled, 5W PEP) - Signal path display
DK7IH – Simple SSB Transceiver for 14MHz (VFO controlled, 5W PEP) – Signal path display

RX amp and interfrequency amplifier

These 2 stages are more or less the same. They provide 2 to 12 dB of gain depending on the AGC voltage applied to gate 2 of the dual gate MOSFET. In this version of the radio a potentiometer of 20kΩ is used to have the possibility to lower the DC voltage manually, by doing this an MGC (manual gain control) is achieved in a simple way.

Audio amplifier

A bipolar transistor and the inevitable LM386 amplify the filtered audio signal from the product detector to a volume that can be discerned even in a louder environment. The audio low pass filter prior to the AF preamp should be selected due to the users individual preferences concerning tone pitch of the audio signal.

RF power amplifier

This is more or less my standard power amplifier for small QRP rigs. I put stress on linear amplification, so I use emitter degeneration and negative feedback in collector circuit to get best IMD3 results. Even if the circuit could deliver one or two more watts I let the output power level at about 5 watts pep.

Here ist the result of a dual tone modulation:

DK7IH microsize QRP SSB transceiver ("Micro24") for 14 MHz - Output signal modulated with 2-tone signal
DK7IH microsize QRP SSB transceiver (“Micro24”) for 14 MHz – Output signal modulated with 2-tone signal

Voltage division is 10 volts per cm, so this is 45Vpp which equals to about 5 watts max. peak output. Quite OK for QRP. And here is the spectrum of a 2-tone-modulated signal:

micro24_dk7ih_output_spektrum_dual_tone

Practical setup

The whole transceiver is built on a 12×8 cm Veroboard (4.7″ x  3.1″). There is only one layer. The cabinet is 4 cm high (1.55″), 14 cm long (5.5″) and 9 cm wide (3.5″).

DK7IH - Simple SSB Transceiver for 14MHz (VFO controlled, 5W PEP) - Inside view
DK7IH – Simple SSB Transceiver for 14MHz (VFO controlled, 5W PEP) – Inside view

Left the vernier drive with the homemade capacitor attached. Left of the 9MHz filter you can see the LO, more far left the S-meter (from an old CB radio) hiding the audio amps. The 2 mixer ICs and the relays are sited around the SSB-filter. On the right side the power amp partly hidden by the DC switching board.

Well, that’s the story how a nearly failed project was saved from the scrapyard and came to life by carefully searching the faulty element in the circuit.

Vy 73 de Peter (DK7IH)

 

Revision of the “Cigarette Pack”- 14MHz SSB QRP Micro-Transceiver

DK7IH microsize QRP SSB  transceiver ("Micro24") for 14 MHz
DK7IH microsize QRP SSB transceiver (“Micro24”) for 14 MHz – Fits into one hand
DK7IH microsize QRP SSB transceiver ("Micro24") for 14 MHz
DK7IH microsize QRP SSB transceiver (“Micro24”) for 14 MHz

This article describes the “Cigarette Pack” SSB QRP transceiver” for 14MHz that I first had mentioned some months before. Recently, when taking it from the shelf, the transceiver dropped to the floor and was severely damaged. This lead to serious defects in the front panel area, the main frame, the cabinet and so on. The interior parts were, luckily, not affected by the crash. So, I had to revise the whole radio, make a new front panel and cabinet, ply the frame straightly (as far as possible) and so on. This is the full description of the rig now to complete the files here. The good news: The radio is fine again and fully operational!  And the even better news: I still have not started smoking!  smile1

During reconstruction the transceiver has been extended for about 5 mm so that overall length now is 100mm (3.9 inch). This was done because I intended to build in a loudspeaker. The other dimensions remain unchanged: Width is 52mm (2 inch.), height is 30mm (1.2inch). OK it is slightly longer now than a standard pack of cancer sticks, but who cares? Total cabinet volume is 150cm³.

Basic concept

The transceiver is based on the “Micro 23” rig, that I have described here. Some simplifications of that already simplified radio have been made. Here is the full schematic of this even smaller transceiver:

DK7IH microsize QRP SSB  transceiver ("Micro24") for 14 MHz - Schematic
DK7IH microsize QRP SSB transceiver (“Micro24”) for 14 MHz – Schematic in full size

Very simple rigs like this one always use parts of the circuit for receive and transmit purpose. Here these parts are the 2 mixers (NE602), the SSB-filter  and the interfrequency amplifier.

Signal flow schematic

The NE602 has a balanced output. With mixer 1 only one of them is used. If higher gain is desired, a broadband (or even better a tuned LC circuit) transformer could be used to connect pin 4 and 5 (the mixer outputs) in push-pull mode. I did not do that to save the transformer.

The signal flow can be derived from the design:

DK7IH microsize QRP SSB transceiver ("Micro24") for 14 MHz - Signal flow on receive and transmit
DK7IH microsize QRP SSB transceiver (“Micro24”) for 14 MHz – Signal flow on receive and transmit

Receive mode signal flow

From the antenna relay (not drawn) the rf energy runs through a 2 pole LC filter for 14 MHz. The coils are wound small TOKO coil formers, all respective data is given in the schematic. Coupling is loose via a 3.3pF cap.

NExt stage is an rf preamp for 14MHz with a broadband output. The acitve element here is a dual-gate MOSFET.

After having left this stage the 14MHz signal travels through another 470pF capacitor. This one has high resistance for audio frequency and low for rf frequencies due to the equation: XC =1/(2*PI*f*C). The signal is then fed, together with the audio signal from the microphone (when on transmit), into mixer 1 input on pin 1.  The 1k resistor prevents the rf energy from flowing into the microphone circuit. The two signals are separated from each other by simply exploiting reactance and resistance in a rather clever way 😉.

When receiving the Si5351A clock chip is programmed in a way that the VFO signal (23 MHz) is present on output CLK0.  It is fed into mixer 1 via a small cap to prevent overloading of the mixer. The Si5351A breakout board delivers about 3 Vpp. clock signal, so this must be reduced to about 200mVpp. A 5.6pF capacitor is OK here.

The resulting signal is sent to the SSB filter (a 9MXF24D) that is terminated with 1kOhm and 20pF in parallel. The wanted SSB signal is present at the output of the filter.

Next stage is the interfrequency amplifier, equipped with a dual-gate-MOSFET semiconductor. This one is connected to the AGC chain, on receive a variable voltage is applied to gate 2 (range 0 to 6 V), on transmit the AGC is fully powered to ensure maximum gain.

Next is mixer 2 which is the product detector when receiving. The signal (9MHz +/- sideband shift) is applied to pin 6. Due to the fact that this mixer also serves as transmit mixer, the two signals are taken from the two mixer outputs on pin 4 (serving as audio output) and pin 5 (serving as rf output for transmitting).

Two audio amplifiers (preamplifier and power stage) give a sufficient signal level for an 8 ohm loudspeaker or a headphone.

For the loudspeaker I tried out the tiny ones for smartphones with good success. Only the volume was a little bit low. Then I found another speaker in an old toy of my daughter that turned out to be very much OK for this transceiver. Its diameter is about 3 cm (1.2 inch) and just fits in the housing.

Transmit signal flow

The microphone in this radio is an electret one. The advantage is that these microphones have an internal preamplifier equipped with a field-effect-transistor. The output voltage is fairly high, about 1Vpp. when normally speaking into it. Therefore an audio preamp is obsolete. The microphone signal is directly fed into pin 1 of the first mixer. On transmit the Si5351 signal generator is switched that the 9MHz (+/- sideband shift) signal is fed into pin 6. The SSB filter eliminates the unwanted sideband, the interfrequency amplifier lifts the SSB signal to an appropriate level. The TX mixer is fed with the 23MHz signal resulting in a 14 and 37 MHz signal. The TX band pass filter cleans the signal from the unwanted 37MHz component resulting from the mixer process.

RF power amplifier

The power amplifier is a 3 stage circuit. Stage 1 (preamplifier) brings the signal to about 10 mW. This is coupled into the driver stage via a cap of 0.1uF without any further impedance matching.

The subsequent driver stage shifts the signal level to about 200mW. Linear amplification is ensured her (as well as in the previous stage) by negative feedback in the collector circuit and emitter degeneration with a non-bypassed resistor to GND. An output transformer (winding rate 4:1, impedance rate thus 16:1) lowers the impedance of some 100 ohms to a few 10 ohms present on the input of the final amplifier stage.

The final amplifier brings up a signal level of 3 to 4 Watts PEP. This stage is in AB mode, the appropriate bias is achieved by the 1k resistor going to +12V TX and the current to GND via the silicon diode. This diode must be thermally connectod to the final transistor to stabilize the bias.When the transistor heats up, the silicon diode increases the current through it thus decreasing bias to the transistor.

The 68 ohm resistors serves 2 purposes: First it prevents the input signal from being shorted by the bypass caps in the bias circuit and it stabilizes the rf behavior  of the stage by limiting the gain because certain amounts of the input power are led to GND. This prevents self-oscillation.

DC ad the collector is fed through a radio frequency choke to hinder rf from flowing into the DC line. Radio frequency is directly fed into the low-pass-filter. The output impedance of this stage is roughly 50 Ohms, so the filter can be a 50 ohm circuit with a cutoff frequency slightly above 14MHz.

The VFO section

The Si5351A clock chip used here has three frequency outputs that can be set individually. Only CLK0 and CLK1 are used in this radio. The Si5351A chip is programmed by software in the following manner:

  • Receive: CLK0 is the VFO, CLK1 is the BFO.
  • Transmit: CLK0 is the BFO, CLK1 is the VFO.

The microcontroller reads the tx/rx status and switches the frequencies respectively.

Construction

The radio is a full SMD design on a 0.1″ pitch double sided Veroboard:

DK7IH microsize QRP SSB  transceiver ("Micro24") for 14 MHz - Inside
DK7IH microsize QRP SSB transceiver (“Micro24”) for 14 MHz – Inside

The control panel on the left with tuning knob and volume set. The 64×32 pixel OLED between these controls. Following the microcontroller behind the fron panel (here covered). The controller is an ATmega168 on an Arduino Pro mini board.

The isolated board left of the SSB is the AGC section. The receiver and transmitter shared parts follow, the TX band pass filter is in the foreground. The power transmitter is on the right behind the shield. The shield is necessary to avoid unwanted oscillations when rf is coming back from the power transmitter to the band pass filter prior to the tx section.

On the right there is the SMA socket for connecting the antenna plus a 3 pin header for connecting a headphone. When there is no headphone in use a jumper connects the internal speaker to the speaker line. VDD is applied via a standard DC connector.

The underside of the board has only some SMD components and the wiring on it:

DK7IH microsize QRP SSB transceiver ("Micro24") for 14 MHz - Underside
DK7IH microsize QRP SSB transceiver (“Micro24”) for 14 MHz – Underside

“On the air”

My longest distance achieved with this transceiver (after rebuilding it) has been R2DLS near Moscow who gave me a “59”-report. smile1 The antenna in use is, as always, a Deltaloop.

73 and thanks for reading this article!

Peter (DK7IH)

 

 

Improving the receiver for the 7MHz QRO transceiver

After having done lots of QSOs with the 7MHZ QRO transceiver I found that the receiver still had to be improved. The sensitivity was great, the sound also was but there were some difficulties when operating the radio during evening and night times because  some (minor) interference was audible. This symptom had been caused, as usual, by strong broadcast stations transmitting from 7200+ kHz 41m-band. Occasionally “Radio China International” and “Radio Romania International” were discernable. But cure was on the way: The transceiver has a modular concept. Based on this I decided to do a full reconstruction of the receiver module.

The mixer, which is the most crucial part in a 7MHz receiver was changed to an IC mixer using an SL6440 double balanced mixer (formerly produced by Plessey).

sl6440_mixer_ic

The mixer IC offers a very good IMD3 performance (30dBm maximum according to datasheet) plus some decibels (1 to say exactly!) of gain and thus is a good alternative to the dual-gate MOSFET I had used before.

As a special feature there is an input (pin 11) where a current can be applied that determines the overall mixer current. The higher this value is set, the better the IMD3 performance will be. Max. power dissipation for the IC is 1.2 watts but that will require a heatsink. I found that a resistor of 820Ω will lead to a current of 4mA (13V VDD) on pin 11 line and produces good performance without thermally stressing the IC. For optimized IMD3 performance the SL6440 ic should be run in balanced mode.

The receiver schematic in full:

DK7IH 7MHz QRO SSB transceiver - Receiver section (Version 2)
DK7IH 7MHz QRO SSB transceiver – Receiver section (Version 2) – Full size image

Circuit explanation

Input BPF

From the left we start with a two pole band filter for 7 MHz. LC coupling  again is very loose what reduces the receiver’s tendency to overload.

Receive mixer

Next is the SL6440 mixer ic. Input and output are equipped with broadband transformers (data see schematic, please!). The purpose is to convert an unbalanced signal to a balanced one and vice versa. According to the respective entry in data sheet running the mixer in balanced mode enhances performance. Pin11 is used to control the DC operating conditions of the mixer, a resistor (820Ω) sets appropriate  bias for mixer stage. The 3 diodes (1N4148) supply correct voltage to a pin that is called “VCC2” which should be slightly lower than VCC supplied to the output stage. 3 diodes in series produce the required voltage drop.

In experiments it has turned out the even when gain of the mixer ic is only about 1 dB the resulting output of the whole receiver is higher than that of its predecessor and taking into account that receiver generated noise is not a problem on the lower short wave bands, there is no rf preamplifier.

If you encounter birdies maybe the signal level of the VFO is too high. Then switching a smaller capacitor into the VFO signal feedline is the best idea.

Next stage is the filter switch that has been copied from the previous schematic.

Interfrequency amplifier

This stage contains the well-known MC1350 by Motorola. To simplify this section a minimum design has been chosen, Output is unbalanced and broadband. Input also. The only filtering in the whole interfrequency section is done by the SSB filter prior to the interfrequency amplifier. A 100uF capacitor in VDD line helps to suppress audio frequency feedback and self-oscillation in the receiver strip.

Product detector

As you might have realized the transceiver not longer is a “NE602 free zone”, because this mixer now serves as a product detector. A type of usage where the low IMD3 performance does not matter. The low pass filter by the end of the mixer must be chosen according to the user’s preferences concerning pitch and tone.

Audio amp section

Audio preamp is again an ic, the “antique” LM741. Negative feedback has been set to an amount that there is significant gain in this stage (R=330kΩ).

The audio final amp here has been equipped with the TBA820M integrated circuit, the smaller version of the 16 pin TBA820 integrated audio amplifier. The advantage of this ic compared to LM386 is lower distortion and the fact that this ic is not so prone to self-oscillate.

AGC

Automatic gain control nearly is the same like in the former version. The main difference is that MC1350 needs positive voltage to reduce amplifier gain. Thus the output has been punt into the emitter line. The problem when using an NPN transistor in such a circuit is that maximum voltage is limited to Vmax = VDD – VBE. As a consequence you can not get full 12V out when you supply 12V between C and E. Here this does not matter because AGC significantly reduces gain already when reaching 6 volts (Source: Datasheet):

mc1350_vagc_gain-function

Maximum gain reduction (>60dB) occurs between 6.7 and 7 volts.

Also a manual method to reduce gain has been applied. This is by chosing a voltage between 0 and 12V using a potentiometer. To prevent current flowing from the center of the potentiometer into the ADC input detecting the AGC voltage a silicon diode has been installed. To prevent a short circuit of the AGC voltage against GND when the potentiometer is at 0 position (delivering full gain in the MC1350 amp ic) the 5.6k resistor is used.

Changing time constant can be achieved by a second capacitor set in parallel (either by a switch or by microcontroller).

To protect the analog-digital-converter (ADC) in the microcontroller from excessive input voltage, this is limited to 5.1V by a zener diode.

Performance

The receiver is very sensitive. Reception is possible with the famous “wet finger” ;-). With a large antenna (full sized delta loop) no overload is detectable even during evening and night times. Noise is slightly higher compared to that with the MOSFET equipped receiver but very much acceptable for a 7MHz receiver.

73 and thanks again for watching!

Peter (DK7IH)

SSB Transceiver, 7MHz, 50 Watts, with Dual-DDS-System

DK7IH QRO SSB transceiver for 7MHz/40m
DK7IH QRO SSB transceiver for 7MHz/40m

In this paper we will discuss a single sideband amateur radio transmitter/receiver for the 40 meter band that has been designed to ensure  good performance characteristics with reasonable number of parts (no “overkill” in component use), particularly concerning the receiver. Circuit simplicity and over-average performance were to be combined.

The background: Some years ago I had built the ancestor of this transceiver and afterwards posted an incomplete series of articles (starting here). The transmitter was considered to be quite OK (I could even work a station from South Korea when operating as GJ/DK7IH some years ago) but the receiver was weak.

The shortcomings originated from the rf preamplifier I used together with the 1st mixer, an NE602. The latter had severe problems to cope with the high signal levels on the 40 meter band from out-of-band broadcast stations transmitting on the 41m band (f>7200kHz) or from very strong amateur stations transmitting in-band. This is caused by the technical specs of this Gilbert cell mixer. NE602 has been designed for mobile phone applications and not for shortwave radios. Its IMD 3 is only -15dBm whereas it is able to detect weak signals (-119dBm with an S/N ratio of 12 dB) according to datasheet. Due to this NE602 was excluded from being used at least in the receiver.

Another point was that the rig was too small and too densely packed to be called “service friendly”. Thus I dismantled the radio some times afterwards and had in mind rebuilding it with another receiver and a little bit more space inside.

The Basics

The project has had to meet certain requirements that I would like to point out first:

Frequency generation: Dual-DDS-System: AD9835 as local oscillator and AD9834 as VFO. ATMega644A as MCU (Download source code here)

Receiver: Single conversion superhet, 9 MHz interfrequency with commercial filter (supplied by http://box73.de) shared by transmitter and receiver and relay switched, “NE 602-free zone” ;-), 4 dual gate MOSFETs in rf preamp, rx mixer, if amplifier and product detector, audio stages with BC547 as preamp and LM386 as main audio amplifier.

Edit: I found that there was strong signal of self-reception around 7.100kHz which was not a spurious signal from one of the DDS. It has been a mixing product of one or two oscillators together with a signal from the microcontroller. So I changed the interfrequency to 10.7MHz which cured the problem. I tried to calculate the issue but was not succcesful because I do not know all the frequencies in the microcontroller. I think it is most probable that it is a harmonic of the PWM signal I use for controlling the LED front lights.

Transmitter: 4 stages, 3 of them in push-pull mode, Siemens made mixer IC S042P (really old fashioned, but still available) as DSB generator and TX mixer, rf amplifiers (2N2219A) after filter and tx mixer.

Design: Really “cool” with blue backlight. Sandwich built, not the size of a “micro transceiver”, but handy for travelling.

The Block Diagram

The diagram can be derived from the old project, it is nearly the same:

DK7IH QRO SSB transceiver for 7MHz/40m - Block diagram
DK7IH QRO SSB transceiver for 7MHz/40m – Block diagram

The basic outline of the radio is standard and should not be further discussed.

Dual DDS (VFO and Local Oscillator (LO))

This time I wanted to use 2 digital oscillators. The reason was just to have fun. 😉 Here is the schematic:

DK7IH QRO SSB transceiver for 7MHz/40m - Dual DDS (VFO and LO)
DK7IH QRO SSB transceiver for 7MHz/40m – Dual DDS (VFO and LO) – (Full sized image)

Microcontroller (MCU)

The source code has got about 2200 lines. With the GNU C compiler this leads to a HEX-file of about 43kB. Because of this the controller had to have a little bit of more memory. A “644” is a good choice here. It is clocked internally to 8 MHz clock rate. Radio and user data (user operated keys, S-Meter, TX PWR meter, temperature sensors attached to final transistors) is lead to the analog-digital-converter (ADC) of the MCU. Rotary encoder (optical) is fed into digital inputs. Integration of an RTC is projected but not done yet.

DDS1 (VFO)

Here an AD9834 is used. It is overclocked with 110MHz clock rate. For my receiver with a DDS chip purchased from Mouser this works without any abnormality. With a a chip from the “free market” (ebay) I found that there were strange clicks in the signal. So, I do not really recommend overclocking under any circumstance and/or not to such a high degree.

This DDS is is not terminated with a low pass filter. Due to the high clock rate there is no clock oscillator feedthrough which is supported by the  design of the following amplifier having an audio frequency transistor in the last stage (BC547 and later BCY59) that limits high frequency components due to its early gain decay in the frequency spectrum. The two stage amplifier has been designed for excellent linearity to prevent impurities in output spectrum.

mini43-qro-7mhz-dk7ih_12

The first peak showing the 16MHz signal and the next peak is the first harmonic about 30dB below. Other peaks are from local sources (PC, Printer).

The sine wave also looks quite OK:

mini43-qro-7mhz-dk7ih_13

DDS2 (LO)

This one contains an AD9835 synthesizer clocked to 50 MHz. An LPF here is mandatory. A simple but linear amplifier brings the signal up to 3Vpp which is OK for driving the dual gate MOSFET in the receiver. For the transmitter mixers this amount of voltage is too high, small capacitors reduce the voltage to an acceptable value.

LCD

From another project that I once had built and that is not more in use, a dive computer, I had a 4 lines/20 characters text display that is fairly large. This was to be designated as the LCD for this transceiver.

The Receiver

Building a receiver for the 7MHz amateur band is challenging. On one hand the circuit should be very sensitive for weak signal reception, particularly during day when the band conditions are low due to solar radiation and density of the D-layer. This means the receiver should have a higher gain whereas noise figure does not play a predominant role due to band characteristics with high atmospheric noise on 7MHz.

Next request is high dynamic range to eliminate the spurious signals that occur when front end stages are loaded with high input signal levels.

And last but not least AGC control range should be as wide as possible to cope with weak and very strong signals without the request to intervene by adapting manual gain control. For this a preamp also benefits.

Active mixers like the NE602 show low performance under these conditions. Some high-current mixers like the SL6440 exist, but there are alternatives. On one hand the classical diode ring mixer might come into perspective, otherwise Dual-Gate MOSFETs are well known as having a fairly good ability to cope with high signal levels and so don’t tend to  deteriorating the receiver’s performance severely. Besides they offer some gain and low noise figure (which has not been the main objective in this case) and the circuit is very compact and therefore it was the best choice for a receiver that had been intended to be constructed onto a board of 6 x 8 centimeters.

After these thoughts the following circuit turned out to be the right onset for a receiver inside the projected rig.

DK7IH QRO SSB transceiver for 7MHz/40m - The Receiver
DK7IH QRO SSB transceiver for 7MHz/40m- The Receiver (full sized image)

Circuit explanation (Receiver)

Front end

On the left we start with a 2 pole LC band pass filter for 7 MHz. The coils are wound on TOKO style coil formers (5.5mm size), winding data and parallel capacitors are given in the drawing. The coupling capacitor (2.7pF) between the two LC circuits is very small for such a low frequency. This makes the filter response curve sharper but leads to a slight weakening of the signal coming through the filter. But as the whole receiver has plenty of gain and a very good noise figure, this is the reason why  some weakening of the input signal is acceptable.

Preamplifier

Next is the preamplifier for the received band. It is connected to the AGC chain. You can expect some 25 to 30dB  gain swing by driving up gate 2 of the dual gate MOSFET from 0 V to 6V. A 1:1 voltage divider decrease the 0..12V AGC voltage to 0..6 V where th3N205 MOSFET is close to amplify with maximum gain. Exceeding 6 to 7 volts does not result in significant more gain swing, so I usually drive the MOSFET from 0 to 6.5 volts UG2 (with 13 Volts of supplied voltage.

3n205-ug2-gain-figure
UG2->Gain-Function 3N205 (Source: Datasheet)

The coupling when going from the preamplifier to the receiver mixer is in broadband style. The 3N205 has a very high gain and tends to self-oscillate. A second LC circuit makes the device more prone to going self-resonant and hence produce unwanted signals.

RX mixer

This mixer is very simple and needs only a few components. Both signals are fed into the gates of the dual gate MOSFET. Rf goes to gate 1 whereas gate 2 (the AGC input) is fed with the oscillator signal). Gate voltage depends on the voltage drop at the source resistor and therefore is stabilized. The oscillator signal should be in the range of 2 to 3 volts rf (pp) for a dual gate MOSFET. Lower values will deteriorate the performance of the mixer, e. g. its dynamic range. This signal switches the semiconductor and a superposition of the two signals occurs thus leading to the production of sum and difference of the original frequencies. These signals are fed into…

The SSB filter

which is a commercial one (Supplier box.73.de). The reason why I don’t ladder filters anymore is that I found it extremely difficult (not to say impossible) to get a symmetric filter response curve thus making the lower and upper sideband of the receiver sounding different even when the carrier frequency has been adjusted very thoroughly.

The filter is used for the SSB transmitter as well. To ensure maximum signal separation between the two branches (tx and rx) and between filter input and output I again us a high quality rf relay made by Teledyne. When choosing a relay intercontact capacitance  is crucial. It should (if possible) be < 1 pF.

Don’t forget a clamp diode to VDD over the relay coil to eliminate high voltage voltage peaks generated by self inductance when the coil is switched off. Voltages up to 100 Volts can occur. This might damage the transmit-receive section of this transceiver that is equipped with semiconductors only and does not use a relay.

IF amplifier

This circuit is the same like that of the rf preamp. It also is part of the AGC chain, thus delivering another 25 to 30 dBs of gain swing so that overall gain swing is around 50 to 60dB. In practical research over a long period of observation I found that with an antenna delivering high signal voltage (Delta loop) it was not possible to overdrive the receiver  to a level where signal distortion was audible.

A tuned circuit is also placed here to increase gain. Tuned amplifiers usually have higher gain than broadband ones. It is highly recommended to ground the metal cans of the coil to prevent any self-oscillation. But as I found out, this amplifier is not very prone to go to self-oscillation state.

Product detector

Here again a dual gate MOSFET is used. The circuit is nearly the same like the RX mixer except from the output section. We can see a low pass filter here, consisting of 2 Cs (0.1uF) and a resistor (1k). You can use a radio frequency choke instead, 1mH is recommended.

Audio amplifier

This section consists of two parts, a preamp (with bipolar BC547) and a final amplifier (LM386 IC). It is well-known that this IC tends to oscillate. One measure to prevent this is to keep leads short, switch a low-pass filter (capacitor 100uF and R=33Ω) into the VDD line and to reduce the gain capacitor between pins 1 and 8 to a degree where self-oscillations terminate.

A switching transistor cuts off the audio line by short circuiting it when on transmit. This eliminates any noise when switching. The rx/tx switch now is 100% “click free”. A very pleasant way of operation. 😉

AGC

This is another re-use of a circuit I have frequently used before. It is desired to reduce its output voltage down to 0 volts when a more or less strong af signals appear at the input. The agc voltage is derived from the audio signal of the receiver. Some say that this is not the best choice because you need more time (an af cycle last much longer as an rf cycle) for the waveform to generate the regulating DC voltage.

Nonetheless I have never observed popping or unpleasant noise from incoming very strong signals. The agc response rate is so fast that you won’t notice that it just has regulated even when a strong signal comes in. Only with very, very strong signals a slight “plopp” sound is observable but it is not unpleasant.

A second capacitor can be switched in parallel to the 33uF one. This can either be done by a transistor switch (like shown in the schematic) that in this case is controlled by an output PIN of the MCU. An alternative that I found later is to use the MCU pin directly to switch the cap. When not using the additional cap you must switch the pin as an input so that there is no positive voltage from the pin to the circuit. When you intend to ground the transistor (agc in “slow” position) then the pin mus be set as output by defining the DDR-register respectively AND the pin must be set to 0. So you can get rid of the switching transistor.

Another possibility would be to derive the agc from the interfrequency signal. The problem that occurs in this case is that you have to decouple the local oscillator (bfo) very carefully from the place where agc circuit is placed. Otherwise you are at risk to detect the bfo signal by the agc which leads to reduced response range in the agc. In addition this receiver uses a higher rf voltage level for the mixers (2 to 3 Vpp each). By this the amount of stray energy is higher inside the circuit and thus this rf energy might be detected very early by the agc.

In the emitter line there is a resistor (68Ω) which produces a voltage drop when the transistor is driven. This is fed into the ADC of the microcontroller driving the S-meter display part.

The Transmitter

First the circuit:

DK7IH QRO SSB transceiver for 7MHz/40m - The Transmitter
DK7IH QRO SSB transceiver for 7MHz/40m – The Transmitter (Full sized image)

Microphone amplifier

This amplifier is a simple common-emitter circuit with the directly grounded emitter of the BC547 transistor. This circuit is linear only for low input voltages but suitable for the connected dynamic microphone since this does not produce more than some millivolts of audio energy. Bias comes from the 390kΩ resistor. At the input you find a 2.2nF capacitor from base to GND which helps to prevent coupling in rf energy from the transmitter to the audio stage and thus leading to an impure signal.

The DSB generator + amplifier

The amplified microphone signal is used to produce a double-sideband signal. The ic I use here is an antique but still available part by German manufacturer Siemens, the S042P. It includes a so-called “Gilbert-cell” mixer and an oscillator but the latter is not used here (Datasheet Application note (in German)).

The S042P mixer needs some more components compared to the well-known NE602 integrated circuit but fewer ones than the MC1496. It is designed for 12V usage, thus no voltage regulation is required.The ic can be applied in balanced mode or non-symmetrical. To save components I use the unbalanced circuit alternative. A slight loss in output power is acceptable in this case, there are amplifiers post each mixer in this transmitter.

Ic gain is about 16.5 dB, DC current is about 3 mA.

A crucial point is the signal level of the local oscillator. S042P needs only some hundred  millivolts of oscillator voltage. To prevent overdriving I experimented with different values of the coupling capacitor. 5.6pF seemed best because the LO produces some volts peak-to-peak.

Following there is an amplifier that is a standard circuit and has been tuned for maximum linearity in order to reduce distortion to a minimum (which is also true for the following stages). You can see the well understood 2 master ways of achieving max. linearity in an amplifier stage:

  • Negative feedback between collector and base (i)
  • Emmitter degeneration (II)

Explanation:

i) The first measure goes along with the 2.7kΩ resistor between collector and base of the transistor. This resistor provides positive dc bias voltage to the base and leads 90° out-of-phase ac voltage to the transistor’s input. This reduces gain and therefore distortion. But due to the fact that the whole transmitter strip has plenty of gain, this loss in gain is not a serious problem.

ii) The 10Ω resistor in the emmitter line is not bypassed by a capacitor. This stabilizes the circuit. When the current through transistor increases the emmitter voltage will rise (according to Ohm’s law) and the voltage between collector and emmitter drops. This reduces voltage difference between base and emmitter and hence also reduces gain.

The coupling to the next stage is done by a capacitor of 0.1uF. This causes some impedance mismatch. But that is as well not a big problem because the gain reduction here helps to prevent the whole transmitter from unwanted oscillations by diminishing overall gain.

TX mixer

Here the second S042P is used. The 9 MHz SSB signal is coupled to pin 13 of the ic, a DC connection is established to pin 11. These two pins represent the base connectors for the two current control transistors and should be bridged by a DC resistor in this circuit.

The 150Ω resistor from pin 10 and pin 12 to GND defines the gain of the mixer. Here you can use down to 150Ω but should have a resistor towards VDD to limit current and avoid excessive heating of the device. In this case another 150Ω is used.

VFO signal is coupled symmetrically to pins 7 and 8 via a small trifilar toroid. See schematic for details and please note that center tap is not used here. This is in contrast to the output transformer where the tap is used to feed supply voltage into the mixer.

Another 7 MHz band pass filter terminates the mixer, data for coils and capacitors is in the schematic.

Power amplifier

This amplifier has got 4 stages and except from the first one all are in push-pull mode. The power distribution for these 4 stages is as follows:

Stage Power
Preamp 5mW
Predriver 200mW
Driver 2.5 W
Final amp 50W

Preamplifier

The first of the 4 power stages is the same as the post dsb generator amplifier so there is not more to add concerning this stage. Rf energy is taken out via a transformer with a primary and a tapped secondary winding. This is to provide the balanced structure necessary for the following push-pull stage.

Prediver

This is the first push-pull stage. Its bias is derived from a voltage divider connected to the tap of the input transformer.

Please note: In contrary to the schematic I have installed 2 devices of the 2SC1973 type because the signal turned out to be much purer with these ones on the spectrum analyzer.

A tapped output transformer feeds the amplified rf energy to next board. Output impedance is 50Ω. The coupling to next stage then is done via a shielded cable of (nearly) the same impedance.

Driver stage

This one has an input transformer also center tapped. The tap goes to a bias network consisting of a current limiting resistor (1kΩ), two diodes forming the lower part of a voltage divider and some capacitors as part of a low pass filter to avoid coupling in of radio frequency (rf) energy. The two diodes must be thermally connected to the cases of the transistors. In case these heat up, the diode increases its conductivity thus reducing its resistance. The bias voltage drops and heating is stopped. So, thermal runaway is prevented.

For these two stages (predriver and driver) DC is fed through low pass filter (RFC and 2 caps 0.1uF) to prevent coupling of rf energy via the VDD line.

Final stage

This stage receives input from a balanced structure without a center fed transformer. Instead bias current is linked in via a network of radio frequency chokes and two resistors of 5.1Ω each.

Bias is provided by a current regulating transistor and should be set to about 100mA.

The MRF455 transistors are mounted directly to the aluminium structure of the sheet metal carrying the whole transceiver boards. When mounting them to the Veroboard I did not solder them directly. I used 1.6mm screws and washers to press the brass connectors to the copper strips of the amplifier board:

DK7IH QRO SSB transceiver for 7MHz/40m - Power amplifier underside
DK7IH QRO SSB transceiver for 7MHz/40m – Power amplifier underside

With this I could have been able to remove the precious transistors without having to unsolder them when the device might have turned out to be a failure. But it was not, thank God!

The output transformer is the one I have used in my old 14MHz PA and the ancestor of this radio. It is from an old ATLAS 215 transceiver and I hope that this will be the final place for the transformer.

Two temperature sensors (KTY-81-210) have been installed to measure the temperature of each transistor. They are connected to the microcontroller via voltage dividers (see schematic, please!)

Low Pass Filter and Power Measurement Unit

For the low pass filter I use 2 toroids T50-2. These might appear small but from one source (that I have forgotten) I remember to have found that for 50 watts of power this core is still suffice. Metal powder cores can stand much more power compared with same sized ferrite toroids.

The power measurement unit consists of a network that starts with a resistor of 12kΩ to ensure a significant voltage drop in signal level, then two rectifier diodes (1N1418 or equivalent) follow, some low pass filtering eliminating the last rf energy and the resulting direct current voltage is fed to a variable resistor to set an adequate voltage level for the ADC in the microcontroller.

The rf output made out of a two-tone audio signal measured at the antenna connector:

DK7IH QRO SSB transceiver for 7MHz/40m - Two tone signal, power about 57 watts, close to overdrive
DK7IH QRO SSB transceiver for 7MHz/40m – Two tone signal, power about 57 watts, close to overdrive

The spectroscopical analysis shows the signal on the f -> V figure:

DK7IH QRO SSB transceiver for 7MHz/40m - Output spectrom with max. Pout (>50W PEP)
DK7IH QRO SSB transceiver for 7MHz/40m – Output spectrum with max. Pout (>50W PEP)

RX/TX-switching

A very simple circuit. Two PNP power transistors are used but they don’t have that much to do. They are only designed for switching the low-power parts of the radio. The high current to the drivers and final amplifiers is permanently present in the collector lines but the bias lines are tx/rx-switched and go to 0V during receive periods. This reduces requirements for the power rating of the switch board.

DK7IH QRO SSB transceiver for 7MHz/40m - RX/TX switch board.
DK7IH QRO SSB transceiver for 7MHz/40m – RX/TX switch board.

When pushing the PTT the base of the lower transistor is pulled to GND. So it becomes conductive and TX DC is applied. Via the diode the upper transistor loses its negative voltage and becomes non-conductive.

Construction

The Backlight

One interesting thing was the blue backlight to illuminate the front panel controls. It is made using SMD LEDs that are soldered to small pieces of Veroboard and fixed with 2-component glue to transparent light-scattering plastic bought from a local supplier for architects and designers. This material is used for making models of houses and stuff like that. As light distributor this material is excellent. The LEDs are powered by a linear transistor connected to the pulse width modulation (PWM) output of the microcontroller so that light intensity is adjustable.

Hint: When programming the PWM functions it might occur that PWM frequency is audible in the receiver. If something like that occurs another frequency can be selected without changing the performance as soon as it is high enough that human eyes aren’t able to recognize a flickering.

DK7IH QRO SSB transceiver for 7MHz/40m
DK7IH QRO SSB transceiver for 7MHz/40m

The covers used for the labels and the LCD shield are made from 2mm acrylic and fixed with screws of 1.6 respective 2mm diameter.

The two push-buttons right in top position consist of two bars of acrylic (4.2mm diameter) and are having mechanical contact to small spring-loaded switches behind the front panel:

mini43-qro-7mhz-dk7ih_16

Directly under these acrylic bars there are two LEDs shining into these rods and because of total reflection inside the tubing the optic conductor is sending the light to the front side when the LEDs are powered on. That is how it looks at night:

mini43-qro-7mhz-dk7ih_19

 

General setup

This is a sandwich construction again. On the first side there is the DDS  board (left), the receiver (center) TX mixer and preamplifier (right) and the SSB generator (back). Also there is a 5 lead connector holding the 5 ISP lines (MOSI, MISO, CLK, RESET and GND). This makes firmware updates easy because you don’t have to open the case when you want to update software.

DK7IH QRO SSB transceiver for 7MHz/40m - DDS, RX, TX mixer and SSB generator
DK7IH QRO SSB transceiver for 7MHz/40m – DDS, RX, TX mixer and SSB generator

The other side holds the TX low pass filter plus power measurement unit (left), the power amplifier (center) and the predriver and driver (right). In the back you can see the rx/tx switch board:

DK7IH QRO SSB transceiver for 7MHz/40m - TX LPF, PA, Drivers, RX/TX switch board.
DK7IH QRO SSB transceiver for 7MHz/40m – TX LPF, PA, Drivers, RX/TX switch board.

“On the air”

Again big fun this transceiver! During the ARRL DX contest last weekend I could work some statesiders. With Delta Loop and 50 watts, fairly OK. Working Europe all day is no problem with 50 watts.

During the first QSOs I had reports that the audio sounded clear but somehow “narrow”. I had used an electret mike that time and could not use a dynamic one because the preamplifier following the microphone did not have enough gain. Then, to solve this problem, I decided to do a full reconstruction of the SSB generator board. The one then had used had an AN612 mixer integrated circuit (from an old CB radio). This one was dismantled and replaced by the S042P board. The change took me 3 hours to develop and solder but it paid. I use a Motorola dynamic microphone now that has a very rich and clean sound. I monitored it on a web based SDR receiver, made a recording and found it to be OK.

OK, dear fellow hams, that’s the story so far, some supplements will sure be made, so stay tuned!

Thanks for reading and vy 73 de

Peter (DK7IH)

Interfacing an LCD12864 (ST7920 controller) to a microcontroller

I am currently planning a new all band transceiver that might be a little larger than the “Microtransceivers” I built this  year. For this project I bought one of those larger graphic display modules that are sold from incountable Chinese vendors:

LCD-12864-ST7920
LCD-12864-ST7920

Unfortunately I did not find any “non-Arduino” modules on the web to meet my requirements. So I decided to write my own code (mainly as an academic exercise 😉 ) in standard C language for the AVR controller family. You will find this source code by the end of this paper.

General aspects to know

What to do first: RTFM! Data sheets for this module are widely available and I strongly recommend reading one of them. The problem is: You can get ones that don’t cover even the minimum you must know to get the thing working. Other are more suitable (Example). Read them before you start! At least once and by skimming. I will only refer to the things that I think are not clear in the data sheets or are controversial between the various versions of the papers.

Hardware: These LCD modules have 2 controllers of the ST7920-type inside. The screen is 128 pixels wide and 64 pixels high. The modules have built-in ROM-based standard character sets like they are familiar from the well-known line oriented modules like the 16×2 ones. But there is also the possibility to drive them in full graphics mode with your own fonts to be used.

The LCD module communicates via 8- or 4-bit parallel mode or a serial “SPI” derivate.

Connectors

The LCD12864 is wired to the rest of the circuit by a 20-pin header strip in 2.54mm  (0.1″) spacing. The pins are

  • VSS (0V or “minus”)
  • VDD (2.7 to 5.5V, “plus”)
  • VO (a contrast set but without function with my module)
  • RS (determining if there is data (pin to VDD i. e. “hi”) or an instruction (pin to VSS i. e. “low”) transferred)
  • RW (“low” when writing data, “high” when reading data from the module)
  • E (the “enabled” pin that goes high when a byte of data is transferred)
  • D0:D7: The parallel data lines
  • PSB (set “low” if you project serial communication or “high” if you project parallel communication (4- or 8 bit bus width  possible))
  • NC: no connection
  • RST: The reset pin, must be pulled to GND for 1 ms or so and then set to high to reset the module when program starts
  • VOUT: A reference voltage but not used in my project
  • BLA: Backlight, connected to +12 V via R=270Ohm
  • BLK: Backlight cathode, connected to GND

If you want to run my software, connect the microcontroller to the LCD module as follows:

  • LCD-Data: PD0..PD7
  • RS: PC0
  • RW: PC1
  • E: PC2
  • RST PC3
  • PSB: GND
  • BLA to +12V via R=270 Ohms
  • BLK to GND

Driving the module by software

In my software I use 8 bit parallel transfer because it is the fastest way to get the data displayed. I use the full PORTD of the microcontroller for this purpose. In general this LCD module is something for the “bigger” controllers if you intend to do something more than just displaying funny messages. 😉

Initializing

Before the first data can be displayed the module must be reset and subsequently initialized:

//Init LCD
void lcd_init(void)
{ 
    //Reset
    PORTC &= ~(8);
    _delay_ms(1);
    PORTC |= 8;
    _delay_ms(40);

    lcd_write(0, 0x30); //Use 8-bit mode parallel
    _delay_ms(1);

    lcd_write(0, 0x0C); //All on Cursor on, Blink on , Display on
    _delay_ms(1);

    lcd_write(0, 0x01); //Perform CLS in text mode to eliminate random chars from screen
    _delay_ms(20);

    lcd_write(0, 0x34); //Switch to extended mode, redefine function set
    _delay_us(100);

    lcd_write(0, 0x36); //Add graphic mode
    _delay_us(100);

    lcd_write(0, 0x12); //Display control and display ON
    _delay_us(100);
}

I found that getting the module ready for work correctly is not easy. I encountered the problem that I still had some random characters of the text mode on my graphics screen after having switched the LCD to graphics mode. To solve this problem I did the initialization procedure in the following manner:

  1. Set 8-Bit parallel mode first,
  2. Switch the module to a standard text module behavior,
  3. Clear the screen,
  4. Switch the module to extended and graphics mode and perform the remaining initialization process.
  5. Clear screen once again and you are “ready for take off”.

After you have switched to the “Extended instruction set” you can access the pixels of the module individually.

How to set and reset pixels in graphics mode

The data sheets I browsed through concerning this aspect were not concise. Following is the correct addressing mode for my module:

GDRAM (Graphics RAM) organisation of LCD12864 (ST7920)
GDRAM (Graphics RAM) organisation of LCD12864 (ST7920)

You can see 16 banks each 16 pixels wide and 32 pixels high. 16 pixels are treated as one integer, consisting of MSB (bit 15:8) and LSB (bit 7:0). They are read from the left side (MSB) to the right end (LSB).

If you want to set a 16 bit section, you must specify the graphics address of the line you want to set. This address is stored in the so-called “GDRAM” (graphics data ram).

X coordinate is the bank starting with no. 0 to no. 15 max. Y coordinate is a number between 0:31 referring to the respective line that shall be set.

Hint: If you are about to access the lower part of the screen, you must use a “bank” numbered >= 8. To prevent the screen from looking “broken” it is necessary to set row back to a value diminished by 0x20 (32 dec.) and the “bank” increased by the value of 0x08 (8 dec.).

if(row & 0x20) //Enter lower part of screen => go to next page bank
{
    row &= ~0x20;
    col |= 8;
}

After having written an address to the module the address counter automatically increases by 1 for the horizontal part of the address. The vertical address remains unchanged. This increment for the X direction can be compensated by defining the specified GDRAM address each time you project to set or reset a 16-bit line of data.

You will totally have to transfer 4 bytes to set one line of 16 pixels:

1. Set vertical address(Y) for GDRAM
2. Set horizontal address(X) for GDRAM
3. Write D15:D8 to GDRAM (first byte)
4. Write D7:D0 to GDRAM (second byte)

(excerpt from data sheet)

Setting GDRAM address

Before you can write data to a GDRAM cell you must state where this data shall be displayed. Therefore you specify the GDRAM (here “DDRAM”) address:

LCD12864_set_CDRAM_adr

DB7 is set to one, so 80H (0x80) has to be added to the address value for each writing.

Here is the software code for this procedure:

//Set address
lcd_write(0, 0x80 + row);  //”0″ means “instruction”
lcd_write(0, 0x80 + col);
lcd_write(1, msb); //”1″ means “data”
lcd_write(1, lsb);

Hint: If you use a font that is only 8 bits wide you will have to cope with the following problem: When writing one character (8 bits wide) you will override the other 8 bits that might already be on the screen because you can only address 16 bits at once. So, before you write a character to the LCD, you have to read the GDRAM for the other half of the character and store this value. Later you assemble both characters (old one and new one) to a complete 16-bit value and write this data back to the LCD. You also have to find out if the new char is the left or the right one of a 16-bit cell.

This works out as:

//Set address
lcd_write(0, 0x80 + row + t1);
lcd_write(0, 0x80 + col);

//Get old values of 2 GDRAM bytes 
v1 = lcd_read(1); //Dummy read required!
v1 = lcd_read(1);
v2 = lcd_read(1);

//Set address
lcd_write(0, 0x80 + row);
lcd_write(0, 0x80 + col);

if(!inv) //Char normal or inverted?
{
    ch = font[ch0 * FONTHEIGHT + t1];
}
else 
{
     ch = ~font[ch0 * FONTHEIGHT + t1];
}

if(odd) //"Odd" or "even" position in 16 bit integer?
{ 
    //Write data on RIGHT side of existing character
    lcd_write(1, v1);
    lcd_write(1, ch);
}
else 
{ 
    //Write data on LEFT side of existing character
    lcd_write(1, ch);
    lcd_write(1, v2);
}

This only prints out one line of the char, please see the full code attached to this article to get the full information for printing the full character to the LCD screen!

How to use my software

I always put all of my code into one C-file because I cannot post ZIP-files with header-files etc. here. Sorry for that!

There are various functions that you can use to write text or data to the screen:

void lcd_putchar(int, int, unsigned char, int);
void lcd_putchar2(int, int, unsigned char, int);
void lcd_putchar3(int, int, unsigned char, int);

  • lcd_putchar() prints a character defined in the font in normal size. If you want to invert the character looking, set the last “int” to 1.
  • lcd_putchar2() produces the same character in double height.
  • lcd_putchar3() set a character in double height and double width.

void lcd_putstring_a(int, int, char*, int, int);
void lcd_putstring_b(int, int, char*, int);

  • lcd_putstring_a() prints a “0”-terminated string to a given position (row, col, string, height (0==normal, 1==double) and inverted.
  • lcd_putstring_b() does the same in double height and double width.

First three parameters are always column, row and data. Following additional information about size, character inverted printing etc. You will find that in the code by the end of this paper.

The most complex function is

  • void lcd_putnumber(int, int, long, int, int, int);

which converts a number to a string and subsequently displays it. Parameters are

void lcd_putnumber(col, row, number, decimal, size, invert);

“Number” can be a long variable or an integer or even char. “Decimal” sets the decimal separator (counted from the right) if wanted, if not set this parameter to “-1”, “Size” is 0 for normal and 1 for double height. “Invert” = 1 for inverted and 0 for normal appearance.

The full software

I apologize for the ugly looking code but the web based “beautifier” and highlighter destroyed my indenting to a max. ! 😦

So, if there are still questions, feel free to mail me: peter(dot)rachow(ät)web(dot)de!

73 de Peter

/*****************************************************************/
/*           LCD12864-ST7920-Demo with ATMega32                  */
/*  ************************************************************ */
/*  Mikrocontroller:  ATMEL AVR ATmega32, 8 MHz                  */
/*                                                               */
/*  Compiler:         GCC (GNU AVR C-Compiler)                   */
/*  Author:           Peter Rachow (DK7IH)                       */
/*  Letzte Aenderung: 2018-12-25                                 */
/*****************************************************************/

// O U T P U T for LCD 

//Connection LCD to uC:
//LCD-Data: PD0..PD7
//RS:       PC0
//RW:       PC1
//E:        PC2
//RST       PC3

#define F_CPU 8000000
#define FONTHEIGHT 8

#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/eeprom.h>
#include <util/delay.h>

int main(void);

void lcd_write(char, unsigned char);
char lcd_read(char);
void set_rs(char);
void set_e(char);
void set_rw(char);
int is_lcd_busy(void);
void lcd_init(void);
void lcd_cls(void);
void lcd_putchar(int, int, unsigned char, int);
void lcd_putchar2(int, int, unsigned char, int);
void lcd_putchar3(int, int, unsigned char, int);
void lcd_putstring_a(int, int, char*, int, int);
void lcd_putstring_b(int, int, char*, int);
void lcd_putnumber(int, int, long, int, int, int);

//STRING FUNCTIONS
int int2asc(long, int, char*, int);

//Font for graphics LCD 5x8
unsigned char font[] =
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	// 0x00
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	// 0x01
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	// 0x02
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	// 0x03
0x00,0x04,0x0E,0x1F,0x1F,0x0E,0x04,0x00,	// 0x04
0x04,0x0E,0x0E,0x04,0x1F,0x1F,0x04,0x00,	// 0x05
0x00,0x04,0x0E,0x1F,0x1F,0x04,0x0E,0x00,	// 0x06
0x0E,0x1F,0x15,0x1F,0x11,0x1F,0x0E,0x00,	// 0x07
0x0E,0x11,0x1B,0x11,0x15,0x11,0x0E,0x00,	// 0x08
0x00,0x0A,0x1F,0x1F,0x1F,0x0E,0x04,0x00,	// 0x09
0x0E,0x11,0x1B,0x11,0x15,0x11,0x0E,0x00,	// 0x0A
0x00,0x07,0x03,0x0D,0x12,0x12,0x0C,0x00,	// 0x0B
0x0E,0x11,0x11,0x0E,0x04,0x0E,0x04,0x00,	// 0x0C
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	// 0x0D
0x03,0x0D,0x0B,0x0D,0x0B,0x1B,0x18,0x00,	// 0x0E
0x00,0x15,0x0E,0x1B,0x0E,0x15,0x00,0x00,	// 0x0F
0x08,0x0C,0x0E,0x0F,0x0E,0x0C,0x08,0x00,	// 0x10
0x02,0x06,0x0E,0x1E,0x0E,0x06,0x02,0x00,	// 0x11
0x04,0x0E,0x1F,0x04,0x1F,0x0E,0x04,0x00,	// 0x12
0x0A,0x0A,0x0A,0x0A,0x0A,0x00,0x0A,0x00,	// 0x13
0x0F,0x15,0x15,0x0D,0x05,0x05,0x05,0x00,	// 0x14
0x0E,0x11,0x0C,0x0A,0x06,0x11,0x0E,0x00,	// 0x15
0x00,0x00,0x00,0x00,0x00,0x1E,0x1E,0x00,	// 0x16
0x04,0x0E,0x1F,0x04,0x1F,0x0E,0x04,0x0E,	// 0x17
0x04,0x0E,0x1F,0x04,0x04,0x04,0x04,0x00,	// 0x18
0x04,0x04,0x04,0x04,0x1F,0x0E,0x04,0x00,	// 0x19
0x00,0x04,0x06,0x1F,0x06,0x04,0x00,0x00,	// 0x1A
0x00,0x04,0x0C,0x1F,0x0C,0x04,0x00,0x00,	// 0x1B
0x00,0x00,0x00,0x10,0x10,0x10,0x1F,0x00,	// 0x1C
0x00,0x0A,0x0A,0x1F,0x0A,0x0A,0x00,0x00,	// 0x1D
0x04,0x04,0x0E,0x0E,0x1F,0x1F,0x00,0x00,	// 0x1E
0x1F,0x1F,0x0E,0x0E,0x04,0x04,0x00,0x00,	// 0x1F
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	// 0x20
0x04,0x0E,0x0E,0x04,0x04,0x00,0x04,0x00,	// 0x21
0x1B,0x1B,0x12,0x00,0x00,0x00,0x00,0x00,	// 0x22
0x00,0x0A,0x1F,0x0A,0x0A,0x1F,0x0A,0x00,	// 0x23
0x08,0x0E,0x10,0x0C,0x02,0x1C,0x04,0x00,	// 0x24
0x19,0x19,0x02,0x04,0x08,0x13,0x13,0x00,	// 0x25
0x08,0x14,0x14,0x08,0x15,0x12,0x0D,0x00,	// 0x26
0x0C,0x0C,0x08,0x00,0x00,0x00,0x00,0x00,	// 0x27
0x04,0x08,0x08,0x08,0x08,0x08,0x04,0x00,	// 0x28
0x08,0x04,0x04,0x04,0x04,0x04,0x08,0x00,	// 0x29
0x00,0x0A,0x0E,0x1F,0x0E,0x0A,0x00,0x00,	// 0x2A
0x00,0x04,0x04,0x1F,0x04,0x04,0x00,0x00,	// 0x2B
0x00,0x00,0x00,0x00,0x00,0x0C,0x0C,0x08,	// 0x2C
0x00,0x00,0x00,0x1F,0x00,0x00,0x00,0x00,	// 0x2D
0x00,0x00,0x00,0x00,0x00,0x0C,0x0C,0x00,	// 0x2E
0x00,0x01,0x02,0x04,0x08,0x10,0x00,0x00,	// 0x2F
0x0E,0x11,0x13,0x15,0x19,0x11,0x0E,0x00,	// 0x30
0x04,0x0C,0x04,0x04,0x04,0x04,0x0E,0x00,	// 0x31
0x0E,0x11,0x01,0x06,0x08,0x10,0x1F,0x00,	// 0x32
0x0E,0x11,0x01,0x0E,0x01,0x11,0x0E,0x00,	// 0x33
0x02,0x06,0x0A,0x12,0x1F,0x02,0x02,0x00,	// 0x34
0x1F,0x10,0x10,0x1E,0x01,0x11,0x0E,0x00,	// 0x35
0x06,0x08,0x10,0x1E,0x11,0x11,0x0E,0x00,	// 0x36
0x1F,0x01,0x02,0x04,0x08,0x08,0x08,0x00,	// 0x37
0x0E,0x11,0x11,0x0E,0x11,0x11,0x0E,0x00,	// 0x38
0x0E,0x11,0x11,0x0F,0x01,0x02,0x0C,0x00,	// 0x39
0x00,0x00,0x0C,0x0C,0x00,0x0C,0x0C,0x00,	// 0x3A
0x00,0x00,0x0C,0x0C,0x00,0x0C,0x0C,0x08,	// 0x3B
0x02,0x04,0x08,0x10,0x08,0x04,0x02,0x00,	// 0x3C
0x00,0x00,0x1F,0x00,0x00,0x1F,0x00,0x00,	// 0x3D
0x08,0x04,0x02,0x01,0x02,0x04,0x08,0x00,	// 0x3E
0x0E,0x11,0x01,0x06,0x04,0x00,0x04,0x00,	// 0x3F
0x0E,0x11,0x17,0x15,0x17,0x10,0x0E,0x00,	// 0x40
0x0E,0x11,0x11,0x11,0x1F,0x11,0x11,0x00,	// 0x41
0x1E,0x11,0x11,0x1E,0x11,0x11,0x1E,0x00,	// 0x42
0x0E,0x11,0x10,0x10,0x10,0x11,0x0E,0x00,	// 0x43
0x1E,0x11,0x11,0x11,0x11,0x11,0x1E,0x00,	// 0x44
0x1F,0x10,0x10,0x1E,0x10,0x10,0x1F,0x00,	// 0x45
0x1F,0x10,0x10,0x1E,0x10,0x10,0x10,0x00,	// 0x46
0x0E,0x11,0x10,0x17,0x11,0x11,0x0F,0x00,	// 0x47
0x11,0x11,0x11,0x1F,0x11,0x11,0x11,0x00,	// 0x48
0x0E,0x04,0x04,0x04,0x04,0x04,0x0E,0x00,	// 0x49
0x01,0x01,0x01,0x01,0x11,0x11,0x0E,0x00,	// 0x4A
0x11,0x12,0x14,0x18,0x14,0x12,0x11,0x00,	// 0x4B
0x10,0x10,0x10,0x10,0x10,0x10,0x1F,0x00,	// 0x4C
0x11,0x1B,0x15,0x11,0x11,0x11,0x11,0x00,	// 0x4D
0x11,0x19,0x15,0x13,0x11,0x11,0x11,0x00,	// 0x4E
0x0E,0x11,0x11,0x11,0x11,0x11,0x0E,0x00,	// 0x4F
0x1E,0x11,0x11,0x1E,0x10,0x10,0x10,0x00,	// 0x50
0x0E,0x11,0x11,0x11,0x15,0x12,0x0D,0x00,	// 0x51
0x1E,0x11,0x11,0x1E,0x12,0x11,0x11,0x00,	// 0x52
0x0E,0x11,0x10,0x0E,0x01,0x11,0x0E,0x00,	// 0x53
0x1F,0x04,0x04,0x04,0x04,0x04,0x04,0x00,	// 0x54
0x11,0x11,0x11,0x11,0x11,0x11,0x0E,0x00,	// 0x55
0x11,0x11,0x11,0x11,0x11,0x0A,0x04,0x00,	// 0x56
0x11,0x11,0x15,0x15,0x15,0x15,0x0A,0x00,	// 0x57
0x11,0x11,0x0A,0x04,0x0A,0x11,0x11,0x00,	// 0x58
0x11,0x11,0x11,0x0A,0x04,0x04,0x04,0x00,	// 0x59
0x1E,0x02,0x04,0x08,0x10,0x10,0x1E,0x00,	// 0x5A
0x0E,0x08,0x08,0x08,0x08,0x08,0x0E,0x00,	// 0x5B
0x00,0x10,0x08,0x04,0x02,0x01,0x00,0x00,	// 0x5C
0x0E,0x02,0x02,0x02,0x02,0x02,0x0E,0x00,	// 0x5D
0x04,0x0A,0x11,0x00,0x00,0x00,0x00,0x00,	// 0x5E
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,	// 0x5F
0x0C,0x0C,0x04,0x00,0x00,0x00,0x00,0x00,	// 0x60
0x00,0x00,0x0E,0x01,0x0F,0x11,0x0F,0x00,	// 0x61
0x10,0x10,0x1E,0x11,0x11,0x11,0x1E,0x00,	// 0x62
0x00,0x00,0x0E,0x11,0x10,0x11,0x0E,0x00,	// 0x63
0x01,0x01,0x0F,0x11,0x11,0x11,0x0F,0x00,	// 0x64
0x00,0x00,0x0E,0x11,0x1E,0x10,0x0E,0x00,	// 0x65
0x06,0x08,0x08,0x1E,0x08,0x08,0x08,0x00,	// 0x66
0x00,0x00,0x0F,0x11,0x11,0x0F,0x01,0x0E,	// 0x67
0x10,0x10,0x1C,0x12,0x12,0x12,0x12,0x00,	// 0x68
0x04,0x00,0x04,0x04,0x04,0x04,0x06,0x00,	// 0x69
0x02,0x00,0x06,0x02,0x02,0x02,0x12,0x0C,	// 0x6A
0x10,0x10,0x12,0x14,0x18,0x14,0x12,0x00,	// 0x6B
0x04,0x04,0x04,0x04,0x04,0x04,0x06,0x00,	// 0x6C
0x00,0x00,0x1A,0x15,0x15,0x11,0x11,0x00,	// 0x6D
0x00,0x00,0x1C,0x12,0x12,0x12,0x12,0x00,	// 0x6E
0x00,0x00,0x0E,0x11,0x11,0x11,0x0E,0x00,	// 0x6F
0x00,0x00,0x1E,0x11,0x11,0x11,0x1E,0x10,	// 0x70
0x00,0x00,0x0F,0x11,0x11,0x11,0x0F,0x01,	// 0x71
0x00,0x00,0x16,0x09,0x08,0x08,0x1C,0x00,	// 0x72
0x00,0x00,0x0E,0x10,0x0E,0x01,0x0E,0x00,	// 0x73
0x00,0x08,0x1E,0x08,0x08,0x0A,0x04,0x00,	// 0x74
0x00,0x00,0x12,0x12,0x12,0x16,0x0A,0x00,	// 0x75
0x00,0x00,0x11,0x11,0x11,0x0A,0x04,0x00,	// 0x76
0x00,0x00,0x11,0x11,0x15,0x1F,0x0A,0x00,	// 0x77
0x00,0x00,0x12,0x12,0x0C,0x12,0x12,0x00,	// 0x78
0x00,0x00,0x12,0x12,0x12,0x0E,0x04,0x18,	// 0x79
0x00,0x00,0x1E,0x02,0x0C,0x10,0x1E,0x00,	// 0x7A
0x06,0x08,0x08,0x18,0x08,0x08,0x06,0x00,	// 0x7B
0x04,0x04,0x04,0x00,0x04,0x04,0x04,0x00,	// 0x7C
0x0C,0x02,0x02,0x03,0x02,0x02,0x0C,0x00,	// 0x7D
0x0A,0x14,0x00,0x00,0x00,0x00,0x00,0x00,	// 0x7E
0x04,0x0E,0x1B,0x11,0x11,0x1F,0x00,0x00,	// 0x7F
0x0E,0x11,0x10,0x10,0x11,0x0E,0x04,0x0C,	// 0x80
/* 

//If you operate a microcontroller with more memory space 
//than an ATmega32 you can also use the following 127 characters!

0x12,0x00,0x12,0x12,0x12,0x16,0x0A,0x00,	// 0x81
0x03,0x00,0x0E,0x11,0x1E,0x10,0x0E,0x00,	// 0x82
0x0E,0x00,0x0E,0x01,0x0F,0x11,0x0F,0x00,	// 0x83
0x0A,0x00,0x0E,0x01,0x0F,0x11,0x0F,0x00,	// 0x84
0x0C,0x00,0x0E,0x01,0x0F,0x11,0x0F,0x00,	// 0x85
0x0E,0x0A,0x0E,0x01,0x0F,0x11,0x0F,0x00,	// 0x86
0x00,0x0E,0x11,0x10,0x11,0x0E,0x04,0x0C,	// 0x87
0x0E,0x00,0x0E,0x11,0x1E,0x10,0x0E,0x00,	// 0x88
0x0A,0x00,0x0E,0x11,0x1E,0x10,0x0E,0x00,	// 0x89
0x0C,0x00,0x0E,0x11,0x1E,0x10,0x0E,0x00,	// 0x8A
0x0A,0x00,0x04,0x04,0x04,0x04,0x06,0x00,	// 0x8B
0x0E,0x00,0x04,0x04,0x04,0x04,0x06,0x00,	// 0x8C
0x08,0x00,0x04,0x04,0x04,0x04,0x06,0x00,	// 0x8D
0x0A,0x00,0x04,0x0A,0x11,0x1F,0x11,0x00,	// 0x8E
0x0E,0x0A,0x0E,0x1B,0x11,0x1F,0x11,0x00,	// 0x8F
0x03,0x00,0x1F,0x10,0x1E,0x10,0x1F,0x00,	// 0x90
0x00,0x00,0x1E,0x05,0x1F,0x14,0x0F,0x00,	// 0x91
0x0F,0x14,0x14,0x1F,0x14,0x14,0x17,0x00,	// 0x92
0x0E,0x00,0x0C,0x12,0x12,0x12,0x0C,0x00,	// 0x93
0x0A,0x00,0x0C,0x12,0x12,0x12,0x0C,0x00,	// 0x94
0x18,0x00,0x0C,0x12,0x12,0x12,0x0C,0x00,	// 0x95
0x0E,0x00,0x12,0x12,0x12,0x16,0x0A,0x00,	// 0x96
0x18,0x00,0x12,0x12,0x12,0x16,0x0A,0x00,	// 0x97
0x0A,0x00,0x12,0x12,0x12,0x0E,0x04,0x18,	// 0x98
0x12,0x0C,0x12,0x12,0x12,0x12,0x0C,0x00,	// 0x99
0x0A,0x00,0x12,0x12,0x12,0x12,0x0C,0x00,	// 0x9A
0x00,0x00,0x01,0x0E,0x16,0x1A,0x1C,0x20,	// 0x9B
0x06,0x09,0x08,0x1E,0x08,0x09,0x17,0x00,	// 0x9C
0x0F,0x13,0x15,0x15,0x15,0x19,0x1E,0x00,	// 0x9D
0x00,0x11,0x0A,0x04,0x0A,0x11,0x00,0x00,	// 0x9E
0x02,0x05,0x04,0x0E,0x04,0x04,0x14,0x08,	// 0x9F
0x06,0x00,0x0E,0x01,0x0F,0x11,0x0F,0x00,	// 0xA0
0x06,0x00,0x04,0x04,0x04,0x04,0x06,0x00,	// 0xA1
0x06,0x00,0x0C,0x12,0x12,0x12,0x0C,0x00,	// 0xA2
0x06,0x00,0x12,0x12,0x12,0x16,0x0A,0x00,	// 0xA3
0x0A,0x14,0x00,0x1C,0x12,0x12,0x12,0x00,	// 0xA4
0x0A,0x14,0x00,0x12,0x1A,0x16,0x12,0x00,	// 0xA5
0x0E,0x01,0x0F,0x11,0x0F,0x00,0x0F,0x00,	// 0xA6
0x0C,0x12,0x12,0x12,0x0C,0x00,0x1E,0x00,	// 0xA7
0x04,0x00,0x04,0x0C,0x10,0x11,0x0E,0x00,	// 0xA8
0x1E,0x25,0x2B,0x2D,0x2B,0x21,0x1E,0x00,	// 0xA9
0x00,0x00,0x3F,0x01,0x01,0x00,0x00,0x00,	// 0xAA
0x10,0x12,0x14,0x0E,0x11,0x02,0x07,0x00,	// 0xAB
0x10,0x12,0x14,0x0B,0x15,0x07,0x01,0x00,	// 0xAC
0x04,0x00,0x04,0x04,0x0E,0x0E,0x04,0x00,	// 0xAD
0x00,0x00,0x09,0x12,0x09,0x00,0x00,0x00,	// 0xAE
0x00,0x00,0x12,0x09,0x12,0x00,0x00,0x00,	// 0xAF
0x15,0x00,0x2A,0x00,0x15,0x00,0x2A,0x00,	// 0xB0
0x15,0x2A,0x15,0x2A,0x15,0x2A,0x15,0x2A,	// 0xB1
0x2A,0x3F,0x15,0x3F,0x2A,0x3F,0x15,0x3F,	// 0xB2
0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,	// 0xB3
0x04,0x04,0x04,0x3C,0x04,0x04,0x04,0x04,	// 0xB4
0x06,0x00,0x04,0x0A,0x11,0x1F,0x11,0x00,	// 0xB5
0x0E,0x00,0x04,0x0A,0x11,0x1F,0x11,0x00,	// 0xB6
0x0C,0x00,0x04,0x0A,0x11,0x1F,0x11,0x00,	// 0xB7
0x1E,0x21,0x2D,0x29,0x2D,0x21,0x1E,0x00,	// 0xB8
0x14,0x34,0x04,0x34,0x14,0x14,0x14,0x14,	// 0xB9
0x14,0x14,0x14,0x14,0x14,0x14,0x14,0x14,	// 0xBA
0x00,0x3C,0x04,0x34,0x14,0x14,0x14,0x14,	// 0xBB
0x14,0x34,0x04,0x3C,0x00,0x00,0x00,0x00,	// 0xBC
0x00,0x04,0x0E,0x10,0x10,0x0E,0x04,0x00,	// 0xBD
0x11,0x0A,0x04,0x1F,0x04,0x1F,0x04,0x00,	// 0xBE
0x00,0x00,0x00,0x3C,0x04,0x04,0x04,0x04,	// 0xBF
0x04,0x04,0x04,0x07,0x00,0x00,0x00,0x00,	// 0xC0
0x04,0x04,0x04,0x3F,0x00,0x00,0x00,0x00,	// 0xC1
0x00,0x00,0x00,0x3F,0x04,0x04,0x04,0x04,	// 0xC2
0x04,0x04,0x04,0x07,0x04,0x04,0x04,0x04,	// 0xC3
0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,	// 0xC4
0x04,0x04,0x04,0x3F,0x04,0x04,0x04,0x04,	// 0xC5
0x05,0x0A,0x0E,0x01,0x0F,0x11,0x0F,0x00,	// 0xC6
0x05,0x0A,0x04,0x0A,0x11,0x1F,0x11,0x00,	// 0xC7
0x14,0x17,0x10,0x1F,0x00,0x00,0x00,0x00,	// 0xC8
0x00,0x1F,0x10,0x17,0x14,0x14,0x14,0x14,	// 0xC9
0x14,0x37,0x00,0x3F,0x00,0x00,0x00,0x00,	// 0xCA
0x00,0x3F,0x00,0x37,0x14,0x14,0x14,0x14,	// 0xCB
0x14,0x17,0x10,0x17,0x14,0x14,0x14,0x14,	// 0xCC
0x00,0x3F,0x00,0x3F,0x00,0x00,0x00,0x00,	// 0xCD
0x14,0x37,0x00,0x37,0x14,0x14,0x14,0x14,	// 0xCE
0x11,0x0E,0x11,0x11,0x11,0x0E,0x11,0x00,	// 0xCF
0x0C,0x10,0x08,0x04,0x0E,0x12,0x0C,0x00,	// 0xD0
0x0E,0x09,0x09,0x1D,0x09,0x09,0x0E,0x00,	// 0xD1
0x0E,0x00,0x1F,0x10,0x1E,0x10,0x1F,0x00,	// 0xD2
0x0A,0x00,0x1F,0x10,0x1E,0x10,0x1F,0x00,	// 0xD3
0x0C,0x00,0x1F,0x10,0x1E,0x10,0x1F,0x00,	// 0xD4
0x04,0x04,0x04,0x00,0x00,0x00,0x00,0x00,	// 0xD5
0x06,0x00,0x0E,0x04,0x04,0x04,0x0E,0x00,	// 0xD6
0x0E,0x00,0x0E,0x04,0x04,0x04,0x0E,0x00,	// 0xD7
0x0A,0x00,0x0E,0x04,0x04,0x04,0x0E,0x00,	// 0xD8
0x04,0x04,0x04,0x3C,0x00,0x00,0x00,0x00,	// 0xD9
0x00,0x00,0x00,0x07,0x04,0x04,0x04,0x04,	// 0xDA
0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,	// 0xDB
0x00,0x00,0x00,0x00,0x3F,0x3F,0x3F,0x3F,	// 0xDC
0x04,0x04,0x04,0x00,0x04,0x04,0x04,0x00,	// 0xDD
0x0C,0x00,0x0E,0x04,0x04,0x04,0x0E,0x00,	// 0xDE
0x3F,0x3F,0x3F,0x3F,0x00,0x00,0x00,0x00,	// 0xDF
0x06,0x0C,0x12,0x12,0x12,0x12,0x0C,0x00,	// 0xE0
0x00,0x1C,0x12,0x1C,0x12,0x12,0x1C,0x10,	// 0xE1
0x0E,0x0C,0x12,0x12,0x12,0x12,0x0C,0x00,	// 0xE2
0x18,0x0C,0x12,0x12,0x12,0x12,0x0C,0x00,	// 0xE3
0x0A,0x14,0x00,0x0C,0x12,0x12,0x0C,0x00,	// 0xE4
0x0A,0x14,0x0C,0x12,0x12,0x12,0x0C,0x00,	// 0xE5
0x00,0x00,0x12,0x12,0x12,0x1C,0x10,0x10,	// 0xE6
0x00,0x18,0x10,0x1C,0x12,0x1C,0x10,0x18,	// 0xE7
0x18,0x10,0x1C,0x12,0x12,0x1C,0x10,0x18,	// 0xE8
0x06,0x00,0x12,0x12,0x12,0x12,0x0C,0x00,	// 0xE9
0x0E,0x00,0x12,0x12,0x12,0x12,0x0C,0x00,	// 0xEA
0x18,0x00,0x12,0x12,0x12,0x12,0x0C,0x00,	// 0xEB
0x06,0x00,0x12,0x12,0x12,0x0E,0x04,0x18,	// 0xEC
0x06,0x00,0x11,0x0A,0x04,0x04,0x04,0x00,	// 0xED
0x00,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,	// 0xEE
0x0C,0x0C,0x08,0x00,0x00,0x00,0x00,0x00,	// 0xEF
0x00,0x00,0x00,0x0E,0x00,0x00,0x00,0x00,	// 0xF0
0x00,0x04,0x0E,0x04,0x00,0x0E,0x00,0x00,	// 0xF1
0x00,0x00,0x1F,0x00,0x00,0x1F,0x00,0x00,	// 0xF2
0x30,0x1A,0x34,0x0B,0x15,0x07,0x01,0x00,	// 0xF3
0x0F,0x15,0x15,0x0D,0x05,0x05,0x05,0x00,	// 0xF4
0x0E,0x11,0x0C,0x0A,0x06,0x11,0x0E,0x00,	// 0xF5
0x00,0x04,0x00,0x1F,0x00,0x04,0x00,0x00,	// 0xF6
0x00,0x00,0x00,0x0E,0x06,0x00,0x00,0x00,	// 0xF7
0x0C,0x12,0x12,0x0C,0x00,0x00,0x00,0x00,	// 0xF8
0x00,0x00,0x00,0x0A,0x00,0x00,0x00,0x00,	// 0xF9
0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,	// 0xFA
0x08,0x18,0x08,0x08,0x00,0x00,0x00,0x00,	// 0xFB
0x1C,0x08,0x0C,0x18,0x00,0x00,0x00,0x00,	// 0xFC
0x18,0x04,0x08,0x1C,0x00,0x00,0x00,0x00,	// 0xFD
0x00,0x00,0x1E,0x1E,0x1E,0x1E,0x00,0x00,	// 0xFE
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 	// 0xFF
*/
};
	
  /////////////////////////////////////
 // Functions for LCD12864 control  //
/////////////////////////////////////
//Write instruction (code==0) or data (code==1) to LCD
void lcd_write(char lcdmode, unsigned char value)
{
	DDRD = 0xFF;     //Set port for write operation
	
	set_rw(0);	     //Write operation
	set_rs(lcdmode); //0 for instruction, 1 for data
       
	PORTD = value;
    set_e(1);
    _delay_us(10);
	set_e(0);	
		
	set_rs(0);
}    

//Read data from LCD
char lcd_read(char lcdmode)
{
	unsigned char value;
	
	DDRD = 0x00;       //Set port for read operation
	//PORTD = 0xFF;      //Activate pull-up resistors to ensure proper data transmission
    
    set_rw(1);	       //Read operation
	set_rs(lcdmode);   //Get value 0: for busy flag, 1 for other data
	
	set_e(1);          //Read data
    _delay_us(10);       
	value = PIND;
    set_e(0);	
	
	//set_rs(0);
	
	return value;
}    

//Set RW line
void set_rw(char status)  
{
    if(status)
	{
        PORTC |= 2;
	}	
    else
	{
	    PORTC &= ~(2);
	}	
}

//Set RS line
void set_rs(char status) 
{
    if(status)
	{
        PORTC |= 1;
	}	
    else
	{
	    PORTC &= ~(1);
	}	
}

//Set E line
void set_e(char status)  
{
    if(status)
	{
        PORTC |= 4;
	}	
    else
	{
	    PORTC &= ~(4);
	}	
}

//Check for busy flag (BF)
int is_lcd_busy(void)
{
	int v = lcd_read(0);
	_delay_us(10);
	v = lcd_read(0);
			
	if(v & 0x80)
	{
		return -1;
	}
	else
	{
		return 0;
	}	
		
}

//Send one character to LCD (Normal size)
//
void lcd_putchar(int row0, int col0, unsigned char ch0, int inv)
{
	int t1;
	int odd = 0;
	unsigned char v1, v2;
	int col = col0 / 2;
	int row = row0 * FONTHEIGHT;
	unsigned char ch;
		
	if(row & 0x20)  //Enter lower part of screen => go to next page
	{
        row &= ~0x20;
        col |= 8;
    }
        
	if(col0 & 1) //Detect odd coloumn
	{
		odd = 1;
	}
		
	for(t1 = 0; t1 < FONTHEIGHT; t1++)
    {
	    //Set address
        lcd_write(0, 0x80 + row + t1);
        lcd_write(0, 0x80 + col);
             
        //Get old values of 2 GDRAM bytes	
	    v1 = lcd_read(1);                
        v1 = lcd_read(1);
        v2 = lcd_read(1);

        //Set address
        lcd_write(0, 0x80 + row + t1);
        lcd_write(0, 0x80 + col);
        
        if(!inv)
        {
			ch = font[ch0 * FONTHEIGHT + t1];
		}
		else	
        {
			ch = ~font[ch0 * FONTHEIGHT + t1];
		}
		     
        if(odd)
        {     
            //Write data on RIGHT side of existing character
            lcd_write(1, v1);
            lcd_write(1, ch);
        }
        else    
        {   
			//Write data on LEFT side of existing character
            lcd_write(1, ch);
            lcd_write(1, v2);
        }
    }    
}   

//Send one character to LCD (DOUBLE size and normal width)
//
void lcd_putchar2(int row0, int col0, unsigned char ch0, int inv)
{
	int t1, t2;
	int odd = 0;
	unsigned char v1, v2;
	int col = col0 >> 1;
	int row = row0 * FONTHEIGHT;
	unsigned char ch;
		
	if(row & 0x20)  //Enter lower part of screen => go to next page
	{
        row &= ~0x20;
        col |= 8;
    }
        
	if(col0 & 1) //Detect odd coloumn
	{
		odd = 1;
	}
		
	for(t1 = 0; t1 < FONTHEIGHT; t1++)
    {
		if(!inv) //Calculate character position in array and xor invert number if needed
        {
			ch = (font[ch0 * FONTHEIGHT + t1]);
		}
		else	
        {
			ch = (~font[ch0 * FONTHEIGHT + t1]);
		}
		
		for(t2 = 0; t2 < 2; t2++)
		{
	        //Set address
            lcd_write(0, 0x80 + row + t1 * 2 + t2);
            lcd_write(0, 0x80 + col);
             
            //Get old values of 2 GDRAM bytes	
	        v1 = lcd_read(1);                
            v1 = lcd_read(1);
            v2 = lcd_read(1);

            //Set address
            lcd_write(0, 0x80 + row + t1 * 2 + t2);
            lcd_write(0, 0x80 + col);
        		     
            if(odd)
            {     
                //Write data on RIGHT side of existing character
                lcd_write(1, v1);
                lcd_write(1, ch);
            }
            else    
            {   
			    //Write data on LEFT side of existing character
                lcd_write(1, ch);
                lcd_write(1, v2);
            }    
        }
    }    
}   

//Send one character to LCD (DOUBLE size and DOUBLE width)
//
void lcd_putchar3(int row0, int col0, unsigned char ch0, int inv)
{
	int t1, t2;
	unsigned char ch;
	//unsigned int i[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
	unsigned int i[FONTHEIGHT] = {0, 0, 0, 0, 0, 0, 0, 0};
	int col = col0 >> 1;
	int row = row0 * FONTHEIGHT;
	
	if(row & 0x20)  //Enter lower part of screen => go to next page
	{
        row &= ~0x20;
        col |= 8;
    }
        
		
	for(t1 = 0; t1 < FONTHEIGHT; t1++)
    {
		if(!inv) //Calculate character position in array and xor invert number if needed
        {
			ch = (font[ch0 * FONTHEIGHT + t1]);
		}
		else	
        {
			ch = (~font[ch0 * FONTHEIGHT + t1]);
		}
		
		//Double 8 to 16 bits
	    i[t1] = 0;
		for(t2 = 7; t2 > -1; t2--)
		{
			if(ch & (1 << t2))
			{
				i[t1] += (1 << ((t2 << 1) + 1)) + (1 << (t2 << 1)); //Double bit pattern 2 by 1
			}
		}
	}
	
	t2 = 0;
	for(t1 = 0; t1 < FONTHEIGHT; t1++)
	{
		for(t2 = 0; t2 < 2; t2++)
		{
	        //Set address
            lcd_write(0, 0x80 + row + t1 * 2 + t2);
            lcd_write(0, 0x80 + col);
                       
            lcd_write(1, ((i[t1] >> 8) & 0xFF));
            lcd_write(1, i[t1] & 0xFF); 
            //lcd_putnumber(t1, 0, i[t1] , -1, 0, 0);
        }    
    }    
		
}	

//Send string (\0 terminated) to LCD normal or double height
void lcd_putstring_a(int row, int col, char *s, int size, int inv)
{
    unsigned char t1;

    for(t1 = col; *(s); t1++)
	{
		if(!size)
		{
            lcd_putchar(row, t1, *(s++), inv);
        }
        else    
        {
            lcd_putchar2(row, t1, *(s++), inv);
        }
	}	
}

//String in DOUBLE height and DOUBLE width
void lcd_putstring_b(int row, int col, char *s, int inv)
{
    unsigned char t1;

    for(t1 = col; *(s); t1++)
	{
	    lcd_putchar3(row, t1 * 2, *(s++), inv);
	}	
}

//Clear LCD
void lcd_cls(void)
{
	int x, y;
    for(x = 0; x < 16; x++)
    {
		for(y = 0; y < 64; y++)
		{
			//Set address
            lcd_write(0, 0x80 + y);
            lcd_write(0, 0x80 + x);
             
            //Write data
            lcd_write(1, 0);
            lcd_write(1, 0);
        }
    }    
}

//Convert a number to a string and print it
//col, row: Coordinates, Num: int or long to be displayed
//dec: Set position of decimal separator
//
//inv: Set to 1 if inverted charactor is required
void lcd_putnumber(int col, int row, long num, int dec, int lsize, int inv)
{
    char *s = malloc(16);
	if(s != NULL)
	{
	    int2asc(num, dec, s, 16);
	    lcd_putstring_a(col, row, s, lsize, inv);
	    free(s);
	}	
	else
	{
		lcd_putstring_a(col, row, "Error", 0, 0);
	}	
}


//Init LCD
void lcd_init(void)
{            
    //Reset
    PORTC &= ~(8);
    _delay_ms(1);
    PORTC |= 8;
    _delay_ms(40);
    
    lcd_write(0, 0x30);	//Use 8-bit mode parallel
    _delay_ms(1);
         
    lcd_write(0, 0x0C); //All on Cursor on, Blink on , Display on
    _delay_ms(1);
        
    lcd_write(0, 0x01); //Perform CLS in text mode to eliminate random chars from screen
    _delay_ms(20);
    
    lcd_write(0, 0x34); //Switch to extended mode, redefine function set
    _delay_us(100);
    
    lcd_write(0, 0x36); //Add graphic mode
    _delay_us(100);
                   
    lcd_write(0, 0x12); //Display control and display ON
    _delay_us(100);
}

//////////////////////
// STRING FUNCTIONS //
//////////////////////
//INT 2 ASC
int int2asc(long num, int dec, char *buf, int buflen)
{
    int i, c, xp = 0, neg = 0;
    long n, dd = 1E09;

    if(!num)
	{
	    *buf++ = '0';
		*buf = 0;
		return 1;
	}	
		
    if(num < 0)
    {
     	neg = 1;
	    n = num * -1;
    }
    else
    {
	    n = num;
    }

    //Fill buffer with \0
    for(i = 0; i < 12; i++)
    {
	    *(buf + i) = 0;
    }

    c = 9; //Max. number of displayable digits
    while(dd)
    {
	    i = n / dd;
	    n = n - i * dd;
	
	    *(buf + 9 - c + xp) = i + 48;
	    dd /= 10;
	    if(c == dec && dec)
	    {
	        *(buf + 9 - c + ++xp) = '.';
	    }
	    c--;
    }

    //Search for 1st char different from '0'
    i = 0;
    while(*(buf + i) == 48)
    {
	    *(buf + i++) = 32;
    }

    //Add minus-sign if neccessary
    if(neg)
    {
	    *(buf + --i) = '-';
    }

    //Eleminate leading spaces
    c = 0;
    while(*(buf + i))
    {
	    *(buf + c++) = *(buf + i++);
    }
    *(buf + c) = 0;
	
	return c;
}


int main(void)
{
    // Set ports for LCD output and input data
    DDRC = 0x0F; //LCD RS, RW, E and RST at PC0:PC3
	DDRD = 0xFF; //LCD data on PD0:PD7
   	                 		
	//Display init
	_delay_ms(100);
    lcd_init();
	_delay_ms(100);
	
    lcd_cls();
        
    lcd_putstring_a(0, 0, "LCD 12864 ST7920", 0, 0);
    lcd_putstring_a(1, 0, "   DK7IH 2018   ", 0, 1);
    lcd_putstring_a(2, 0, "Graphical Fonts:", 0, 0);
    lcd_putstring_a(3, 0, "8x8px.", 0, 0);
    lcd_putnumber(4, 0, 1234, 1, 0, 0);    
       
	lcd_putstring_a(4, 0, "16x8px.", 1, 0);
	
	lcd_putstring_b(6, 0, "16x16px.", 0);
			    
    for(;;) 
	{
    
    
	}
	return 0;
}

Decoding NMEA data from a GPS module with a microcontroller

I found one of those widely distributed GPS receiver modules in my collection of electronic material. I had bought it some years ago for a school project I did with students to launch a balloon into the stratosphere (Link to text in German). Here a video of the flight phase when maximum altitude was reached:

The GPS module that was used then has still been sleeping in my junk box. When looking through the C code I had written then I found that this could be optimized. I connected the GPS receiver to an AVR ATmega32 microcontroller, added a simple 2-lines-16-characters lcd and started rewriting the software for communicating with the module.

The Hardware

OK, an ATmgea32 is somehow “overkill” for this simple circuit, but I had already installed it on my breadboard. The data lines of the ATmega32 controller that are in use for this project are pointed out here:

ATMega32_GPS-Connections

The GPS module is connected to the controller via the serial RS232 interface (TxD, RxD, PIN 15 and 14 of the micro). Note that RX of the GPS module must be connected to TxD with the controller and vice versa.

The rest of the layout is also very simple.

Microcontroller interface connections for this project

The lcd is driven in 4-bit-mode, thus only four display data lines are needed. Plus the 2 controls RS and E:

ATMega32 => LCD

  • PD4 => LCD PIN D4
  • PD5 => LCD PIN D5
  • PD6 => LCD PIN D6
  • PD7 => LCD PIN D7
  • PC2 => LCD RS
  • PC3 => LCD E

ATMega32 => GPS Module

  • PD0 (RxD) => GPS TX
  • PD1 (TxD) => GPS RX

Decoding GPS data

GPS data from the module comes coded as a so-called “NMEA”-string. “NMEA” stands for the American National Marine Electronics Association . The data is encrypted using a standardized ASCII format that appears in various patterns and is transferred by the GPS module regularly in very short intervals (about 1 second or less). The communication settings for the RS232 interface are fixed and can not be altered:

9600 baud, 8 data bits, 1 stop bit, no parity

The ATMega’s UART (universal asynchronous receiver and transmitter)  must be set with an initialization routine to the required parameters:

//Init UART
void uart_init()
{
    /* 9600 Baud */
    UBRRL = 51; UBRRH = 0;

    /* RX and TX on */
    UCSRB = (1<<RXEN)|(1<<TXEN);

    /* 8 databits, 1 stopbit, No parity */
    UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);
}

NMEA strings (example)

There are various types of data strings transferred by the module, each serving an individual purpose. They are all coded with a leading identifier marked by a $-sign and an identifier code. We will discuss the absolute basic string that delivers essential GPS information, the so-called “GPRMC”-string, where the letters “RMC” represent “recommended minimum sentence”.

This sequence contains all the necessary data for a basic GPS operation and therefore is ideal for experiments with the GPS module. Example for such a string:

$GPRMC,191410,A,4735.5634,N,00739.3538,E,0.0,0.0,181102,0.4,E*19

The various data fields are separated by a comma, please note that the data fields may have various width so just counting bytes is not reliable if you want to “land” in a specific field. Instead the comma separators must be counted.

What do the separate data fields mean?

The first entry (“191410”) after the “$GPRMC” intro is the UTC time stamp, telling when the recent GPS data set was processed, i. e. the current time in HHMMSS- format. There might be even 10th of seconds then the format is HHMMSSss.

Next is a letter (“A” for correct data, “V” (void) if the receiver was not able to get correct data), that indicates if the data is valid and can be used or not. You can use that to discard the data set when the receiver is not able to detect a valid GPS signal.

Following a number (here “4735.5634”) together with the subsequent letter (N or S) indicates the latitude of the current position. This is a 100fold decimal value formatted  by a decimal separator. Latitude can be in the range from 0 to 90 degrees North or South. Here it is about 47 degrees in the northern hemisphere.

Subsequently the same coding is used for transferring longitude data. Longitude is in the range from 0 to 180 degrees East or West with the Prime Meridian in Greenwich as separator.

Next two values (0.0 and 0.0) indicate ground speed in knots and bearing in degrees.

“181102” is the date stamp (18th Nov 2002).

“0.4” and “E” indicate a magnetic declination (degrees and direction) that might occur.

“19” is the  XOR generated checksum of the string. I found that data transfer is very reliable so I don’t calculate this.

Calculating QTH locator

For a radio amateur it is clear that GPS data must be converted to the Maidenhead system to determine the current QTH locator (JN49AB in my case). Thus I have programmed a short function that can convert decimal data for latitude and longitude to the Maidenhead system:

//Calc QTH locator from decimal value for latitude and
//longitude. o1 and o2 are signifier for 'E or 'W' (longitude) resp.
//'N' or 'S' for latitude
//example for calling: calc_maidenhead(8.002, 'E', 49.1002, 'N', buf);
void calc_maidenhead(double longitude, char o1, double latitude, char o2, char *buffer)
{

    //Longitude
    int deg_lo, lo1, lo2, lo3;
    double deg10_lo;

    //Lattitude
    int deg_la, la1, la2, la3;
    double deg10_la;

    deg_lo = longitude; //Truncate values for longitude and latitude
    deg_la = latitude;

    deg10_lo = (longitude - deg_lo) * 12; //Calculate fraction behind decimal separator
    deg10_la = (latitude - deg_la) * 24;

    //Longitude
    if(o1 == 'E')
    {
        lo1 = 180 + deg_lo;
    }
    else
    {
        lo1 = 180 - deg_lo;
    }
    lo2 = lo1 / 20;
    lo3 = lo1 - lo2 * 20;

    //Lattitude
    if(o2 == 'N')
    {
        la1 = 90 + deg_la;
    }
    else
    {
        la1 = 90 - deg_la;
    }

    la2 = la1 / 10;
    la3 = la1 - la2 * 10;

    *(buffer + 0) = lo2 + 65;
    *(buffer + 1) = la2 + 65;
    *(buffer + 2) = lo3 / 2 + '0';
    *(buffer + 3) = la3 + '0';
    *(buffer + 4) = (int) deg10_lo + 'A';
    *(buffer + 5) = (int) deg10_la + 'A';
}

Parameters handed over to the function are

  • Longitude in decimals (double),
  • ‘E’ or ‘W’ for orientation,
  • Latitude in decimals (double),
  • ‘N’ or ‘S’ for orientation,
  • a 6 byte buffer (+ 1 byte for “\0” termination) referenced by address for the locator data.

The program rolls through the data permanently giving relevant information on the display with changes every second. Due to the fact that I haven’t implemented error handling there might be more or less useles indications when data from the GPS receiver is corrupted or invalid. But mostly the routine works out very fine without any complaint. You can use the ‘A’ or ‘V’ indicator transferred to change the display routine if unvalid data is detected.

By the end of this article you can get the full  C-code for the project. Thanks for reading!

73 de Peter (DK7IH)

/*****************************************************************/
/*                    GPS with ATMega32                          */
/*  ************************************************************ */
/*  Mikrocontroller:  ATMEL AVR ATmega32, 8 MHz                  */
/*                                                               */
/*  Compiler:         GCC (GNU AVR C-Compiler)                   */
/*  Autor:            Peter Rachow  2018                         */
/*  Fuses: -U lfuse:w:0xe4:m -U hfuse:w:0xd9:m                   */
/*****************************************************************/
// This code reads data from a GPS module via RS232 interface and
// decodes the basic NMEA string of $GPRMC type, the so-called
// "RMC" (Recommended Minimum Specific GNSS Data) string.
// Displayed are Date, Time, QTH-Locator (Maidenhead), Lattitude and
// Longitude and Ground speed
/*   PORTS */
// O U T P U T
// LCD
// RS      = PC2
// E       = PC3
// D4...D7 = PD4..PD7

// PD0  TX at GPS module
// PD1  RX at GPS module

#define F_CPU 8000000

#include
#include
#include
#include 

#include
#include
#include
#include
#include 

int main(void);

/***************/
/* LCD-Display */
/***************/
#define LCD_INST 0x00
#define LCD_DATA 0x01

void lcd_write(char, unsigned char);
void lcd_write(char, unsigned char);
void set_rs(char);
void set_e(char);
void lcd_init(void);
void lcd_cls(void);
void lcd_line_cls(int);
void lcd_putchar(int, int, unsigned char);
void lcd_putstring(int, int, char*);
void lcd_putnumber(int, int, long, int);
void lcd_display_test(void);
void setcustomcharacters(void);

/*******************/
/* Stringhandling */
/*******************/
int strlen(char *s);
int instring(char*, char*);
int strcomp(char*, char*);
void int2asc(long, int, char*, int);

/*******************/
/*   U  A  R  T    */
/*******************/
void uart_init(void);
void uart_putc(char);
void uart_send_string(char*);
void init_rx_buffer(void);
char make_crc(int, int);

/**********************/
/* V A R I A B L E S  */
/**********************/
//USART defines &amp; variables
#define RX_BUF_SIZE 64
char rx_buf[RX_BUF_SIZE];
int rx_buf_cnt = 0;

/*****************************/
/* Result string formatting  */
/*****************************/
void get_time(char*, char*);
int get_receiver_status(char*);
void get_latitude(char*, char*);
void get_longitude(char*, char*);
void get_latitude_ns(char*, char*);
void get_longitude_ew(char*, char*);
double get_gps_coordinate_decimal(char*, int, char*);
void get_ground_speed(char*, char*);
void get_ground_speed(char*, char*);
void get_date(char*, char*);
void calc_maidenhead(double, char, double, char, char*);

/**************************************/
/*            L  C  D                 */
/**************************************/
/* Send one byte to LCD */
void lcd_write(char lcdmode, unsigned char value)
{
    int x = 16, t1;

    set_e(0); 

    if(!lcdmode)
	{
        set_rs(0);
	}
    else
	{
        set_rs(1);
	}	

    _delay_ms(1);

    set_e(1);

    /* Hi */
	for(t1 = 0; t1 &lt; 4; t1++)
	{
	    if(value &amp; x)
	    {
	       PORTD |= x;
	    }
        else
	    {
           PORTD &amp;= ~(x);
	    }  

		x *= 2;
	}
	set_e(0);

	x = 16;

	set_e(1);
	/* Lo */
	for(t1 = 0; t1 &lt; 4; t1++)
	{
	    if((value &amp; 0x0F) * 16 &amp; x)
	    {
	       PORTD |= x;
	    }
        else
	    {
           PORTD &amp;= ~(x);
	    }  

		x *= 2;
	}

    set_e(0);
}

/* RS */
void set_rs(char status)
{
    if(status)
	{
        PORTC |= (1 &lt;&lt; PC2);
	}
    else
	{
	    PORTC &amp;= ~(1 &lt;&lt; PC2);
	}
}

/* E */
void set_e(char status)
{
    if(status)
	{
        PORTC |= (1 &lt;&lt; PC3);
	}
    else
	{
	    PORTC &amp;= ~(1 &lt;&lt; PC3);
	}
}

void lcd_putchar(int row, int col, unsigned char ch)
{
    lcd_write(LCD_INST, col + 128 + row * 0x40);
    lcd_write(LCD_DATA, ch);
}

void lcd_putstring(int row, int col, char *s)
{
    unsigned char t1;

    for(t1 = col; *(s); t1++)
	{
        lcd_putchar(row, t1, *(s++));
	}
}

void lcd_putnumber(int y, int x, long number, int dec)
{
	char *buf;

	buf = malloc(32);
	int2asc(number, dec, buf, 31);
	lcd_putstring(y, x, buf);
	free(buf);
}	

void lcd_cls(void)
{
    lcd_write(LCD_INST, 1);
}

void lcd_init(void)
{

    lcd_write(LCD_INST, 40);

    //Matrix 5*7
    lcd_write(LCD_INST, 8);

    /* Display on, Cursor off, Blink off */
    /* Entrymode !cursoincrease + !displayshifted */
    lcd_write(LCD_INST, 12);

	//4-Bit-Mode
    lcd_write(LCD_INST, 2);	

	lcd_cls();
}

void lcd_line_cls(int ln)
{
    int t1;

	for(t1 = 0; t1 &lt; 15; t1++)
	{
	    lcd_putchar(1, t1, 32);
	}
}	

//Define own chars
void setcustomcharacters(void)
{
    int i1;
    unsigned char adr=64;

    unsigned char customchar[]={ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,     // \0
		                         0x04, 0x0A, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00};    //°-sign
    lcd_write(LCD_INST, 0);
    lcd_write(LCD_DATA, 0);

    //Send data to CGRAM in lcd
    for (i1 = 0; i1 &lt; 16; i1++)
    {
        lcd_write(LCD_INST, adr++);
        lcd_write(LCD_DATA, customchar[i1]);
    }
}

/***********************/
//
// STRING-FUNCTIONS
//
/**********************/
//Convert int number to string
void int2asc(long num, int dec, char *buf, int buflen)
{
    int i, c, xp = 0, neg = 0;
    long n, dd = 1E09;

    //Write 0 to buffer in case value == 0
    if(!num)
    {
		buf[0] = &#039;0&#039;;
		buf[1] = 0;
		return;
	}	

    if(num &lt; 0)
    {
     	neg = 1;
	    n = num * -1;
    }
    else
    {
	    n = num;
    }

    //Fill buffer with \0
    for(i = 0; i &lt; 12; i++)
    {
	    *(buf + i) = 0;
    }

    c = 9; //Max. number of displayable digits
    while(dd)
    {
	    i = n / dd;
	    n = n - i * dd;

	    *(buf + 9 - c + xp) = i + 48;
	    dd /= 10;
	    if(c == dec &amp;&amp; dec)
	    {
	        *(buf + 9 - c + ++xp) = &#039;,&#039;;
	    }
	    c--;
    }

    //Search for 1st char different from &#039;0&#039;
    i = 0;
    while(*(buf + i) == 48)
    {
	    *(buf + i++) = 32;
    }

    //Add minus-sign if neccessary
    if(neg)
    {
	    *(buf + --i) = &#039;-&#039;;
    }

    //Eleminate leading spaces
    c = 0;
    while(*(buf + i))
    {
	    *(buf + c++) = *(buf + i++);
    }
    *(buf + c) = 0;
}

//Compare 2 strings
int strcomp(char *s1, char *s2)
{
    int t1;
    for(t1 = 0; t1 &lt; strlen(s2); t1++)
    {
        if(*(s1 + t1) != *(s2 + t1))
	    {
	       return 0;
		}
    }

    return 1;
}

//Get length of string
int strlen(char *s)
{
   int t1 = 0;

   while(*(s + t1++));

   return (t1 - 1);
}

//Find s2 in s1 if present, hand back position in string if yes
int instring(char *s1, char *s2)
{
   int t1, t2, ok;

   for(t1 = 0; *(s1 + t1) ; t1++)
   {
	    ok = 1;
	    for(t2 = 0; t2 &lt; strlen(s2); t2++)
	    {

	        if(*(s1 + t1 + t2) != *(s2 + t2))
	        {
	            ok = 0;
	        }
	    }

		if(ok)
	    {
	        return t1;
	    }
    }

    return 0;
}

//************/
//    UART
//************/
//Init UART
void uart_init()
{
    /* 9600 Baud */
    UBRRL = 51;
    UBRRH = 0;

    /* RX and TX on */
    UCSRB = (1&lt;&lt;RXEN)|(1&lt;&lt;TXEN);    

    /* 8 Datenbits, 1 Stopbit, keine Paritaet */
    UCSRC = (1&lt;&lt;URSEL)|(1&lt;&lt;UCSZ1)|(1&lt;&lt;UCSZ0);
}

//Send 1 char to UART
void uart_putc(char tx_char)
{
    while(!(UCSRA &amp; (1&lt;&lt;UDRE)));

    UDR = tx_char;
}

//Send one string to UART
void uart_send_string(char *s)
{
    int t1 = 0;
	while(*(s + t1))
	{
	    uart_putc(*(s + t1++));
	}
	uart_putc(13);
	uart_putc(10);

}

uint8_t uart_getc(void)
{
    while(!(UCSRA &amp; (1&lt;&lt;RXC)));   // warten bis Zeichen verfuegbar
    return UDR;                   // Zeichen aus UDR an Aufrufer zurueckgeben
}

//Init RX buffer
void init_rx_buffer(void)
{
	int t1;

    for(t1 = 0; t1 &lt; RX_BUF_SIZE - 1; t1++)
    {
		rx_buf[t1] = 0;
	}
	rx_buf_cnt = 0;
}		

/* CRC-calculation */
char make_crc(int buflen, int addchar)
{
    int t1, x = 0;

    for(t1 = 0; t1 &lt; buflen; t1++) /* Puffer bis dato */
	{
        x = x ^ rx_buf[t1];
    }
	x = x ^ addchar;                /* Sendebyte */

    return x;
}

///////////////////// END UART ///////////////

//////////////////////////////
//                          //
// NMEA decode functions    //
//                          //
//////////////////////////////

//Format date string
void get_date(char *buffer2, char *buffer3)
{

    int t1, t2 = 0;
    int p1, p2;

    //Zero buffer3
    for(t1 = 0; t1 &lt; RX_BUF_SIZE; t1++)
    {
		*(buffer3 + t1) = 0;
	}

	//Find 9th datafield
	t1 = 0;
	t2 = 0;
	while(t2 != 9 &amp;&amp; t1 &lt; RX_BUF_SIZE)
	{
		if(*(buffer2 + t1) == &#039;,&#039;)
		{
			t2++;
		}
		t1++;
	}
	p1 = t1;

	//Find end of datafield
	t1 = 0;
	t2 = 0;
	while(t2 != 10 &amp;&amp; t1 &lt; RX_BUF_SIZE)
	{
		if(*(buffer2 + t1) == &#039;,&#039;)
		{
			t2++;
		}
		t1++;
	}
	p2 = t1;

	//Copy relevant prt of string to buffer3
	t2 = 0;
    for(t1 = p1; t1 &lt; p2 - 1; t1++)
    {
		if((t1 - p1) == 2 || (t1 - p1) == 4)
		{
			*(buffer3 + t2++) = &#039;.&#039;;
		}
		*(buffer3 + t2++) = *(buffer2 + t1);
	}
}	

//Format time string
void get_time(char *buffer2, char *buffer3)
{

    int t1, t2 = 0;
    int p1, p2;

    //Zero buffer3
    for(t1 = 0; t1 &lt; RX_BUF_SIZE; t1++)
    {
		*(buffer3 + t1) = 0;
	}

	//Find first datafield
	t1 = 0;
	while(*(buffer2 + t1) != &#039;,&#039; &amp;&amp; t1 &lt; RX_BUF_SIZE)
	{
		t1++;
	}
	p1 = t1 + 1;

	//Find dot in timestring
	t1 = p1 + 1;
	while(*(buffer2 + t1) != &#039;.&#039; &amp;&amp; t1 &lt; RX_BUF_SIZE)
	{
		t1++;
	}
	p2 = t1;

	//Copy relevant prt of string to buffer3
    for(t1 = p1; t1 &lt; p2; t1++)
    {
		if(t1 == 9 || t1 == 11)
		{
			*(buffer3 + t2++) = &#039;:&#039;;
		}
		*(buffer3 + t2++) = *(buffer2 + t1);
	}
}	

//Format receiver status string
int get_receiver_status(char *buffer)
{
	int t1, t2 = 0;
    int p1;

	//Find second datafield
	t1 = 0;
	while(t2 != 2 &amp;&amp; t1 &lt; RX_BUF_SIZE)
	{
		if(*(buffer + t1) == &#039;,&#039;)
		{
			t2++;
		}
		t1++;
	}
	p1 = t1;

	if(*(buffer + p1) == &#039;A&#039;)
    {
		return 0;
	}
	else
	{
		return 1;
	}
}	

//Format latitude to degrees
void get_latitude(char *buffer2, char *buffer3)
{

    int t1, t2;
    int p1, p2;

    //Zero buffer3
    for(t1 = 0; t1 &lt; RX_BUF_SIZE; t1++)
    {
		*(buffer3 + t1) = 0;
	}

	//Find 3rd datafield
	t1 = 0;
	t2 = 0;
	while(t2 != 3 &amp;&amp; t1 &lt; RX_BUF_SIZE)
	{
		if(*(buffer2 + t1) == &#039;,&#039;)
		{
			t2++;
		}
		t1++;
	}
	p1 = t1;

	//Find end of datafield
	t1 = 0;
	t2 = 0;
	while(t2 != 4 &amp;&amp; t1 &lt; RX_BUF_SIZE)
	{
		if(*(buffer2 + t1) == &#039;,&#039;)
		{
			t2++;
		}
		t1++;
	}
	p2 = t1;

	//No data available
	if(p2 - p1 == 1)
	{
		return;
	}	

	//Copy relevant prt of string to buffer3
	t2 = 0;
    for(t1 = p1; t1 &lt; p2 - 1; t1++)
    {
		if(t1 == p1 + 2)
		{
			*(buffer3 + t2++) = 1; //°
		}	

		*(buffer3 + t2++) = *(buffer2 + t1);
	}
}	

//Format latitude orientation
void get_latitude_ns(char *buffer2, char *buffer3)
{

    int t1, t2;
    int p1, p2;

    //Zero buffer3
    for(t1 = 0; t1 &lt; RX_BUF_SIZE; t1++)
    {
		*(buffer3 + t1) = 0;
	}

	//Find 4th datafield
	t1 = 0;
	t2 = 0;
	while(t2 != 4 &amp;&amp; t1 &lt; RX_BUF_SIZE)
	{
		if(*(buffer2 + t1) == &#039;,&#039;)
		{
			t2++;
		}
		t1++;
	}
	p1 = t1;

	//Find end of datafield
	t1 = 0;
	t2 = 0;
	while(t2 != 5 &amp;&amp; t1 &lt; RX_BUF_SIZE)
	{
		if(*(buffer2 + t1) == &#039;,&#039;)
		{
			t2++;
		}
		t1++;
	}
	p2 = t1;

	//No data available
	if(p2 - p1 == 1)
	{
		return;
	}	

	//Copy relevant prt of string to buffer3
	t2 = 0;
    for(t1 = p1; t1 &lt; p2 - 1; t1++)
    {
		*(buffer3 + t2++) = *(buffer2 + t1);
	}	

}	

//Format longitude
void get_longitude(char *buffer2, char *buffer3)
{

    int t1, t2;
    int p1, p2;

    //Zero buffer3
    for(t1 = 0; t1 &lt; RX_BUF_SIZE; t1++)
    {
		*(buffer3 + t1) = 0;
	}

	//Find 5th datafield
	t1 = 0;
	t2 = 0;
	while(t2 != 5 &amp;&amp; t1 &lt; RX_BUF_SIZE)
	{
		if(*(buffer2 + t1) == &#039;,&#039;)
		{
			t2++;
		}
		t1++;
	}
	p1 = t1;

	//Find end of datafield
	t1 = 0;
	t2 = 0;
	while(t2 != 6 &amp;&amp; t1 &lt; RX_BUF_SIZE)
	{
		if(*(buffer2 + t1) == &#039;,&#039;)
		{
			t2++;
		}
		t1++;
	}
	p2 = t1;

	//No data available
	if(p2 - p1 == 1)
	{
		return;
	}	

	//Copy relevant prt of string to buffer3
	t2 = 0;
    for(t1 = p1; t1 &lt; p2 - 1; t1++)
    {
		if(t1 == p1 + 3)
		{
			*(buffer3 + t2++) = 1; //°
		}	

		*(buffer3 + t2++) = *(buffer2 + t1);
	}	

}	

//Format longitude orientation
void get_longitude_ew(char *buffer2, char *buffer3)
{

    int t1, t2;
    int p1, p2;

    //Zero buffer3
    for(t1 = 0; t1 &lt; RX_BUF_SIZE; t1++)
    {
		*(buffer3 + t1) = 0;
	}

	//Find 5th datafield
	t1 = 0;
	t2 = 0;
	while(t2 != 6 &amp;&amp; t1 &lt; RX_BUF_SIZE)
	{
		if(*(buffer2 + t1) == &#039;,&#039;)
		{
			t2++;
		}
		t1++;
	}
	p1 = t1;

	//Find end of datafield
	t1 = 0;
	t2 = 0;
	while(t2 != 7 &amp;&amp; t1 &lt; RX_BUF_SIZE)
	{
		if(*(buffer2 + t1) == &#039;,&#039;)
		{
			t2++;
		}
		t1++;
	}
	p2 = t1;

	//No data available
	if(p2 - p1 == 1)
	{
		return;
	}	

	//Copy relevant prt of string to buffer3
	t2 = 0;
    for(t1 = p1; t1  lattiude
double get_gps_coordinate_decimal(char *buf, int ctype, char *retbuf)
{
	int t1, t2;
	int sp; //Position of 1st comma of relevant data field
	double rval = 0;
	double x;

	char *l_str = malloc(20);

	if(!ctype)
	{
		sp = 5;  //Longitude
	}
	else
	{
		sp = 3; //Lattitude
	}	

	//Init temporary string
	for(t1 = 0; t1 &lt; 20; t1++)
	{
		*(l_str + t1) = 0;
	}

	//Search start position of value (3rd or 5th &#039;,&#039;)
	t1 = 0; t2 = 0;
	while(buf[t1] != 0 &amp;&amp; t2 != sp)
	{
		if(buf[t1] == &#039;,&#039;)
		{
			t2++;
		}
		t1++;
	}
	sp = t1;	

	//Load relevant part of original string to new bufferstring
	t2 = 0;
	for(t1 = sp; *(buf + t1) != &#039;,&#039;; t1++)
	{
		*(l_str + t2++) = buf[t1];
	}

    //Check multiplier
	if(!ctype)
	{
		x = 100;
	}
	else
	{
		x = 10;
	}

	//Convert string to FP number
	for(t1 = 0; l_str[t1] != 0; t1++)
	{
		if(l_str[t1] != &#039;.&#039;)
		{
		    rval += (double)(l_str[t1] - 48) * x;
		    x /= 10;
		}
	}

	free(l_str);

	//Get orientation indicator (N/S or W/E)
	if(!ctype)
	{
		sp = 6; //Longitude
	}
	else
	{
		sp = 4; //Lattitude
	}	

	//Search start position (4th or 6th &#039;,&#039;)
	//where letter is expected
	t1 = 0; t2 = 0;
	while(buf[t1] != 0 &amp;&amp; t2 != sp)
	{
		if(buf[t1] == &#039;,&#039;)
		{
			t2++;
		}
		t1++;
	}
	sp = t1;	

	retbuf[0] = buf[sp];

	return rval;
}	

//Ground speed
void get_ground_speed(char *buffer2, char *buffer3)
{

    int t1, t2;
    int p1, p2;

    //Zero buffer3
    for(t1 = 0; t1 &lt; RX_BUF_SIZE; t1++)
    {
		*(buffer3 + t1) = 0;
	}

	//Find 5th datafield
	t1 = 0;
	t2 = 0;
	while(t2 != 7 &amp;&amp; t1 &lt; RX_BUF_SIZE)
	{
		if(*(buffer2 + t1) == &#039;,&#039;)
		{
			t2++;
		}
		t1++;
	}
	p1 = t1;

	//Find end of datafield
	t1 = 0;
	t2 = 0;
	while(t2 != 8 &amp;&amp; t1 &lt; RX_BUF_SIZE)
	{
		if(*(buffer2 + t1) == &#039;,&#039;)
		{
			t2++;
		}
		t1++;
	}
	p2 = t1;

	//No data available
	if(p2 - p1 == 1)
	{
		return;
	}	

	//Copy relevant prt of string to buffer3
	t2 = 0;
    for(t1 = p1; t1 = RX_BUF_SIZE)
        {
			/*
			for(t1 = 0; t1 = 0; t1--)
		    {
			    //Scan for specific identifier of NMEA message RMC Example &quot;$GPRMC&quot;
		        valid = 1;
		        for(t2 = 0; t2 &lt; 5; t2++)
		        {
				    if(rx_buf[t1 + t2] != msg_code[t2])
				    {
					    valid = 0;
				    }
			    }		

		        if(valid) //Infotype OK, data found
		        {
					//Copy rx-buffer to buffer2

					//Init buffer2
					for(t2 = 0; t2 &lt; RX_BUF_SIZE; t2++)
					{
						*(buf2 + t2) = 0;
					}	

					//Copy relevant part of rx-buffer to buffer2
					t3 = 0;
					for(t2 = t1; t2 &lt; rx_buf_cnt &amp;&amp; rx_buf[t2] != &#039;*&#039;; t2++)
			        {
					    *(buf2 + t3) = rx_buf[t2];
					    t3++;
					}   

					lcd_cls();

					//Display information
					lcd_putstring(0, 0, &quot;  GPS Receiver&quot;);
                    lcd_putstring(1, 0, &quot;   DK7IH 2018&quot;);
                    _delay_ms(dispdelay);
					lcd_cls();

					//Message string
					lcd_putstring(0, 2, &quot;Message type&quot;);
					lcd_putstring(1, 4, msg_code);
					_delay_ms(dispdelay);
					lcd_cls();

					//QTH locator (Maidenhead)
					lcd_putstring(0, 2, &quot;QTH Locator&quot;);
					rbuf0 = malloc(4);
					rbuf1 = malloc(4);
					for(t1 = 0; t1 &lt; 4; t1++)
					{
						*(rbuf0 + t1) = 0;
						*(rbuf1 + t1) = 0;
					}	

					lon = get_gps_coordinate_decimal(buf2, 0, rbuf0); //Calc longitude
					lat = get_gps_coordinate_decimal(buf2, 1, rbuf1); //Calc latitude

					rstr = malloc(7);
					for(t1 = 0; t1 <span data-mce-type="bookmark" id="mce_SELREST_start" data-mce-style="overflow:hidden;line-height:0" style="overflow:hidden;line-height:0;"></span>&lt; 7; t1++)
					{
						*(rstr + t1) = 0;
					}
					calc_maidenhead(lon, rbuf0[0], lat, rbuf1[0], rstr);
					//calc_maidenhead(8.3362, &#039;E&#039;, 49.0145, &#039;N&#039;, rstr); //Test for &quot;JN49EA&quot;
					lcd_putstring(1, 4, rstr);
					_delay_ms(dispdelay);
				    free(rbuf0);
   			        free(rbuf1);
   			        free(rstr);
					lcd_cls(); 

					//DATE
					lcd_putstring(0, 6, &quot;Date&quot;);
					get_date(buf2, buf3);
					lcd_putstring(1, 4, buf3);
					_delay_ms(dispdelay);
					lcd_cls();

					//TIME
					lcd_putstring(0, 3, &quot;Time (UTC)&quot;);
					get_time(buf2, buf3);
					lcd_putstring(1, 4, buf3);
					_delay_ms(dispdelay);
					lcd_cls();

					//Receiver status
					lcd_putstring(0, 0, &quot;Receiver status&quot;);
					if(get_receiver_status(buf2) == 0)
					{
						lcd_putstring(1, 7, &quot;OK&quot;);
					}
					else
					{
						lcd_putstring(1, 7, &quot;ERR&quot;);
					}
					_delay_ms(dispdelay);
					lcd_cls();

					//Lattitude
					lcd_putstring(0, 4, &quot;Lattitude&quot;);

					get_latitude(buf2, buf3);
					lcd_putstring(1, 2, buf3);
					get_latitude_ns(buf2, buf3); //N or S
					lcd_putstring(1, 14, buf3);
					_delay_ms(dispdelay);
					lcd_cls();

					//Longitude
					lcd_putstring(0, 4, &quot;Longitude&quot;);
					get_longitude(buf2, buf3);
					lcd_putstring(1, 2, buf3);
					lcd_putstring(0, 4, &quot;Longitude&quot;); //E or W
					get_longitude_ew(buf2, buf3);
					lcd_putstring(1, 15, buf3);
					_delay_ms(dispdelay);
					lcd_cls();

                    //Ground speed
					lcd_putstring(0, 2, &quot;Ground speed&quot;);
					get_ground_speed(buf2, buf3);
					lcd_putstring(1, 5, buf3);
					lcd_putstring(1, 12, &quot;[kn]&quot;);
					_delay_ms(dispdelay);
					lcd_cls();
		        }
		    }
	        rx_buf_cnt = 0;
	    }
	    else
	    {
		    rx_buf[rx_buf_cnt++] = ch;
	    }
    }
	return 0;
}