
#define TIMER2_LOW_USE_SYSCLK	0x10
#define TIMER2_HI_USE_SYSCLK	0x20

#define TIMER3_LOW_USE_SYSCLK	0x40
#define TIMER3_HI_USE_SYSCLK 	0x80

sbit LED1 = P2 ^ SBIT_P2_1;	/* LED of port 2.1 */
sbit LED2 = P2 ^ SBIT_P2_3;	/* LED of port 2.3 */

void init_timer2(const INT count_val)
{
    /* reset timer 2; clear TF2; use SYSCLK / 12 */
    TMR2CN = 0x00;					

    /* use SYSCLK for timer 2 */
    CKCON  = TIMER2_LOW_USE_SYSCLK | TIMER2_HI_USE_SYSCLK;	
	
    /* initialize reload value */
    TMR2RLL = count_val;
    TMR2L   = 0xFF;  /* set to overflow to reload right away */					
    ET2 = 1;  /* enable timer 2 interrupt */
    TR2	= 1;  /* enable timer 2 */
}

void init_timer3(const INT count_val)
{
    /* reset timer 3; clear TF3; use SYSCLK / 12 */
    TMR3CN &= ~(0x01 | 0x04 | 0x40 | 0x80);
    
    /* use SYSCLK for timer 3 */
    CKCON  = TIMER3_LOW_USE_SYSCLK | TIMER3_HI_USE_SYSCLK;	
	
    /* initialize reload value */
    TMR3RLL = count_val;
    TMR3L   = 0xFF;  /* set to overflow to reload right away */		
						
    EIE1   |= 0x80;
    TMR3CN |= 0x04;
}

void _isr_timer2(void) interrupt INTERRUPT_TIMER2
{
    TF2H = 0x00;  /* clear the interrupt flag */

    /* toggle the LED */
    LED1 = ~LED1;
}

void _isr_timer3(void) interrupt INTERRUPT_TIMER3
{
    /* clear the interrupt flag */
    TMR3CN &= ~0x80;

    /* toggle the LED */
    LED2 = ~LED2;
}
