Electronic Notes.

This page is not intended to deal with electronic design in general, but only to consider those aspects related to my current projects.

Generating sine waves digitally.

Why would we want to generate sine waves digitally ?.  In the case of the Zapper circuit it was because I had to generate two frequencies around 10 KHz with a difference frequency as small as 1 Hz.  This really calls for crystal controlled accuracy rather than the use of free running oscillators. Another problem is that two free running oscillators beat together tend to lock onto each others frequency ..... and one ends up with no difference frequency !.

For many applications low distortion is not really a problem, for example when driving an inductive load, resonant load or simply for electrocuting people. If you want an absolutely distortion free sine wave you are wasting your time in my little cave !.

Square waves are traditionally described as being constructed from an infinite number of sine waves. Specifically a square wave is made up of a fundamental sine wave and it's ODD harmonics.  Square to sine wave conversion simply requires the removal of the harmonics and one is left with the pure fundamental sine wave. The  main ways of doing this are .....

a.    A wave shaping circuit.

b.    A low pass RC or LC filter.

c.    A fundamental frequency band pass filter.

d.     Digital Quasi sine waves.

e.     Pulse Width Modulation (PWM).

Wave shaping.

If one passes a square wave through an amplifier with poor high frequency response, the leading and trailing edges start to noticeably slope inwards. At the extreme, they slope in so much that the square wave ends up as a symmetrical triangular wave. A circuit that does this deliberately is call an "Integrator".  Symmetrical triangular wave forms also only contain the odd harmonics but at a much lower level. If you look at a triangular wave it becomes obvious that if we clip off the points at the top and bottom, it will look even more like a sine wave.  Biased diodes can also be used to shape the waveform to a reasonable approximation of a sine wave.

Low pass filters.

An ideal low pass filter will allow everything below a spot frequency to be passed through it. Practical filters are somewhat less than perfect and have a more gradual effect over a  wider frequency threshold.  The frequency response of a low pass filter may therefore be described as having an attenuation rate above the design cutoff frequency,  of so many db's per octave or decade. Low pass filters vary from the simple, using only passive components such as resistors, capacitors and inductors to the  more complex using active components such as operational amplifiers, switched capacitive filter devices etc.. Basically you get what you pay for, so there is no point in going for 0.001% distortion in a sine wave .... if 5% will be OK for a particular application. If you need the former it may pay you to have a look at The MF6 switched capacitor filter which will provide a single chip solution for Square wave to sine conversion, giving a 60 db attenuation of the harmonics. The MF6 can be driven by an output of a PIC microprocessor or a simple 555 timer.  It should be remembered that we set a low pass filter to a frequency between the fundamental frequency and the third harmonic. If we want a sine wave that we can tune over a wide range, we must also arrange for the filter to follow these frequencies excursions.

Resonant LC filters.

An LC filter will always have the advantage that it is eight times more effective in this application than a simple RC filter.  The other point of interest is that an RC filter will always attenuate a signal whereas a series LC filter can give considerable voltage gain at the center point of the L and the C.  If we consider the Zapper application, the PIC generates a square wave of 5 volts peak to peak because the PIC has a +5 volts supply.  Now assume that the PIC is generating a square waveform at 14 kHz and we feed it into a simple series LC filter of 1mH and 47nF .... at resonance the actual sine wave output voltage across either the L or the C can exceed 30 volts peak to peak, depending on the Q of the L and the C.  Resonance has a way of ironing out lots of distortion , certainly enough for this relatively crude transformer driving application. One does have to consider the danger that generated voltages may exceed the maximum voltage ratings of a given chip or device !. Please note that the values are for resonance at 14 kHz.  For other frequencies you will have to calculate the values or find them experimentally.

A useful tool for establishing the correct value of capacitance for any given inductor, is a capacitor substitution 'box'.  This can be simply a tag board with preferred value capacitors soldered in place.  My own works well for me and covers from 1 nF to 1 ufd in 18 different values. Connect all the capacitors together on one side of the tag board and run a croc clip along the opposite tags to 'tune'. When you find the correct value, clip the croc clip onto the relevant tag or substitute the tag board capacitor in the circuit, with one of the same type and value.

The filter shown in the above circuit is a simple series "L"  filter. In this application it works as a band pass filter and is therefore suitable only when we are generating a fixed frequency. We can improve the slope of this filter by adding further frequency conscious components. For example if we include a capacitor across the input to the filter we have now created a "Pi" low pass filter.  The extra capacitor acting to further attenuate the higher harmonic frequencies. One can cascade several such stages to obtain a sharper cutoff, but again it is probably more cost effective to use an active filter in those cases.

How effectively can a simple LC filter be in converting a Square wave to a sine wave?. Below is a screen shot of the response of a simple resonate LC filter being driven by a 50/50 ratio square wave (generated by a PIC).

Digital Quasi sine wave generation.

Ground referencing.

First lets have a look at a special non - quasi requirement. In this case we are not so much interested in waveform shape, only that it has a zero sum, ie positive excursions must equal negative excursion. This is important in some medical applications to prevent electrolysis  in the flesh under an electrode. The output of a normal half bridge is referenced to ground, which means that the waveform is either zero or at supply+  level and if this was applied to skin electrodes electrolysis  would occur. The solution is to reference the free end of the load to VCC/2 and there are many ways of doing this.  Assuming a square wave input to the half bridge, the output would be .....

As can be seen the half bridge output now swings between .... VCC+  to  VCC- ..... with reference to O volts. Of course each positive excursion will produce electrolysis, as will the negative excursion, but they will cancel each other out.

Quasi or 'modified sine wave generation.

Also known as 'modified sine'. At one time, most invertors generated a square wave output, which was totally unsuitable for powering some types of equipment. In order to digitally generate a waveform closer to the required sine wave, 'quasi'  or 'modified' sine waves were introduced.  In practice the 'sine' waveform was digitally constructed from a number of voltage steps. In the simplest case, only one positive voltage step and one negative voltage step is used. To produce these voltage steps we use the highly efficient half bridge, using Bi-polar or MOSFET transistors.  Any Half bridge requires two switching signals, one for the lower transistor in the bridge and the other for the upper.

Probably the best way of describing how a quasi sine wave is constructed is to consider a practical design. The heart of the circuit is a classical PNP / NPN transistor pair in a half bridge  configuration. The transistors can either be bi-polar or MOSFET's.  The upper and lower transistors are separately switched and when one is on the other is off. The truth table for this is .....

 Upper transistor Lower transistor Bridge output voltage On Off High Off On Low Off Off Open circuit On On Not Allowed

To generate the Quasi sine waveform we switch on the top transistor, then, switch it off, after the pause switch on the lower transistor followed by another pause etc., etc.. That produces a complete quasi sine wave, which can be repeated as required.

Simple Quasi.

There must be a million and one ways of constructing a Quasi sine wave, but this is the way I did it ...

The chip is a  CD4017 feeding a half bridge. Output 0 of the 4017 is the switching signal to the lower transistor of the bridge. Output 2 is the switching signal for the upper bridge transistor, which has to be inverted by the top left transistor. Output 4 is connected to the reset pin.  The top left terminal block is for 5 volts+ plus ground to power the CD4017.  The lower terminal block is the clock input from the PIC (or a 555 etc).  This circuit produces the following waveform .....

Figure 1.

Generating the switching waveform. We could of course program a PIC to generate the two switching signals on two pins, however that would tie up the whole PIC.  Another way is to use a PWM module output which then leaves the PIC the ability to go deal with other tasks in real time.  One solution is to use a Johnson counter such as a 4017 to sequentially generate the two switching signals, H for the PNP transistor and L for the lower NPN transistor in an half bridge configuration.

This may look rather crude but for many applications it is acceptable. First of all it better approximates a sine wave and is easier to 'clean up'. Second it is referenced to zero volts to prevent electrolysis. For battery powered equipment it has the advantage of reducing power consumption by 50%.

Slightly more complex Quasi sine wave construction.

