
Here's a simple sine wave audio generator. It generates a nice looking, and sounding sine wave by using a resistor A/D converter connected to 8 pins on port D. To the right is the waveform as displayed on a CRO. Running at 8Mhz (internal oscillator) it puts out a 400Hz tone.

I generate the 256 constants for the samples of the sine wave with a little python program - the output is pasted into the C source code. The board you see here is a nifty little target board from Evil Mad Scientist.
The resistor ladder is an R-2R design using 10k and 20k resistors. Here's the
circuit I used.
The python code is shown here.
from math import sin, radians
for i in range(256):
degrees = i * 360 / 255
value = (sin(radians(degrees)) + 1) * (255 / 2)
print "0x%02x," % int(value),
if ((i + 1) % 16) == 0:
print
And here is the actual code for the chip:
#include // Defines pins, ports, etc to make programs easier to read
#define F_CPU 800000UL // Sets up the default speed for delay.h
#include
#define DELAY_MS 0
const int sinevalues[] = //256 values
{
0x7f, 0x81, 0x83, 0x87, 0x8a, 0x8e, 0x90, 0x92, 0x97, 0x99, 0x9d, 0x9f, 0xa2, 0xa6, 0xa8, 0xac,
0xae, 0xb2, 0xb4, 0xb6, 0xba, 0xbc, 0xc0, 0xc2, 0xc4, 0xc7, 0xc9, 0xcd, 0xce, 0xd0, 0xd3, 0xd5,
0xd8, 0xda, 0xdd, 0xde, 0xe0, 0xe3, 0xe4, 0xe7, 0xe8, 0xe9, 0xeb, 0xec, 0xef, 0xf0, 0xf1, 0xf3,
0xf3, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfa, 0xfb, 0xfc, 0xfc, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd,
0xfe, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfc, 0xfc, 0xfb, 0xfa, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5,
0xf4, 0xf3, 0xf2, 0xf0, 0xef, 0xec, 0xeb, 0xea, 0xe8, 0xe7, 0xe4, 0xe3, 0xe1, 0xde, 0xdd, 0xda,
0xd8, 0xd7, 0xd3, 0xd2, 0xce, 0xcd, 0xc9, 0xc7, 0xc6, 0xc2, 0xc0, 0xbc, 0xba, 0xb8, 0xb4, 0xb2,
0xae, 0xac, 0xaa, 0xa6, 0xa4, 0x9f, 0x9d, 0x99, 0x97, 0x95, 0x90, 0x8e, 0x8a, 0x87, 0x85, 0x81,
0x7f, 0x7a, 0x78, 0x76, 0x71, 0x6f, 0x6b, 0x68, 0x64, 0x62, 0x60, 0x5b, 0x59, 0x55, 0x53, 0x51,
0x4d, 0x4b, 0x47, 0x45, 0x43, 0x3f, 0x3d, 0x39, 0x37, 0x34, 0x32, 0x30, 0x2d, 0x2b, 0x28, 0x26,
0x25, 0x22, 0x20, 0x1d, 0x1c, 0x1a, 0x18, 0x16, 0x14, 0x13, 0x11, 0x0f, 0x0e, 0x0c, 0x0b, 0x0a,
0x09, 0x08, 0x06, 0x06, 0x04, 0x04, 0x03, 0x02, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x02, 0x03, 0x04, 0x04, 0x06, 0x06, 0x07, 0x09,
0x0a, 0x0b, 0x0c, 0x0d, 0x0f, 0x11, 0x13, 0x14, 0x15, 0x18, 0x19, 0x1c, 0x1d, 0x20, 0x22, 0x23,
0x26, 0x28, 0x2b, 0x2d, 0x2f, 0x32, 0x34, 0x37, 0x39, 0x3b, 0x3f, 0x41, 0x45, 0x47, 0x4b, 0x4d,
0x4f, 0x53, 0x55, 0x59, 0x5b, 0x5e, 0x62, 0x64, 0x68, 0x6b, 0x6d, 0x71, 0x73, 0x78, 0x7a, 0x7e
};
int main()
{
DDRD = 0xff; // port D all output
while(1)
{
short int i;
for(i = 0; i < 0xff; i++)
{
PORTD = sinevalues[i];
//_delay_ms(DELAY_MS);
}
}
return(0);
}