Saquare waves - embedded

I

ivan

Hi all,

What is the best way to generate many square waves concurrently using only
one timer?
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

so the interrupt routine will look like:

static BYTE bIdx=0;

//compute array index
if(bIdx > 13)
bIdx = 0;
else
bIdx++;

//output wave
if(b770Hz[bIdx])
PCOUT |= 0x01;
else
PCOUT &= ~0x01;

Is there a better way of doing this? Maybe something with a better accuracy
on the time axis?

Thanks in advance
Ivan
 
J

jacob navia

ivan said:
Hi all,

What is the best way to generate many square waves concurrently using only
one timer?
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

so the interrupt routine will look like:

static BYTE bIdx=0;

//compute array index
if(bIdx > 13)
bIdx = 0;
else
bIdx++;

//output wave
if(b770Hz[bIdx])
PCOUT |= 0x01;
else
PCOUT &= ~0x01;

Is there a better way of doing this? Maybe something with a better accuracy
on the time axis?

Thanks in advance
Ivan

You can forget about the table and do

//compute array index
if(bIdx > 13)
bIdx = 0;
else
bIdx++;

//output wave
if (bIdx <= 6)
PCOUT |= 0x01;
else
PCOUT &= ~0x01;
 
I

Ivanna Pee

ivan said:
Hi all,

What is the best way to generate many square waves concurrently using only
one timer?
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

so the interrupt routine will look like:

static BYTE bIdx=0;

//compute array index
if(bIdx > 13)
bIdx = 0;
else
bIdx++;

//output wave
if(b770Hz[bIdx])
PCOUT |= 0x01;
else
PCOUT &= ~0x01;

Is there a better way of doing this? Maybe something with a better accuracy
on the time axis?

Thanks in advance
Ivan

Yeah, only write to the port when you need to!
Something like

if( (cnt++ % 7) == 0) {
if(state == high) {
set bit for low
state=low;
}
else {
set bit for high
state=high;
}
}


if the other bits in the register are don't cares for you use
if( (cnt++ % 7) == 0) {
PCOUT ^= 0x01;
}

and of course reset count at sometime ....
 
W

Walter Roberson

What is the best way to generate many square waves concurrently using only
one timer?
[/QUOTE]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top