PDA

View Full Version : timer/counter questions



missouri100
- 13th January 2010, 16:34
I would like to start a timer and then trigger events as the time increases. The timer would need to run in the background as the program executes the events. I have read and read and read and I can't figure out how to do this. Could someone give he a hint?

I also have an application that requires me to count pulses and calculate speed while executing a program doing other things. I can pause my program to make the speed calculation but I don't want to pause it to count pulses. My gut feeling is that this is a very similar to the first question. If I can set up the timer and/or counter running in the back ground, I should be able to use the timer value to trigger interrupts. Upon the interrupt I can read the pulse count and figure a speed.

My question boils down to having the timer/counter running in the background. The microcontroller is a PIC18F4525.

help!!

donnie

ScaleRobotics
- 13th January 2010, 17:16
The answer to both your questions is the use of interrupts. The PBP manual doesn't mention much about interrupts. Darrel has made this a lot easier with his routines. Here is an example of a timer:

http://darreltaylor.com/DT_INTS-18/elapsed.html
http://darreltaylor.com/DT_INTS-18/home.html

Your counter could be based on an interupt on change of pin. Maybe INT0 INT1 or INT2?

Archangel
- 13th January 2010, 17:20
Hi Donnie,
I will answer what I can.
You can configure a timer as a hardware counter to count in the background. You are correct about the timer having the ability to trigger interrupts. Your main loop can track a variables value and jump to a subroutine when that variable reaches a certain value, and continue "resume" where it left off, without interrupts, or your interrupt routine can check the value and do . . .
EDIT: Outdrawn again ! :D

Demon
- 7th April 2011, 05:22
So here I am looking how to make a timer, going through samples, browsing tutorials, digging the manuals, sifting the forums and then I fall on this thread.

I don't think we can say enough times THANK YOU D. His code makes it so simple to use a timer. I rarely use them so I forget just how much there is behind these suckers.

:)


EDIT: Ah well, that sucks. I can't use the DT Interrupts 'cause I run out of room on this 16F877. I tried trimming some of the stuff out of the include, but that wasn't enough. I've cut arrays out of my program, still not enough. I've reduced ihe code to the point that the logic isn't workable(I have to work with strings/arrays), and I still run out of room. :(

Looks like I'll have to figure how to run the basic timer feature at 20MHz. I need it to run 30 seconds and pull a pin to sound off a buzzer.

aratti
- 7th April 2011, 11:00
Robert, without an ISR the timing is not very precise but you can try the following:


'-------------- set the registers -------------------------------
T1CON = %00111101 ' set timer1 as timer with prescaler 1:8
PIE1.0 = 1 ' enable interrupt on overflow
PIR1.0 = 0 ' clear interrupt flag
TMR1L = 220 ' 100 millisecs overflow
TMR1H = 11 ' 100 millisecs overflow
W_Count VAR WORD
'-----------------------------------------------------------------

Poll in the approriate point, somewhere in your program:

If PIR1.0 = 1 THEN
T1CON.0 = 0
PIR1.0 = 0
TMR1L = 220
TMR1H = 11
W_Count = W_Count +1
T1CON.0 = 1
endif

IF W_Count = 300 then
W_Count = 0
Do your job since 30 seconds have been elapsed..
endif

You can trim the 300 to better achieve the 30 seconds delay. (300 x 100 millisecs = 30 seconds)

Cheers

Al.

Dave
- 7th April 2011, 12:01
Robert, If I were you I would look at replacing the OLD 16F877 with something like an 18F4620. If you are using the 40 pin dip package it should drop right in and the part has LOTS more program memory space and Eeprom. Just about all I use these days is the 18F2620 as I don't need all the pins but it has the same amount of memory. Just a thought....

Dave Purola,
N8NTA