473,322 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Saquare waves - embedded

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
Sep 12 '06 #1
3 1712
ivan wrote:
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;
Sep 12 '06 #2

ivan wrote:
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 ....

Sep 12 '06 #3
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
Sep 12 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Jean-David Beyer | last post by:
I have some programs running on Red Hat Linux 7.3 working with IBM DB2 V6.1 (with all the FixPacks) on my old machine. I have just installed IBM DB2 V8.1 on this (new) machine running Red Hat...
11
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate...
8
by: pillip | last post by:
I am trying to generate a number of waves. For square wave i am using a mod funcion in a loop: (i & 2)* amplitude , and running a circular buffer. For a triangular and saw tooth waves I am all out...
0
by: Nick White [MSFT] | last post by:
Hello fellow Microsoft Windows Mobile and Embedded enthusiasts: I invite you to peruse the list below of upcoming technical chats and Webcasts offered by the Windows Mobile and Embedded Devices...
59
by: Jeff Bowden | last post by:
For ease of configuration and other reasons, I would like for my single-user GUI app to be able to use postgresql in-process as a library accessing a database created in the users home directory. ...
0
by: YellowFin Announcements | last post by:
Whitepaper: "Yellowfin Reporting" enables Embedded Business Intelligence -------------------------------------------------------------------------------- Embedded reports are a standard...
2
by: sarahwax | last post by:
How do i make the effect of waves moving in the ocean using flash?> Sarah
1
by: YOUSSEF | last post by:
hi dears, my senior project is TTS(TEXT TO SPEECH ) AND I WANTED TO ASK U IF U KNOW A CLASS IN C# WHICH MAKE CONCATINATE FOR SOUND WAVES WITH HIGH QUALITY . I WILL BE GRATEFUL IF ANY HELPED ME ...
30
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Let's say we had a simple function for returning the amount of days in a month: unsigned DaysInMonth(unsigned const month) { switch (month) { case 8: case 3: case 5:
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.