By using the same approach as above, we can improve the quasi waveform shape and reduce harmonics. Here we have used two clock pulses for each excursion and only one clock pulse for the zero volts period.

Figure 2

The  above is reference to the ground of a split power supply. A full bridge could be used with the advantage of doubling the output voltage.  In many cases the quasi sine wave will be fed to a transformer which has the effect of smoothing out the waveform.

The first clock pulse will set output zero HI, the second clock pulse will low output zero and Hi output one. If we OR these two outputs we get the switching signal for H the PNP transistor, (please note an inverter is required here ).

The next clock pulse will switch output one to LO and output two to HI, but since we do not use the output two, both transistors are in their non conducting state and we get zero volts out of the bridge.

On the next clock pulse output two goes lo and output three goes HI. On clock pulse five, output three goes LO and output four goes HI.  We need to 'OR' output three and four together to create the switching signal for the lower NPN transistor.

On the next clock pulse output output four goes LO and output five goes high, but since we are not using it, both transistors are in their non conducting state and the output from the bridge transistors is zero.

On the next clock pulse output six will try to go HI, but we return this output to the reset pin and it has the effect that the chip is reset and output zero goes HI, starting the cycle again.  The wise man will immediately spot that the clock frequency needs to be three times the output frequency we actually require. Don't forget to take the enable pin to ground !. Using the 4017, any frequency below about 4 MHz can be created and swept over a very wide range. Figure 2 gives a power saving of 33% over a square wave form.

A simple quasi sine wave design. Top left terminal block 5 volts in to power only the CD4017 and CD4001 chips.  Bottom left terminal block is for the 'clock in' signal from the PIC and ground. The two far right transistors make up the 300 volt 500mA bridge. The transistor to the left of the red PNP is a 300 volt NPN inverter used to switch on the PNP (see text above).  This works over the supply range 10 - 300 volts which is supplied to the two right hand pins. The output from the center right pin is the quasi sine wave referenced to VCC/2, so the positive excursion is always equal to the negative excursion.  The CD4001 uses two of the gates as NOR and the other two gates as inverters to convert the NOR to OR.

I have also used the same technique to generate precision three phase signals,  from a single 4017 using 3  x 3 input NOR gates, for testing aircraft instruments.

___________

Another method is to superimpose  two or more square waves to produce a  quasi sine wave. For example, PIC's with two PWM channels such as the 16F877 and 18F252 are capable of producing two square waves, which must be of the same frequency, but can have different duty cycles in the range 0 - 100%.  Lets us set one PWM square wave to a 50/50 duty cycle so that it looks like this ....

The other PWM channel we set to give an output of reduced cycle, say 50% and phase shift it (or use the 4017 method described above), so it looks something like this ....

If we superimpose one of these waveforms onto the other the new envelope produced looks something like this .....

Quasi 'sine' wave generation is obviously a very crude and simple approach, but it  only uses one bit of the PIC.  However by selecting the right  duty ratios and pauses between excursions and, one can produce quasi waveforms that may be sufficient for some applications.  The circuit of Figure 1. offer a 33% power reduction over using a square wave. Also it omits many of the low frequency harmonics making it easier to filter the waveform.

If we want low harmonic distortion, we need the higher resolution of more construction points. There is loads of data on the internet concerning eight bit sine wave generation, so I thought it might be interesting to see what results we can get using only  four bits. Please excuse the PIC program which was written in 2003, when I was trying to learn about them.

Digital R2R Sine Wave generation using PIC's

