question on using tmr2 to generate interrupt every 5ms

Status
Not open for further replies.

ryan_ryan

New Member
dear all,

(1)if using a 4mhz quartz, and using no prescalar or post scalar, and if i want to interrupt every 5ms, that wld requird 5ms/1us = 5000 instructions.. 5000 = 0x1388 and pr2 register only 8 bits so unable to hold such a big value...

(2)So with the same configurations but with a prescale of 1:16, this will give me (1/(4*prescaler)) * CLK freq = (1/(4*16)) * 4Mhz = 62500Hz or 16us per machine instruction...To interrupt every 5ms, i will need 5ms/16us = 312.5 , again too big for the pr2 register which is 8 bits..

(3)So my question is how do i use postscaler? if i am using both post and pre scal, will i be able to make the value smaller so that i can write to pr2?what is the general way to calculate the value i need to write to pr2 reg if i am using both prescalar and post scalar. Is there a maths like formula as i wrote in part (2) that explains how to get the value if i am using pre and post scaler??

I hope those who have an idea of what i am saying can sound me out..thanks
 

This sets tmr2 to give 1mS interrupts, you can increase it 5mS by increasing the PR2 value to "D'125'-1", I've used it at "D'250'-1" to give 10mS interrupts for a clock display.

Code:
; Set up Timer 2 to generate interrupts every 1 ms.  Since we're assuming an instruction
; cycle consumes 1 us, we need to cause an interrupt every 1000 instruction cycles.
; We'll set the prescaler to 4, the PR2 register to 25, and the postscaler to 10.  This
; will generate interrupts every 4 x 25 x 10 = 1000 instruction cycles.  
; ***********************************************************************************

        clrf    TMR2            ; Clear Timer2 register

        bsf     STATUS, RP0     ; Bank1
        bsf     INTCON,PEIE     ; Enable peripheral interrupts
        clrf    PIE1            ; Mask all peripheral interrupts except
        bsf     PIE1,TMR2IE     ; the timer 2 interrupts.
        bcf     STATUS, RP0     ; Bank0

        clrf    PIR1            ; Clear peripheral interrupts Flags
        movlw   B'01001001'     ; Set Postscale = 10, Prescale = 4, Timer 2 = off.
        movwf   T2CON

        bsf     STATUS, RP0     ; Bank1
        movlw   D'25'-1         ; Set the PR2 register for Timer 2 to divide by 25.
        movwf   PR2
        bcf     STATUS, RP0     ; Bank0

        bsf     INTCON,GIE      ; Global interrupt enable.
        bsf     T2CON,TMR2ON    ; Timer2 starts to increment
 
Use the clock period, as Nigel says, not the frequency and you'll have no problem...

If you use a prescaler value of 4 (4-usec) and a postscale value of 5:1 (20-usec), then a PR2 value of 250 (operand 250-1) will yield interrupts every 5,000 usecs...

Good luck... Regards, Mike - K8LH
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…