I'll very appreciate if you can help me with the following issue: Configured Timer 2 on P89C51RD2 . I'm getting around me and become confused from it ... So, I configure Timer 2 / 16-bit timer (the timer overflows (goes from 0xFFFF to 0x0000) An interrupt service routine (ISR) have to be invoked whan timer 2 have expired. (In which register I have to set the interrupt number void timer2_ISR (void) interrupt xx ??) Can you send me a 'C' example code which demonstrated this issue. Thanks Moshe
Hi, Here is an example of using timer 2 from one of my projects, which you can easily customise to suit your own requirements:-
#define XTAL 22118200L #define TIMER2_CLOCK_TICK_PERIOD 1e-03 /* 1ms */ #define TIMER2_CLK_TICK_RELOAD (unsigned int)(-TIMER2_CLOCK_TICK_PERIOD * XTAL / 12) /* init timer2 to required delay and start it*/ static void timer2_init(void) { T2CON = 0x00; /* timer 2 16 bit auto-reload. */ TL2 = TIMER2_CLK_TICK_RELOAD & 0xFF; TH2 = TIMER2_CLK_TICK_RELOAD % 0x0100; RCAP2L = TIMER2_CLK_TICK_RELOAD & 0xFF; /* lower byte of 16 bit reload value. */ RCAP2H = TIMER2_CLK_TICK_RELOAD % 0x0100;/* upper byte of 16 bit reload value. */ TR2 = 1; /* start timer 2. */ ET2 = 1; /* enable timer 2 interrupt. */ } /* timer 2 interrupt function */ static void isr_timer2(void) interrupt 5 { TF2 = 0; /* Clear Timer 2 overflow flag bit. Must be cleared in software. */ /* anything else to be done in timer2 interrupt? */ }
Previous Post continued.. I just realise that there is a mistake on how I have shown the upper byte is calculated to be loaded into TH2 and RCAP2H, they should of been '/' rather than '%' so whe now have :-
TH2 = TIMER2_CLK_TICK_RELOAD / 0x0100; RCAP2H = TIMER2_CLK_TICK_RELOAD / 0x0100;/* upper byte of 16 bit reload value. */
Hi Thanks. It's works but u have to add EA=1 Moshe /* init timer2 to required delay and start it*/ static void timer2_init(void) { T2CON = 0x00; /* timer 2 16 bit auto-reload. */ TL2 = TIMER2_CLK_TICK_RELOAD & 0xFF; TH2 = TIMER2_CLK_TICK_RELOAD % 0x0100; RCAP2L = TIMER2_CLK_TICK_RELOAD & 0xFF; /* lower byte of 16 bit reload value. */ RCAP2H = TIMER2_CLK_TICK_RELOAD % 0x0100;/* upper byte of 16 bit reload value. */ TR2 = 1; /* start timer 2. */ ET2 = 1; EA = 1; }