PDA

View Full Version : Timer1 and Timer2 interrupts, Darrel Taylor



ronbowalker
- 17th December 2009, 14:21
I am using a PIC16F819 and I am using a Timer1 interrupt on it. I now need to add a second (Timer2) interrupt into my program. I have printed and read the following files of Darrel Taylor's to try and wrap my head around the concept of how to make this happen. I have spent two days reading a lot of posts and examples from others on this forum, but being a rookie is weighing heavy on my progress. I hope someone can shed some light on what I am not understanding on this issue.

.bas files:
DT_INTS-14.bas
ReEnterPBP.bas

Here is what I am currently attempting to do, please review and let me know what to do. I am sure mine is incorrect, but I am trying none the less. TMR2 has no TMR2L or TMR2H according to data sheet. But not sure what to do.



OSCCON = %00000000 '32kHz
ADCON1=7 'Digital

TRISA = %01010100
TRISB = %01000000
PORTA = %00000000
PORTB = %00000010

TICK1 VAR BYTE
TOCK1 VAR BYTE
TICK2 VAR BYTE
TOCK2 VAR BYTE

TICK1=0:TOCK1=0:TICK2=0:TOCK2=0

INCLUDE "DT_INTS-14.bas"
INCLUDE "ReEnterPBP.bas"

ASM
INT_LIST macro
INT_Handler TMR1_INT, ReloadTMR1, ASM, no
INT_Handler TMR1_INT, _T1handler, PBP, yes
INT_Handler TMR2_INT, ReloadTMR2, ASM, no
INT_Handler TMR2_INT, _T2handler, PBP, yes
endm
INT_CREATE
ENDASM

@Freq = 10
@Prescaler = 8

T1CON = $00
TMR1 OFF
T2CON = $00
TMR2 OFF


@ INT_ENABLE TMR1_INT
@ INT_ENABLE TMR2_INT

Main:

IF TOCK1 = 3 then
PULSOUT PORTA.0,20 'RED LED BLINKS ONCE
GOSUB StopTimer1
ENDIF

IF TOCK2 = 9 then
PULSOUT PORTA.1,20 'GREEN LED BLINKS ONCE
GOSUB StopTimer2
ENDIF

GOTO Main

''''''''''''''''''''TIMER INTERRUPTS''''''''''''''''''''''''''''''''''''''''
T1handler:
TICK1 = TICK1 + 1
If TICK1 = 46 then
TOCK1 = TOCK1 + 1
TICK1 = 0
ENDIF
@ INT_RETURN

T2handler:
TICK2 = TICK2 + 1
If TICK2 = 46 then
TOCK2 = TOCK2 + 1
TICK2 = 0
ENDIF
@ INT_RETURN
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''
ASM
ReloadInst = 8
if ((Prescaler == 1)||(Prescaler == 2)||(Prescaler == 4)||(Prescaler == 8))
MaxCount = 65536 + (ReloadInst / Prescaler)
TimerReload = MaxCount - (OSC*1000000/4/Prescaler/Freq)
if ((TimerReload < 0) || (TimerReload > (65535-ReloadInst)))
error Invalid Timer Values - check "OSC", "Freq" and "Prescaler"
endif
else
error Invalid Prescaler
endif
ENDASM

@Timer1 = TMR1L
Timer1 VAR WORD EXT
TimerReload CON EXT
TMR1ON VAR T1CON.0

@Timer2 = TMR2L
Timer2 VAR WORD EXT
Timer2Reload CON EXT
TMR2ON VAR T2CON.0

'''''''''''''''''''''''RELOAD TIMER1''''''''''''''''''''''''''''''''''
ASM
ReloadTMR1
MOVE?CT 0, T1CON, TMR1ON
MOVLW LOW(TimerReload)
ADDWF TMR1L,F
BTFSC STATUS,C
INCF TMR1H,F
MOVLW HIGH(TimerReload)
ADDWF TMR1H,F
MOVE?CT 1, T1CON, TMR1ON
INT_RETURN
ENDASM

'''''''''''''''''''''''RELOAD TIMER2''''''''''''''''''''''''''''''''''
ASM
ReloadTMR2
MOVE?CT 0, T2CON, TMR2ON
MOVLW LOW(TimerReload)
ADDWF TMR2L,F
BTFSC STATUS,C
INCF TMR2H,F
MOVLW HIGH(TimerReload)
ADDWF TMR2H,F
MOVE?CT 1, T2CON, TMR2ON
INT_RETURN
ENDASM
'''''''''''''''''''''''''''TIMER STARTS''''''''''''''''''''''''''''''
StartTimer1:
Timer1 = TimerReload
TMR1ON = 1
RETURN

StartTimer2:
Timer2 = TimerReload
TMR2ON = 1
RETURN
'''''''''''''''''''''''''''TIMER STOPS'''''''''''''''''''''''''''''''
StopTimer1:
TMR1ON = 0
RETURN

StopTimer2:
TMR2ON = 0
RETURN



Thanks in advance for the help,
ronbo

Darrel Taylor
- 17th December 2009, 17:52
Timer2 is an 8-bit timer, so it doesn't have an L and H register. It's just TMR2.
And you don't need to reload it like you do with Timer1.

Changing the value of the PR2 register adjusts the Period, along with the pre and postscalers.
<br>

ronbowalker
- 17th December 2009, 18:38
I see this is the case on the data sheet Darrel, and thanks for the help.....
I see the TMR2 settings, so these will not conflict with one another or be inhibited by looping MAIN program? What else in my program is incorrect?