This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Timer 0 Interrupt

Hi

I am trying to use a Timer 0 as a 16-bit Timer to generate an Interrupt every 10 ms. In this example (downloaded from keil), the Interrupt is generated every 65536 clocks

My question is how would i modify the example so that the interrupt is generated not according to the clock cycles, but rather by how much time passed away?

Do I have to prescale the clock that is benig feed to Timer 0? if so, how?

static unsigned long overflow_count = 0;

void timer0_ISR (void) interrupt 1
{
overflow_count++; /* Increment the overflow count */
}

void main (void)
{

TMOD = (TMOD & 0xF0) | 0x01; /* Set T/C0 16-bit Timer Mode */
ET0 = 1; /* Enable Timer 0 Interrupts */
TR0 = 1; /* Start Timer 0 Running */
EA = 1; /* Global Interrupt Enable */

while (1)
{
}
}

Parents
  • Hi,

    Try this:-

    #define	XTAL			22.1184e6		/* system clock frequency */
    #define	CLOCK_TICK_PERIOD	20e-3		/* time period required (= 20ms.) */
    #define	CLOCK_TICK_RELOAD	(unsigned int)(-CLOCK_TICK_PERIOD * XTAL / 12)	/* Timer 0 16 bit relaod value for 20ms. clock tick */
    
    TH0 = CLOCK_TICK_RELOAD / 0x100;   /* For my example a 20ms. Timer */
    TL0 = CLOCK_TICK_RELOAD & 0xFF;
    

    You just need change the #defines to reflect your requirements and set up the timer to be 16 bit and reload when it overflows (and generates an IRQ?)
    Mark.

Reply
  • Hi,

    Try this:-

    #define	XTAL			22.1184e6		/* system clock frequency */
    #define	CLOCK_TICK_PERIOD	20e-3		/* time period required (= 20ms.) */
    #define	CLOCK_TICK_RELOAD	(unsigned int)(-CLOCK_TICK_PERIOD * XTAL / 12)	/* Timer 0 16 bit relaod value for 20ms. clock tick */
    
    TH0 = CLOCK_TICK_RELOAD / 0x100;   /* For my example a 20ms. Timer */
    TL0 = CLOCK_TICK_RELOAD & 0xFF;
    

    You just need change the #defines to reflect your requirements and set up the timer to be 16 bit and reload when it overflows (and generates an IRQ?)
    Mark.

Children
No data