Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
07/05/07 08:33
Read: times


 
Msg Score: +1
 +1 Good Answer/Helpful
#141505 - Use Timer0 auo reload
Responding to: ???'s previous message
Surely you will not get a perfect 100kHz from 11.059 Mhz xtal.

However if you use Mode2 auto-reload, you only need to do one toggle in your interrupt service routine.

UNTESTED:-

void timer0() interrupt 1 // interrupt address is 0x000b 
{ 
    P0_1 = !P0_1;      // 5uS hi / 5uS low makes 10uS period.
}
void main(void)
{
    TH1 = 0xFD;
    SCON = 0x50;           // for serial communication
    TMOD = 0x20 | 0x02;    // uart Timer1 auto-reload as well.
    TH0 = -5;              // every 5 clocks assuming 12 clocker.
    TR0 = 1;               // start timer
    EA = 1;                // start IRQ
    while (1) ;
}


Compile this and look at the generated code. If your compiler puts a lot of rubbish into ISR, then just re-write in assembler.
No registers need to be saved, because you can toggle the port pin by itself.

You only have 5 machine cycles to play with anyway including the interrupt vectoring which is between 3 and 8 machine cycles. SDCC compiles the ISR into CPL P0_1 and RETI which is 3 machine cycles.

So my conclusion is that it will work with a 6 clocker 8051 if you never use MUL or DIV. The vectoring is quicker. However your main routine will be fairly lame (IRQ 6-9 machine cycles leaving 1-4 cycles for MAIN)

If you have an 8051 variant that can do PWM then it is possible to do without interrupts.


Prev Ravanth said:
Hi,

I want to generate 100 Khz square wave output from the 80c51 using the crystal 11.059 Mhz.

But i am able to generate only 50 khz maximum,

can you correct my code for making 100 Khz

void main(void)
{


TMOD = 0x20;
TH1 = 0xFD;
SCON = 0x50; // for serial communication
EA = 1;
TR1 = 1;


TMOD=0x01;
TL0=0xFD; // TL0 Reload value
TH0=0xFF; // TH0 Reload value 50 Khz
ET0=1;
TR0=1; // Start Timer0
EA=1;


while(1)
{
}


}

void timer0() interrupt 1 // interrupt address is 0x000b
{

TF0 = 0; // reset interrupt flag
P0_1 = ~P0_1; // P3.4 toggle when interrupt.
TL0=0xFD; // TL0 Reload value
TH0=0xFF; // TH0 Reload value 50 Khz


}


Please help me
Thanks





List of 24 messages in thread
TopicAuthorDate
How to generate 100 Khz square wave 80C51            01/01/70 00:00      
   Tips            01/01/70 00:00      
      1 KHz            01/01/70 00:00      
         that's ...            01/01/70 00:00      
         SI Units            01/01/70 00:00      
            1024 = Ki            01/01/70 00:00      
   C interrupt overhead            01/01/70 00:00      
      RE: C interrupt overhead            01/01/70 00:00      
         and listen to the guru! :-)            01/01/70 00:00      
            several reasons            01/01/70 00:00      
               Yet another reason to avoid using HLL's!            01/01/70 00:00      
               Optimizations            01/01/70 00:00      
   Use Timer0 auo reload            01/01/70 00:00      
      using            01/01/70 00:00      
      interrupt vectoring vs. latency            01/01/70 00:00      
         Then the Interupt must be in ASM            01/01/70 00:00      
            do you mean...            01/01/70 00:00      
               No            01/01/70 00:00      
               Compiler generated IRQ vectors            01/01/70 00:00      
                  OOPS we both meant LJMP REALisr            01/01/70 00:00      
                  the original problem...            01/01/70 00:00      
         Interrupt Latency + Vectoring + Service            01/01/70 00:00      
   indufficient info            01/01/70 00:00      
   use MCS52 better option            01/01/70 00:00      

Back to Subject List