Вы находитесь на странице: 1из 102

Development of an Electronic Compass

Development of an Electronic Compass

Presented by
Ting Song

A Thesis Submitted
In Total Fulfillment of the Requirements for
The Degree of

Master of Engineering in Electrical Engineering and


Information Technology

Department of Electrical Engineering and Information Technology,

Faculty of Electrical Engineering, Production Engineering and Information Technology

University of Applied Science Rosenheim

Rosenheim, February, 2006

1
Development of an Electronic Compass

TABLE OF CONTENTS

1 SYSTEM DESCRIPTION............................................................................................................................. 4
1.1 OVERVIEW OF THE SYSTEM ........................................................................................................................ 4
1.2 GENERAL INTRODUCTION ABOUT THE SIGNAL GENERATION ...................................................................... 6
1.3 SENSOR MODEL .......................................................................................................................................... 7
1.4 HOW TO OPERATE AND SWITCH SENSORS ................................................................................................... 8
1.5 LOW PASS FILTER AND GAIN ADJUSTMENT ................................................................................................. 9
1.6 SYNCHRONOUS RECTIFIER ......................................................................................................................... 9
1.7 INTEGRAL CONTROLLER........................................................................................................................... 10
1.8 U/I CONVERTER ....................................................................................................................................... 12
1.9 DC ZERO_BIAS GENERATION ................................................................................................................... 13
1.10 DC-DC CONVERTER AND INVERTER ...................................................................................................... 14
2. MATHEMATICS PART ........................................................................................................................... 15
2.1 TASK OF THE MATHEMATICS PART ........................................................................................................... 15
2.2 GENERAL INTRODUCTION ABOUT EARTH MAGNETIC FIELD...................................................................... 17
2.3 COORDINATE TRANSFORMATION ............................................................................................................. 17
2.3.1 Changing the heading angle α........................................................................................................ 18
2.3.2 Changing the pitch angle ............................................................................................................... 19
2.3.3 Changing the roll angle.................................................................................................................. 20
2.4 CALCULATE Z COMPONENT WITH MAGNITUDE. ....................................................................................... 21
2.5 CALCULATE PITCH ANGLE  AND ROLL ANGLE  USING AN ACCELEROMETER......................................... 22
2.6 CALCULATE HEADING ANGLE .................................................................................................................. 23
2.7 PROBLEM: THE SIGN OF THE HZ3 .............................................................................................................. 24
3 HARDWARE DESCRIPTION .................................................................................................................. 27
3.1 DC-DC CONVERTER ................................................................................................................................ 27
3.2 DC-DC INVERTER .................................................................................................................................... 28
3.3 TILT MEASUREMENT (ACCELEROMETER PART)........................................................................................ 29
3.3.1 Introduction of the tilt sensor ......................................................................................................... 29
3.3.2 Circuit description.......................................................................................................................... 29
3.4 SIGNAL GENERATION ............................................................................................................................... 30
3.4.1 Clock signal generation.................................................................................................................. 30
3.4.2 Switch signal generation ................................................................................................................ 31
3.4.3 Sinusoidal signal generation .......................................................................................................... 33
3.4.3.1 The Fourier Transform of the 50% duty cycle signal.................................................................. 33
3.4.3.2 Design the low pass filter .......................................................................................................... 33
3.4.3.3 Design the voltage divider. ........................................................................................................ 36
3.4.3.4 All pass filter network and amplification for the sinusoidal signal ........................................... 37
3.5 SENSOR MODEL ........................................................................................................................................ 39
3.5.1 The material and the structure of sensors ...................................................................................... 39
3.5.2 Sensor output signal ....................................................................................................................... 39
3.6 SENSOR ACTIVATION ............................................................................................................................... 40
3.6.1 Switch signal to active one sensor at a time................................................................................... 40
3.6.2 Switch the sensor output signal ...................................................................................................... 42
3.7 LOW PASS FILTER AND NON-INVERTING AMPLIFIER FOR THE SENSOR OUTPUT SIGNAL ............................. 42
3.8 SYNCHRONOUS RECTIFIER ....................................................................................................................... 43
3.8.1 The function of synchronous rectifier............................................................................................. 43
3.8.2 Mathematical description of the synchronous rectifier .................................................................. 44
3.8.3 How to realize the function by hardware? ..................................................................................... 46
3.9 INTEGRAL CONTROLLER........................................................................................................................... 47
3.9.1 Controller activation signal switch ................................................................................................ 47
3.9.2 Design the integral controller ........................................................................................................ 48
3.10 U/I CONVERTER ..................................................................................................................................... 48
3.11 DC ZERO_BIAS GENERATION ................................................................................................................. 50
3.12 MICROCONTROLLER PART ..................................................................................................................... 51

2
Development of an Electronic Compass

3.13 LAYOUT DESCRIPTION............................................................................................................................ 53


3.14 CONNECTOR PIN DESCRIPTION ............................................................................................................... 56
3.14.1 Connector between the microcontroller board and Sensor board ............................................... 56
3.14.2 Connector between Sensor board and Oscillation board............................................................. 56
4 SOFTWARE PART ..................................................................................................................................... 59
4.1 THE MAIN PROGRAM ................................................................................................................................ 59
4.2 MICROCONTROLLER INITIATION .............................................................................................................. 61
4.3 DISPLAY INITIATION ................................................................................................................................ 63
4.4 FUNCTION: ACCELEROMETER_CALIBRATION ........................................................................................... 65
4.5 FUNCTION: START_CONDITION ................................................................................................................ 67
4.6 FUNCTION: VERTICAL_CHECK .................................................................................................................. 68
4.7 FUNCTION: A_CALCULATION ................................................................................................................... 69
4.8 FUNCTION: WAIT ...................................................................................................................................... 70
4.9 FUNCTION: MAGNITUDE_MEASUREMENT ................................................................................................. 71
4.10 FUNCTION: HEADING_CALCULATION ..................................................................................................... 75
4.11 FUNCTION: ADC_READING ................................................................................................................... 78
4.12 FUNCTION: CONVERT ............................................................................................................................. 79
4.13 FUNCTION: ‘WRITE_STRING’ .................................................................................................................. 80
5. TEST DOCUMENTATION ...................................................................................................................... 82
5.1 TESTING FOR THE DC-DC CONVERTER AND INVERTER ............................................................................ 82
5.2 TESTING FOR THE DC-DC CONVERTER AND INVERTER ON THE FINAL PCB............................................. 83
5.3 TESTING FOR THE TILT MEASUREMENT CIRCUIT ....................................................................................... 84
5.4 TESTING THE OSCILLATOR PART .............................................................................................................. 84
5.5 CALIBRATION FOR THE ACCELEROMETER ................................................................................................ 85
5.6 CALIBRATION FOR THE MAGNETIC FIELD DETECTOR CIRCUIT OUTPUTS ................................................... 86
5.7 TESTING THE HORIZONTAL DEVIATION WITH ACCELEROMETER SUPPLY VOLTAGE CORRECTION AFTER
EVERY 10 TIMES OF THE HEADING CALCULATION.............................................................................................. 86
5.8 TESTING THE DEVIATION WITH 10,20,30,40 PITCH ....................................................................... 88

APPENDIX SOURCE CODE ..................................................................................................................... 89

3
Development of an Electronic Compass

1 System description

1.1 Overview of the system

This new electronic compass is based on the old one made in Operational Amplifier lab course
2004 in University of Applied Sciences in Rosenheim. The improvements are included:

 A new software programming


 A new layout design
 A new design for +5V and –5V power supply
 A new housing

The target of the thesis is to make electronic compass that can display the heading angle with
respect to geographic north when its pitch range is ≤±45º, roll range is ≤±15º. The old
electronic compass only can be used in the horizontal position. This new function is mainly
realized by software. The layout is another part of work in order to minimize the size. One of
the disadvantages of the old electronic compass is the power supply. The +5V and –5V that are
generated by a DC-DC converter and inverter is too noisy. In the software, we have to calculate
the average over 128 samples. It needs much time. To redesign the DC-DC converter and
inverter is also the task of this thesis.

The function of the electronic system is to display the direction of the electronic compass with
respect to geographic north. The angle between the magnetic and geographical meridians is
expressed as the magnetic declination. It is about 1.8 degree in Rosenheim. So if we get the
direction of the magnetic north, the geographic north can be easily calculated.

In order to calculate the direction of the electronic compass with respect to magnetic north, we
need the value of the components of the earth's magnetic field in two perpendicular directions,
i.e. we need two sensors. However, the sensors are not closed magnetic loops without leakage
flux. If they are mounted not to far apart (the electronic compass shall be small in order to use
it) one sensor will influence the other, if both are operated simultaneously and at the same
frequency. Experience shows, that the deviations arising from the mutual influence of one
sensor on the other lead to non-tolerable errors. /Mayr 2003/ So there are two possibilities:

 both sensors are operated at different frequencies or


 only one sensor is operated at a time

The latter method has the additional advantage, that the operating current is lower. However,
operating only one sensor at one time will make the entire system more complicated. Digital
control signals have to be generated in order to switch from one sensor to the other.

If one uses a relaxation oscillator (with the LMC 555 CMOS universal timer), it can be used to
generate the control signals as well as two other signals

 the sinusoidal signal with frequency f 0 by the use of a low pass filter and
 the digital signal with frequency 2 f 0 with exactly 50% duty cycle for the synchronous
rectifier ( mixer ) /Mayr 2003/

4
Development of an Electronic Compass

Two magnetic field controllers exist in the magnetic detector circuit. The controller generates
an zero_bias current, so that the static magnetic field component of the earth’s magnetic field is
exactly compensated. So the output of the controller can be used to measure the magnetic earth
field along the sensor axis.

The system block diagram is shown in figure 1.

Figure 1: System block diagram of electronic electronic compass /Mayr 2003/

5
Development of an Electronic Compass

1.2 General introduction about the signal generation

This part is the general introduction about signal generation. In this system, we need to
generate the sensor switch signal, the synchronous rectifier switch signal, the integral
controller control signal and the sensor operation signal.

The block diagram of the signal generation is shown in Figure 2.

Figure 2: The block diagram of signal generation part

A signal with frequency 4f0 with 6364Hz is generated from LMC555 timer circuit. A trimm
resistor can adjust this frequency. In order to get an exact 50% duty cycle signal, the frequency
of the LMC555 output signal is first reduced by a counter (MM74HC393) by a factor of two to
get 2 f 0 . This signal is used to control the synchronous rectifier for the extraction of the
second harmonic. In order to operate the sensors, the frequency of this signal is reduced by
factor of two again to get f 0 . By a 4’th order low pass filter, a low THD sinusoidal signal is
generated for sensor excitation. The following all pass filter is used to adjust the phase of the
sensor excitation signal, so that it fits to the phase of the synchronous rectifier.

With the counter, the frequency is reduced to f 0 / 4 . This signal is used to be the CLK of a D
flip flop. Then the frequency is reduced by a factor of 16 that is f 0 / 64 by the 2nd 4 bit counter.
This signal from the counter with frequency f 0 / 64 is the sensor switch signal. It controls,
that only one sensor active at a time. After a D flip flop, the input signal with frequency
f 0 / 64 is delayed by a clock pulse. Then, the two signals with same frequency f 0 / 64 are
used as inputs to a NOR and an AND gate in order to generate the controller activation signals.
(The AND gate is realized by three NOR gates is our design, because they are already there in
the chip. See chapter 3.4.2 in detail)

6
Development of an Electronic Compass

1.3 Sensor model

Figure 3: The structure of the sensor

The structure of the sensor is shown in figure 3. There are two coils around the ceramic tube.
One is the excitation coil for sinusoidal current input. Another one is the signal coil to output
the signal.

The sensors are made by soft magnetic materials, which are very nonlinear. So, there is a non
linear dependence of the magnetic flux density B within the soft magnetic material and the
magnetic field H that is described in the famous hysteresis loop. Because of the hysteresis loop,
the actual magnetic flux density does not only depend on the current magnetic field H, but also
on the entire history of H. For this feature, the magnetic field controller is used in the system.
This part will be introduced in 1.7 integral controller. /Mayr 2003/

Another of fact of nonlinearity is generating of harmonics. If a linear system is excited by a


purely sinusoidal signal, the output signal of this system will also be purely sinusoidal with the
frequency of the input signal. There will be no harmonics. But if the system is nonlinear,
harmonics will be generated. The output signal will contain additional frequency with 2f0, 3f0,
4f0….nf0, n=2,3,…. /Mayr 2003/

The sensor is excited by sinusoidal current signal with frequency f 0 . With the theory above,
the output voltage signal contains the base sinusoidal voltage signal with frequency f 0 and its
harmonics.

For the pure sinusoidal magnetic field, the hysteresis loop will run through symmetrically and
B(t+T/2)=-B(t). If the magnetic field H gets a DC component, i.e. the component of the earth’s
magnetic field in sensor direction, then B(t+T/2) ≠ -B(t). This means, that now there will be a
frequency component with for example 2f0 in the output signal. Experiments show, that the
second harmonic (frequency 2f0) is the even harmonic with the highest level. So the amplitude
of the second harmonic (frequency 2f0) is the best suited for use as quantity to measure
magnetic field. /Mayr 2003/

7
Development of an Electronic Compass

1.4 How to operate and switch sensors

In order to avoid any influence between two sensors, only one sensor is operated at a time. We
use a multiplexer to realize this function.

The function diagram of sensor switching is shown in figure 4. The input of the switch is the
sinusoidal signal. The 50% duty cycle square signal with frequency 25Hz controls the switch.
The diagram of switching the sensors is shown in figure 5. When the switch control signal is
high, the sinusoidal signal is used as the input to the E/W sensor. The sensor E/W sensor is
active. However, when the switch control signal is low, the N/S sensor is active.

Figure 4: The function diagram of sensor switching

1591Hz_sine

Sensor switch sensor N/S active


control signal sensor E/W active

E/W sensor
switch control
signal

N/S sensor
switch control
signal

Figure 5: The diagram of switching sensor

The outputs from the sensors have to be combined to one signal since the following part of the
electronic circuit only exists once. In one period, the combined signal is the output from E/W
sensor in the half of the time. For another half of the time it is the output from N/S sensor. The
principle diagram of the sensor output signal switching is shown in figure 6. The signals from
two sensors are the inputs of the switch. In the time that sensor N/S is active and sensor switch
control signal is low, the output is the signal from sensor N/S. However, in the time that sensor
E/W is active and sensor control switch signal is high, outputs is the signal from sensor E/W.

8
Development of an Electronic Compass

Sensor switch control signal with frequency 25Hz


Signal from
E/W sensor
Output
Signal from
N/S sensor

Figure 6: The principle of sensor output signal switching

1.5 Low pass filter and gain adjustment

This part consists of a low pass filter and a gain adjustment for the sensor output signal. The
output of the sensors contains harmonics. We use the low pass filter to attenuate the harmonics
of the sensor output signal with frequencies higher than 2 f 0 because they would also
generate a DC-output at the synchronous rectifier.

We use an operational amplifier IC OPA4743 to realize the low pass filter. The following part
after the low pass filter is a gain adjustment, which is a non-inverting amplifier circuit. The
amplitude is amplified by a factor of 3.2. It is also realized by OPA4743.

1.6 Synchronous rectifier

In order to measure the amplitude of the 2nd harmonic, the signal output from the gain
adjustment is used as input to a synchronous rectifier. It extracts the second harmonic
(frequency f 0 ) of the input signal.

The principle of this part is showed in figure 7. The synchronous rectifier consists of a voltage
inverter and a multiplexer. The inverter is realized by an operational amplifier. The Uin is the
input for synchronous rectifier. It is a combined of the base signal with frequency f 0 and its
harmonics. The switch control signal is U_2F. It is a square signal with frequency 2 f 0 . The
value of the output of this synchronous rectifier depends on the component of the magnetic
field in sensor direction. The switch function is realized by an analog multiplexer, 75HC4053.

Switch
Signal
U_2F
9
Development of an Electronic Compass

Uin Inverter
amplifier
Uout

Synchronous
rectifier

Figure 7: The principle of synchronous rectifier

1.7 Integral controller

The rectified signal is used for an integral controller to measure the magnetic field.

