PDA

View Full Version : Hardware timers



mitchf14
- 11th July 2008, 01:26
I'm using "COUNT" command to count the number of pulses coming in but now i need to use the hardware timers. I want to count my pulse for 30 seconds, i was thinking of using TMR2 8 bit with prescaler. I'm using a PIC16F873 with a 20 Mhz crystal, could anyone let me know how to approach this.

Bruce
- 25th July 2008, 18:54
Are you trying to use a timer for the 30 second period, or trying to use a timer/counter?

mitchf14
- 27th July 2008, 17:34
I would just like to use the hardware timers, just to pause for 30 seconds so in the mean time my pic can keep running and keep collecting data for that 30 sec.


thanks

Jerson
- 27th July 2008, 18:54
I would think, you need a timer to keep count of the seconds. Any timer would do fine as long as you know how to extend it, in code, to count 1 second. This timer can then be further extended to count the 30 seconds interval you want.

Similarly, the pulses could come into the INT pin on your device and you could count the interrupt when it occurs.

Just ideas you could evaluate :)

Bruce
- 27th July 2008, 19:25
Why not just let the timer/counter count your pulses?

MyCount VAR WORD

TMR1L=0 ' clear the low byte
TMR1H=0 ' clear the high byte
T1CON = %00000011 ' set Timer1 up as a hardware counter

This counts rising edge pulses on RC0. The count is held in TMR1L and TMR1H.

Do whatever you need to collect other data for 30 seconds then just read the count.

MyCount.LowByte = TMR1L
MyCount.HighByte = TMR1H

You could set Timer1 to overflow, count overflows incrementing a variable, but it would be
a lot easier using a built-in hardware counter. And it doesn't interrupt whatever else your
program is doing. It counts in the background.

Perrin`
- 30th July 2008, 16:10
When I had to learn how to use timers, I used this thread.

This thread also happens to have EXACTLY what you want... a timer that counts down from 30.

http://www.picbasic.co.uk/forum/showthread.php?t=6957&highlight=O_FLOWS

Sorry the link highlights O_FLOWS, I just happen to rememberthat variable so used it to find the thread.
Also wish I'd have read the forum sooner >.<;

mitchf14
- 7th August 2008, 04:05
I looked at this forum http://www.picbasic.co.uk/forum/show...hlight=O_FLOWS and it seems a little to complicated for what i need.

I am using a 20mhz crystal so I'm not sure if i need to use a prescaler.

but all i want to do is monitor a battery I'm using adc 10 bits so when my battery reaches 9 volts i want a 10 second pause using TMR1 so my adc can still keep collecting, and after the 10sec pause is done just keep resuming the program.

so something like this.......

voltage var byte

timercount:

'10 sec TMR1 pause here

if voltage = 9 then


goto timercount

portb.4 = 1 ' after 10 sec resume from here

endif


Can anyone point me in the right direction thanks

Bruce
- 7th August 2008, 16:54
At 20MHz, if you're using the internal clock, Timer1 will count to 65,535, then roll-over on
the next tick in just 13.1072mS with a 1:1 prescaler.

If you set the prescaler to 1:8 it would take 8 times this period before it rolls-over. The
only way you could use Timer1 for a 10 second delay period would be to monitor & count
over-flows until your 10 second period is up.

Unless you're just trying to learn to use timers, a long pause would be the easiest.

Steve has a nice utility here: http://www.mister-e.org/pages/utilitiespag.html you might
find helpful. Check out the timer helper.

b1arrk5
- 8th August 2008, 00:19
For a long delay like 9 seconds, I use Steve's calculator utility to set up a timer to go for 10 milliseconds. Then I use Darrell's Instant Interrupts, and set up an interrupt, let's say for Timer 3. I just set my variable seconds to 0, and then enable the timer interrupt. When the 10 ms. is up, the interrupt goes to the following;

'********************** TIMER 3 INTERRUPT*************************************
TIMER3INT: 'THIS INTERRUPT OCCURS EVERY 10 MILLISECONDS
TICKS = TICKS + 1 'INCREMENT TICKS
IF TICKS > 100 THEN
TICKS = 0
SECONDS = SECONDS + 1
ENDIF
@ INT_RETURN
'********************** END TIMER 3 INTERRUPT ********************************
now I just add a line in my main program to see if seconds is equal to nine, and when it is I disable the interrupt and go do what ever comes next. This works well if I'm waiting for a user to make a selection, I'll wait ten seconds, and if they don't respond, then the program will move on.

Good Luck!

Jerry.

mitchf14
- 8th August 2008, 02:26
Wow that Picmulticalc is really amazing thanks for referring me to it i will defiantly be using it.
I don't need to use a interrupt. Right now i am using a really long pause 10 sec, so when my battery charges up to 9 volts my led comes on saying the battery is fully charge. Then i have my pause that checks back in ten seconds if my battery is still full or not. The only things i don't like about this process is that for that seconds my pic hangs and all my other operations are on a alt until the 10 seconds is completed. Now i was thinking if i could figure out how to use the hardware timers, my timers could count in the background without affecting my ADC, and LCD etc.

Could anyone clarify this for me and maybe suggest a better way of doing this.

thanks