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

  • 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?


    It sounds like you want to reconfigure the chip so that that you get an interrupt every X milliseconds, instead of every X clock cyles. If so, you should know that it doesn't work that way.

    Calculate the desired time interval in terms of clock cycles. Interrupts are based on clock cycles, not objective time. You'll need to take into account the number of clock cycles per second (based on your device and your oscillator frequency) and write a function that translates milliseconds into cycles, and places that value in the timer reload register.
    Also, the resolution of this timing method is a limiteddue to your chip's internal clock multiplier/divisor/whatever, interrupt latency and your oscillator frequency. Good luck!

Reply

  • 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?


    It sounds like you want to reconfigure the chip so that that you get an interrupt every X milliseconds, instead of every X clock cyles. If so, you should know that it doesn't work that way.

    Calculate the desired time interval in terms of clock cycles. Interrupts are based on clock cycles, not objective time. You'll need to take into account the number of clock cycles per second (based on your device and your oscillator frequency) and write a function that translates milliseconds into cycles, and places that value in the timer reload register.
    Also, the resolution of this timing method is a limiteddue to your chip's internal clock multiplier/divisor/whatever, interrupt latency and your oscillator frequency. Good luck!

Children
  • 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.