Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How to make decent 20kHz sinewaves with a PIC?

Status
Not open for further replies.

Mr RB

Well-Known Member
Hi, I'm thinking of making a piece of test equipment with a PIC microcontroller.

The test equipment needs to generate accurate frequency sinewaves of a (reasonably) fixed amplitude under PIC control for sweep and band testing purposes.

I know how to make accurate frequency sinewaves at lowish frequencies using the PIC timer and PWM module and feeding the PWM module from a sine table after each exact period. However with my PIC 18F running at 10 MIPs this system is only good for about 8kHz sine generated, once it gets much higher than that the system must severely sacrifice the quality of the sinewave so it won't reach the 20kHz required.

Another option is to generate the sine wiht an external IC like an XR2206 controlled from the PIC but this reuires a lot of extra complexity and even with closed loop frequency control (PIC measuring frequency) there is still a big chance of freq error or freq instability.

It would be much preferred to generate the frequency directly using the PIC xtal locked timer, and also be good to avoid the use of too much external hardware.

Can anyone suggest a clever solution? Going to a faster PIC 24F etc is not possible, but I am open to the option of PIC generating an accurate squarewave and using some small amount of external hardware to shape that into a sine.

Maybe there is a way to generate 2 lower frequency sines on the PIC and use the higher beat frequency?

Even though I am limited to PIC 18F I have quite a few digital output pins available so maybe there is an option using an external R2R DAC or something?
 
Not knowing all of your parameters involved like sweep range, upper and lower limits, etc, have you thought of a V-to-F converter such as a LM331 with an active output LP filter? Perhaps that may work for you, but the part count might be more than tolerable.
 
I think the better way to generate Sine Waves by an Microcontroller is to use an DDS chip for example from Analog Devices like the AD9833 or the AD9834.

There is additional the possibility to generate triangular or rectangular waves.

The output frequency can be very stabile, it depends from an external clock Source like an crystal oscillator.

The chips can be get data by an simple SPI Bus.
 
Two reliable options.

Hola Roman,

I know of two immediate options:

The DDS chips that are exactly for that. More resolution than what you could eventually need, I think.

The other, (I did it for audio frequency with excelent results) is to drive a CD4518 (dual counter) with the micro. IIRC, driving frequency has to be 8 times the desired one.

Outputs, loaded with resistors going all to a common one so as to have a current through this last following a sinusoidal law. Calculating resistor values was a breeze in Excel.

Buffer and filter.

Notice that prior the filter the amplitude is inherently fixed.

Using a 4MHz xtal I managed to control the output frequency down to the Hz.

From what I recall now, there are two details: last output of first counter is the input of the second and to repeat the wole there was an inverter at the end. (Used a simple transistor for that).

This topology was also used to generate 3 signals ranging from 40 to 450 Hz with 120º phase difference. But IIRC I used more CD4518 thus a higher driving frequency.

All with 18F micros.
 
Hi, I'm thinking of making a piece of test equipment with a PIC microcontroller.

The test equipment needs to generate accurate frequency sinewaves of a (reasonably) fixed amplitude under PIC control for sweep and band testing purposes.

I know how to make accurate frequency sinewaves at lowish frequencies using the PIC timer and PWM module and feeding the PWM module from a sine table after each exact period. However with my PIC 18F running at 10 MIPs this system is only good for about 8kHz sine generated, once it gets much higher than that the system must severely sacrifice the quality of the sinewave so it won't reach the 20kHz required.

Another option is to generate the sine wiht an external IC like an XR2206 controlled from the PIC but this reuires a lot of extra complexity and even with closed loop frequency control (PIC measuring frequency) there is still a big chance of freq error or freq instability.

It would be much preferred to generate the frequency directly using the PIC xtal locked timer, and also be good to avoid the use of too much external hardware.

Can anyone suggest a clever solution? Going to a faster PIC 24F etc is not possible, but I am open to the option of PIC generating an accurate squarewave and using some small amount of external hardware to shape that into a sine.

Maybe there is a way to generate 2 lower frequency sines on the PIC and use the higher beat frequency?

Even though I am limited to PIC 18F I have quite a few digital output pins available so maybe there is an option using an external R2R DAC or something?


Hello Mr RB,

There are a number of solutions but before picking one we have to know what kind of spec's you are after. For example, can you get away with say 16 steps up and 16 steps down for the analog part of the sine generation? There's a number of ways to do that. If you need a really really clean sine you may have to go analog. Also, how adjustable does it have to be? When we go from analog to digital we give up that nice smooth analog transition between frequencies and have to put up with jumps between adjacent bands.
You should probably look at the link Nigel posted first because that looks like a full project planned out and tested already. They are saying it goes up to 60kHz apparently in steps of 500Hz.
 
As you said, outputting a squarewave then shaping (filtering out all harmonics except the fundamental) should work nicely. I've used the MAX294 (**broken link removed**) (8th-order elliptic switched cap filter) which provides a LPF up to 25kHz; just feed it a clock 100 times higher than your desired corner frequency. Very sharp roll off & 58dB stopband.

I have some surplus from an old product if you want them.
 
I had a project to generate square waves in the kHz range. I used a DDS IC. As I wanted square waves the sine output of the DDS was taken to a level detector, but you could use the sine wave directly.

Although I only had a 10:1 frequency requirement, the system worked all the way down to millihertz.

You can use a digital pot for level control.

With those you set the frequency on the DDS IC and the amplitude on the digital pot. There is no processor overhead at all for fixed frequency and amplitude.
 
Wow. Thanks everyone for all the comprehensive answers! :)

Ok it looks like a DDS IC woudl be the better choice if I go for the cost and complexity of an external IC. It beats the XR2206 as it will be xtal locked.

ATferrarri- Thanks for suggesting the binary counter idea. IF using the cost of an external IC I'd probably prefer a dedicated IC that does not need to be driven.

Nigel- Thanks for the link to the Mondo site and the 16F functions generator. It looks similar to what I anticipated as a basic system, using 8 output pins and an R2R resistor network to generate the sine. I'm not sure how it would go for the high frequencies as at 60kHz (claimed) and 5MIPS PIC that leaves only 83 instructions, and it uses a 256 entry sine table. So it's obviously not outputting 256 entries in 83 instructions. The asm source code for the sine function looks about 18 instructions so even at 20kHz that's about 27 entries per full sine wave.

Mr AL- Thanks for your input. Yes I could live with a sine waveform of 32 units amplitude resoluton (5bits). I can do the frequency generation part using some varation of bresenham or DDS so it can make an exact frequency by dithering between periods to make the desired frequency. I have a number of ways to do this and enough experience.

Did you have a suggestion of generating this 5bit waveform on a 10 MIPs PIC 18F? I can get (or make) a 5bit R2R ladder. What number of waveform entries or time resolution would be required? The PIC has to be doing some testing at the same time as the frequency generation, so ideally the generation should be interrupt based and can use about 50% of the processor time. It's still pushing the limit, if I use time resolution of 128 instructions at 10 MIPs that only gives me about 4 events per 20kHz wave...

Dougy83, thanks for the suggestion and the sample offer, but I'm still thinking if it is going to need an extenral specialty IC I may as well use one that generates the whole sine automatically.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top