First, I want to introduce the generation of the switch signal to activate and deactivate the
integral controller. The switch signal that is used to control the switching from one sensor to
another is very slow. It is generated at the very end of the signal generation part. There is a
problem for the following integral controller. The transient behaviour of the low pass filter and
gain adjustment during the switching process will disturb the controller. In order to neglect this
switching transient behaviour, we have to generate two additional signals that activate the
controller a certain time after the switching over. This part is done in the end of the signal
generating part. /Mary 2003/ The principle of generating the switch signal to control the
integral controller is shown figure 8.

With the two 4 bit counters, we get the square signal with frequency f 0 / 64 that called signal
A. It is used as input to a D flip flop to get signal B. B has one clock time delay compared with
A. Then signals A and B are used as inputs to a AND gates to generate the controller signals,
UZ_E/W and UZ_N/S.

UZ _ E / W  A  B
(from chapter 3.4.2)
UZ _ N / S  A  B

10
Development of an Electronic Compass

CLK

D input of D
flip flop

Output Q of
D flip flop

Controller E/W
UZ_E/W Controller E/W inactive
active

Controller N/S
UZ_N/S inactive Controller N/S
active

Figure 8: The principle of generating switch signal to control the integral controller.

There are several reasons that we prefer an integral controller as the analogue controller. They
are expressed clearly in the lecture of Op Amp circuit design by Prof.Mayr.

The excitation frequency to excite the coil is about 1590 Hz that is f0 / 4. So the time, for which
a single controller is active (hereafter referred to as the active time in contrast to the inactive
time), is
1 1 1 1
t active      20.1 ms .
2 f 0 / 256 2 6360 Hz / 256

If one would like to have longer active times, he would have to use an additional counter.
However, note that both sensors have to be operated in order to get the angle with respect to
magnetic north. The active time for a single sensor can’t become too long, otherwise the time
for a reaction of the electronic compass display will become very long (i.e. seconds), which
will be very inconvenient.

The controller cannot be made very fast. The reason is the following. The input signal for the
controller is not a "DC voltage", i.e. a very slowly changing signal, but the switched output
signal of the sensor coil after notch filtering, amplification and phase shifting. The
synchronous rectifier acts like a mixer: the local oscillator frequency is 2 f 0 , the input signal
from the sensor contains components with frequencies f 0 , 2 f 0 , 4 f 0 , 8 f 0 , . . . So the lowest
components in the output signal of the "mixer" will be a DC part (from the 2 f 0 component)
and f 0 (from the f 0 component of the sensor signal mixed with the local oscillator
frequency 2 f 0 ). Many higher frequency components will also be generated.

11
Development of an Electronic Compass

The output of the controller should be a very slowly changing signal, which is proportional to
the component of the earth's magnetic field in the direction of the sensor. In a fixed position,
the controller output signal would ideally be a pure DC voltage. However, the controller is a
linear system with a specific frequency response. So the non-DC frequency components have
to be attenuated by the controller to a sufficiently small level.

This means, that during one active time, the controller will not be able to settle to its final
value. It must remain in the state, which is reached at the end of the active time during its
inactive time and then begin again at the state, which has been reached at the end of the former
active time. The only controller with this behaviour is the integral controller: If the input
during the inactive time is identically zero (in our case zero input current!), the output will not
change. So an integral controller will be best suited for our purposes. Another advantage of the
integral controller is the fact, that there will not be any static control deviation. /Mayr 2003/

The principle of integral controller is shown in figure 9.

Figure 9: The principle of integral controller

The output voltage can be calculated as follows:


