# PIC 16F877 Timer1

Status
Not open for further replies.

#### spurnymart

##### New Member
Hi
I am beginner in PIC programming. I need help with Timer1 in PIC 16F877.

My code is:

list p=16f877a
#include <p16f877A.inc>

org 000
goto start

org 004
nop
retfie

start
bsf STATUS,RP0
bsf PIE1,0
bcf STATUS,RP0

bsf INTCON,7

bsf T1CON,0

man
nop
nop
nop
goto man

end

My problem: I will not receive the interrupt when the Timer1 overflows and I don't know why. The PIR1 <0> bit is correctly set to log1 on overflow. But the interrupt routine don't start. I use the PIC simulator MPLAB IDE 6.6 and MPLAB SIM debugger.

#### Nigel Goodwin

##### Super Moderator
I've not bothered looking at the datasheet to test your code, but you're not writing any value to Timer1?.

#### spurnymart

##### New Member
Nigel Goodwin said:
I've not bothered looking at the datasheet to test your code, but you're not writing any value to Timer1?.

I think it does not matter. There is some value and when the timer is running, it will overflow soon or later. I try your advice but there is the same problem.

Code:
list p=16f877a
#include <p16f877A.inc>

org 000
goto start

org 004
nop                 ;interrupt routine - doesn't work
retfie

start
bsf STATUS,RP0
bsf PIE1,0          ;allow timer1 interrupt
bcf STATUS,RP0

bsf INTCON,7    ;allow global interrupt
movlw 000h
movwf TMR1L     ;set the value
movwf TMR1H
bsf T1CON,0     ;run timer

man
nop
nop
nop
goto man

end
[/code]

#### Nigel Goodwin

##### Super Moderator
spurnymart said:
I think it does not matter. There is some value and when the timer is running, it will overflow soon or later. I try your advice but there is the same problem.

But you need to write to the Timer1 registers to clear the Interrupt flag, if you don't do this it will never generate an interrupt.

#### spurnymart

##### New Member
Nigel Goodwin said:
But you need to write to the Timer1 registers to clear the Interrupt flag, if you don't do this it will never generate an interrupt.

The interrupt flag for timer1 is PIR1<0> and it is set to log0 at the start of program. When the timer overflows it is set to log1. This part works but the interrupt is not generated so its routine can't clear the flag to log0.

#### motion

##### New Member
Two things:

1. You also need to set INTCON,PEIE to enable timer1 interrupts to get through.

2. You have to clear PIR1,TMR1IF in the interrupt service.

#### spurnymart

##### New Member
motion said:
Two things:

1. You also need to set INTCON,PEIE to enable timer1 interrupts to get through.

2. You have to clear PIR1,TMR1IF in the interrupt service.

PIEI that is it. There is no word in the data sheet.
Thank you very much.

Status
Not open for further replies.

Replies
1
Views
3K
Replies
5
Views
3K
Replies
2
Views
1K
Replies
4
Views
2K
Replies
28
Views
6K