CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Sine wave using PWM and LPF

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
OldGuest
Guest







Sine wave using PWM and LPF
PostPosted: Fri Apr 07, 2006 4:39 am     Reply with quote

Hi,

Can we generate a perfect sinusoid from a PWM signal using a simple RC circuit. Ive observed a simple LowPass RC circuit wont be enough to generate a perfect sine, when the pulse width is changing after intervals?
Actually im making a simple D/A in this way.
OldGuest
Guest







Follow up
PostPosted: Fri Apr 07, 2006 5:02 am     Reply with quote

Or let me simple put it. I am generating a simple square wave of 250Hz and would like to make it a perfect sine using an RC LPF. Frequency can also be changed if im able to get desired output by doing so
Ttelmah
Guest







PostPosted: Fri Apr 07, 2006 7:06 am     Reply with quote

'Perfect sine'. Answers would depend on your definition of 'perfect'. Basically the closer to 'perfect' you want, the narrower the filter bandwidth, and faster the rolloff of the filter needs to be. Realistically, you will need more than a simple RC filter to do this. A very tightly tuned, Hi-Q LC filter could give a good result, or an active filter. In either case, if you want the change the incoming frequency, you will have to shift the centre frequency of the filter as well. The easiest way to get a good rolloff,and easy tuning, would probably be to use a filter 'IC', which allows the centre frequency to be adjusted by changing the clock feeding it.

Best Wishes
Charlie U



Joined: 09 Sep 2003
Posts: 183
Location: Somewhere under water in the Great Lakes

View user's profile Send private message

PostPosted: Fri Apr 07, 2006 7:09 am     Reply with quote

I wouldn't say that you could generate "perfect" sine wave, but you could generate a "mighty nice" one. How close to perfect depends on your PWM frequency, how many bits of resolution in the PWM, your desired output frequency, the update rate, and your filter frequency and order. You can run through the design and calculate your signal to noise if you want, but if you are just wondering if its possible, then just try it. To get the best output, start with your output frequency, then design a low pass filter with a cut off just above that. You will need to set your PWM frequency about 10x above your output frequency. You want the most resolution possible in the PWM, so pick an oscillator frequency accordingly. You will need to dig into the processor data sheet to solve that one. Pick an update rate for the PWM and calculate the values needed for the sine wave. You can get away with only 90deg of the wave form if you add and subtract, and increment and decrement your way through the data. You can store this as a constant array in your code. Then go for it.
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Fri Apr 07, 2006 7:16 am     Reply with quote

As an educational experiment you should try generating a square wave and put it though several RC filters in a row. Look at how the square wave gradually shifts to a sine wave with each filter section.

If you want to change the frequency much you will need to retune the filter. The easiest way to do that is to get a clocked filter that uses a clock 50x the cutoff frequency. Then generate a square wave 64x the desired frequency. Use a 74HC4040 type chip to divide the square wave by 64 to get the desired frequency which you then put through the filter, clocked at the 64x rate. As you tune the 64x square wave the output and filter cutoff track together. Finally you need a real analog filter to get rid of the clocked filter artifacts.
_________________
The search for better is endless. Instead simply find very good and get the job done.
C Turner



Joined: 10 Nov 2003
Posts: 40
Location: Utah

View user's profile Send private message

Sine wave using PWM and LPF
PostPosted: Fri Apr 07, 2006 11:28 am     Reply with quote

It depends upon exactly how "busy" your processor is, but you can make some very nice-looking sine waves with minimal hardware filtering and a bit more code/CPU time if you go through the trouble of making a sine lookup table and use DDS techniques.

Several years ago, I needed to make some *very* pure sine waves at precise frequencies from about 67.0 to 250.0 Hz (it was for a subaudible encoder for an amateur radio transmitter, actually...) so I chose the 12C509 (the then-newest 8-pin PIC and version 2.something of the compiler...)

Not having a PWM, I simulated such in software, using Timer0 as a "software interrupt" to simulate an ISR and ending up with a PWM frequency/sampling rate of 3906.25 Hz ((4 MHz / 4) / 256) with the PWM pulse width being generated with tight assembly code inside the "ISR."

For the arbitrary frequency, "DDS" (Direct Digital Synthesis) techniques were used, along with a 128-entry sinewave lookup table. Two additional variables were used: An "accumulator" and the "frequency word." and the lookup table was indexed using the top byte of the "accumulator" - ignoring the MSB.

Here's how it works:

- At each ISR call, the "frequency word" is added to the accumulator.
- The top byte of the accumulator is then used to index the "sine" table (not using the MSB - that is simply masked off in the lookup)
- The indexed sine value is then fed into the PWM generator, yielding a voltage proportional to the selected value.

With this scheme, one can generate any frequency (obeying Nyquist's theorum, of course) with the accuracy afforded by the "accumulator" - that is, in this case 3096.25Hz / (2^16) or about 0.06 Hz. (With appropriate filtering, one can actually "oversample" and generate frequencies above the Nyquist limit - but that's another story...)

