Function Generator with Pic16f690

Thread Starter

lmartinez

Joined Mar 8, 2009
224
Hello everyone,

I am planning to built a function generator which can produce the following waves: Square, Pulse, Sine, Triangle, and ramp waves. I will be utilizing the PIC16F690 and C-code will be the programming language for the job. Does anyone have any ideas as to how to produce the ramp, sine and triangle waves on the outputs of the microchip? I will like to vary the frequency for the waves from 10 to 10 Mhz. The amplitude I can work on it as I finalize the project. Please advice.........
 

mik3

Joined Feb 4, 2008
4,843
You can use a DAC to convert the digital outptus of the PIC to an analog signal. The digital output of the PIC will vary according to the code to generate a sine, ramp or triangular.
 

Thread Starter

lmartinez

Joined Mar 8, 2009
224
You can use a DAC to convert the digital outptus of the PIC to an analog signal. The digital output of the PIC will vary according to the code to generate a sine, ramp or triangular.
Can you provide more specifics. Thank you
 

DonQ

Joined May 6, 2009
321
I will like to vary the frequency for the waves from 10 to 10 Mhz.
To output even a roughly digitized sine wave, you will have to update a DAC output with at least 10 values per cycle, preferably more like 50-100 values per cycle to get a fairly smooth sine wave.

Even assuming you could calculate a sine, and update the DAC with a new value in only one instruction cycle, you would still need a PIC with a clock frequency of 10MHz * 10 updates/cycle * 4 cycles per instruction or something greater than 400 MHz for your crystal frequency.

I haven't been able to find a PIC that operates at this frequency.

You probably need to either change your requirements, or plan on much more processing power.
 

Thread Starter

lmartinez

Joined Mar 8, 2009
224
To output even a roughly digitized sine wave, you will have to update a DAC output with at least 10 values per cycle, preferably more like 50-100 values per cycle to get a fairly smooth sine wave.

Even assuming you could calculate a sine, and update the DAC with a new value in only one instruction cycle, you would still need a PIC with a clock frequency of 10MHz * 10 updates/cycle * 4 cycles per instruction or something greater than 400 MHz for your crystal frequency.

I haven't been able to find a PIC that operates at this frequency.

You probably need to either change your requirements, or plan on much more processing power.
In other words, can sine waves be generated but the the frequency would have to be a lot less than 10MHz, right? What is a reasonable upper frequency limit for generating sine waves by using the above mentioned pic chip. If you can explain the calculations you mentioned above, it would help me out tremendously understand how to go about it. Thank you in advance
 

DonQ

Joined May 6, 2009
321
In other words, can sine waves be generated but the the frequency would have to be a lot less than 10MHz, right?
Right!

If you are going to calculate the sine values as you output them, it will be very, very, very slow... More like kHz, not MHz.

If you pre-calculate the values, put them in a lookup table, and sequence through the table, you can get much faster, but nowhere near the 10MHz you want. After that, it depends on the code you write.

The math I used is described in the equation. This is assuming a 10MHz sine wave, with 10 values per cycle, on a PIC which uses 4 clock cycles per instruction, and assumes you can update the output value in one instruction (a very optimistic assumption!).

This results in:
clock frequency of 10MHz * 10 DAC values per sinewave cycle * 1 instruction per new DAC value * 4 clocks per instruction

This says you would need a 10MHz*10*1*4 = 400MHz clock to do this, even if you could meet the assumptions (which you can not).

In a similar way, since you can probably not find better than a 20MHz PIC, your max frequency, even using that very optimistic calculation, you would only be able to get about a 500kHz sine wave, and a very rough one at that.

Closer to what is actually possible... Optimistically assuming it will take you 10 instructions to output new values to the DAC (along with the loop tests, table fetch, multibyte values, etc), and you use 100 steps per cycle of the sine wave for a smoother signal, this would slow down your best output to about 5kHz. :(

Better plan is to use the DAC output to supply an analog command to a discrete signal generating circuit separate from the PIC. Sometimes you just have to use the right tool for the job.


"If the only tool in your tool-box is a hammer, everything in the world will look like a nail." :D
 

Thread Starter

lmartinez

Joined Mar 8, 2009
224
"Better plan is to use the DAC output to supply an analog command to a discrete signal generating circuit separate from the PIC. Sometimes you just have to use the right tool for the job."

Thank you very much for the information. Do you have any suggestions on what type of discrete circuit will do the job that you mentioned above.
 

DonQ

Joined May 6, 2009
321
With a quick search I couldn't find one with an analog control, but one I used to use a lot is an ADC08038 or similar. This chip is no longer made, but for awhile, almost every signal generator on the planet in the DC to 2MHz range had one in it. You could probably still find a schematic of the chip for ideas on making a discrete circuit.

Something like an AD9833 might do what you want. This is a fully programmable chip. You write to it using a 3-wire serial interface rather than outputting an analog value. It's about $10 though. This is like a modern day update of the 8038.



If you want to build your own... a discrete circuit is based on charging and discharging a capacitor at a current set by the DAC voltage supplied to it. This makes square and triangle waves fairly easily. Converting to a sine wave is based on taking the triangle wave and rounding the corners, then filtering it to finish forming into a smooth sine wave. If you search for things like "voltage to frequency" or "waveform generator" you can probably find all sorts of stuff.
 

atferrari

Joined Jan 6, 2004
4,763
Something like an AD9833 might do what you want. This is a fully programmable chip. You write to it using a 3-wire serial interface rather than outputting an analog value. It's about $10 though. This is like a modern day update of the 8038.
The AD9833 is a DDS generator. The 8038 isn't.
 

DonQ

Joined May 6, 2009
321
The AD9833 is a DDS generator. The 8038 isn't.

Yeah, actually the chip I was naming from memory is an ICL8038 (copy and paste typo). And it's still not a DDS generator. But, Command in -> Waveforms out... Gets the job done.

DDS or not, who cares? The only problem is, like I said, "This chip is no longer made..."

However, it is on my shelf, and is still used by many. The methodology of this chip can fairly easily be replicated with discrete components on a standard square/triangle generator circuit to create high quality sine waves. The AD9833 would not be a good candidate to try to replicate with discrete components.

http://www.datasheetcatalog.org/datasheet/intersil/fn2864.pdf




How about some other suggestions... while you're here...
 

DonQ

Joined May 6, 2009
321
I've derived a sine wave from a triangle wave using an op amp, 2 diodes, and 2 resistors.
Looks like this takes a triangle wave and puts it through an amplifier stage (U2b) with a gain "break-point" set by the forward voltage drop of the diodes. The 8038 does largely same thing, but with several break-points, set at different voltages. The break-points are part of a divider on the output of the triangle wave (gain<1). This makes the modified triangle wave much closer to a true sine wave than using just one break-point. Now a final stage of filtering would have less work to do.

I had a signal generator that used a discrete version of this same idea, with several breakpoints. The schematic was easier to follow than the data-sheet for the IC chip. If you look at the data-sheet, the repeating pattern of transistors on the right half of the schematic on page 4 is all about modifying the triangle wave.

A version of this with less stages would be easy to do with discrete components. The number of stages would just depend on how pure you want your sine wave to be.
 

Thread Starter

lmartinez

Joined Mar 8, 2009
224
Thank you once again. I will take a look at the information you provided. It would also be nice if I could build a tank(or colpitts oscillator) circuit where I could vary the parameters digitally at any time.
 
Top