1 t
RC 0
Vout (t )   uin (t ' )dt 'V (t  0)

V (t  0) is the initial output voltage


 1 RC is the integral gain

1.8 U/I converter

This part is to convert the sum of the excitation voltage from sensors and the controller output
voltage to a current. There are three voltage inputs of this converter. They are the sinusoid
sensor switch signal, the controller output voltage and the zero_bias compensation voltage.

The principle of U/I converter is shown in figure 10.

12
Development of an Electronic Compass

Figure 10: The principle of the U/I converter

If R1 *    R1 , R2 *    R2 , and R3 *    R3 ,

then we can calculate the output current.


U U U
I out  1  2  3
R1 * R2 * R3 *

1.9 DC zero_bias generation

In order to be used as input to the ADC of the microcontroller, the signal has to be between 0V
to the reference voltage of microcontroller, which is 2.5V. The controller output contains
positive and negative voltages. So the VREF from the microcontroller that about 2.5V is used to
generate the DC zero_bias voltage. The principle of DC zero_bias generation is shown in
figure 11.

Figure 11: The principle of DC zero_bias generation.


13
Development of an Electronic Compass

1.10 DC-DC converter and inverter

The DC-DC converter and inverter are not included in the system diagram. They generate the
+5V and –5V power supply for the whole system. We use the MAX1706 to be the DC-DC
converter. The noise of the +5V output is much lower than the old electronic compass. It has a
maximum value of +3mV and minimum value of –3mV.

The DC-DC inverter is realized by MAX1853. It generates the negative output about –5V.

Literature
/Mayr 2003/ Mayr Wolfgang,
Lecture in Operational Amplifier circuit design 2,
University of Applied Sciences Rosenheim
Winter 2003/04

14
Development of an Electronic Compass

2. Mathematics part

2.1 Task of the mathematics part

The aim of the mathematics part is to use the signals from the magnetic detector circuit and tilt
measurement circuit to calculate the heading angle  with respect to magnetic north.

We use the definition of heading, pitch and roll for an airplane. Airplane convention defines
the attitude parameters in terms of three angles: heading  , pitch  and roll  (see figure
12). These angles are referenced to the local horizontal plane. That is, the plane perpendicular
to the earth’s gravitational vector. Heading is defined as the angle in the local horizontal plane
measured clockwise from a true North (earth’ polar axis) direction. Pitch is defined as the
angle between the aircraft’s longitudinal axis and the local horizontal plane (positive for nose
up). Roll is defined as the angle about the longitudinal axis between the local horizontal plane
and the actual flight orientation (positive for right wing down). /Michael/ For the electronic
compass, when looking at the display, it is like we watch the airplane from backward.

Figure 12: Coordinate direction (X,Y,Z) and attitude orientation (roll, pitch) on an airplane.
/Michael/

Figure13: The heading angle .

15
Development of an Electronic Compass

There are two perpendicular magnetic sensors fixed on the electronic compass. The directions
of the sensors are X1 and Y1. The magnetic components we get from the circuit are in the

sensor direction. The earth magnetic vector H is in the plane XZ. Assume that the electronic
compass is horizontal, so the axes X, Y, X1 and Y1 are horizontal. There is a heading angle 
between the X axis and the sensor direction X1. This angle is what we want to know.

In the case that the electronic compass is horizontal like in figure 13. It is very easy to calculate
the heading angle .

H X 1  Hx  cos 
(1)
H Y 1  Hx  sin 

(HX1 is the earth magnetic component in X1 direction and HY1 is the magnetic component in
Y1 direction. HX1 and HY1 can be measured by the sensors.)

But if the electronic compass is not horizontal, the calculation is much more complicated. We
must first calculate the magnetic value in the horizontal plane. And then calculate the heading
angle with formula 5.

X1

X3

Y1

Y3

Z3

Z1
Figure 14: The electronic compass is no more horizontal. Axes X1 and Y1 are
perpendicular and in the horizontal plane. They have been the sensors directions before the
electronic compass was tilt. Z1 is vertical to ground. X3 and Y3 are the actual sensors
direction. Z3 is vertical to the X3Y3 plane.

In figure 14, the electronic compass is not horizontal anymore. It has a roll angleψand pitch
angleθ. The output magnetic value HX3 and HY3 are in the directions X3 and Y3. In order to
calculate the heading angle  by formula 5, we need to transfer the HX3, HY3, HZ3 to the
direction X1, Y1 and Z1. So the main task of the mathematics part is to find this transformation
and how to get the values of the variables in the transformation.

16
Development of an Electronic Compass

2.2 General introduction about earth magnetic field.

Figure 15: Magnetic field in the earth /Honeywell 2003/

The earth magnetic vectors are shown in figure 15. The earth's magnetic field is about 0.6
gauss in an open-air environment, and has a direction from the magnetic south pole to the
magnetic north pole. This pointing to the north pole is the basis for magnetic electronic
compassing. At the equator, the magnetic field direction is entirely a horizontal vector, but as
the electronic compass is moved further into the northern or southern hemispheres, the
magnetic field will point partially downwards (northern hemisphere) or upwards (southern
hemisphere). This angle down or up at the earth's surface is called the inclination (dip) angle.
/Honeywell 2003/

city Berlin Düsseldorf Frankfurt Hamburg München


inclination 67.6 68.8 65.6 66.9 63.9
Table 1: The inclination of five city in Germany /Boll 1990/

2.3 Coordinate transformation

The sensors are fixed with respect to the electronic compass. So every time you move the
electronic compass, the magnetic values are in different directions. The values that are not in
horizontal plane are not suitable to be used directly to calculate the heading angle. It is
necessary to transfer them to the direction that is horizontal.

We assume that, the movement of the electronic compass is divided to three steps. The original
directions of two sensors are X and Y. First, turn left or right that the electronic compass has a
heading angle . The directions of sensors become X1 and Y1. The plane X1Y1 is horizontal.
Second, move the electronic compass, so that has a pitch angle θwith respect to horizontal
plane. The directions of sensors become X2 and Y2. Third step, turn up or down so that the

17
Development of an Electronic Compass

electronic compass that has a roll angle ψ. The directions of sensors become X3 and Y3.
Finally, two magnetic components are in the directions X3 and Y3. So if we can transfer the
magnetic components to the directions X1 and Y1, the heading angle  can be easily
calculated.

2.3.1 Changing the heading angle α.

Figure 16: The axis changes if electronic compass has a heading angle with respect to
magnetic north. In the left diagram, the compass is seen from the back. In this case,   0 .
The electronic compass is seen from top in the right diagram.

The transformation of the coordinates when the electronic compass has a heading angle  with

respect to the earth magnetic vector H is shown in figure 16. The directions of the sensors are
X1 and Y1.

In figure 16, the earth magnetic vector H is in the XZ plane, that we call it magnetic plane. The

axis Z is vertical to ground. Magnetic vector H has its value HX, HY and HZ in XYZ
coordinates. We initially H Y  0 , so that the X-axis points exactly towards magnetic north.
Now, we assume the electronic compass is horizontal, but has a heading angle α with respect to
the magnetic north. The magnetic components Hx1 and Hy1 are in the directions X1 and Y1.
The Z direction doesn’t change. The magnetic vector expressions in XY coordinate and X1Y1
coordinate are as follows:

H  HH 
  X   
H   HY  0  (2)
H  H 
 Z   V

18
Development of an Electronic Compass

 H   H  cos  
  X1   H 
H 1   H Y 1     H H  sin   (3)
H   HV 
 Z1   

2.3.2 Changing the pitch angle

X2 Electronic
compass X2
θ
θ
X1
X1 HX1

Z2 HZ1

Y1
Z1
Z2

Figure 17: In the left diagram, the compass is seen from the back. The X1Y1 plane is
horizontal. The sensors are in the directions X1 and Y1 before the movement. After the
movement, the sensors are in the directions X2 and Y2. The direction of the Y1 axis does not
change. The right diagram is seen from left.

The electronic compass is not horizontal anymore after adding a pitch angle. It turns around the
Y1 axis. The relation of the magnetic vector’s value in two coordinates is shown below.

 H X 2   cos 0  sin   H X 1   H X1 
      
 HY 2    0 1 0  H Y 1   DP   H Y 1  (4)
 H   sin  0 cos  H Z 1  H 
 Z2    Z1 

 cos 0 sin  
1  
DP  0 1 0  (5)
  sin 
 0 cos 

 H X1  HX2 
1   1  
Using DP instead  H Y 1   DP   H Y 2  (6)
H  H 
 Z1   Z2 

19
Development of an Electronic Compass

2.3.3 Changing the roll angle

X2
Electronic
compass

Y2 HY2
Y2
ψ
ψ
Y3 HZ2 Y3
Z3
Z3
Z2

Figure 18: In the left diagram, the compass is seen from the back. The sensors are in the
directions X2 and Y2 when the roll   0 . After the movement, the directions of sensors
change to X3 and Y3. The direction of the X2 axis does not change in this case. The right
diagram is seen from the back.

The electronic compass is turned around the X2 axis and then the directions of sensors are Y3
and Z3. The X2 direction does not change. So there is a roll angle ψ between the Y2 and Y3
axes. We can find out the relation of magnetic vector’s value in two coordinates:

 H X 3  1 0 0  H X 2  HX2 
      
 H Y 3    0 cos sin  H Y 2   DR   H Y 2  (7)
 H   0  sin cos  H Z 2  H 
 Z3    Z2 

1 0 0 
1  
DR   0 cos  sin  (8)
 0 sin
 cos 

HX2  H X3 
1   1  
Using DR instead  H Y 2   DR   H Y 3  (9)
H  H 
 Z2   Z3 

HX3   H X1 
   
With the calculation above, we can easily get  H Y 3   D P  DR   H Y 1  (10)
H  H 
 Z3   Z1 

20
Development of an Electronic Compass

 H X1  HX3 
  1 1  
 H Y 1   D P  DR   H Y 3  (11)
H  H 
 Z1   Z3 

1 1
With equation 9 and 12, the DP  DR can be calculated as follows:

 cos 0 sin    1 0 0  cos sin  sin sin  cos 


1 1      
D P DR  0 1 0    0 cos  sin    0 cos  sin  (12)
  sin 
 0 cos   0 sin cos    sin  cos sin cos cos 

H X 1  cosH X 3  sin  sinH Y 3  sin  cosH Z 3


H Y 1  cosH Y 3  sinH Z 3 (13)
H Z 1   sin H X 3  cos sinH Y 3  cos cosH Z 3

In the above formula, there are five variables. They are , , Hx3, Hy3 and Hz3. The Hx3 and
Hy3 we get from the magnetic detector circuit. So the next step is to calculate the unknown
variables ,  and Hz3.

2.4 Calculate Z component with magnitude.

The magnitude is calculated with software. The electronic compass has two perpendicular
direction sensors. The magnitude of magnetic vector is sensitivityant in any position. i.e.
2 2 2
H 2  H X  HY  H Z (14)

H X and H Y are the sensor direction. H Z is in the direction vertical to the electronic
2 2
compass. So the H Z can now be calculated as: H Z   H 2  H X  HY

In order to fix the sign, restrictions on the roll and pitch angle have to be made. (see chapter
2.7)

Figure 19: How to calculate the magnitude. The sensors are in the directions X and Y. Z
axis is vertical to XY plane.
21
Development of an Electronic Compass

In figure 19, when the electronic compass is horizontal, we can get the magnetic component in
X and Y directions. Because the magnetic vector is downwards, the z component is
sensitivityant and the x and y components are sinusoidal function in principle. If the electronic
compass is vertical, the sensor direction X becomes vertical and Y is still horizontal. The
magnetic value in X direction becomes sensitivityant and the components in the Y and Z
2 2
direction are a sinusoidal function. The software calculates the H X  H Y when the
electronic compass is turning around 360 degree. There must have the maximum value that
equals to H 2 , in which place the Z component is zero. In this case, the magnitude
2 2
H  H Y  H X . In this way, we can get the magnitude H. Even, if the compass is not
exactly turned down vertically during the rotation. There must be a position, where H Z  0 ,
because H Z is sometimes positive sometimes negative and continues. So it must go through
zero.

2.5 Calculate pitch angle  and roll angle  using an

accelerometer.

The pitch and roll value we get from accelerometer ADXL203. It can be used as 2-axis tilt
sensor with a roll axis and a pitch axis. The output signals AX and AY from the accelerometer
is converted to an acceleration that varies between –1g and +1g.


gH

g  g gravity field
direction

Figure 20: The gravity vector when the electronic compass has a pitch angle 

The accelerometer can detect the gravity vector in two directions: the direction in electronic
compass plane and another direction vertical to the compass. These two directions are g H
and g  in the figure.

The gravity vector when the electronic compass only has a pitch angle is:

22
Development of an Electronic Compass

g H  g  sin 
(15)
g   g  cos

If turn the electronic compass has a roll angle , the g H doesn’t change and g  is calculated
as follows: g   g  cos  sin /Analog 2004/

So, the output signals of the accelerometer are:

AX  zero _ bias  sensitivity  g  sin 


AY  zero _ bias  sensitivity  g  cos  sin

The nominal sensitivity value is 1000mV/g if VS  5V . The zero bias is the accelerometer
output if it is horizontal, and it is 2.5V nominally.

The allowed pitch and roll value is -45º≤ θ ≤+45º → cosθ > 0
-15º≤ ψ ≤+15º → cosψ > 0

sin   ( AX  zero _ bias ) /( sensitivity  g )  cos   1  sin 2 


So (16)
sin  ( AY  zero _ bias ) /( sensitivity  g  cos )  cos   1  sin 
2

In this case, we can use the square root function to get the sine from the cosine.

2.6 Calculate heading angle

We now have all the value needed. With formula 17,

H X 1  cosH X 3  sin  sinH Y 3  sin  cosH Z 3


H Y 1  cosH Y 3  sinH Z 3
H Z 1   sin H X 3  cos sinH Y 3  cos cosH Z 3

sin   ( AX  zero _ bias ) /( sensitivity  g )


With
cos    1  sin 2 

sin  ( AY  zeor _ bias ) /( sensitivity  g  cos )


cos   1  sin 2 

2 2
H Z3  H 2  H X 3  HY 3

We can get the value of the magnetic components in horizontal.

H X 1  H H cos 
Because:
H Y 1   H H sin 

23
Development of an Electronic Compass

So the heading angle is: tan   sin  / cos    H Y 1 / H X 1


  a tan( H Y 1 / H X 1 ),90     90 
We use the following formula to transfer the heading angle to 0 ~ 360 . The ‘heading’ is the
degree we want to display. The  is the angle we get from the arc tangent function.

heading    180.0 if H X 1  0
heading    360.0 if H X 1  0, H Y 1  0
In other cases, heading  

The detailed explanation for the above transformation is in chapter 4.10.

2.7 Problem: the sign of the HZ3

Only the value HX3 and HY3 for H-measurement can be known directed from sensor that fixed
on the electronic compass. The HZ3 can be calculated as follows:

2 2 2
H X 3  HY 3  H Z3  H 2
2 2
H Z3   H 2  H X 3  HY3

But in the calculation, HZ3 is considered positive. The allowed pith angle is ≤±45º, and the
allowed roll angle is ≤±15º. So we must be sure that in the pitch angle range and the roll angle
range, HZ3 is positive all the time. The following two figures show the situation, when the
electronic compass has largest pitch.

geog.North
Hz3
X
Worst negative pitch
Z
H

Figure 21: HZ3 when the pitch angle is  45 .

24
Development of an Electronic Compass

Worst positive pitch


X

geog. North

Hz3

H Z

Figure 22: HZ3 when the pitch angle is 45 .

So HZ3 should be positive even in the worst case. We checked the allowed θ value range when
α is 0º~360º and ψ is +15º and -15º. The allowed range of the θ value means, for example, α
is 15 º and ψ=15 º, θ in this range, HZ3 is positive all the time.

The heading The allowed range of pitch The allowed range of pitch
angle α (º) angle  when ψ=15 (º) angle  when ψ= -15 (º)

0 θ>-65 (º) θ>-65 (º)


15 θ>-68 θ>-64
30 θ>-72 θ>-65
45 θ>-77 θ>-67
60 θ>-83 θ>-71
75 θ>-90 θ>-71
90 -90<θ<90 -83<θ<83
105 -90<θ<90 -90<θ<77
120 -90<θ<83 -90<θ<71
135 -90<θ<77 -90<θ<67
150 -90<θ<72 -90<θ<65
165 -90<θ<68 -90<θ<65
180 -90<θ<65 -90<θ<65
195 -90<θ<65 -90<θ<68
210 -90<θ<65 -90<θ<72
225 -90<θ<67 -90<θ<77
240 -90<θ<71 -90<θ<83
255 -90<θ<77 -90<θ<90
270 -84<θ<84 -90<θ<90
285 θ>-77 -90<θ<90
300 θ>-71 θ>-83
315 θ>-67 θ>-77
330 θ>-65 θ>-72
345 θ>-65 θ>-68
360 θ>-65 θ>-65
Tabele 1: Checking the allowed θ value.

25
Development of an Electronic Compass

We can see the minimum allowed θ value is period and larger than -45(the minimum value that
the user will turn) whatever α equals to any value. But when ψ is -15º and α is 0~30º, the θ
value is critical. So I decrease the α distance to 2º.

α ( º) The allowed range of pitch


angle  when ψ= -15 (º)
2 θ>-64.8 (º)
4 θ>-64.7
6 θ>-64.5
8 θ>-64.4
10 θ>-64.3
12 θ>-64.2
14 θ>-64.1
16 θ>-64.1
18 θ>-64.1
20 θ>-64.1
22 θ>-64.2
24 θ>-64.3
26 θ>-64.4
28 θ>-64.6
30 θ>-64.8
Table 2: Checking the allowed θ value when α is from 2ºto 30º.

From the above two tables, we can make sure that in the range -45º≤ θ ≤+45ºand -15º≤ ψ ≤
+15º, value of HZ3 must be positive.

Literature
/Honeywell 2003/ www.ssec.honeywell.com
Internet text and figure
Honeywell International
2003

/Boll/ Boll Richard


Weichmagnetische Werkstoffe
Vacuumschmelze GmbH
Hanau, 1990

/Michael/ Michael J. Caruso


Application of Magnetic Sensor For Low Cost Compass System
Honeywell, SSEC

/Analog 2004/ Datasheet for the ADXL 203,


Analog Devices, Inc.
One Technology Way, P.O.Box.9106, Norwood, MA02062-9106, USA,
2004

26
Development of an Electronic Compass

3 Hardware description

3.1 DC-DC converter

The system needs +5V as the positive power supply. For the old electronic compass, the +5V
power supply has strong ripples with +600mV for the maximum value and -400mV for the
minimum value. The noise of the power supply causes ripples of the accelerometer outputs and
also the magnetic circuit outputs that are input to the analog to digital converter (simply use
ADC instead below) of the microcontroller. So in the software for the old electronic compass,
the inputs for the ADC of the microcontroller are read 128 times and divided by 128 to get the
average. That needs lot of time. This situation is improved in the new electronic compass. The
maximum positive value is reduced to +3mV and the minimum negative value is -3mV.

We use the MAX 1706 to generate the +5V from battery input. The allowed range of the
battery voltage is from 0.7V to 5.5V. The circuit diagram is shown in figure 23.

Figure 23: The circuit diagram of the DC-DC converter.

There are two main functions of the Max1706. One is a step-up converter with step-up output
POUT. Another is a linear regulator with output LDO. The step-up converter generates an
adjustable output to supply both power circuitry and the internal low-dropout linear regulator.
The linear regulator steps down the output from the step-up converter and reduces switching
ripples. The maximum output current is limited by the current available from the boost
converter and by the voltage different between OUT and LDO. We use a 22uF capacitor C6
with a low equivalent series resistance (ESR) at the output for low ripples. During power-up,
the linear regulator remains off until the step-up converter goes into regulation for the first
time. /MAXIM 97/

The linear regulator is working in track mode. Connecting TRACK to the step-up converter
output implements a tracking mode, that sets the step-up converter outputs to 300mV above the
linear-regulator output, improving efficiency. In track mode, the feedback for the step-up
converter is derived from the OUT pin. When TRACK is low, the step-up converter and linear
regulator are separately controlled by their respective feedback inputs, FB and FBLCO.
TRACK is a logic input with a 0.5Vout threshold, and should be hardwired or switched with a

27
Development of an Electronic Compass

slew rate exceeding 1V/us. VLDO must be set above 2.3V for track mode to operate properly.
/MAX IM 97/

On power-up with TRACK=OUT, the step-up converter initially uses the FB input to regulates
its output. After the step-up converter goes into regulation for the first time, the linear regulator
turns on. When the linear regulator reaches 2.3V, track mode is enabled and the step-up
converter is regulated to 300mV above the linear regulator output. /3/

To set the low-dropout linear-regulator output, we use a resistor voltage-divider connected to


FBLDO from LDO to GND. We set the output to be 5.0V using the following formula:

 V   5.0V 
R4  R5  LDO  1  R5   1  3R5 , (17)
 VFBLDO   1.250V 

where VFBLDO, the linear-regulator feedback input, is 1.250V. The above formula is from the
datasheet of Max1706 by MAXIM. Since the input bias current into FBLDO is less than 50nA,
and R4 can be a large value. We choose
R4  470k and R5  150k

With the above values, the linear regulator output is:


R   470k 
VLDO  VFBLDO   4  1  1.250V    1  5.17V (18)
 R5   150k 

The step-up converter output POUT is above the linear regulator by 300mV and is 5.47V.

The inductor L2 with 330H is used to reduce the ripples to the value of  3mV as stated in
chapter 3.1.1.

3.2 DC-DC inverter

We use the MAX1853 to realize the DC-DC inverter. It generates a negative output of
 1 Vin . The circuit diagram is shown in figure 24. The input voltage is +5V. The output is
–5V. SHDN is driven to high for normal operation. The C1- ad C1+ is the negative and
positive terminal of the flying capacitor C1. The value of C1 and output capacitor C2 is
recommended in the datasheet. R17 and C19 are used for filtering.

Figure 24: The circuit diagram of the DC-DC inverter.

28
Development of an Electronic Compass

3.3 Tilt measurement (Accelerometer part)

3.3.1 Introduction of the tilt sensor

The pitch and roll value are measured by the accelerometer ADXL203 that is used as a
dual-axis tilt sensor. It uses the force of gravity as an input vector to determine the orientation
of an object in space. When the accelerometer is oriented in such way that both its X axis and
Y axis are initially parallel to the earth's surface, it can be used as a 2-axis tilt sensor with a roll
axis and a pitch axis. Once the output signals from the accelerometer have been converted to an
acceleration, that varies between -1g and +1g, the sine value of the pitch and roll angle are
calculated as: (/Analog 2004/ and chapter 2.5 of this thesis)

sin( pitch)  ( AX  zero _ bias ) ( sensitivity  g )


(19)
sin(roll )  ( AY  zero _ bias ) ( sensitivity  g  cos( pitch))

The AX and AY are the accelerometer outputs from XOUT and YOUT. The zero bias is the
accelerometer output, if it is horizontal.

3.3.2 Circuit description

Figure 25: Circuit diagram of the tilt measurement.

The circuit diagram of this part is shown in figure 25. R2 and the capacitors C26 and C9 are
used to decouple the ADXL203 from power supply noise, as recommended in the datasheet.

The bandwidth of the accelerometer output is selected by capacitor C13 and C8. These two
capacitors are added to implement low-pass filtering and noise reduction. The accelerometer

29
Development of an Electronic Compass

has an internal filter resistor R filt  32k for X out and Yout . So the bandwidth of output is
calculated by:
1
f 3dB  /Analog 2004/ (20)
2  R filt  C

We choose the bandwidth to be 3.3Hz by using a 1.5uF filter capacitor. The Xout and Yout vary
from 1.5V to 3.5V at -1g and +1g, respectively. Because the reference voltage of the
microcontroller is 2.5V, the Xout and Yout have to be below 2.5V. The voltage dividers (R11
and R10, R14 and R19 respectively) realize this function. So the range of the voltage, which is
input to the ADC of is reduced to value between 0.75V~1.75V. The voltage followers (IC3A,
IC3B) after the voltage dividers are used as a buffer to eliminate loading effects.

The outputs sensitivity varies proportionally to supply voltage. According to the datasheet of
ADXL203, at VS  5V , the sensitivity is 1000mV / g . At VS  3V , it is 560mV / g . The
zero g bias output is also ratiometric, so the zero g output is nominally equal to VS / 2 at all
supply voltages. In the software, after every 10 times of the heading calculation, the supply
voltage of the accelerometer is read once to adjust the sensitivity, if necessary.

Because the output of accelerometer is very sensitive with respect to the power supply voltage
(see above), a power testing circuit is necessary. The reference voltage of the microcontroller is
2.5V and the power testing signal has to be below 2.5V in order to be used as input to the ADC.
The voltage at the supply power pin Vs of accelerometer is about 5.0V. It is divided by a factor
of 2.79 by a voltage divider and then inputs to the ADC of microcontroller after a voltage
follower.

3.4 Signal generation

3.4.1 Clock signal generation

The signal generation is divided to 3 parts: clock signal generation, switch signal generation
and sinusoidal signal generation. We introduce the clock signal generation part first.

Figure 26: The circuit diagram of the clock signal generation.

30
Development of an Electronic Compass

The circuit diagram of the clock signal generation is shown in figure 26. The clock of the
whole system is generated by a 555 timer circuit. The frequency of the output is determined as
follows:

1.43
f0  /National 2000/ (21)
R23  2  R24  Rtrimm  C19
The trim resistor is used to adjust the output frequency, in order to get output signal with
frequency 4 f 0 (the reason is explained in chapter 3.4.3.2). This frequency is determined by
the operation frequency of the magnetic field detector circuit. The values of the components
are set as follows: R23  1.5k , R24  10k and C19  10n

The trim resistor is Rtrimm  1k . By adjusting this resistor, the frequency of the output signal can
be set from 6651Hz to 6085Hz.

3.4.2 Switch signal generation

The second part is the switch signal generation. The circuit diagram is shown in figure 27.

Figure 27: The circuit diagram of the switch signal generation.

The clock signal generated by the timer 555 is the input to an 8 bit counter to generate 4 signals
with frequency 2 f 0 , f 0 , f 0 / 4 and f 0 / 64 . With the first counter, the signals with
frequency 2 f 0 , f 0 , f 0 / 4 are generated. The functions of these signals are follows: We
require a 50% duty cycle signal. However, the output signal of the 555 is not a 50% duty cycle
signal. So the frequency of the 555 circuit output signal is first reduced by a counter
(MM74HC393) by a factor of two, which is 2 f 0 (3183Hz). This signal is named U_2F. It is
used in the synchronous rectifier part. The signal with frequency f 0 (1591Hz) is used to
generate the sinusoidal signal. The signal with frequency f 0 / 4 is used to generate a delay
between the sensor and controller activation. (see chapter 3.9.1)

The signal with frequency f 0 / 4 is also the input to the second counter to generate the signal
with frequency f 0 / 64 . This 50% duty cycle signal controls, that only one sensor is active at
one time.

31
Development of an Electronic Compass

The following part consists of a D-flip flop and NOR gates. The purpose of this part is to
generate additional two switch signals with frequency f 0 / 64 , that are needed for activating
the controllers. These two signals are called UZ_N/S and UZ_E/W. The generation of the
signals UZ_N/S and UZ_E/W is described now.

The f 0 / 4 signal inputs to a D flip flop, that is also called delay flip flop. The delay flip-flop
transfers whatever is at the input D to the output Q. This does not happen immediately, but
only happen on a rising clock pulse (i.e. as CLK goes from 0 to 1). The input is thus delayed by
up to a clock pulse before appearing at the output. (see figure 28)

CLK

Sensor
switch signal

Output Q of
D flip flop

UZ_E/W
Sensor E/W Sensor N/S
active active

UZ_N/S

Figure 28: Generation of the controller switching signals. /Mayr 2003/ When the signal
UZ_E/W is high, the sensor E/W is active. When the signal UZ_N/S is high, the sensor N/S is
active. From the above figure we can see, that only one sensor is activated at a time.

The signals UZ_N/S and UZ_E/W are generated by 4 NOR gates. There are two input signals
for the gates. One is the sensor switch signal (input of the D flip flop), another is the output of
the D flip flop. Call the two inputs A and B for simplification. The expression of UZ_N/S and
UZ_E/W are as follows:

UZ _ E / W  A  B  A  B (22)
UZ _ N / S  A  B  A  B

The reason that to generate two such signals will be explained in detain in chapter 3.6.

32
Development of an Electronic Compass

3.4.3 Sinusoidal signal generation

3.4.3.1 The Fourier Transform of the 50% duty cycle signal

We use a 4’th order low pass filter to generate a low THD sinusoidal voltage from a square
signal. The THD of the sinusoidal output signal shall be less than 0.05%. This filter is realized
by two 2 order Sallen & Key filters. The calculations below are from Theorie & Design eines
Tiefpassfilter mit Q>1. /Andreas/

U
U0

t
U0

Figure 29: 50% square input for the low pass filter.

The input of the low pass filter is shown in figure 29. As a Fourier series we get

y  y 0   (a k cos(k 0 t )  bk sin(k 0 t )) (23)
k 1
T T
2 2
With a k   y (t ) cos(k 0 t )dt bk   y (t ) sin( k 0 t )dt for k  1,3,5... (24)
T 0 T 0

4U 0
We get bk  for k  1,3,5... and y0  0 , a k  0 , a2 k  b2 k  0 for k  2,4,6...
k

So the amplitude of the partial waves in the signal is:

4U 0 4U 0
b1   1.2732  U 0 (25a) b2   0.4244  U 0 (25b)
 3
4U 0 4U 0
b3   0.2546  U 0 (25c) b4   0.1819  U 0 (25d)
5 7

3.4.3.2 Design the low pass filter

We use the Sallen & Key Filter to realize the function. The typical circuit of the Sallye & Key
filter is shown in figure 30.

33
Development of an Electronic Compass

Figure 30: Salley & Key Filter

We use the same value resistors and capacitors.

R  R1  R2  10k and C  C1  C2  10nF

Figure 31: Simulation the logUout with PSPICE. The Q  1 at the frequency f 0  1591Hz

We can calculate the transfer functions H 1 ( j ) , H 2 ( j ) of the two equal Sallen & Key
Filters.

K
H 1 ( j )  H 2 ( j  )  (26)
j 1  2
1 
0 Q 0 2

For the two filters H  H1  H 2

The transfer function at   0  1 RC of one Sallen & Key filter is for high Q

34
Development of an Electronic Compass

H 1 (    0 )  H 2 (   0 )  K  Q

So the transfer function at    0 of two equal Sallen & Key Filters is

H (   0 )  H 1 (   0 )  H 2 (   0 )  ( K  Q) 2 (27)

The transfer function at   30 of two equal Sallen & Key Filters is
K2 K2
H (  30 )  H 1 (  30 )  H 2 (  30 )  
2 2 1 2 (30 ) 2 1 (3 0 ) 2
(1  2 )  (1  ) 
2

0 Q 0 2 0 2 Q 0 2
K2 K2
 
1 9
(1  9) 2  9 (8) 2 
Q2 Q2
(33)

The THD of the sinusoidal output shall be less than 0.05%. We get approximately

Uˆ 2  Uˆ 3  Uˆ 4  ...
2 2 2

 3  0.0005 (28)
Uˆ 1  Uˆ 2  Uˆ 3  Uˆ 4  ... uˆ1
2 2 2 2

û1 and û 3 are the amplitudes of the partial waves at    0 and   3 0 in the output.
Their ration is

uˆ 3 b3  H (  30 )

uˆ1 b1  H (   0 )

If we use formula 29, 32 and 33 to instead of b1 , b3 , H (   0 ) and H (  3 0 ) , we get

4U 0 K2

3 9 K 2  Q 2  4U 0
64 
uˆ 3 Q2 (64  Q 2  9)  3 1 1
     0.0005
uˆ1 4U 0 Q  K  4U 0
2 2
(64  Q  9)  3 192Q 2  27
2
Q2  K 2
 

This is equal to 192Q 2  27  2000  Q 2  10.276  Q  3.206

1
For a Sallen & Key filters with R1  R2 , C1  C , the Q- factor is given by Q .
3 k

1 1
So that we get K  3  3  2.69
Q 3.206

R3
The value of K depends on the R3 and R4 , K  1 .
R4

35
Development of an Electronic Compass

It follows, that R3  R4 ( K  1)  R3  1.69  R4

We decide to use two resistors with following value:

R3  100k and R4  56k

R3 100k
The value of K then is: K  1  1  2.786 ,
R4 56k

1 1
and the Q- factor of the filter is Q    4.67
3  K 3  2.786

This is sufficient, since it is only required, that Q  3.206 .

3.4.3.3 Design the voltage divider.

The transfer factor of the entire low pass filter at 0 should be  1 . But according to the
above calculation, we have

H 1 (   0 )  H 2 (   0 )  K  Q  2.786  4.67  13.01  13

So H (  0 )  H 1 (  0 )  H 2 (   0 )  132  169

The operation amplifier would limit the output signal to not be sinusoidal any longer. So we
need to add a voltage divider with the relation U in ' / U in  1 / 13 before the low pass filter. The
circuit diagram of the voltage divider and low pass filter is shown in figure 32.

Figure 32: The circuit diagram of the voltage divider and low pass filter. The equivalent
resistance of the left two parallel resistors ( Ra and Rb ) is the output impedance of the
equivalent circuit for the voltage divider.

The R’ should be the same value as R1.

36
Development of an Electronic Compass

Ra  Rb
R'   10k (29)
Ra  Rb

Because U in ' / U in  1 / 13 , we get

U 'in Rb 1 12
   Rb  10k  Rb  10.833k
U in Ra  Rb 13 13

We use two resistors Rb1 and Rb2 to combine 10.833k .

Rb1  10k and Rb 2  560 to get Rb  Rb1  Rb 2  10.56k

For Ra we get: Ra  12 Rb  120k

The final design of the two Sallen & Key filters with voltage dividers is shown in figure 33.

Figure 33: Generation of the sinusoidal signal by two Sallen & Key Filters.

3.4.3.4 All pass filter network and amplification for the sinusoidal signal

Figure 34: The circuit diagram of the phase adjustment and amplitude amplifier.

The circuit diagram of the phase adjustment stage and amplitude amplifier is shown above.
The reason for using the phase adjustment is explained in chapter 3.8.

The C19 and C13 are parellel connected. The equal capacitance C is:

37
Development of an Electronic Compass

C  C19  C13  10n (30)

The theory of the all pass filter network for phase adjustment shall be given now. /Mayr 2004/

The voltage at the non inverting input is given by


1
jC 1
U  U1  U1 (31)
1 1  jRTrimm _ 1C
RTrimm _ 1 
jC

And the voltage drop at the feed back resistor is

U1  U  U U
U FB  R10  I1  R10   10k  1  U1  U  (32)
R27 10k

From a loop around the circuit we get  U   U FB  U 2  0

Because the both inputs of an ideal Op Amp are at the same potential U U

We get
1 1  2 
U 2  U   U FB  U1  U1  U1    1  U 1
1  jRTrimm _ 1C 1  jRTrimm _ 1C  
 1  jRTrimm _ 1C  (33)
2  1  jRTrimm _ 1C 1  jRTrimm _ 1C
 U1  U1
1  jRTrimm _ 1C 1  jRTrimm _ 1C

The phase shift therefore is



  2  arctan  2  arctan(RTrimm C ) if we set  0  1 / RTrimm _ 1C (34)
0

The circuit after the phase adjustment is an inverting amplifier. The capacitor C9 is to filter the
DC zero_bias. The gain is given by:

K   RTrimm _ 2 R2

38
Development of an Electronic Compass

3.5 Sensor model

3.5.1 The material and the structure of sensors

Figure35: The photo of the magnetic sensor.

The structure of the sensors has been shown in chapter 1.3. The sensors are made by a ceramic
tube with a metallic glass in it. It contains two equal coils, made of 0.05, copper wire. The
input of the excitation coil is a low THD sinusoidal current with frequency f 0 . There are two
sensors in the system. One in east, west direction is called sensor E/W. Another in north, south
direction is called sensor N/S.

3.5.2 Sensor output signal

The metallic glass is magnetically very soft and non linear. This causes a non linear
dependence of the magnetic flux density B within the soft magnetic material as function of the
magnetic field H. There are two influence of the soft magnetic material. One is the actual
magnetic flux density does not only depend on the current magnetic field H, but also on the
entire history of H, which is described by the famous hysteresis loop. The second influence is
the generation of harmonics. The output will contain additional frequency components with
frequencies of n  f 0  2,3,4,5,... , if f 0 is the input signal frequency.

Figure 36: The output from the magnetic sensor and its Spectrum when there is static
magnetic field. /Compass 2003/

39
Development of an Electronic Compass

If there is static magnetic field component along the sensor axis, then there are harmonics with
frequency 2 f o ,4 f o ,6 f o ,... (see figure 36). Otherwise, there are no harmonics (see figure 37).

The integral controller in the magnetic detector circuit generates an zero_bias current to
compensate the static magnetic field component of the earth’s magnetic field. So the output of
the controller can be used to measure the magnetic earth field along the sensor axis. The
criterion for compensation is, that the 2 f 0 frequency component vanishes. The 2 f 0
component is used, because it has the highest amplitude of the even harmonics.

Figure 37: The output from the magnetic sensor and its Spectrum when there is no static
magnetic field. /Compass 2003/

3.6 Sensor activation

3.6.1 Switch signal to active one sensor at a time

Only one sensor is active at a time to avoid the influence of each other. This is realized by a
switch between the sinusoidal sensor excitation signal and 0V. A multiplexer 74HC4053 is
used to realize the switching function.

74HC4053 are triple 2-channel analog multiplexers with a common enable input ( E ). Each
multiplexer has two independent inputs (nY0 and nY1), a common output (nZ) and three
switch control inputs (S1 to S3).

With E low, the switches can be controlled by S1 to S3.


With E HIGH, all switches are in the high impedance OFF-state, independent of S1 to S3.

40
Development of an Electronic Compass

Inputs
Channel on
E Sn
L L nY0 – nZ
L H nY1 – nZ
H X none
Table 3: The function table of 74HC4053 /PHILIPS/

There are three channels used. The control inputs of three channels (S1 to S3) are the signal
called sensor switch signal Uy.

For channel one, the input 1Y0 is connected to GND and another input 1Y1 is connected to a
sinusoidal signal with frequency f 0 . When the sensor switch signal Uy is low, the output 1Z
is GND. Otherwise; the 1Z is the sinusoidal signal. The output is used to active the E/W
sensor.

For channel two, the input 2Y0 is connected to the sinusoidal signal. Another input 2Y1 is
GND. The situation in channel two is opposite to channel 1. When the sensor switch signal Uy
is low, the output 2Z is the sinusoidal signal. Otherwise, it is GND. The output of channel two
is used to active the N/S sensor.

In channel three, the output signals from the two sensors are connected to the inputs 3Y0 and
3Y1. So the output 3Z is always the output of the currently active sensor.

1591Hz_sine

Sensor switch sensor N/S active


Signal Uy sensor E/W active
About 25Hz

The output of
channel one,
1Z

The output of
channel two,
2Z

Figure 38: Sensor activation switch signal.

41
Development of an Electronic Compass

3.6.2 Switch the sensor output signal

Since the switching behavior of the sensor input signal, the output of one sensor appears one
half of every period, while the output of another sensor appears another half of the period. We
need to switch two outputs to one signal, because the following low pass filter and gain
adjustment exist only once. This function is also realized by the multiplexer 74HC4053. The
principle diagram is shown in figure 39. The signals from E/W sensor and N/S sensor are
mixed into one signal called sensor output signal.

Sensor switch signal Uy


Signal from E/W
sensor Sensor output
signal
Signal from
N/S sensor

Figure 39: Sensor output switch

3.7 Low pass filter and non-inverting amplifier for the sensor
output signal

The sensor output signal inputs to a low pass filter to attenuate the higher harmonics of the
sensor output signal with frequencies higher than 2 f 0 . This improves the performance of the
synchronous rectifier, where higher harmonics would also contribute to its output signal (see
chapter 3.8)

Since the filters Q- factor is 2, the frequency component with frequency


1
f   3388Hz  2 f 0 is amplified by a factor of 2 compared to a filter with
2 10nF  4.7k
an initially completely feat amplitude response like a Bufferworth type low pass filter.

Figure 40: Circuit diagram of the low pass filter and non-inverting amplifier.
42
Development of an Electronic Compass

After the low pass filter is a non-inverting amplifier. The amplitude is amplified by the
following factor:
R  R18
K  13  3.2
R18

3.8 Synchronous rectifier

3.8.1 The function of synchronous rectifier

Because of the non linear behavior of the sensor material, the output from sensors is a
combined of signals with many harmonics of the base signal. The base signal frequency
depends on the sensor generation signal frequency that is f 0 (1591Hz). So the output contains
the sinusoidal signal with frequency f 0 , 2 f 0 , 4 f 0 ,… nf 0 , n  1,2... Experiments show that the
second harmonic (frequency 2 f 0 ) is the even harmonic with the highest level. So the
amplitude of the second harmonic is the best suited for using as quantity to measure magnetic
field. /Mayr 2003/

H is not only necessary to measure the component of the 2nd harmonic at 2 f 0  2 1591Hz , but
also its phase. The controller, that generates the zero_bias current of the excitation coil must
know, whether to increase or to decrease the zero_bias current in order to get zero amplitude of
the 2nd harmonic. Fortunately, the second harmonic changes its phase by 180 , when the
component of the earth magnetic field along the sensor axis changes its direction. In a
simplified figure that looks as follows:

Figure 41: The synchronous rectifier

43
Development of an Electronic Compass

3.8.2 Mathematical description of the synchronous rectifier

The Uout is multiplied with Uin like in figure 42.

Figure 42: The diagram of signal multiplexer.

The synchronous rectifier acts like a mixer. The following calculation is from Synchronous
rectifier by Milan. /Milan/

U out  U in  nctrl (35)

The nctrl controls the switch, the zero_bias in U in is described by sctrl .

We define the input signal as follows:



U in  U in  sin(in  t   )
  n 
 1 t  n,
   ctrl  (36)
nctrl  sctrl (t )  
 1 t   n , 2n 
  
  ctrl ctrl 

sctrl is a rectangular signal that can be described by Fourier transform.


4  1
sctrl    sin((2n  1) ctrl  t ) for n  0,1,2,3... (37)
 n  0 2n  1

So we can get the description of Uout

U out  U in  U ctrl
 4  1  (38)
 U in  sin( in  t   )     sin((2n  1) ctrl  t ) 
  n  0 2n  1 

U out is used as input to a integral controller. For its output mainly the DC- part of U out is
important, called U out .

44
Development of an Electronic Compass

T
1
U out   U out (t )dt (39)
T 0

Put formula 49 instead of Uout

T
1  4  1
U out   U in  sin( in  t   )    sin(( 2n  1) ctrl  t )dt
T0  n 0 2n  1

Switch the integral sign and the sum sign

 T
4 1 1
U out  Uˆ in     sin( in  t   )  sin((2n  1) ctrl  t )dt (40)
 n 0 2n  1 T 0

Because
1
T  0 for any other case
T 0
sin(in  t   )  sin((2n  1) ctrl  t )dt   1 (41)
 2  cos  for 1   2

So we finally get the formula for Uout

 0 for any other case


U out  2 ˆ 1 (42)
 U  cos   for in  (2n  1)  ctrl
 in 2n  1

The output of synchronous rectifier depends on two aspects. One is the relation between input
signal frequency and control signal frequency. Another is the phase difference of input signal
and control signal. The mean value of Uout is for n  0,1,2,3,4,...

2 ˆ
n  0, in  1  ctrl : U out   U in  cos  (43a)

n  1, in  2   ctrl : U out  0 (43b)
1 2
n  2, in  3   ctrl : U out    Uˆ in  cos  (43c)
5 
n  3, in  4  ctrl : U out  0 (43d)
1 2
n  4, in  5   ctrl : U out    Uˆ in  cos  (43e)
9 

The control signal frequency is 2 f 0  3182 Hz . The sensor output signal contains the base
frequency f 0 and its harmonics. After the low pass filter, the sensor output signal mainly
consists of the base frequency and the second harmonic 2 f 0 . From the equation above, we can
get:

1
For the base frequency signal: in  ctrl , U out  0
2

45
Development of an Electronic Compass

2 ˆ
For the second harmonic signal: in   ctrl , U out   U in  cos 

The other harmonics can be neglected. The phase difference  can be adjusted in the
sinusoidal generation circuit part by the all pas filter network to get the maximum of cos .
So, this output from synchronous rectifier is suited to input to an integral controller to calculate
the history of the second harmonic.

3.8.3 How to realize the function by hardware?

The block diagram is shown in figure 43. The input signal Uin is a combined of the base signal
of the sensor output (1591Hz sinusoidal signal) and the second harmonic. The Uin inputs to an
inverting amplifier. The amplitude doesn’t change. The switch between the Uin and its
inverting signal is controlled by signal Uctrl, which is a square signal with frequency 3182Hz.

Figure 43: The block diagram of synchronous rectifier.

The switch function is realized by a multiplexer 74HC4053. The circuit diagram is shown in
figure 44. In channel one, the Uin and its inverting signal –Uin are connected to two individual
inputs 1Y1 and 1Y0. The switch between Uin and –Uin is controlled by Uctrl. The output of this
channel is Uout.

Figure 44: The circuit diagram of the switch.

46
Development of an Electronic Compass

3.9 Integral controller

3.9.1 Controller activation signal switch

Because of the transient behavior of the low pass filter during the switching process, the
controller will be disturbed. So two additional switch signals are needed to activate the integral
controller at a certain time after the switching over. How to generate these controller activation
signals is described in 3.4.2 Switch signal generation.

The two sensors output signals are mixed in one signal. The function of the switch is to pick up
the signal from one sensor to input to certain integral controller. Like, pick up the part from
E/W sensor and then input to the E/W integral controller. The principle diagram of this
function is shown in figure 45.

Figure 45: The principle of the controller activation signal switch

Signal UZ_E/W and UZ_N/S control the switch behavior. The frequency is about 25Hz. When
the signal UZ_E/W is high, the E/W integral controller is active. At this time, the signal
UZ_N/S is low and the N/S integral controller output remains sensitivityant. And when the
signal UZ_N/S is high, the N/S integral controller is active. At that time, the E/W integral
controller output is sensitivityant.

This switch function is realized by the multiplexer 74HC4053.

Figure 46: The circuit diagram of the integral controller input signal switch.

47
Development of an Electronic Compass

3.9.2 Design the integral controller

Figure 47: Circuit diagram of the integral controller

The reason to choose integral controller is in chapter 1.7. The controller integrates the input
signal to produce the output signal. Integration is obtained by reversing the resistor and the
capacitor. For an ideal operational amplifier, there is no current into or out of any input. Since
the inverting input is at virtual ground, the current through R must flow to the capacitor C. So
the output voltage is:

t
1
RC 0
U out (t )  U out (t  0)   U in (t ' )dt ' (44)

1
The  is the gain that is -45.45 in our case.
RC

3.10 U/I converter

Figure 48: Circuit diagram of U/I converter.

48
Development of an Electronic Compass

The sensors are excited by a current through the excitation coil, so an U/I converter is needed.
There are 3 inputs for the U/I converter. U1 is the controller output. It is a DC voltage. U2 is a
sinusoidal voltage signal with frequency f 0  1591Hz . In one period, this signal is active for
about 20ms and then becomes 0V for 20ms. This is controlled by the sensor switch signal
which frequency is f 0 / 64  24.85Hz . This function is used to activate two sensors one by
one. U3 is the zero_bias voltage. It is used for adjusting the controller output, to be symmetric
with respect to ground.

The relation between the non inverting input of the Op Amp and three input signals can be
calculated as follows: /Milan 2/

U1  U  U 2  U  U 3  U 
  0 (45)
R1 R2 R3

From this formula we can get

R2 R3U 1  R1 R3U 2  R1 R2 U 3
U  (46)
R2 R3  R1 R3  R1 R2

R1    R1 , R2    R2 and R3    R3
* * *
Assume the following:

* * *
Because R1 , R2 and R3 are parallel connected with each other, the equivalent resistance of
these resistor is as follows:

* * * * * *
1 1 1 R R  R1 R3  R1 R2
Rsum  * * *  2 3 * * *
R1 R2 R3 R1 R2 R3
* * * * * *
(47)
U R R  R1 R3  R1 R2
I    U  2 3 * * *
Rsum R1 R2 R3

* * *
We use R1 , R2 and R3 to express R1 , R2 and R3 , and get

 2 R2 R3   2 R1 R3   2 R1 R2 1 R R  R1 R3  R1 R2
I U   U   2 3 (48)
 R1 R2 R3
3
 R1 R2 R3

Because for an ideal operational amplifier, we have U  U 

We get

R2 R3U 1  R1 R3U 2  R1 R2 U 3 1 R2 R3  R1 R3  R1 R2 1 R2 R3U 1  R1 R3U 2  R1 R2 U 3


I    
R2 R3  R1 R3  R1 R2  R1 R2 R3  R1 R2 R3
1  U1 U 2 U 3  U1 U 2 U 3
      
  R1 R2 R3  R1* R2 * R3*
(49)

49
Development of an Electronic Compass

So the output current is determined by the input voltage and the in parallel connected output
resistors. The value of  is set as:

  0.01 , R1  R2  100k and R3  1M


R1 *  R2 *  1k , R3 *  10k

The R4 and C1 are used to make the circuit stable, since the sensor acts as an inductive load,
and so the phase margin is very low.

3.11 DC zero_bias generation

The signal from the integral controller is polar, from about -1.5V to +1.5V. The positive and
negative parts are symmetric. The allowed range for the input of the ADC is 0V~+2.5V. The
DC zero_bias is used to fulfill the requirement. The circuit diagram is shown in figure 49. The
signal from the integral controller is called Ucontroller. The DC zero_bias voltage is called Vref. It
is the reference voltage of the microcontroller, about 2.5V. The output signal with DC
zero_bias is called UADC. It is used as input to a voltage follower and then to the ADC.

Figure 49: The circuit diagram of the DC zero_bias generation

The voltage of UADC is calculated as follows:

U controller  U ADC U ADC  Vref U controller  Vref


  U ADC  (50)
200k 200k 2

U controller
Because Vref  2.5V , we get U ADC   1.25V (51)
2

If Ucontroller varies in the range from -1.5V to +1.5V, the ADC input voltage will have values
between +0.5V and +2V. It is suitable to input to the ADC.

50
Development of an Electronic Compass

3.12 Microcontroller part

The microcontroller is ADuC832. It has embedded 12 bits ADCs and DACs. The ADCs is
used to convert the analog signals of the tilt measurement circuit outputs and the magnetic field
detector circuit outputs from analog to digital. The microcontroller is connected to the display
with, its P0 and P2 port. There is also a connection for downloading programs via the serial
interface.

Figure 50: The connection of the microcontroller

Pin no. Name Function


1 ADC0 ADC channel 0. The component of the earth magnetic field
in the N/S sensor direction is used to as input to this pin.
2 ADC1 ADC channel 1. The component of the earth magnetic field
in the E/W sensor direction is used to as input to this pin.
3 ADC2 ADC channel 2. The tilt measurement circuit output in X
direction is used as input to this pin. It is used for the tilt
measurement.
4 ADC3 ADC channel 3. The tilt measurement circuit output in Y
direction is used as input to this pin. It is used for the tilt
measurement.
5 AVDD Analog power supply.
6 AGND Analog ground.
7 CREF Decoupling Input for On-Chip voltage reference. A 0.1uF

51
Development of an Electronic Compass

capacitor is connected between this pin and AGND.


8 VREF Reference Output. It is used as a reference voltage for the
DC-zero_bias generation circuit.
9 DAC0 Not used.
10 DAC1 Not used.
11 ADC14 ADC channel 4. Test power supply from the accelerometer
inputs to this pin. It’s for testing the supply voltage of the
accelerometer and adjusting the zero bias and sensitivity of
the outputs.
12 P1.5/ADC5 Not used.
13 P1.6/ADC6 Not used.
14 P1.7/ADC7 Not used.
15 RESET Reset for down loading the program.
16 RXD Data Input/Output of /serial Port. RXD and TXD are used or
down loading the program.
17 TXD Clock Output of Serial Port.
18 P3.2 Not used.
19 P3.3 Not used.
20 DVDD Digital power supply.
21 DGND Digital ground.
22 P3.4 Not used.
23 P3.5 Not used.
24 P3.6 Not used.
25 P3.7 Not used.
26 SCLCOK Not used.
27 SDATA Not used.
28 P2.0 Port 2.0. It is connected to the enable signal of the display.
29 P2.1 Port 2.1. It is connected to the data read/write of the display.
30 P2.2 Port 2.2. It is connected to the register select signal of the
display.
31 P2.3 Not used.
32 XTAL1 There is a 32.768 kHz crystal connected between XTAL1
and XTAL2.
33 XTAL2 See XTAL2.
34 DVDD Digital power supply.
35 DGND Digital ground.
36 P2.4 Not used.
37 P2.5 Not used.
38 P2.6 Not used.
39 P2.7 Not used.
40 EA External Access Enable. A 1k resistor is connected between
this pin and DVDD to enable the compass to fetch code from
internal program memory location 0000H to 1FFFH.
41 PSEN Program Store Enable. It is a control signal that enables the
program memory to the bus.
42 ALE Not used.
43 P0.0 Port 0 is connected to the Data line of the display. It can be
used as the data bus and multiplexed low order address. P0.0
is connected to Data pin 0 of the display.
44 P0.1 Connected to Data pin 1 of the display.

52
Development of an Electronic Compass

45 P0.2 Connected to Data pin 2 of the display.


46 P0.3 Connected to Data pin 3 of the display.
47 DGND Digital ground.
48 DVDD Digital power supply.
49 P0.4 Connected to Data pin 4 of the display.
50 P0.5 Connected to Data pin 5 of the display.
51 P0.6 Connected to Data pin 6 of the display.
52 P0.7 Connected to Data pin 7 of the display.

Table 4: The pins description of the microcontroller

The display is the model PC1602-K from Actron. It uses the LCD driver HD44780.
/POWERTIP/

Pin no. Name Function


1 Vss Power supply (GND)
2 Vdd Power supply (+)
3 Vo Contrast Adjust. An external trim resistor should be used to
adjust this voltage from Vss to ground. It is used to adjust the
LCD operation voltage Vop. Vop=Vss-Vo The typical Vop
is 4.7V at 25 degree temperature.
4 RS Register select signal
5 R/W Data read/writer
6 E Enable signal
7 DB0 Data bus line
8 DB1 Data bus line
9 DB2 Data bus line
10 DB3 Data bus line
11 DB4 Data bus line
12 DB5 Data bus line
13 DB6 Data bus line
14 DB7 Data bus line
15 A Power supply for LED B/L (+). This voltage should not be
high than 4.2V because of the current consumption. In the
datasheet, the current consumption of the LCD is 20mA if
A=4.2V. In our testing, it increases quickly to 100mA when
the supply voltage for the LCD reaches 4.7V.
16 K Power supply for LED B/L (-)

Table 5: The pin description of the display from POWERTIP

3.13 Layout description

The layout is made by the software ‘Eagle’. It is an easy to use, powerful tool for designing
PCBs.

53
Development of an Electronic Compass

There are three PCBs in the system, the microcontroller board, the oscillator board and the
sensor board. They are all 2-layer boards. In order to minimize the size of the electronic
compass, the PCBs should be as small as possible. Due to this requirement, we mostly use
SMD components.

The most critical board is the microcontroller board. This board contains the DC-DC converter,
DC-DC inverter and the microcontroller part. The size is reduced to 43.94mm  71.12mm .
The layout of this part has to be considered very carefully for DC-DC converter. The ripples of
the output strongly depend on the layout design.

The battery input current and step up converter of the DC-DC converter output current are high.
The track should be as short as possible and the width of the wire should be wider than others.
The width is 0.81mm in our case. The power components such as the inductor, converter IC,
filter capacitors, and the output diode should be placed together as close as possible, and their
tracks should be kept short, direct, and wide. The feedback network should be very close to the
pin FB and FBLDO of MAX1706. /11/

Ground connection is very important. Because the high input and output current, the ground
should be easy to get across. Ground planes are rectangular sheets of a conductor that occupy
the entire layers. They distribute electrical power and heat better than narrow tracks. For the
test microcontroller board, the ground was not very good. Some narrow tracks caused the
ripples to increase 4 times compared with the final and much better ground plane.

Figure 51: The photo of the microcontroller board.

The Oscillator board and the Sensor board are less critical. The Oscillator board is shown in
figure 52. The size is 71.2mm  71.2mm . This board contains the tilt measurement and the
signal generation part. The tracks especially for the +5V and -5V power supply should be
direct and wide. The filter capacitors should be as close as possible.

54
Development of an Electronic Compass

Figure 52: Photo of the Oscillator board.

The photo of the Sensor board is shown in figure 53. This board carries the magnetic sensors
and the magnetic field detector circuit. The two sensors are placed in the central of the board
and perpendicular to each other. One is on the top layer; another is on the bottom layer.

Figure 53: The photo of the Sensor board.

55
Development of an Electronic Compass

3.14 Connector pin description

There are three PCBs in the electronic compass. The connections are built by two connectors.
The pin descriptions are as follows:

3.14.1 Connector between the microcontroller board and Sensor board

Pin no. Name Function


1 -5V Analog -5V power supply. -5V is generated on the
microcontroller board. This is the negative power
supply for oscillator board and sensor board.
2 AGND Analog ground. The ground connection for oscillator
board and sensor board. The digital ground for the
microcontroller is noisy. To reduce the noise, the
ground for the other two boards is separated.
3 +5V Analog +5V power supply. It is generated on the
microcontroller board. This is the positive power supply
for oscillator board and sensor board.
4 XOUT_ACCE The accelerometer X direction output signal. There are
two outputs from accelerometer, XOUT and YOUT.
With these two values, we can calculate the pitch and
roll of the electronic compass. These two outputs will
input to the ADC
5 NC Not connectted.
6 YOUT_ACCE The accelerometer Y direction output signal.
7 OUTPUT_FROM_E/W_ The output magnetic component in E/W sensor
SENSOR direction. It is the integral controller output signal.
8 TEST_ACCE_POWER The accelerometer power supply test pin. This pin is
connected to the power supply of the accelerometer.
9 DC_ZERO_BIAS_VOL 2.5V from the VREF output of the microcontroller. It is
TAGE_FOR_SENSOR_ used in a voltage divider circuit to set the output voltage
OUTPUT of the integral controller vary from 0.5V to2V.
10 OUTPUT_FROM_N/S_ The output magnetic component in N/S direction.
SENSOR

3.14.2 Connector between Sensor board and Oscillation board

Pin no. Name Function


1 -5V Analog -5V power supply
2 AGND Analog ground
3 +5V Analog +5V power supply
4 1591_HZ_SINE 1591Hz sinusoidal voltage output. It is generated
from a 1591Hz square voltage with 4th order low
pass filter. It is used to generate the sinusoidal
excitation current for the sensor.
56
Development of an Electronic Compass

5 SWITCH_CONTROL_SIGN The signal controls the sensors switching. This is a


AL square signal with 25Hz frequency. It is generated
by counters.
6 UZ_N/S The signal is used to control the activation of the
N/S integral controller.
7 UZ_E/W The signal is used to control the activation of the
E/W integral controller
8 U_2F The control signal of the synchronous rectifier.
9 XOUT_ACCE The XOUT from the accelerometer. It inputs to the
ADC to calculate the pitch and roll of the
electronic compass.
10 YOUT_ACCE The YOUT from the accelerometer. It inputs to the
microcontroller to calculate the pitch and roll of
the electronic compass.
11 TEST_ACCE_POWER The accelerometer power supply test pin.

Literature

/MAXIM 97/ Datasheet for the MAX1706,


19- 1198; Rev 0: 4/97,
MAXIM Integral Product,
April 97

/Analog 2004/ Datasheet for the ADXL 203,


Analog Devices, Inc.
One Technology Way, P.O.Box.9106, Norwood, MA02062-9106, USA,
2004

/National 2000/ Datasheet for the LM 555, DS007851,


National Semiconductor Corporation,
Feb. 2000

/Mayr 2003/ Mayr Wolfgang,


Lecture in Operational Amplifier circuit design 2,
University of Applied Sciences in Rosenheim,
Winter 2003/04

/Andreas/ Andreas Böck and Christian Moderegger,


Theorie & Design eines Tiefpassfilter mit Q>1,
Operation Amplifier lab course, University of Applied Sciences,
Rosenheim,
2003

/Mayr 2004/ W.A.Mayr,


Script for the lectures on ‚Operational Amplifier Design 1’,
University of Applied Sciences, Rosenehim,
57
Development of an Electronic Compass

Winter,2004/05

/Compass 2003/ W.A.Mayr,


Documentation for the electronic compass project,
University of Applied Sciences in Rosenehim,
2003

/Milan/ Bhatt Milan,


The design for the Synchronous rectifier,
Lecture ‘Operational Amplifier Circuit Design 2’,
University of Applied Sciences in Rosenheim
Sept, 2003

/Milan 2/ Bhatt Milan,


Design for the U/I converter,
In the lecture ‘Operational Amplifier Circuit Design 2’
Sept, 2003

/PHILIPS/ Datasheet for 74HC4053


Philips Semiconductors
December 1990

/POWERTIP/ Datasheet for PC1602-K


Powertip Technology, INC.
26231 Enterprise Court, Lake Forest, CA 92630

58
Development of an Electronic Compass

4 Software part

4.1 The main program

The final target of software part is to display the heading angle. The flow chart is shown in
figure 54.

Figure 54 The flow chart of the main program

59
Development of an Electronic Compass

The software begins with initiation the Microcontroller ADuC832. It is a complete transducer
front end, integrating a high performance self-calibrating multi-channel 12-bit ADC, dual
12-bit DACs, and programmable 8-bit MCU on a single chip.

The controller core is based on 8051. It has 62kBytes of non-volatile Flash/EE program
memory, 4kBytes of non-volatile Flash/EE data memory, 256 bytes RAM and 2kBytes of
extended RAM. It uses 32kHz Ext crystal, On-chip Programmable PLL. During the initiation
of the microcontroller, the core frequency is set at 16.78MHz. /Analog Devices 2002/

After that is initiation the display. It is 2x16 dot matrix liquid crystal display. The LCD driver
is HD44780. It can be configured to drive the display under the control of an 8-bit
microcontroller.

The corrections for the zero bias and sensitivity of the tilt measurement circuit outputs is done
following. Because the sensitivity and the zero bias depend on the power supple voltage for the
accelerometer, the corrections are necessary every time turn on the electronic compass.

The user can choose to use the magnitude saved in the Flash memory or to measure the
magnitude again. If the user wants to measure the magnitude, he should turn the electronic
compass down. Otherwise, he should hold it horizontal.

It is necessary to make sure electronic compass is nearly vertical during the magnitude
measurement. This function is realized by function ‘start_condition’. This function will be
over until the electronic compass is vertical.

Next step is the main part of the program, function ‘magnitude_measurement’. This function
will return the magnitude, when it is over.

The frequency for the magnitude testing should be high to make the process fast. So we set the
core frequency at 16.78 MHz in the initiation for the microcontroller. After finishing the
magnitude measurement, we don’t need such a fast speed. The core frequency is reduced to 4
MHz in order to save power.

After we get the magnitude of the magnetic vector, the heading angle can be calculated. The
function ‘heading_calculation’ is to calculate the heading angle in degree. It will return the
heading angle. This angle is what we want to display. The resolution of the heading angle is 0.1
degree. After every ten times the heading calculation, the zero bias and sensitivity of the tilt
measurement circuit outputs are corrected once, to take into account any accelerometer supply
voltage changes.

60
Development of an Electronic Compass

4.2 Microcontroller Initiation

Figure 55: The flow chart of the initiation the Microcontroller.

To initiate the microcontroller, we need to set the core frequency, baud rate and serial port.

The core frequency is set by a PLL clock. The ADuC832 is intended to be used with a 32.768
kHz watch crystal. A PLL locks onto a multiple (512) of this to provide a stable 16.78 MHz
clock for the system. The core can operate at this frequency or at binary submultiples of it to
allow power saving in cases, where maximum core performance is not required. The default
core clock is the PLL clock divided by 8 or 2.097152 MHz. The ADC clocks are also derived
from the PLL clock, with the modulator rate being the same as the crystal oscillator frequency.
The above choice of frequencies ensures that the modulators and the core will be synchronous,
regardless of the core clock rate. The PLL control register is PLLCON. /Analog Devices 2002/

The last three bit of PLLCON is to set core frequency. The bit designation is in Table 6.

61
Development of an Electronic Compass

Table 6: The bit designation of last three bit of PLLCON /Analog Devices 2002/

In the initiation of the Microcontroller, the PLLCON is set to 0x80. So the core frequency is
16.777216MHz.

The ADCCON1 register controls conversion and acquisition times. The bit designation is as
follows:

Table 7: The bit designation of ADCCON1 /Analog Devices 2002/

We set the baud rate at 9600bit/s. The core frequency is 16.78 MHz. The baud rate is set as
follows:

 f CORE   16.78M 
log  log 
 32  BaudRate   32  9600 
DIV    5.77  5
log(2) log(2)
2  f CORE 2  16.78M
T 3FD  DIV  64  5  64  45  2 DH
2  BaudRate 2  9600

So we set T 3CON  85H , T 3FD  2 DH

The serial port is set to be 0x52H. The operation mode is the mode 1: 8-bit UART, variable
baud rate. The serial Port receive enable bit is 1.

62
Development of an Electronic Compass

4.3 Display Initiation

Figure 56: The Connecting between the HD44780 and the microcontroller ADuc832.

The interfacing between the LCD driver and the microcontroller is shown in figure 56. The
eight data lines that form the data bus are referred to as DB0 to DB7. The three control lines are
referred to as EN, RS, and RW.

EN is the enable line to indicate the start of a transmission of a data byte to the LCD controller.
RS is the register select line to indicate the LCD controller, whether the data byte is to be
treated as a command or as text data to be displayed on the screen. RW is the read/write line.
The flow chart to initiate the display is shown in figure 57.

To display text to the LCD screen, it first must be configured. The LCD controller is told what
typet of LCD screen is being used as well as the data bus format and the font. An internal reset
circuit automatically initializes the HD44780, when the power is turned on. The following
instructions are executed during the initialization. /HD44780U/

1. Function set
2. Display on, cursor off
3. Entry set
4. Clear display

Set clock EN high then low after the new instruction is to clock in the data to the LCD
controller.

63
Development of an Electronic Compass

Figure 57: The flow chart of the ‘initiate display’

The list of each instruction is Table 8.

64
Development of an Electronic Compass

Table 8: The instruction. /HD44780U/

4.4 Function: accelerometer_calibration

Function: accelerometer_calibration( )

Purpose: Adjust the zero bias and sensitivity for accelerometer outputs

AX  zero _ bias  sensitivity  g  sin 


The accelerometer output is
AY  zero _ bias  sensitivity  g  cos  sin
(see chapter 2.5)

The ADXL203 output is ratiometric, so the output sensitivity will vary proportionally to the
supply voltage. If the supply power changes 1mV, the sensitivity will change 0.22mV / g .

The zero bias output is also ratiometric, so the zero g output is nominally equal to VS / 2 at all
supply voltage.

Because of the voltage divider, the inputs for the ADC of the microcontroller are:

TEST _ ACCE _ POWER  0.36  VS


XOUT _ ACCE  0.5  AX
YOUT _ ACCE  0.5  AY

TEST_ACCE_POWER is the signal input to ADC4 of the microcontroller. XOUT_ACCE and


YOUT_ACCE are the signals input to ADC2 and ADC3.

65
Development of an Electronic Compass

In the testing, the supply power VS for the accelerometer is 4.9V. The TEST_ACCE_POWER
is 1.763V and 2883 in digital by ADC.

According to the ratio, the changes of the relation between the TEST_ACCE_POWER,
zero_point and sensitivity are as follows:

zero _ bias _ changes 0.5  0.5


  0.7
TEST _ ACCE _ POWER _ changes 0.36
sensitivity _ changes 0.22  0.5
  0.3
TEST _ ACCE _ POWER _ changes 0.36

That means, if the TEST_ACCE_POWER signal changes 10 in digital with respect to 2883,
the zero_bias will change 7 and the sensitivity will change 3.

The calibration is as follows:

zero _ bias _ now  zero _ bias  0.7  TEST _ ACCE _ POWER _ now  TEST _ ACCE _ POWER 
sensitivity _ now  sensitivity  0.3  TEST _ ACCE _ POWER _ now  TEST _ ACCE _ POWER 

The TEST_ACCE_POWER, the zero_bias and the sensitivity are the value got in the testing.
They are defined in the program. The TEST_ACCE_now, the zero_bias_now and the
sensitivity_now are the actual value got every time runs the accelerometer correction.

Variables:

Type Name Description


Internal Variable int test_acce_power_now The actual accelerometer supply
voltage
Output Variables int pitch_zero_bias_now The actual zero bias for the pitch

int roll_zero_bias_now The actual zero bias for the roll


int pitch_sensitivity_now The actual sensitivity for the
pitch
int roll_sensitivity_now The actual sensitivity for the roll

Used function:
ADC_reading( )

66
Development of an Electronic Compass

4.5 Function: start_condition

Figure 58: The flow chart of ‘start_condition’

Function: start_condition( )

Purpose: This part is to check if the user has turned down the electronic compass. If not, the
electronic compass displays turn down until the pitch angle gets 83 degree. Since
sin 83  0.993 , we set the function ends until sin( pitch)  0.993 .

The outputs from the accelerometer are follows: (see chapter 2.5)

AX  zero _ bias  sensitivity  g  sin 


AY  zero _ bias  sensitivity  g  cos  sin

67
Development of an Electronic Compass

The above  and  are the pitch and roll with respect to the accelerometer. But the compass
and the accelerometer are in the opposite direction. The pitch for the accelerometer is the roll
for the compass. The roll for the accelerometer is the pitch for the compass.

sin(roll )  ( AX  zero _ bias ) /( sensitivity  g )


For the electronic compass:
sin( pitch)  ( AY  zero _ bias ) /((cos( pitch)  sensitivity  g ))

Variables:

Type Name Description


Internal Variable int Hx_s The component of the earth magnetic field in
X direction.
float sinpitch_s The sine value of the pitch

Output: This function will run until the pitch gets 83 degree.

Used functions: ADC_reading( )

4.6 Function: vertical_check

Figure 59: The flow chart of ‘vertical_check’

68
Development of an Electronic Compass

Function: vertical_check( )

Purpose: Display the hint if the electronic compass is vertical. If the user holds the
electronic compass vertical, it will display ‘go on’. Otherwise, it will display ‘vertical’ to imply
the user should hold it vertical. The boundary is 83º pitch. If the pitch angle is large than 83º,
we consider the compass is vertical.

It is called before reading the components of earth magnetic field every time in function
magnitude_test( ). The purpose of using this function is that, if the electronic compass is not
vertical, the magnitude got at the end will be a large difference from the actual one.

Variables:

Type Name Description


Internal Variable float sinpitch_v The sine value of the pitch.
Output char go on Display ‘go on’ if the compass is vertical.

char vertical Display ‘vertical’ if the compass is not


vertical.

Used function: ADC_reading( )

4.7 Function: a_calculation

Figure 60: The flow chart of ‘a_calculation’.

Function: a_calculation( )

2 2
Purpose: Calculate H Y  H Z . It is called in the function ‘magnitude measurement’.
The HY is the component of the earth magnetic field in horizontal plane, which is a sine
function. HZ is the vertical component, which is sensitivityant. When H Y reaches its
2 2
maximum value, the HY  H Z is equal to the magnitude H.

69
Development of an Electronic Compass

Variables:

Type Name Description


Internal Variables float Hx_a The component of the earth magnetic field in
sensor direction X.
float Hy_a The component of the earth magnetic field in
sensor direction Y.
Output Variable float a_a The value of
2
HY  H Z
2

Used function: ADC_reading( )

4.8 Function: wait

Figure 61: The flow chart of function ‘wait’

Function: wait(unsigned long time)

Purpose: Wait for a certain time. This function just runs for i times of loops for delay. Every
one loop takes 0.03ms if the core frequency is 16.78MHz.

Variables:

Type Name Description


Input Variables unsigned long time The number of loops for delay

Internal Variable unsigned long i The number of loops the program should
run.

70
Development of an Electronic Compass

4.9 Function: magnitude_measurement

71
Development of an Electronic Compass

Figure 62: The flow chart of the magnitude test

Function: magnitude_measurement( )

Purpose: Measure the magnitude. The magnitude H of the earth magnetic vector is
2 2 2
sensitivityant in any position. H  H X  H Y  H Z . The X and Y axes are horizontal and
Z axis is vertical. The magnetic vector is downwards. The component H Z in Z direction is
sensitivityant, and the H Y in Y direction is a sine function. The electronic compass has two

72
Development of an Electronic Compass

perpendicular direction sensors. If the electronic compass is vertical, it can measure the Hz and
2 2
HY components. The program calculates the value of H Y  H Z when the electronic
compass is being turned around by 360 degree. The largest value should be equal to H , in
which place the H X  0 . In this way, we can get the magnitude H .

2 2
Figure 63: H Y and HY  H Z

2 2
The program is divided to two parts to get two maximum values of H Y  H Z , which are
called max1 and max2. The final magnitude is defined as magnitude  max1  max 2  / 2 . But
2 2
this function may get a wrong result, if it just measures the H Y  H Z for a while and pick
the maximum one. This situation appears when the user just holds the electronic compass, but
doesn’t turn around. So we set five reference points.

2 2
The diagram is shown in figure 63. H Y is a sin function. The frequency of HY  H Z is
twice of H Y ’s.

2 2
In the first part, in order to get the first maximum of H Y  H Z , the H Y has to across
through three points. The first is a zero point. The second is a point with H Y   H Y 1 , to avoid
the case that the user stops to turn around after reaches the first zero point. The reason is as
follows:

In the beginning of the function ‘magnitude_measurement’, we calculate the


H Y 1  0.32  H Z  H Y / H Y

At the zero point, HY  0

When H Y reaches the maximum value, H Y  0.4 H Z

73
Development of an Electronic Compass

If the H Y reaches the value H Y   H Y 1 , the angle between the compass and the zero point of
 0.32 
H Y is, a sin    53
 0.4 

That means the electronic compass is away from the zero point by 53°.

The third reference point is the second zero point. If H Y crosses these points, there must has
2 2
max 1  H Y  H Z  H . In the second part, H Y has to across point 4 and 5 to get the
max2.

Variables:

Type Name Description


Internal Variables int max1 The first maximum value of
2
HY  H Z
2

int max2 The second maximum value of


2 2
HY  H Z
int count1 It is used to check if the H Y has passed
the zero point.
int count2 It is used to check if the H Y has passed
the H Y 1 and  H Y 1
int a 2
The value of H Y  H Z
2
from function
‘a_calculation’.
float Hy_m The component of the earth magnetic field
in sensor direction Y.
float Hx_m The component of the earth magnetic field
in sensor direction X.
Output Variable float magnitude The magnitude we get at the end of this
function.

Used functions: a_calculation( ), vertical_check( ), write_string( ), convert( )

74
Development of an Electronic Compass

4.10 Function: heading_calculation

Figure 64: The flow chart of ‘heading_calculation’.

Function: heading_calculation( )

Purpose: Calculate the heading angle with Hx, Hy, pitch and roll value. This function is
divided to two parts.

In the first part, we calculate H X , H Y , H Z , sin( pitch) , cos( pitch) , sin(roll ) and
cos(roll ) . The components of the earth magnetic field H X and H Y , can read from function
ADC_reading( ), and then minus the zero_bias to get the exact value.
2 2
H Z  magnitude 2  H X  H Y . And the other variables we get as follows:

Because the accelerometer and electronic compass are in opposite position: (see chapter 4.5)

75
Development of an Electronic Compass

sin( roll )  ( AX  pitch _ zero _ bias ) /( sensitivity  g )  cos(roll )  1  sin(roll ) 2

sin( pitch)  ( AY  roll _ zero _ bias) /( sensitivity  g  cos(roll ))


 cos( pitch)  1  sin( pitch) 2

The AX and AY are the outputs of the tilt measurement circuit. They are read from ADC2 and
ADC3.

Second, we transfer the H X and H Y to the horizontal plane and calculate the heading. (see
chapter 2.3)

H X 3  cos( pitch)  H X  sin( pitch)  sin( roll )  H Y  sin( pitch)  cos(roll )  H Z


H Y 3  cos(roll )  H Y  sin(roll )  H Z
tan(heading )   H Y 3 H X 3

Heading angle in radian a tan( H Y 3 / H X 3 )


Transfer the heading to degree (a tan( H Y 3 / H X 3 ))  180 /  .

The heading angle is from  90 ~ 90 . We expect to display it in the range of 0 ~ 360 .

Magnetic north
Zone4 Zone1
Hx>0 Hx>0
HY>0 HY<0
Heading  90 ~ 0 Heading 0 ~ 90
Convert to 270 ~ 360 Convert to 0 ~ 90

Zone3 Zone2
Hx<0 Hx<0
HY>0 HY<0
Heading 0 ~ 90 Heading  90 ~ 0
Convert to 180 ~ 270 Convert to 90 ~ 180

Figure 65: The heading angle range when the electronic compass is in certain position. The
space is divided to four zone. For example, if the electronic compass is in zone 2, the heading
angle got from arc tangent function is from  90 ~ 0 , we should convert it to 90 ~ 180 ,
which is with respect to magnetic north.

The final step is to convert it with respect to geographic north. The declination is about 1.8
degree in Rosenheim. The flow chart of the conversion process is shown in figure 66.

76
Development of an Electronic Compass

Figure 66: The flow chart of converting the heading angle to the range of 0 ~ 360 and
with respect to geographic north.

Variables:

Type Name Description


Internal Variables float Hy_h The component of the earth magnetic
field in sensor direction Y.
float Hx_h The component of the earth magnetic
field in sensor direction X.
float Hz_h The component of the earth magnetic
field in Z direction, which is vertical to
the compass.
float cospitch_h The cosine value of the pitch
float sinpitch_h The sine value of the pitch
float sinroll_h The sine value of the roll
float cosroll_h The cosine value of the roll
float Hx3_h The X direction component of the earth
magnetic field before we change the
pitch and roll.
float Hy3_h The Y direction component of the earth
magnetic field before we change the
pitch and roll.
Output Variable float heading_h The heading angle in degree.

Used function: ADC_reading( ), roll_calculation( )

77
Development of an Electronic Compass

4.11 Function: ADC_reading

Figure 67: The flow chart of ‘ADC_reading’

Function: ADC_reading(char channel)

Purpose: Read the value from a certain ADC channel 64 times and get the average. The
division by 64 is performed by a shift operation. H X is in Channel 0. H Y is in Channel 1.
The AX from the tilt measurement circuit is in Channel 2. It is used to calculate the sin(roll )
for the electronic compass. The AY is in channel 3. It is used to calculate the sin( ptich) .

Variables:

Type Name Description


Internal Variable int i counter
Input Variable char channel The channel we want to read
Output Variable int value The average of reading the ADC over 64 times.

Used function: wait( )

78
Development of an Electronic Compass

4.12 Function: convert

Figure 68: The flow chart of ‘convert’.

Function: convert (char ausgabe_string[15], long Zahl)

Purpose: Convert a number to a string. For example, 123 is converted to string [1] = 3,
string[2] = 2, string[3] =1. So every number can be input to the controller of the display.

79
Development of an Electronic Compass

Variables:

Type Name Description


Internal Variable int i counter
int Vorzeichen The sign of the number.
int Letzte_Ziffer The remainder
Input Variable long Zahl The number we want to convert
Output Variable char ausgabe_string[15] The string we save the number

4.13 Function: ‘write_string’

Figure 69: The flow chart of the function ‘write_string’.

80
Development of an Electronic Compass

Function: write_string( )

Purpose: Write data to certain address. There are 2 lines on the display. If the line =1, set
cursor address 0x80. If the line=2, cursor address is 0xC0. The following loop is to write the
text one character at a time on LCD screen.

Variables:

Type Name Description


Input Variables char *s The text we want to display
unsigned char line Display on which line of the display

Internal Variable char i counter

Literature
/Analog Devices 2002/ Datasheet for the ADuC 832
Analog Devices Inc.,
One Technology Way, P.O. Box 9106, Norwood, MA 02062, U.S.A
2002

/HD44780U/ Datasheet for the HD44780


Hitachi, Ltd.
Semiconductor & Integral Circuits.
Nippon Bldg., 2-6-2, Ohte-machi, Chiyoda-ku, Tokyo 100-0004, Japan
ADE-207-272(Z) 99.9 Rev. 0.0’

81
Development of an Electronic Compass

5. Test documentation

5.1 Testing for the DC-DC converter and inverter

Date: 25/10/2005

Task:
1. Test the DC-DC converter and inverter, whether they work correctly.
2. Test the ripples at TP1, TP2 and TP3.

Note:
1. This testing is based on the second test µc board.
2. All the connection is checked.
3. In this test, we used Max1705(more powerful) instead of Max1706 to be the DC-DC
converter.
4. The DC-DC inverter is MAX1853.
5. TP1 (test point 1): VCC. It’s the digital 5V for other parts. It is the voltage after the
inductor.
6. TP2 (test point 2): VCC for –5V. It’s the power supply for the DC-DC inverter. It is the
voltage before the inductor.

7. TP3 (test point 3): -5V from DC-DC converter.

Test result:
1. Battery voltage: 4.173V ( drop 1mV every second)
2. TP2 voltage: 5.016V
3. TP1 voltage: 4.896V (TP1 has voltage drop compared with TP2 because of L1)
4. TP3 voltage: -5.017V
5. Ripple at TP1: Max: 12mV
Min: -15mV
Ripple at TP2: Max: 12mV
Min: -8mV

Problems:
1. The ripples at TP1 are higher than TP2. That means the inductor L1 is useless.
2. The ripples are larger than the values in the datasheet.
3. The AGND near the inductor is rather noisy. The ripples of AGND are similar with the
TP1’s. May be the ripples at TP1 is partly caused by the noisy AGND.
Improvement:
1. Resold the connections to AGND
82
Development of an Electronic Compass

2. The AGND polygon is critical. The current of converter may reach 1A. If this high current
can’t flow directly and some connections are narrow, the noise increases. So after
checking, we added two bridges to get better AGND connection.
3. The ripples after the improvement of AGND:
Ripples at TP1: Max: +3mV
Min: -3mV
Ripples at TP2: Max: +3mV
Min: -6mV
Ripples at TP3: Max: +1.5mV
Min: -2mV

Conclusion of this testing:


DC-DC converter and inverter worked correctly. But we need to improve the connection of
AGND.

5.2 Testing for the DC-DC converter and inverter on the final
PCB

Date: 10/01/2005

Task:
1. Test the DC-DC converter and inverter for the final PCB, whether they work correctly.
2. Test the ripples at TP1, TP2 and TP3.

Note:
1. This testing is based on the final µc PCB.
2. In this test, we used Max1706.
3. The DC-DC inverter is MAX1853.
4. TP1 (test point 1): VCC. It’s the digital 5V for other parts. It is the voltage after the
inductor.
5. TP2 (test point 2): VCC for –5V. It’s the power supply for the DC-DC inverter. It is the
voltage before the inductor.
6. TP3 (test point 3): -5V from DC-DC converter.

Test result:
1. Battery voltage: 4.732V
2. TP2 voltage: 5.132V
3. TP1 voltage: 4.781V (TP1 has voltage drop compared with TP2 because of L1)
4. TP3 voltage: -5.124V
Ripple at TP1: Max: +3mV
Min: -3mV
Ripple at TP2: Max: +5mV
Min: -3mV

Conclusion of this testing:


DC-DC converter and inverter worked correctly. The ripples on the output voltage are
acceptable.

83
Development of an Electronic Compass

5.3 Testing for the tilt measurement circuit

Date: 26/10/2005

Task:
1. Test the X and Y direction outputs of the accelerometer.
2. Analyze if it works correctly and the ripples of the output signals.

Note:
1. The accelerometer is ADXL203.
2. +5V power is from the power generator.

Test result:
Power supply: 4.968V
Power supply pin Vs for accelerometer: 4.90V

X director output:
Accelerometer is in horizontal position: AX  2.38V
Turn the board down by 90 , the maximum value: AX  3.36V
Turn the board up by 90 , the minimum value: AX  1.47V

The zero bias in X direction: 2.38V


Sensitivity: 950mV/g

Y director output:
Accelerometer is in horizontal position: AY  2.32V
Turn the board right by 90 , the maximum value: AY  3.34V
Turn the board left by 90 , the minimum value: AY  1.44V

The zero bias in Y direction: 2.32V


Sensitivity: 950mV/g

Most ripples of the AX and AY are in the range of  1.0mV ~ 1.0mV . It is acceptable.

Improvement: The GND connection can be better.

Conclusion: The accelerometer works quite well.

5.4 Testing the oscillator part

Task:
1. Test the digital signal used for the sensor part.
2. Test the sine signal and analyze the THD.

Test result:
Adjust the value of trim resistor R1 to get 1590Hz sine signal.

84
Development of an Electronic Compass

frequency Measurement value Real value


Fundamental 1600Hz -20.85dBm (-20.85+40)dBm=19.15dBm
Second harmonic 3175Hz -101dBm -61dBm
Third harmonic 4775Hz -85dBm -45dBm
(The 40dBm is caused by attenuator.)

19.15
f0 : u 0  0.775V  10 20
 7.03V

61
2 f 0 : u1  0.775V  10 20  0.69mV

45
3 f 0 : u 2  0.775  10 20
 4.4mV

2 2
u1  u 2 0.69mV  0.69mV  4.4mV  4.4mV
THD =   0.06%
2 2
u 0  u1  u 2
2
7.03V  7.03V  0.69mV  0.69mV  4.4mV  4.4mV

Conclusion:
THD is very low. The sine signal is nearly pure.

5.5 Calibration for the accelerometer

Task:
Calibrate the accelerometer. Test the zero bias, sensitivity of the tilt measurement circuit
outputs

Note:
The digital number is read from the display.

Result:
The two lines on the display:
Line 1: ADC2, roll _ zero _ bias  roll _ sensitivity  g  sin( roll )
Line 2: ADC3, pitch _ zero _ bias  pitch _ sensitivity  g  sin( pitch)  cos(roll )

Position of the compass Digital number on line 1 Digital number on line 2

Horizontal plan 2015 1976


Turn the compass right by 90 2790 1880
Turn the compass left by 90 1250 1885
Turn the compass up by 90 2020 2768
Turn the compass down by 90 2012 1184

85
Development of an Electronic Compass

So the zero bias and sensitivity is:

line 1:
roll _ zero _ bias : 2015
roll _ sensitivity : 770

line 2:
pitch _ zero _ bias : 1976
pitch _ sensitivity : 790

5.6 Calibration for the magnetic field detector circuit outputs

Task:
Test the X and Y direction sensor zero_bias

Step 1: Adjust the electronic compass to the magnetic north direction. The magnetic detector
circuit output in N/S sensor direction should get the maximum and minimum value.

For sensor N/S:


Min: 1261
Max: 2969
With average Min  Max  2  (1261  2969) 2  2115
Sensitivity is Max  Min  2969  1261  1708

Step 2: Adjust the electronic compass to the direction perpendicular to the magnetic north. The
magnetic detector circuit output in E/W sensor direction should get the maximum and
minimum value.

For E/W sensor:


Min: 2770
Max: 1087
With average Min  Max  2  1087  2770  2  1929
Sensitivity is Max  Min  2770  1087  1683

The sensitivity difference between two sensors is


Sensitivity N / S  Sensitivity E / W 1708  1683
  1.47%
Sensitivity N / S  Sensitivity E / W  2 1708  1683 2

5.7 Testing the horizontal deviation with accelerometer supply


voltage correction after every 10 times of the heading calculation

Task:
Test the heading angle on the display and compare it to the angle degree on the rotator

86
Development of an Electronic Compass

Note:
1. The data are got when the electronic compass is horizontal.
2. The zero bias, sensitivity of the pitch and roll are corrected after every 10 times of
the heading calculation.

Adjust the electronic compass display 0.0 heading angle in the position of the 0.0 degree of the
rotator.

Degree on the rotator (degree) Heading angle on the display (degree)


0.0 0.0
10.0 10.1
20.0 20.0
30.0 30.2
40.0 40.3
50.0 50.6
60.0 60.7
70.0 70.6
80.0 80.6
90.0 90.4
100.0 100.2
110.0 110.1
120.0 120.1
130.0 129.8
140.0 140.0
150.0 150.0
160.0 160.0
170.0 170.1
180.0 180.1
190.0 190.0
200.0 200.0
210.0 210.1
220.0 220.3
230.0 230.4
240.0 240.4
250.0 250.3
260.0 260.2
270.0 270.2
280.0 279.9
290.0 289.7
300.0 299.7
310.0 309.7
320.0 319.8
330.0 329.7
340.0 339.7
350.0 349.7

There is  0.7 deviation. It is a high resolution for such kind of electronic compass.

87
Development of an Electronic Compass

5.8 Testing the deviation with 10,20,30,40 pitch

Task:
Test the heading angle on the display when we change the pitch, and compare it with the value
when the compass is horizontal.

Note:
In principle, if we turn up the electronic compass, the heading angle will not change.

Step 1: Adjust the electronic compass display 0.0 heading angle in the position of the 0.0
degree of the rotator.

Step 2: change the pitch to 10, 20, 30 and 40 degree, write down the heading angle on the
display.

Pitch (degree) 10 20 30 40

Heading angle
on the display
0.0 0.0 0.0 359.3 358.9
30.0 30.0 29.5 28.8 27.9
60.0 59.6 58.9 57.9 57.0
90.0 89.7 89.2 88.4 87.6
120.0 119.7 119.5 119.2 119.6
150.0 150.2 150.5 150.7 151.0
180.0 180.4 181.0 181.8 182.4
210.0 210.7 211.5 212.2 212.8
240.0 240.7 241.5 242.1 242.4
270.0 270.9 271.4 271.8 271.9
300.0 300.7 301.0 301.3 301.3
330.0 330.4 330.4 330.4 330.1

There is 1 deviation. It is acceptable.

88
Development of an Electronic Compass

Appendix Source Code


//Electronic compass 2006, master thesis by Ting Song.
//The final aim of software part is to display the heading angle with respect to geographic
north.
//Microcontroller is ADuc832.
//The type of the display Controller is HD44780.

#include <ADUC832.H>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>

#define MASK 0x0FFF


#define pitch_zero_bias 1976.0
#define roll_zero_bias 2015.0
#define pitch_sensitivity 790.0
#define roll_sensitivity 770.0
#define Hx_offset 2115
#define Hy_offset 1929
#define zero 80
#define sensitivity_zero_bias 0.7
#define sensitivity_sensitivity 0.3
#define power_acce 2883.0

float magnitude;
float Hz;
int roll_zero_bias_now, pitch_zero_bias_now, roll_sensitivity_now,
pitch_sensitivity_now,power_acce_now;

void wait (unsigned long);


void init_display(void);
void write_string(char *s, unsigned char line);
void convert(char ausgabe_string[15], long Zahl);
int ADC_reading(char channel);
void vertical_check();
long a_calculation();
int magnitude_test();
float heading_calculation();
float Hz_calculation();
void start_condition();
void convert_point(char ausgabe_string[15], long Zahl);
void zero_bias_sensitivity_adjustment();
void read_magnitude();

//End declaration

89
Development of an Electronic Compass

//------------------------------------------------------------------------------------
//Main programm
//------------------------------------------------------------------------------------
void main (void)
{

int heading_angle;
char chrASCII[16];
int sinpitch,i;

// Init ADuC832 --------------------------------------------------------------------------------------


PLLCON = 0x80; // Set PLL to generate the 16 MHz core clock frequence

ADCCON1 =0x8C; // ADC ON; CLK divided by 16; 4 acqu. clocks

T3CON = 0x085; // set baud rate 9600, actual rate 9621


T3FD = 0x2D;
SCON = 0x052; //set serial port

//Init ADuC832 end -------------------------------------------------------------------------------------

init_display(); //initial display


wait(1);

write_string(" Welcome",1); //display welcome sentence


write_string("Compass 06,Song",2);
wait(20000);
init_display();

zero_bias_sensitivity_adjustment(); //correct the zero bias and


sensitivity

write_string("AdjustMagnitude",1);
//imply the user can choose to calculate the magnitude

write_string("turn down Comp.",2);


//turn down the compass if he wants to do the calculation
wait(40000);

init_display();
sinpitch=ADC_reading(3); //read the AX
if ((sinpitch<2500)&(sinpitch>1300))
//In this range, the compass is considered horizontal
{
read_magnitude(); //read magnitude from Flash Memory
}

else

90
Development of an Electronic Compass

{
start_condition(); //to make sure the compass is vertical
magnitude = magnitude_test(); //get the magnitude
init_display();
write_string("The magnitude is",1);
convert(chrASCII,magnitude);
write_string(chrASCII,2); //display the magnitude
wait(20000);
}
init_display();
PLLCON = 0x82; //set core clock 4MHz
T3CON = 0x083; // set baud rate 9600, actual 9269
T3FD = 0x31;
i=0;

while (1)
{
write_string(" heading degree",1);
Hz = Hz_calculation(); //read Hz value
heading_angle = heading_calculation(); //read heading
convert_point(chrASCII,heading_angle); //display the heading value
write_string(chrASCII,2);

//correct the zero bias and sensitivity after every 10 times of heading calculation-----------------

i +=1;
if (i==10)
{
zero_bias_sensitivity_adjustment();
i=0;
}

// End main program ---------------------------------------------------------------------------------------

//function wait(), i = 1, delay 0.03ms ------------------------------------------------------------------

void wait (unsigned long times)


{
unsigned long i;

for(i=0; i < times ;i++)


{
;
}
}
//end function wait()----------------------------------------------------------------------------------------

91
Development of an Electronic Compass

//function write_string()------------------------------------------------------------------------------------

void write_string(char *s, unsigned char line)


{
char i;

P2 = 0x00;

if(line == 1)
P0 = 0x80; //set cursor address
if (line == 2)
P0 = 0xC0; //set cursor address

P2 = 0x01; //R/W=0,RS=0,EN=1
P2 = 0x04; //R/W=1,RS=0,EN=0

for(i=0; i<strlen(s); i++)


{
P0 = s[i];
P2 = 0x05; //R/W=1,RS=0,EN=1
P2 = 0x04;

}
//end write_string()------------------------------------------------------------------------------------------

// Display initiation------------------------------------------------------------------------------------------

void init_display(void)
{

P2 = 0x00;
P0 = 0x00;
wait(1);

P0 = 0x38; //function set


P2 = 0x01;
wait(1);
P2 = 0x00;
wait (1);

P0 = 0x0C; //Display on, cursor on, blinking on


P2 = 0x01;
wait(1);
P2 = 0x00;
wait (2);

92
Development of an Electronic Compass

P0 = 0x06; //Entry set


P2 = 0x01;
wait(1);
P2 = 0x00;
wait (2);

P0 = 0x01; //clear display


P2 = 0x01;
wait(1);
P2 = 0x00;
wait (2);

}
//end init_display()----------------------------------------------------------------------------------

//function ADC_reading(char channel),channel 0 is Hx,channel 1 is Hy, channel 2 is AY,


channel 3 is AX---

int ADC_reading(char channel)


{
int i, value;
long temp, adc_value;

adc_value = 0;
for(i=0;i<128;i++)
{
ADCCON2 = channel; // select channel
wait(2);
SCONV = 1; //Start ADC
while((ADCCON3 & 0x80) != 0) //wait for correct sample
wait(1);
temp = (ADCDATAH<<8)+ADCDATAL;
temp &= MASK; //clear upper 4 Bits
adc_value += temp; //summ adc_values
}

value = adc_value >> 6; // divide by 64


return value;

}
//end ADC_reading()---------------------------------------------------------------------------------------

//function convert((char ausgabe_string[15], long Zahl),it is used to convert number to matrix,


like 123 convert to string[0]=3, string[1]=2,string[3]=1

void convert(char ausgabe_string[15], long Zahl)


{
int i,Vorzeichen,letzte_Ziffer;

for(i=0;i<=14;i++)

93
Development of an Electronic Compass

ausgabe_string[i]=32;

Vorzeichen=32; //default: BLANK

if(Zahl<0)
{
Vorzeichen=45; //ASCI f¨¹r "-",
Zahl= -Zahl; //change negtive to positive
}

for(i=13;i>0;)
{

letzte_Ziffer = Zahl%10; //divided by 10, get the remainder

Zahl = Zahl-letzte_Ziffer;
//get the number that ones number is 0, for example 123-3 = 120
Zahl = Zahl/10;
//get the number from tens number, for example 120/10 =12

if(Zahl<=9) //if the number just has tens number


{
ausgabe_string[i]=letzte_Ziffer+48; //in ASCI code number from 48
ausgabe_string[i-1]=Zahl+48;
i--;
break;
}

ausgabe_string[i]=letzte_Ziffer+48;
i--;

ausgabe_string[0]=Vorzeichen; //set the first the sign


ausgabe_string[14]='\0'; //set end

//END convert-----------------------------------------------------------------------------------------------

//function vertical_check(), if the pitch>83 degree, display go on, if <83 degree,display


vertical--------------------------------------------------------------------------------------------------------

void vertical_check()
{
float sinpitch_v,sinroll_v,cosroll_v;

sinroll_v = ADC_reading(2); //read AX from ADC channel 3

94
Development of an Electronic Compass

//calculate sinpitch_v,AX=pitch_zero_bias+pitch_sensitivity*sinroll_v
sinroll_v -= roll_zero_bias_now;

sinroll_v /= roll_sensitivity_now;
cosroll_v = sqrt(1.0 - sinpitch_v * sinpitch_v); // calculate cosroll_r

sinpitch_v = ADC_reading(3); //read AY from ADC channel 2


sinpitch_v -= pitch_zero_bias_now;

// calculate sinpitch_v,AY=roll_zero_bias+roll_sensitivity*cosroll_v*sinpitch_v
sinpitch_v /= pitch_sensitivity_now;
sinpitch_v /= cosroll_v;
sinpitch_v = -sinpitch_v;

//if the pitch is larged than 83 degree,display 'go on please', sin83= 0.993

if (sinpitch_v <-0.993)

{
write_string(" go on please",2);
}
else
//else, display 'vertical'
{
write_string(" vertical ",2);
}
}

//end vertical_check-----------------------------------------------------------------------------------------

//function a_calculation(), it is used in magnitude_test() to get (Hx*Hx+Hy*Hy), the


maximum a value will be decided to be magnitude----------------------------------------------------

long a_calculation()
{
float Hx_a,Hy_a,a_a;
Hx_a = ADC_reading(0); //read Hx value from ADC 0
Hy_a = ADC_reading(1); //read Hy value from ADC 1
Hx_a -= Hx_offset; //minus Hx zero_bias
Hy_a -= Hy_offset; //minus Hy zero_bias
Hy_a *=1.0147; //time the sensitivity difference
a_a = sqrt(Hx_a * Hx_a + Hy_a * Hy_a);
return a_a;
}

//end a_calculation()----------------------------------------------------------------------------------------

//function magnitude_test(), to get the magnitude------------------------------------------------------

95
Development of an Electronic Compass

int magnitude_test()
{
int a,Hx_m,Hy_m,Hy_s,
max1,max2,loop1,loop2,count1,count2,mag_data1,mag_data2;
float magnitude;
char chrASCII[16];

Hx_m = ADC_reading(0); //get Hx


Hx_m -= Hx_offset;
Hx_m = - Hx_m;
Hy_m = ADC_reading(1); //get Hy value
Hy_m -= Hy_offset;
Hy_m *=1.0147;
//Hy_m =- Hy_m;
Hy_s = 0.32 * Hx_m * Hy_m / abs(Hy_m);
//-Hy_s is a point that must be passed during looking for the first maximum. Hy_s is the point
that must be passed through during looking for the second maximum.

wait(1);
max1 = 0;
max2 = 0;
loop1 = 1;
count1 = 0;
count2 = 0;
init_display();
while (loop1)
{
vertical_check(); //check if the compass is vertical
a = a_calculation(); //get value ‘a’
convert(chrASCII,a);
write_string(chrASCII,1); //display value ‘a’

Hy_m = ADC_reading(1); //get Hy value


Hy_m -= Hy_offset;
Hy_m *=1.0147;

//if ‘a’ is larged than max1, ‘a’ will be instead of max1

if ( a > max1)

{
max1 = a;
}

//if the Hy component is passing through zero point


if ((Hy_m<zero) & (Hy_m>-zero))
{
//if the Hy has passed through -Hy_s
if (count2 == 1)
{
write_string("1st Max found",2);

96
Development of an Electronic Compass

wait(20000);
loop1 = 0; //stop calculate the max1
loop2 = 1; //begin the calculation for max2
}
count1 = 1;
}

//if the Hy has passed through a zero point


if (count1 == 1)
{
//if Hy is passing through -Hy_S
if (( Hy_m< (-Hy_s+zero)) & (Hy_m> (-Hy_s-zero)))
{
count2 =1;
}

}
}
count1 = 0;
count2 = 0;
while (loop2)
{
vertical_check();
a = a_calculation();
convert(chrASCII,a);
write_string(chrASCII,1);
Hy_m = ADC_reading(1);
Hy_m -= Hy_offset;
Hy_m *=1.0147;

if ( a > max2)
{
max2 = a;
}

//if Hy is passing through zero


if ((Hy_m<zero) & (Hy_m>-zero))
{

//if Hy has passed through Hy_s


if (count2 == 1)
{
write_string(" Max found",1);
magnitude = (max1 +max2 )/2;
write_string(" Please stop",2);
wait(20000);

//save the magnitude in Flash memory


EADRH =0x00;
EADRL =0x00;
mag_data1=magnitude/160;

97
Development of an Electronic Compass

mag_data2=magnitude-mag_data1*160;
EDATA1=mag_data1;
EDATA2=mag_data2;
ECON=0x05;
ECON=0x02;
write_string("magnitude saved",2);
wait(20000);

loop2 = 0;
return magnitude;
}
}

//if Hy is passing through Hy_s


if (( Hy_m< (Hy_s+zero)) & (Hy_m> (Hy_s-zero)))
{
count2 = 1;
}
}
}

//end magnitude_test()-------------------------------------------------------------------------------------

//function Hz_calcuation(), to calculate the z component with the known magnitude.-----------

float Hz_calculation()
{
float Hx_z, Hy_z;
Hx_z = ADC_reading(0); //read the Hx
Hx_z -= Hx_offset;
Hx_z = - Hx_z;
Hy_z = ADC_reading(1); //read the Hy
Hy_z -= Hy_offset;
Hy_z *=1.0147;

Hz =sqrt(magnitude*magnitude - Hx_z * Hx_z - Hy_z * Hy_z); //calculate Hz


return Hz;
}

//end Hz_calculate()---------------------------------------------------------------------------------------

//function heading_calculation(), to calculate the heading in degree and display it----------------

float heading_calculation()
{
int Hx_h, Hy_h, Hx1_h, Hy1_h;
float sinpitch_h, sinroll_h, cospitch_h, cosroll_h,heading_h;

Hx_h = ADC_reading(0);

98
Development of an Electronic Compass

Hx_h -= Hx_offset;
Hx_h = - Hx_h;
Hy_h = ADC_reading(1);
Hy_h -= Hy_offset;
Hy_h *=1.0147;

sinroll_h = ADC_reading(2); //read AX


sinroll_h -= roll_zero_bias_now; //calculate sinroll_h
sinroll_h /= roll_sensitivity_now;
cosroll_h = sqrt(1.0 - sinroll_h * sinroll_h);

sinpitch_h = ADC_reading(3); //read AY


sinpitch_h -= pitch_zero_bias_now; //calculate sinpitch_h
sinpitch_h /= pitch_sensitivity_now;
sinpitch_h /= cosroll_h;
sinpitch_h = -sinpitch_h;
cospitch_h = sqrt(1.0 - sinpitch_h * sinpitch_h);

Hx1_h = cospitch_h*Hx_h + sinpitch_h*sinroll_h*Hy_h + sinpitch_h*cosroll_h*Hz;


//calculate x value in horizontal
Hy1_h = cosroll_h*Hy_h - sinroll_h*Hz;

heading_h = atan(-Hy1_h*1.0/(Hx1_h*1.0)); //calculate heading in radian


heading_h *= 180.0; //transfer the heading to degree
heading_h /= 3.14;

//transfer the heading to be from 0 to 360 degree


if (Hx1_h<0)
{
heading_h +=180;
}
if ((Hx1_h>0) & (Hy1_h>0))
{
heading_h +=360;
}

//Add the delination 1.8 degree


heading_h +=1.8;

if (heading_h >= 360.0)


{
heading_h -=360.0;
}
heading_h *=10.0;
return heading_h;
}

//end heading calculation()-------------------------------------------------------------------------------

99
Development of an Electronic Compass

//function start_condition(),to make sure the compass is down, it will display 'turn down' until
the pitch is at least 83 degree-----------------------------------------------------------------------------

void start_condition()
{
float sinpitch_s,sinroll_s,cosroll_s;
int loop;

loop = 1;

while (loop)
{
sinroll_s = ADC_reading(2); //read AX
sinroll_s -= roll_zero_bias_now;
sinroll_s /= roll_sensitivity_now;
cosroll_s = sqrt(1.0 - sinroll_s * sinroll_s);

sinpitch_s = ADC_reading(3); //read AY


sinpitch_s -= pitch_zero_bias_now;
sinpitch_s /= pitch_sensitivity_now;
sinpitch_s /= cosroll_s;
sinpitch_s = -sinpitch_s;

write_string("turn down",1);

//if the pitch is large than 83 degree, display 'turn around'


if (sinpitch_s <-0.99)
{
loop =0;
write_string("turn around slow",1);
wait(5000);
}

}
}

//end start_condition()-------------------------------------------------------------------------------------

//function convert_point((char ausgabe_string[15], long Zahl), convert the number/10.0, like


conver 19 to 1.9--------------------------------------------------------------------------------------------

void convert_point(char ausgabe_string[15], long Zahl)


{
int i,Vorzeichen,letzte_Ziffer;

for(i=0;i<=14;i++)
ausgabe_string[i]=32;

Vorzeichen=32; //default: BLANK

100
Development of an Electronic Compass

if(Zahl<0)
{
Vorzeichen=45; //ASCI f¨¹r "-",
Zahl= -Zahl; //change negtive to positive
}

for(i=13;i>0;)
{

letzte_Ziffer = Zahl%10; //divided by 10, get the residual,

Zahl = Zahl-letzte_Ziffer; //get the number that ones number is 0


Zahl = Zahl/10; //get the number from tens number

if(Zahl<=9) //if the number just has tens number


{
ausgabe_string[i]=letzte_Ziffer+48;
ausgabe_string[i-1]=Zahl+48;
i--;
break;
}

ausgabe_string[i]=letzte_Ziffer+48;
i--;

ausgabe_string[0]=Vorzeichen;
ausgabe_string[14]='\0';

for(i=1;i<12;i++) //shift the bits before the point one bit left
{
ausgabe_string[i]=ausgabe_string[i+1];
}
ausgabe_string[12]=46; //The bit 12 is the point

}//ENDE convertierung

//funvtion zero_bias_sensitivity_adjustment, to correct the zero bias and


sensitivity-----------------------

void zero_bias_sensitivity_adjustment()
{

power_acce_now =ADC_reading(4);
pitch_zero_bias_now = pitch_zero_bias
+(power_acce_now-power_acce)*sensitivity_zero_bias;
roll_zero_bias_now = roll_zero_bias
+(power_acce_now-power_acce)*sensitivity_zero_bias;
pitch_sensitivity_now = pitch_sensitivity
+(power_acce_now-power_acce)*sensitivity_sensitivity;

101
Development of an Electronic Compass

roll_sensitivity_now = roll_sensitivity
+(power_acce_now-power_acce)*sensitivity_sensitivity;

}
//End zero_bias_sensitivity_adjustment
----------------------------------------------------------------------------

//function read magnitude, read the magnitude from Flash memory --------------------------------

void read_magnitude()
{
char chrASCII[16];

EADRH=0x00;
EADRL=0x00;
ECON =0x01;

magnitude=EDATA1*160+EDATA2;
write_string(" Magnitude is ",1);
convert(chrASCII,magnitude);
write_string(chrASCII,2);
wait(20000);
}
//End read_magnitude --------------------------------------------------------------------------------------

102

Вам также может понравиться