timer/counter questions

+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2010

    Default timer/counter questions

    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.



  2. #2
    Join Date
    Feb 2006
    Gilroy, CA


    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:


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

  3. #3
    Join Date
    Aug 2006
    Look, behind you.


    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 !
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  4. #4
    Join Date
    Jan 2005
    Montreal, Quebec, Canada

    Thumbs up Re: timer/counter questions

    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.
    Last edited by Demon; - 7th April 2011 at 06:39. Reason: Lack of adequate neurons
    Not as dumb as yesterday, but stupider than tomorrow!

  5. #5
    Join Date
    May 2008

    Default Re: timer/counter questions

    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

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

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


    All progress began with an idea

  6. #6
    Join Date
    Mar 2003
    Commerce Michigan USA

    Default Re: timer/counter questions

    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,
    Last edited by Demon; - 4th October 2016 at 16:02.

Similar Threads

  1. Two quick (and elementary) questions.
    By scorpion990 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 26th June 2008, 23:03
  2. A few 12F683 questions
    By dhouston in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 24th May 2008, 03:54
  3. Usart Questions
    By shawn in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 8th March 2008, 01:17
  4. Still new to PicBasic - i2c questions
    By cometboy in forum mel PIC BASIC
    Replies: 4
    Last Post: - 13th November 2006, 18:27
  5. Hi Everyone! Some newbie questions :-)
    By guest_05 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 28th October 2006, 22:24

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts