Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
LinkBack Thread Tools Display Modes
Old 7th February 2005, 09:02 AM   (permalink)
Default question on using tmr2 to generate interrupt every 5ms

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
ryan_ryan is offline  
Old 7th February 2005, 10:30 AM   (permalink)
Default Re: question on using tmr2 to generate interrupt every 5ms

Quote:
Originally Posted by ryan_ryan
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
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Old 7th February 2005, 01:47 PM   (permalink)
Default

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
Mike, K8LH is offline  
Reply

Bookmarks

Thread Tools
Display Modes





All times are GMT. The time now is 03:48 PM.


Electronic Circuits  |  Learning Electronics
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.

eXTReMe Tracker