473,569 Members | 2,880 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1724
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************ **********@d34g 2000cwd.googleg roups.com>,
Ivanna Pee <fr***@Infected mail.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<somet hing>: 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
4427
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 Enterplise Linux 3 ES, and applied FixPack fp5_mi00069.tar to it. After creating an instance, starting the database, creating a database, and entering...
11
4234
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 threads of a multithreaded program using embedded SQL. Since the threads do not need to share transaction scopes, the sqleAttachToCtx family of APIs do...
8
9720
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 of ideas. Now i know a saw tooth function is already implemented in C, but i was wondering if it could be produced without it. Thanks in advance
0
2194
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 Group. For the full list of upcoming Windows Mobile and Embedded chats, to review archived chat sessions, or to request a reminder for a chat,...
59
7127
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. I think I could possibly get what I want by launching a captive copy of postmaster with appropriate args but it seems conceptually cleaner to not...
0
2248
by: YellowFin Announcements | last post by:
Whitepaper: "Yellowfin Reporting" enables Embedded Business Intelligence -------------------------------------------------------------------------------- Embedded reports are a standard requirement of most applications. But users are increasingly demanding more sophisticated reporting from applications - seeking such features as custom...
2
9723
by: sarahwax | last post by:
How do i make the effect of waves moving in the ocean using flash?> Sarah
1
1309
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 HERE OR TO CONTACT ME MY MAIL IS THX
30
4265
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
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7619
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7930
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7681
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6290
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
2118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
950
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.