Timer Question 16f877


Closed Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2004
    Location
    Scarborough UK
    Posts
    77

    Smile Timer Question 16f877

    Hello All

    All i wanna do is have an led flash at say half a second interval as a status indicator that the pic is running.

    I would like to do it with one of the timers on a 16f877 so that my code can continue to do what it does without having to flash the led.

    I've looked at the datasheet till my head hurts but there are just too many things to set up and i just do not get this prescaler stuff.
    can anyone provide a VERRY SIMPLE pbp example how to do this so that i can understand how a timer works.

    Any help or code will be appreciated.

    Thanks
    ----------------
    Bri
    Reading the datasheet & understanding it are two different things.

  2. #2
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default

    You will have to tweak depending on your oscillator speed, but the following will toggle a LED every time the timer rolls over. At 4 Mhz that is 65.535mSec X 8 or approx 524 milliseconds. If you need more accurate timing you can pre-load the counter with a value that gives you the time you want. There is an uncertainty, of course, that is directly dependent on the time it takes to execute your code.


    Set up Timer1 with a /8 prescaler by setting

    T1CON = 00110001 'Divide by 8 prescaler, no syncronization, enable timer

    Then, in your code:

    TOP:

    IF PIR1.0 = 1 THEN
    PIR1.0 = 0
    TOGGLE LED
    ENDIF


    'Put your code here'
    GOTO TOP

    END

  3. #3
    Join Date
    Dec 2004
    Location
    Scarborough UK
    Posts
    77


    Did you find this post helpful? Yes | No

    Talking Timer Question

    Hi Charles

    I tried the code you posted and it works.
    Is there a way to make the led flash without having to put the little code snip into the main program ?.
    also, what is the PIR1 thingy, i looked at the datasheet (6.7) but i cant find an explanation of what it does, i dont think these datasheets are meant for dabblers like me :--)
    Reading the datasheet & understanding it are two different things.

  4. #4
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default

    PIR1.0 is the Timer 1 overflow flag. Every time Timer 1 overflows, the bit gets set. You have to clear the bit so the next time the timer overflows it gets set again. Information can be found on Microchip's DS3029B page 22.

    This is a very effective way of running an accurate clock. As long as your code's execution time is less than the overflow period, all works very well.
    The loop time is independent from the execution time of your code.

    You can do the same thing with Timer 0. In that case, you have to check
    INTCON.2 for overflow.

    I use the technique all the time. It allows me to control program flow and display while using almost no PAUSE statements - something to be avoided if possible, since the processor can do nothing else while tied up in a PAUSE.

    As I mentioned before, you can pre-load the timer to get accurate periods of any value. Just remember that if you load the timer registers, you must write the LSB last.


    Depending on your application, you could do something similar with:
    ------------------------------------------------------------------
    TOP:

    'Your Code'

    Toggle LED
    Pause 500

    GOTO TOP

    -------------------------------------------------------------------

    This would toggle the LED every time the loop executed. The timing wouldn't be as accurate as in the previous method because the exact time between toggles would vary depending on the execution time of your code.

  5. #5
    Join Date
    Dec 2004
    Location
    Scarborough UK
    Posts
    77


    Did you find this post helpful? Yes | No

    Smile Timer Question

    Hi Charles

    Thanks for taking the time to help me, i'm going to have a play with the timers again when i get out of work today.
    There is a lot to take in but your explanation helps a lot, i have a chance now of using them.
    thanks again
    -------------------
    Brian
    Reading the datasheet & understanding it are two different things.

  6. #6
    Join Date
    Mar 2003
    Posts
    41


    Did you find this post helpful? Yes | No

    Default Flashing status LED

    Timers are often too valuable to use just to blink a LED and adding a PAUSE statement can stuff up other parts of the code. What I do for such time insensitive issues is use a loop counter and the TOGGLE command.

    Activity VAR portb.0 ' activity LED
    LoopCntr VAR word

    MAIN:
    LoopCntr = Loopcntr + 1
    IF loopcntr//1000 = 0 THEN TOGGLE Activity

    your code goes here

    goto MAIN

    Depending on your system cycle times, you can adjust the divisor to get an appropriate flash rate.

    Cheers
    Brian
    Last edited by btaylor; - 8th November 2005 at 02:46.

  7. #7
    Join Date
    Dec 2004
    Location
    Scarborough UK
    Posts
    77


    Did you find this post helpful? Yes | No

    Smile Timers

    Hi Btaylor

    You say that timers are too valuable to use for an led, what kind of things do you use them for generaly ?, i ask because i have not used them before and dont know what they can be used for.
    In the case of the status led, i had the idea that i could somehow tell the timer to flash an led independant of my code, since charles told me how its done i dont think that will be possible, but, its introducing me to a new tool i might use in other projects.
    ---------------
    Bri
    Reading the datasheet & understanding it are two different things.

Similar Threads

  1. Simple Timer question
    By jimseng in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 24th September 2009, 23:12
  2. Timer / CCP Question
    By jamie_s in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 2nd November 2005, 08:22
  3. Timer Question
    By aywho in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 22nd October 2005, 21:54
  4. 16F877 RAM Question
    By Art in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 6th August 2005, 11:47
  5. PIC 16F877 Question
    By koossa in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 5th August 2005, 18:06

Members who have read this thread : 0

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