+ Reply to Thread
Results 1 to 7 of 7

Thread: PIC 16F877 Timer1

  1. #1
    spurnymart Newbie
    Join Date
    Nov 2004
    Posts
    4

    Default PIC 16F877 Timer1

    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.


  2. #2
    Super Moderator Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent
    Join Date
    Nov 2003
    Location
    Derbyshire, UK
    Posts
    29,791

    Default

    I've not bothered looking at the datasheet to test your code, but you're not writing any value to Timer1?.
    PIC programmer software, and PIC Tutorials at:
    http://www.winpicprog.co.uk

  3. #3
    spurnymart Newbie
    Join Date
    Nov 2004
    Posts
    4

    Default

    Quote Originally Posted by Nigel Goodwin
    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]

  4. #4
    Super Moderator Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent
    Join Date
    Nov 2003
    Location
    Derbyshire, UK
    Posts
    29,791

    Default

    Quote Originally Posted by spurnymart
    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.
    PIC programmer software, and PIC Tutorials at:
    http://www.winpicprog.co.uk

  5. #5
    spurnymart Newbie
    Join Date
    Nov 2004
    Posts
    4

    Default

    Quote Originally Posted by Nigel Goodwin
    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.

  6. #6
    motion Newbie
    Join Date
    Jul 2003
    Location
    Quezon City.PH
    Posts
    560

    Default

    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.
    "Having to do with Motion Control"

  7. #7
    spurnymart Newbie
    Join Date
    Nov 2004
    Posts
    4

    Default

    Quote Originally Posted by motion
    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.

+ Reply to Thread

Tags for this Thread