Tmr0


Closed Thread
Results 1 to 7 of 7

Thread: Tmr0

  1. #1
    laughing-gravy's Avatar
    laughing-gravy Guest

    Default Tmr0

    Hello, I'm new to the forum.
    I've been playing with TMR0, trying to get it to work in counter mode as a stopwatch.
    It seems simple enough, but I'm missing something because it only increments intermittently.
    I've tried Pulling the TOCKI pin high, and trying to interrupt by grounding it.
    I've tried pulling the TOCKI pin low, and trying to interrupt by switching it high.

    But I just cant get it to work.

    Does anyone have any suggestions for me?

    Thanks.

    I'm using the PIC16F877A.

    Here is my basic program flow:

    OPTION_REG = %00111000 'Transition on TOCKI (RA4),
    'Increment on falling edge
    'Prescalar assigned to WDT for a 1:1 TMR0 ratio.

    INTCON = %10100000 ' Enable Global Interrupt and TMR0 Interrupt.

    ON INTERRUPT GOTO LapCount
    Main:
    :
    :
    :
    goto Main

    disable
    LapCount:
    If INTCON.2 = 1 then
    i = i + 1

    INTCON = %10100000 'Clear TMR0 Interrupt Flag (Bit 2)

    Resume
    Enable

  2. #2
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Assuming you start with Timer0 clear, in counter mode, with a 1:1 prescaler, you will need 256 clock transitions on T0CKI before it will generate the over-flow interrupt.

    Also, you don't want to set INTCON = %10100000 when using PBP's ON INTERRUPT. Just clear the Timer0 int flag before exiting the interrupt routine with INTCON.2 = 0. PBP will handle GIE for you.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  3. #3
    laughing-gravy's Avatar
    laughing-gravy Guest


    Did you find this post helpful? Yes | No

    Default Still having trouble

    Thanks for the help Bruce.

    OK, now I've eliminated the INTCON set-up, and added a TMR0 Control:

    But I still cant get it to work.


    Program code:

    TRISA = %00010000 ' Set PORTA.4 (TOCKI) to input for timer0

    OPTION_REG = %00111000
    'Transition on TOCKI (RA4),
    'Increment on falling edge
    'Prescalar assigned to WDT for a 1:1 TMR0 ratio.

    TMR0 = $FF ' Set TMR0 to 255. So one more tick and TMROIF will be set.

    ON INTERRUPT GOTO LapCount
    Main:
    :
    :
    :
    goto Main

    disable
    LapCount:
    If INTCON.2 = 1 then
    i = i + 1
    endif
    TMR0 = $FF
    INTCON.2 = 0 'Clear TMR0 Interrupt Flag (Bit 2)

    Resume
    Enable

  4. #4
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    You need to enable the interrupt.
    Code:
    TRISA = %00010000    ' Set PORTA.4 (TOCKI) to input for timer0
    
       OPTION_REG = %00111000 
       'Transition on TOCKI (RA4), 
       'Increment on falling edge
       'Prescalar assigned to WDT for a 1:1 TMR0 ratio.
       INTCON.2 = 0 ' Clear Timer0 int flag
       INTCON.5 = 1 ' Enable Timer0 int
       TMR0 = $FF   ' Set TMR0 to 255.  So one more tick and TMROIF will be set.
    
       ON INTERRUPT GOTO LapCount
    Main:
       ' Do something here
       goto Main
    
       disable
    LapCount:
       If INTCON.2 = 1 then
       i = i + 1
       endif
       toggle PORTB.3
       TMR0 = $FF      ' Indicate we're in interrupt handler
       INTCON.2 = 0    ' Clear TMR0 Interrupt Flag (Bit 2)
       
       Resume 
       Enable
    Place a 10K pull-up resistor on RA4. Have your input switch ground RA4 when pressed to toggle RB3 (or any available port pin).
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  5. #5
    laughing-gravy's Avatar
    laughing-gravy Guest


    Did you find this post helpful? Yes | No

    Default

    Thanks for the help Bruce.

    It's working fine now.

  6. #6
    Join Date
    Jun 2006
    Location
    Greece
    Posts
    302


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Bruce View Post
    You need to enable the interrupt.
    Code:
    TRISA = %00010000    ' Set PORTA.4 (TOCKI) to input for timer0
    
       OPTION_REG = %00111000 
       'Transition on TOCKI (RA4), 
       'Increment on falling edge
       'Prescalar assigned to WDT for a 1:1 TMR0 ratio.
       INTCON.2 = 0 ' Clear Timer0 int flag
       INTCON.5 = 1 ' Enable Timer0 int
       TMR0 = $FF   ' Set TMR0 to 255.  So one more tick and TMROIF will be set.
    
       ON INTERRUPT GOTO LapCount
    Main:
       ' Do something here
       goto Main
    
       disable
    LapCount:
       If INTCON.2 = 1 then
       i = i + 1
       endif
       toggle PORTB.3
       TMR0 = $FF      ' Indicate we're in interrupt handler
       INTCON.2 = 0    ' Clear TMR0 Interrupt Flag (Bit 2)
       
       Resume 
       Enable
    Place a 10K pull-up resistor on RA4. Have your input switch ground RA4 when pressed to toggle RB3 (or any available port pin).
    I test and work fine , but i see a problem(
    I connect a button to RA4.When push the button one time the i increment +1 and after release the button the i increment one +1 again.Why;

  7. #7
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    My guess would be switch bounce. Wait on entry into your interrupt handler for RA4 to return high, or insert a short delay in the beginning of your interrupt handler, and see if that clears up your problem.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

Similar Threads

  1. Can't ID interrupt source with this IntHandler??
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 3rd June 2009, 02:35
  2. TMR0 interrupt and HSERIN
    By boban in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 2nd September 2008, 11:48
  3. help: TMR0 interrupts disabling PORTAchange interrupts???
    By xnihilo in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 19th August 2008, 15:10
  4. using TMR0 as a counter..
    By sirvo in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 28th July 2007, 02:56
  5. Strange behaviour from PIC16F877 on TMR0
    By mikebar in forum mel PIC BASIC Pro
    Replies: 18
    Last Post: - 19th August 2006, 01:31

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts