Child pages
  • Measuring Phase Offset Using a Microcontroller
Skip to end of metadata
Go to start of metadata

Introduction:

Measuring the phase offset, also known as phase shift, between two signals has a variety of uses in practical applications such as power factor correction and signal processing. This article will demonstrate a low cost way to measure the phase offset between two signals.

This article uses the TI Tiva C microcontroller for demonstration purposes, however the methods shown in this article can be used with basically any microcontroller. 

Useful Links:

Datasheets for parts used in this article: 

LM2903

SN74HC86

SN74HC244

TIVA-C

If the reader is unfamiliar with the Tiva C, the following links are useful:

TI Tiva Launchpad Page

Getting Started with the TI Tiva TM4C123GXL LaunchPad Evaluation Board

 

Calculating Phase Offset: 

Phase offset can be calculated using the following equation.

Where t1 is the time when the first waveform crosses a specific point, such as a zero cross, t2 is the time when the second waveform crosses the same point and T is the period of the waveforms. Note, this method only works if the two waveforms have the same period.

 

Measuring Phase Offset with a Known Period:

Theory of Operation:

In the circuit above, two signals are fed into dual comparators, U1. The comparators will convert the input signals into pulses whose width is equal to the amount of time that the input signal exceeds the voltage level of the comparator's non-inverting input. The output of the comparators are then fed into an XOR gate. The XOR gate will then output a pulse whose width is equal to the time difference between the two signals, aka... the (t2-t1) portion of the phase offset equation. The microcontroller measures the width of this pulse by starting a timer on the rising edge of the pulse and then retrieving the timer value when the falling edge of the pulse occurs. From there, it does some basic math in order to convert the timer value to degrees based on a known fixed period which is hard-coded into the firmware. From there, it prints out the phase offset value over a UART.

Phase Offset: In the figure above, the comparators' outputs are represented by the yellow and pink traces. The XOR gate's output is represented by the blue trace. 

Circuit Notes, BOM, and Software: 

 

 Circuit Notes
  • It is very important that the input signals do not exceed the common mode voltage range of the comparators. Thus, it may be necessary to per-condition the input signals using a transformer or clipping diodes in order for the circuit to work properly.

  • In many cases, it is a good idea to AC couple the input signals by using a DC block capacitor. If used in the circuit above, block capacitors should be placed as shown in the figure below:


  • Selection of the resistor values is very important and has a great effect on the performance of the circuit. The function of the resistors in the circuit are as follows:

R1,R2,R5,R6: These are used to set the trip point for the comparators. The value of these can vary depending on the characteristics of the input signal, and the circuit's supply voltage. In some cases, it is possible to eliminate these resistors entirely and simply connect the inverting input of the comparators to ground. In these cases, it is a good idea to AC couple the input signals as described in the second bullet point of this 'Circuit Notes" section. Using relatively large resistance values for these resistors will significantly reduce the power consumption of this circuit. However, they should still be small enough so that the comparator has sufficient input bias current, which is typically on the order of 20 nA for the LM2903, but can be as high as 500 nA.

R3,R4,R7,R8: These resistors are used to add hysteresis to the comparators in order to correctly sample slow moving signals as well as add noise immunity to the circuit. These resistor values vary depending on the amount of hysteresis desired. For more information about adding hysteresis to comparators, it is recommended that the reader consult the following app note from Analog Devices: http://www.analog.com/en/analog-dialogue/articles/curing-comparator-instability-with-hysteresis.html. It should also be noted that hysteresis will add a small amount of error to the circuit since the comparator's output won't trigger precisely on the zero crossing of the input signal.

R9, R10: These resistors are pull up resistors that allow the comparator's output to match the I/O voltage of the microcontroller. In the circuit shown above, R9 and R10 should be connected to a VCC of 3.3V since the Tiva C uses 3.3V logic levels. The actual value of these two resistors varies, it should be large enough to provide a sufficient voltage drop when the comparator's output goes low, but also small enough that it does not significantly degrade the transition time of the comparator's output. The values are selected for R4 and R8 will also have an effect on the values of R9 and R10 respectively.

 Bill of Materials
PartDescriptionDigikey Part NumberDigikey Link
U1Comparator General Purpose 8-PDIPLM2903NGOS-NDlink
U2XOR (Exclusive OR) IC 4 Channel 14-PDIP296-8375-5-NDlink
U3TM4C123G LaunchPad™296-35760-NDlink

The resistor values can vary based on what type of signals are being measured. For more information on resistor selection, see the circuit notes section above.

 Software

A copy of just the main.c file can be downloaded here: Phase_Offset.c

The full Code Composer Studio work space, which contains the main.c file above and all the drivers, can be downloaded below. More info about importing a project into CCS can be found here: http://processors.wiki.ti.com/index.php/Importing_Projects_into_CCS

workspace_Phase_offset_v1.zip

 

All the peripherals in the program above are run off of the system clock which is slowed down to 3.125 MHz. This was done for simplicity's sake and to make the program easier to follow. However, it is not the optimal method since it will inhibit the performance of other parts of an application by running the entire processor at a fairly slow rate. Different microcontrollers will have different clock speeds and configuration options and it's up to the user to determine the optimum configuration for their processor and application.

Disadvantages of Using this Method:

  • There is no way to determine the direction of the phase offset. The circuit shown above is only able to show the magnitude of phase offset up to 180 degrees, but is unable to determine whether a waveform is leading or lagging.
  • This method is fairly slow. The comparator's response time can reach just under 2 MHz with a sufficient overdrive voltage on the comparator inputs. However in most typical use cases, the input signal characteristics and load impedance will limit the response time to several hundred KHz. In addition, several factors related to the microcontroller, such as the timer's resolution, system clock speed, and code efficiency can also impose restrictions on the maximum speed of the circuit. 

Measuring Phase Offset with an Unknown Period:

Theory of Operation:

This circuit functions in pretty much the same manner as the circuit in the previous method. However, it is also able to determine the period of the reference signal by calculating the time between rising edges of the comparator's output.  To maintain signal integrity, a buffer is used to interface the comparator's output to the GPIO pin PD1.

This method is capable of determining whether the signal being measured is leading or lagging the reference signal by analyzing the XOR's output when a rising edge of the reference signal occurs. If the XOR's output goes low when the reference signal goes high, this means that the signal being measured is also high and thus the signal being measured is leading the reference signal. Likewise, if the XOR's output goes high when the reference signal goes high, this means that the signal being measured is low and thus the signal being measured is lagging the reference signal. This process is illustrated in the scope shots below:

Measured Signal Leading Reference Signal: The yellow trace is the signal measured on pin PD1, the blue trace is the signal measured on pin PC7. 

 

Measured Signal Lagging Reference SignalThe yellow trace is the signal measured on pin PD1, the blue trace is the signal measured on pin PC7. 

Circuit Notes, BOM, and Software: 

 

 Circuit Notes
  • It is very important that the input signals do not exceed the common mode voltage range of the comparators. Thus, it may be necessary to per-condition the input signals using a transformer or clipping diodes in order for the circuit to work properly.

  • In many cases, it is a good idea to AC couple the input signals by using a DC block capacitor. If used in the circuit above, block capacitors should be placed as shown in the figure below:


  • U4 is not always necessary in all cases. However, it is important to note that the impedance of the microcontroller's gpio pin can cause issues with the output of the comparator. Unfortunately, this can be difficult to design around since the characteristics of the gpio pins are not always well documented. A simple way to avoid this issue is to use a buffer to interface the comparator's output to the gpio pin.

  • Selection of the resistor values is very important and has a great effect on the performance of the circuit. The function of the resistors in the circuit are as follows:

R1,R2,R5,R6: These are used to set the trip point for the comparators. The value of these can vary depending on the characteristics of the input signal, and the circuit's supply voltage. In some cases, it is possible to eliminate these resistors entirely and simply connect the inverting input of the comparators to ground. In these cases, it is a good idea to AC couple the input signals as described in the second bullet point of this 'Circuit Notes" section. Using relatively large resistance values for these resistors will significantly reduce the power consumption of this circuit. However, they should still be small enough so that the comparator has sufficient input bias current, which is typically on the order of 20 nA for the LM2903, but can be as high as 500 nA.

R3,R4,R7,R8: These resistors are used to add hysteresis to the comparators in order to correctly sample slow moving signals as well as add noise immunity to the circuit. These resistor values vary depending on the amount of hysteresis desired. For more information about adding hysteresis to comparators, it is recommended that the reader consult the following app note from Analog Devices: http://www.analog.com/en/analog-dialogue/articles/curing-comparator-instability-with-hysteresis.html. It should also be noted that hysteresis will add a small amount of error to the circuit since, the comparator's output won't trigger precisely on the zero crossing of the input signal.

R9, R10: These resistors are pull up resistors that allow the comparator's output to match the I/O voltage of the microcontroller. In the circuit shown above, R9 and R10 should be connected to a VCC of 3.3V since the Tiva C uses 3.3V logic levels. The actual value of these two resistors varies, it should be large enough to provide a sufficient voltage drop when the comparator's output goes low, but also small enough that it does not significantly degrade the transition time of the comparator's output. The values are selected for R4 and R8 will also have an effect on the values of R9 and R10 respectively.

 Bill of Materials
PartDescriptionDigikey Part NumberDigikey Link
U1Comparator General Purpose 8-PDIPLM2903NGOS-NDlink
U2XOR (Exclusive OR) IC 4 Channel 14-PDIP296-8375-5-NDlink
U3TM4C123G LaunchPad™296-35760-NDlink
U4Buffer, Non-Inverting Push-Pull Output 20-PDIP296-1582-5-NDlink

The resistor values can vary based on what type of signals are being measured. For more information on resistor selection, see the circuit notes section above.

 Software

A copy of just the main.c file can be downloaded here: Phase_Offset_Period.c

The full Code Composer Studio work space, which contains the main.c file above and all the drivers, can be downloaded below. More info about importing a project into CCS can be found here: http://processors.wiki.ti.com/index.php/Importing_Projects_into_CCS

workspace_Phase_offset_v2.zip

 

All the peripherals in the program above are run off of the system clock which is slowed down to 3.125 MHz. This was done for simplicity's sake and to make the program easier to follow. However, it is not the optimal method since it will inhibit the performance of other parts of an application by running the entire processor at a fairly slow rate. Different microcontrollers will have different clock speeds and configuration options and it's up to the user to determine the optimum configuration for their processor and application.

Disadvantages of Using this Method:

  • Like the method above, this method is fairly slow. The comparator's response time can reach just under 2 MHz with a sufficient overdrive voltage on the comparator inputs. However in most typical use cases, the input signal characteristics and load impedance will limit the response time to several hundred KHz. In addition, several factors related to the microcontroller, such as the timer's resolution, system clock speed, and code efficiency can also impose restrictions on the maximum speed of the circuit. 
  • Noise can play havoc with this method. If noise causes the microcontroller to miss an edge transition or causes it to detect a false transition, both the period and phase offset calculations can be affected and the circuit as a whole will produce erroneous results. When using this method, the designer should take steps to reduce the noise on the comparator and XOR gate outputs or implement some method of error correction in software. 

Contacting the Author: 

Any comments or questions about this article can be directed to EEwiki@digikey.com 

  • No labels