PDA

View Full Version : Timer Question 16f877



Bonxy
- 5th November 2005, 17:56
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

Charles Linquis
- 6th November 2005, 03:36
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

Bonxy
- 6th November 2005, 11:42
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 :--)

Charles Linquis
- 7th November 2005, 01:58
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.

Bonxy
- 7th November 2005, 08:54
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

btaylor
- 8th November 2005, 02:44
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

Bonxy
- 8th November 2005, 08:40
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