Continue to Site

# Produce 2 square wave at the same time

Status
Not open for further replies.

#### nanoha

##### New Member
Hello,
I had a 8051 micro C test last week and it ask us to produce 2 square wave of different freq at the same time. (make it appear at oscilloscope)

I stuck when I need to create both of the wave simultaneously. Actually, I don't even know that we can do something simultaneously using micro C.

Even if I use interrupt (for the second wave), isn't the controller leave the main program and read the interrupt program? So I don't see that I can produce both of them simultaneously.

Any idea??? (Or maybe there is something wrong with my understanding. Hope you guys can help.)

##### New Member
You could be using assembler and modify a peripheral port register in one instruction ( minus other register). It would work for a pic tough.

#### colin mac

##### New Member
You could simply use two timer interrupts. The program jumps to ISR at different times, since there are 2 frequencies, and complements the port pin.

There's an assembly example for doing that here

Last edited:

#### 3v0

##### Coop Build Coordinator
Forum Supporter
Iff the processor does not have other work to do you can easily generate as many square waves as you need without timers or interrupts. This is not finished code but it illustrates the idea.

Code:
#define TIME0   1000
#define TIME1   500

#define BIT0     ??   // a port bit
#define BIT1     ??   // a port bit
unsigned int t0, t1;

t0=TIME0;
t1=TIME1;
// make BIT0 and BIT1 outputs
...
void main(void)
{
while(1)
{
if (!(t0--))
{
toggle(BIT0);
t0 = TIME0;
}
else
{
// waste time equal to above
}
if (!(t1--))
{
toggle(BIT1);
t1 = TIME1;
}
else
{
// waste time equal to above
}
// put some delay here if you want to slow things down
}
}

#### nanoha

##### New Member
Thanks for that. It give me some ideas.

Status
Not open for further replies.

Replies
6
Views
5K
Replies
6
Views
2K
Replies
5
Views
4K
Replies
18
Views
4K
Replies
1
Views
4K