In article <11**********************@d34g2000cwd.googlegroups .com>,
Ivanna Pee <fr***@Infectedmail.comwrote:
>What is the best way to generate many square waves concurrently using only
one timer?
Well, that's really an algorithm question rather than a C question,
and some computer music newsgroup might be a better place to ask.
>The way I have thought is using arrays. So for example for a wave with freq
770Hz I can generate it by using an interrupt every 0.1msec (10KHz) with the
timer and using 10KHz/770=13 elements of the array, hence I scan the
elements of the array which will look like
>BYTE b770Hz [14] EQ {1,1,1,1,1,1,1,0,0,0,0,0,0};//this would be global
That isn't going to get you a 770 Hz wave: that is (at best) going to
get you a 769.23077<something>: Hz wave. That's going to generate
a "beat note" against 770 Hz about every 1.3 seconds.
You could get better accuracy by using a 1299 or 12987 element array
(if you were going to use the array method).
An alternate method is to calculate the constant
floor(10000/770*1000) = 12987 (if you use integer arithmetic make sure
you don't overflow your int size -- use long if you need to.)
Then initialize a storage location with 0. Upon each timer interrupt,
increment the storage location by this constant. If the result
exceeds 99999 then subtract 100000 from the storage location
and toggle the state of the output bit. Just keep going in this
manner. The resulting output frequency will be 770 Hz to better
than one part in 1000. You can generalize this fairly easily for
high precision or different frequencies.
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell