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.)
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
}
}