EmbeddedRelated.com
Forums

Re: PIC Timer0 Interrupt problems

Started by Peter van Hoof March 23, 2005
I have to start with stating I have never programmed
an 18f series chip yet but have looked at your code
and the data sheet (after all we are all here to
learn)

One thing I noted is that PEIE in INTCON is disabled
and in the default state of the IPEN bit (which was
not set in your code snippet) this means that no
peripheral inputs are enabled. (current data sheet
page 75 and diagram on 76 )

As stated before I may very well be wrong.

TTYL Peter
--- allenbv_99 <allenbv_99@alle...> wrote:
>
> Hi, I am writing an IRDA application. This is my
> first time using a
> PIC. I am having trouble trying to figure out how to
> set up the
> TIMER0 properly.
>
> Using a PIC18f1320
> My crystal is 7.3728 Mhz
> I want a 100 mS interrupt or close to it.
>
> The interrupt is working but it takes 9 seconds to
> cycle each time
> through and I am not sure what I am doing wrong.
>
> I have included the pertinent parts of the code
> below. I am running
> another interrupt INT0 and it works ok. I included
> all of the code
> for both interrupts. Right now I am reloading the
> interrupt because I
> do not understand how to make it a free running
> timer.
>
> If someone could show me the formula for getting the
> interrupt timer
> to work correctly I would be grateful. Thanks >
> ; some initialization is here > ; Initialize RB0/INT0 interrupt
> movlw b'11000000' ;
>
INT2IP=high,INT1IP=high,INT2IE=disable,INT1IE=disable,
> ; INT2IF=clear INT1IF=clear
> movwf INTCON3 ; setup INTCON3 as above
> movlw b'11000101' ; PortB pullups on,
> INT0=rising edge,
> INT1ling edge
> ; INT2ling edge,
> TMR0IP=high, RBIP=high
> movwf INTCON2 ; Setup INTCON2 as above
> movlw b'00110000' ; GIE=disable, PEI=disable,
> TMR0IE=enable,
> INT0IE=enable, RBIE=disable
> ; TMR0IF=clear,
> INT0IF=clear, RBIF=clear
> movwf INTCON ; setup INTCON as above
>
> ; Initialize Timer0 interrupt
> movlw 0x02 ; Set TMR0H and TMR0L to
> 0x2CC or 716 for
> 100mS.
> movwf TMR0H
> movlw 0xCC
> movwf TMR0L
> movlw 0x87
> movwf T0CON ; enable timer0, 16 bit
> timer, internal
> clock, use PSA, prescale = 256
>
> ; enable interrupts
> bsf INTCON,GIE ; Enable INTERRUPTS
>
> ; a bunch of code is here
>
> ; Timer0 interrupt handler
> int_timerzero
> bcf INTCON,TMR0IF ; clear interrupt
> TIMER0 request
> movwf W_TEMP ; preserve contents
> movff STATUS,STATUS_TEMP ; preserve contents
> movff BSR,BSR_TEMP ; preserve contents
> movlw 0x02 ; Set TMR0H and TMR0L
> to 0x2CC or 716
> for 100mS.
> movwf TMR0H
> movlw 0xCC
> movwf TMR0L
> movlw 0x87
> movwf T0CON ; enable timer0, 16
> bit timer, internal
> clock,
> ; use PSA, prescale =
> 256
>
> ; TEST CODE START
> ; Toggle line to find out the timer interval
> movlw 0x01 ; xor w with 01
> xorwf PORTA,F ; xor w with 01
> ; TEST CODE END
>
> btfsc dsr ; is the link still active?
> bra tmrfin ; jump if yes (skip this line
> if not active)
> decsleep
> movf sleepcontrol,F ; check Zero bit
> btfss STATUS,Z ; If zero bit set then
> skip next line
> decf sleepcontrol,F ; decriment sleepcontrol
> to zero (main
> program turns on sleep)
> tmrfin
> movff BSR_TEMP,BSR ; Restore BSR
> movff STATUS_TEMP,STATUS ; Restore STATUS
> movf W_TEMP,W ; Restore WREG
> retfie
>
> ; Sensor interrupt handler
> int_sensor
> bcf INTCON,INT0IF ; clear interrupt 0
> request
> incf MASTERCNT,F ; Advance count
> retfie
>
> ; high priority interrupts.
> HighPriority
> btfsc INTCON,INT0IF ; RB0 interrupt?
> bra int_sensor
> btfsc INTCON,TMR0IF ; Timer0 interrupt?
> bra int_timerzero
>
> ; catch other interrupts here and ignore them
> ; why do other interrupts happen when I have them
> turned off?
>
> retfie >




Peter

You are correct, I do not have PEIE enabled. There are no
peripherals so there is no need. Timer0 and INT0 are not peripherals
in the PIC18 (I am not sure if they are in other PICS)

I had forgotten to enable IPEN but my interrupts are working even
though I do have some problems with loading TMR0H. I did enable it
after I read your post but there was no noticeable difference, it
does not look like I need it for my application. The only time both
interrupts are enabled is during the 5 seconds after power up or
reset.

Bradley --- In piclist@picl..., Peter van Hoof <pvhoof@y...> wrote:
> I have to start with stating I have never programmed
> an 18f series chip yet but have looked at your code
> and the data sheet (after all we are all here to
> learn)
>
> One thing I noted is that PEIE in INTCON is disabled
> and in the default state of the IPEN bit (which was
> not set in your code snippet) this means that no
> peripheral inputs are enabled. (current data sheet
> page 75 and diagram on 76 )
>
> As stated before I may very well be wrong.
>
> TTYL Peter