PAL color video with Atmel controllers


November 26, 2007

The next logical step is to add some basic sounds to the project. This can be done with the PWM channels on the AVR microcontroller.

So far, i've managed to leave Timer/Counter 1 and 2 unused. I'll be needing one of them for audio. Since the human ear is good enough to notice 0.25% offkey sounds, i guess i'm stuck with the 16bit counter. After all, the difference between the values 255 and 254 is 0.39% and it gets larger when going down to zero. Any timer value below 256 cannot be used to accurately present a note. Above 256, the error is always less than 0.2% and is acceptable.
This leaves me with 8 octaves:

Counter valuesOctave
256 ~5118
512 ~10237
1024 ~20476
2048 ~40955
4096 ~81914
8192 ~163833
16384 ~327672
32768 ~655351

The standard middle-A is 440Hz. From this value i can calculate the counter compare value, as well as a lookup table for the other notes. Using the CLK/8 prescaler on the 16MHz system clock, i get a base frequency of 2MHz. The value 4545 then corresponds to 440Hz (the middle-A).

Complete notes table:

Octave 1Octave 2Octave 3Octave 4Octave 5Octave 6Octave 7
C 611563057815289764438221911956
C#577242886214431721536081804902
D 544842724213621681034051703851
D#514262571312856642832141607804
E 485402427012135606730341517758
F 458152290811454572728631432716
F#432442162210811540527031351676
G 408172040810204510225511276638
G#3852619263 9631481524081204602
A 3636418182 9091454522731136568
A#3432317161 8581429021451073536
B 3239616198 8099405020251012506
     
Octave 8Octave 9
478 239
451*225
426 213
402 201
379*190
358 179
338 169
319*159
301*150
284 142
268 134
253 127

Using values below 253 is possible, but there will be audible offsets. Numbers marked with * have more than .2% offset, and would be experienced as being offkey. The frequencies for octave 9 would also be very high (above 8kHz) and unsuitable for musical applications anyway.




Console 32