The advantage of this sort of scheme is that one can set the lowpass filter's cutoff frequency well-above the output frequency (in this case, at 250 Hz the output frequency is still less than 1/10th of the sample rate) and that one can generate frequencies that are not integral divisors of any clock rate that might be present: The precision is limited solely by the size of the "frequency word" and "accumulator" registers. With the lowpass so far above the actual output frequency, one can set the frequency anywhere below the lowpass's cutoff with a constant output amplitude. In contrast, generating a "sine wave" using a bandpass filter - or a heavily-laden lowpass filter will result in an amplitude that goes all over the place when one changes frequency.

This "software" PWM had an effective resolution of 7 bits and using a simple 3-stage R/C lowpass filter (owing to the lack of hardware PWM, the 1st R/C stage was used for charge storage and didn't really contribute to the lowpass filtering to any degree) the 3906.25 Hz tone was more than 50 dB down and the low-level "spurs" that the DDS synthesis process generates were all >35-40 dB down and completely inaudible on the communications circuit. (It was even cleaner than some commercially-made modules of this type.)

With a 12F683 one has the obvious advantage of having PWM and interrupt hardware handy. If you have the ISR triggered by Timer2, all you need to do is to throw the lookup table's value at the PWM hardware and you are done! I would, of course, pre-calculate the next entry in the lookup table after updating the PWM so that one can simply feed the PWM generator an already-available value - and *then* calculate the next one: This is important because the process of lookup takes different amounts of time - depending on the carry action that might occur in the 16 bit math - something that *will* cause jitter if it were to be done *before* the time-critical task of updating the PWM.

The disadvantage of this scheme is, of course, some added complexity in software and the requirement of more code space as well as the overhead of an ISR screaming along at a fairly high speed. There is also the added penalty of your *NOT* being able to use ANY OTHER ISR when doing this: Having another ISR active would also be a source of timing "jitter" in the generation of the waveform. In a practical sense, this isn't too much of a problem as one could simply poll and service those things that might need attention (such as A/D conversion, checking pins, etc.) within the "PWM" ISR anyway (after updating the PWM, of course.)

I hope that this helps - and isn't too vague a description. If needed, I could dig up the '509 code - although code for the 'F683 would be much simpler.

CT
Konrad



Joined: 15 Sep 2003
Posts: 34

View user's profile Send private message

Simple Sine Wave
PostPosted: Tue Apr 11, 2006 2:16 pm     Reply with quote

I found this a very simple way to generate a very accurate sine wave,

use a summing amplifier to give 5 points on a sine wave,
Code:

RD0 -20K--
         |
RD1 -62K--    -----10K------
         |    |            |
RD2 -13K-- ----(-)op-amp----

excuse the diagram, I could'nt figure out how to post a bitmap

Code:

int8 sin[8] = {2,1,0,1,2,3,4,3};
int8 cycle;

#int_TIMER2
TIMER2_isr()
{
   if(cycle++ == 7) cycle=0;

   switch(sin[cycle])
   {
      case 0:
         output_d(0x06);//5000mV
         break;

      case 1:
         output_d(0x04);   //4268mV
         break;

      case 2:
         output_d(0x01);   //2500mV
         break;

      case 3:
         output_d(0x02);   //733mV
         break;

      case 4:
         output_d(0x00);   //0mV
         break;

      default:
   }
}


this gives very good results even witout much filtering
a couple of caps around the op-amp gives good results
if you are using a single supply you may have to play with the values, etc

This is as good as using a DAC at the same rate i.e. 8 samples per cycle, and gave me much much better results than PWM (though I never got as complex as the last post)[/img]
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

Re: Simple Sine Wave
PostPosted: Wed Apr 12, 2006 7:19 am     Reply with quote

Code:
use a summing amplifier to give 5 points on a sine wave,

RD0 -20K--
          |
RD1 -62K--     -----10K------
          |   |              |
RD2 -13K-- ---- (-)op-amp---


The code button also helps for ASCII art.
_________________
The search for better is endless. Instead simply find very good and get the job done.
OldGuest
Guest







Re:Summing Amplifier
PostPosted: Thu Apr 13, 2006 12:12 am     Reply with quote

First, thank u all for suggestions.
SherpaDoug, are you using the same input for all three inputs of summing amp.(PWM output). Have you tested it also ?
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Thu Apr 13, 2006 6:57 am     Reply with quote

Its Konrad's design but it looks like it should work. The three resistors from PIC pins go to the (-) input of the op-amp. The (+) input I expect is connected to 1/2 the PIC supply voltage, or it can be adjustable as an offset control. The 10K resistor from the op-amp output to the (-) input sets the general amplitude of the signal output.

For general DAC use I would use a binary weighted set of input resistors (10K, 20K, 40K), but specifically for generating a sine wave Konrad's values may be better. I have not tried it or done the detailed math.
_________________
The search for better is endless. Instead simply find very good and get the job done.
Konrad



Joined: 15 Sep 2003
Posts: 34

View user's profile Send private message

similar idea
PostPosted: Wed Sep 16, 2009 5:27 pm     Reply with quote

http://www.edn.com/article/CA6662631.html?spacedesc=designideas&industryid=44217
dbotkin



Joined: 08 Sep 2003
Posts: 197
Location: Omaha NE USA

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Thu Sep 17, 2009 8:12 am     Reply with quote

Wow, Halloween must be close... a zombie thread just rose from the grave!

Very Happy
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group