Electronic – Simple function generator using microcontroller

function generatormicrocontroller

Is it possible to make a function generator with a microcontroller (PIC18F4550 OR PICAXE20X2)?

I am thinking that for generating square waves it can be enough to make a pin output high and low on desired time frames. But how about a sinewave? Maybe it is possible with the same idea but putting a inductor or something at the output?

If you generaly think this can work I want to start doing it!

UPDATE
First of all I have to thank all the answers, it really gave me some nice start points.

I have a rather good experience with PIC18F4550 as I did lots of small home project around this chip. So it would be my main choice.
Here is a link to its datasheet

My main points is now as following:
1- Best way and mathematic approach for square waves
2- same thing for sine waves
3- Amplitude and frequency control

Best Answer

Not a complete answer, but to address one of your points:

I am thinking that for generating square waves it can be enough to make a pin output high and low on desired time frames.

That depends on your application. This is considered "naive" square wave synthesis, and doesn't produce a mathematically correct square wave. (It's equivalent to sampling an ideal mathematical function without putting it through an anti-aliasing filter first.)

This also applies to triangle waves, sawtooth waves, and anything else with harmonics above the Nyquist frequency.

It will often be "good enough" if you have many samples (or time frames) per cycle, but not otherwise. For example, if you generate a 10 kHz square wave with a 44.1 kHz sampling rate, it will look like this:

enter image description here

You can see that every few cycles are different lengths. The transitions can only occur on sample boundaries, but an actual square wave would transition at a time in between them. Practically, this results in lots of aliased harmonics below the square wave frequency, which you probably don't want, depending on your application. In audio applications, this sounds awful.

You can avoid this by generating a correct band-limited square wave in software, or by using a sampling frequency much higher than necessary for your signal.

Here's a comparison of the two methods on a 5 kHz square:

Simplistic:

enter image description here

Mathematically correct (generated with additive synthesis):

enter image description here