Newb timer problems


Closed Thread
Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2005
    Location
    New Zealand
    Posts
    171

    Default Newb timer problems

    Until now any timing I've done I've managed to get by with just using a simple loop, looking at the datasheets and setting registers that I do not understand scares me.

    What's happening currently is my program has branches withing the timing loop and these stuff up my timing depending on the situation, so I'm gonna bite the bullet and learn how to use the onboard timers. I'm currently uding a 16f676 and was wanting to use timer0 just to increment a time, i qrote a simple bit of code to see if I'd grasped the concept - obviously not!!!

    I set the prescaler to 256 and was going to toggle a red led everytime it interupted so I can see it working

    here's what I've got so far - would greatly appreciate any suggestions - thanks - also how do i get the code in that lil code box displayed on here?

    [code:]
    @ DEVICE PIC16F676, XT_OSC, MCLR_OFF, PROTECT_OFF, BOD_ON, CPD_OFF

    DEFINE OSC 4

    TRISA = %000000
    TRISC = %101100

    ANSEL = %10000000 'enable ch7 analog
    CMCON = 7 'Disable comparitor
    OPTION_REG = %00010111 'set prescaler to 256
    INTCON.2 = 0 ' Clear Timer0 int flag
    INTCON.5 = 1 ' Enable Timer0 int
    TMR0 = 0 ' Set TMR0 to 0.

    ON INTERRUPT GOTO int

    red var PortA.2

    clear
    PortA = 0
    PortC = 0

    Start: 'stay in loop

    goto start

    int: 'toggle led on interupt
    TMR0 = 0
    toggle red
    resume

  2. #2
    Join Date
    Feb 2006
    Location
    Arvada Colorado
    Posts
    31


    Did you find this post helpful? Yes | No

    Default At least one thing is missing

    You need to clear the interupt flag again before resume.

  3. #3
    Join Date
    Feb 2006
    Location
    Arvada Colorado
    Posts
    31


    Did you find this post helpful? Yes | No

    Default Oh one more thing

    You should disable the interupts before the interupt routine then enable them again when it is complete.
    example:
    Disable ' Disable interrupts during interrupt handler
    tickint:
    Stuff during interupt.................
    tiexit: INTCON.2 = 0 ' Reset timer interrupt flag
    Resume
    Enable ' Enable Interrupts

    This is code for 16f877a so the intcon bits are probably different.

  4. #4
    Join Date
    Oct 2005
    Location
    New Zealand
    Posts
    171


    Did you find this post helpful? Yes | No

    Default

    Hey GeoJoe, thanks for the input, I've mad ethe changes but still no joy :-s - the intcon bits are the same according to the data sheet "The T0IF bit
    (INTCON<2>) must be cleared in software by the
    Timer0 module Interrupt Service Routine before reenabling
    this interrupt."
    but no luck yet, do you think it might be something to do with running an external clock?
    also - how do i place the code in a special little code box on here??

    @ DEVICE PIC16F676, XT_OSC, MCLR_OFF, PROTECT_OFF, BOD_ON, CPD_OFF

    TRISA = %000000
    TRISC = %101100

    ANSEL = %10000000 'enable ch7 analog
    CMCON = 7 'Disable comparitor
    OPTION_REG = %00010111
    INTCON.2 = 0 ' Clear Timer0 int flag
    INTCON.5 = 1 ' Enable Timer0 int
    TMR0 = 0 ' Set TMR0 to 0.

    ON INTERRUPT GOTO int

    red var PortC.0
    green var PortA.2
    PDWN var PortC.1
    DAT var PortC.2
    pwr var PortC.4
    Switch var PortC.5

    clear
    PortA = 0
    PortC = 0

    Start:

    goto start

    disable
    int:
    TMR0 = 0
    toggle red
    intcon.2 = 0 ' reset interupt flag
    resume
    enable

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Hi George,

    Try this ...
    Code:
    Start:
        PAUSE 1
    goto start
    Or, this ...
    Code:
    Start:
        @ CLRWDT
    goto start
    Or, maybe ...
    Code:
    @ DEVICE PIC16F676, XT_OSC, WDT_OFF, MCLR_OFF, PROTECT_OFF, BOD_ON, CPD_OFF
    <br>
    DT

  6. #6
    Join Date
    Oct 2005
    Location
    New Zealand
    Posts
    171


    Did you find this post helpful? Yes | No

    Default

    Thanks DT - yr a genius, the first thing i did worked so didn't try the others. Why did it need that pause for it to work - i would have thought it would have just cycled around in the loop

  7. #7
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by George View Post
    ..., the first thing i did worked so didn't try the others. ...
    Well that's unfortunate.
    Trying the other 2, may have answered your question.

    Why did it need that pause for it to work - i would have thought it would have just cycled around in the loop
    It does "just cycled around in the loop".
    But, there's nothing in there to clear the WatchDogTimer. And without it, the processor will reset every ~18ms.
    Timer0 was set up with 1:256 prescaler, so it would interrupt after 65.536 ms had passed. But it never made it that far.

    PAUSE, automatically clears the WDT every so often, so that caused it to work.

    The second example just clears the WDT by using the ASM statement CLRWDT.

    And the third example turns the WDT OFF so that it doesn't need to be cleared.

    Any one of the three examples should have made it work.
    <br>
    DT

  8. #8
    Join Date
    May 2007
    Posts
    65


    Did you find this post helpful? Yes | No

    Red face

    Quote Originally Posted by Darrel Taylor View Post
    Hi George,

    Try this ...
    Code:
    Start:
        PAUSE 1
    goto start
    Or, this ...
    Code:
    Start:
        @ CLRWDT
    goto start
    Or, maybe ...
    Code:
    @ DEVICE PIC16F676, XT_OSC, WDT_OFF, MCLR_OFF, PROTECT_OFF, BOD_ON, CPD_OFF
    <br>
    Quote Originally Posted by George View Post
    Thanks DT - yr a genius, the first thing i did worked so didn't try the others. Why did it need that pause for it to work - i would have thought it would have just cycled around in the loop
    Yes i have the same question:
    Why was that "pause 1" needed for it to work??

  9. #9
    Join Date
    Oct 2005
    Location
    New Zealand
    Posts
    171


    Did you find this post helpful? Yes | No

    Default

    Just after I turned the wdt off and got rid of the pause 1 and it worked also, detailed knowlegde certainly helps with these things doesn't it

    Cheers for your help

Similar Threads

  1. Elapsed Timer Demo
    By Darrel Taylor in forum Code Examples
    Replies: 111
    Last Post: - 29th October 2012, 17:39
  2. High Resolution Timer & Speed Calculator
    By WOZZY-2010 in forum Code Examples
    Replies: 4
    Last Post: - 7th February 2010, 16:45
  3. Timer + rc5
    By naga in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 19th November 2009, 07:56
  4. Timer interrupt frequency
    By Samoele in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 17th January 2009, 23:49
  5. timer interupt help 16f73
    By EDWARD in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 3rd July 2005, 08:41

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