PIC's are digital devices, but it is possible to use them to generate good approximations of sine waves, digitally. We do this by generating a series of voltage steps to produce an approximation of the sine wave. Obviously the greater the number of points, the greater the resolution of the sine wave generated and the less distortion in the final product. Four bits of a PIC , switching a simple resistor attenuator will produce a 16 different voltage levels, which can be used to build up a sine wave. We can of course use the same points more than once. Of course for this to work we will have to actually clock the software counter driving those four bits at 'n' times the frequency we wish to create, where 'n' is the number of points we are going to use to create a single cycle.  The actual frequency depends upon how many points we use and how long we stay at each point during the construction of the sine wave. This tends to limit the upper frequency we can generate for any given PIC xtal frequency. A way around this would be to use a phase lock loop (4046), with a  divide by 'n' (or multiple thereof) counter (4017 etc.) in the feedback loop to the phase detector.  This causes the output of the phase lock loop to run at 'n' times the input frequency. This frequency multiplier signal can then be fed into a simple four bit binary counter to generate the four bits for the resistive attenuator. This also has the advantage that the clocking signal can be generated in the PIC as a background task using the Proton+ HPWM command, while the PIC does other things.

I have 'played around' with this idea a bit, 'tailoring' the frequency and wave shape in software. In practice I found that using 28 points with a one micro second delay between them produced a sine wave good enough to drive a  transformer at 14 kHz.  Feeding the output through the simple LC filter will get rid of all visible distortion.

The Crownhill Proton+ Basic program for this circuit is ...

DEVICE = 16F628
XTAL = 20
Dim i as byte
Dim Var1 as word
OUTPUT
PORTB
DIM X AS BYTE
All_DIGITAL = TRUE
X =
1

Goto Waveout

Waveout:

PORTB = 8
DELAYUS x
PORTB = 9
DELAYUS x
PORTB = 11
DELAYUS x
PORTB = 12
DELAYUS x
PORTB = 14
DELAYUS x
PORTB = 14
DELAYUS x
PORTB = 12
DELAYUS x
PORTB = 11
DELAYUS x
PORTB = 9
DELAYUS x
PORTB = 8
DELAYUS x
PORTB = 6
DELAYUS x
PORTB = 4
DELAYUS x
PORTB = 3
DELAYUS x
PORTB = 2
DELAYUS x
PORTB = 1
DELAYUS x
PORTB = 1
DELAYUS x
PORTB = 2
DELAYUS x
PORTB = 3
DELAYUS x
PORTB = 4
DELAYUS x
PORTB = 6
DELAYUS x
Goto Waveout

Please note that I have not used a loop to read the data because it slows everything up and limits the top frequency to about seven kHz. By rolling out the loop we can get up to about 80 KHz from this circuit. To change the frequency one can either change the value of the delay or adjust the number of points generated. The program was written for the Crownhill Proton+ compiler. It is possible to generate a waveform of almost any shape using this approach, by simply changing the points data. For complex waveform  generation one would simply need to store the waveform data for a single cycle in memory in table form. How well does four bit generation work  ?.  Below is a PC Scope shot of the waveform

Pulse Width Modulation (PWM) sine wave generation.

It is possible to use the PWM output of a PIC to generate a sine wave, by varying the duty cycle of a fixed frequency output.  Imagine that we feed the PWM output of a PIC into a simple RC filter, the output voltage will be directly proportional to the average time spent in the High state. For example using a 5 volt supply, a 50% duty cycle will gives us 2.5 volts at the output of the filter. If we change the duty cycle to 75% we get 3.75 volts out of the filter etc. So by changing the duty cycle in software in a sinusoidal manner ... we produce a sine wave. By choosing suitable RC filter values, the output can be made to vary at an audio rate. A full description of this simple technique can be found in Microchips Application note AN655 which can be seen on the Microchip website.

To calculate  the PWM frequency required we multiply the required sine wave frequency by the number of construction points ..... multiplied by 10.  So to generate a 60 Hz sine wave using 32 construction points we need a PWM frequency of 19.2 kHz.  This tends to make the technique only suitable for generating frequencies below a few hundred Hz.

Conclusions

By using an eight bit R2R network and a low pass filter we can get good low distortion sinewaves up to at least 50kHz.. There are, of course many 'special for purpose' chips available for the generation of sine waves such as the XR range. In nearly all cases they require either relatively high supply currents or/and  high supply voltages. To date I have found no simple 'one chip' solution for this application, and prefer the digital approach. Since electronic design is more an art form rather than a precise science, anything we do that produces the required end result will be absolutely perfect!.