Time delay by incrementing a variable using incoming pulse


Closed Thread
Results 1 to 9 of 9

Hybrid View

  1. #1
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    i had the same feeling too. Using Timer interrupt by example. Look in your datasheet. There's at least one pin that can be assign as clock source for an internal timer. In many case it's RA.4

    Wich PIC are you using?
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

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


    Did you find this post helpful? Yes | No

    Default

    And if you aren't comfortable with interrupts, you can sit in a loop and watch for a timer overflow, reload the timer, run your main program and go back to the loop. As long as your main program takes less time to execute than the timer overflow period (extremely likely), everything works perfectly.

    I do this all the time for run-time counters. If you are using 18F devices, you can easily get a 2 second period, even if you are running at 20Mhz. I count the jumps to MAIN. After 30 jumps, I increment the minutes counter.

  3. #3
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    you should find some hints in the following
    http://www.picbasic.co.uk/forum/show...44&postcount=2

    look the 2 lasts examples

    HTH
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  4. #4
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305


    Did you find this post helpful? Yes | No

    Default

    Thanks for the ideas guys. I'm using a 16F818. It is a run time hourmeter application. I wanted to use the 60 Hz cycle to maintain accuracy over the lifetime of the product. I figured if I counted the pulse and only executed the rest of the program when the correct count had been achieved, this would give me the best accuracy.
    The processor is doing a fair amount of work. Its multiplexing through 6, seven segment displays. Its running at 8Mhz using the internal oscillator as I need all the pins.
    I currently have RA4 used as an output, and RA5 is the pulse in. The reason is that RA5 is input only and therefore cant be used to drive a segment. I have a spare pin, RA7, which I was intending to use to detect mains power fail, so that I can write to EE before the power fails, as currently, it only writes as it increments a digit.
    I will check the datasheet, and if I can set RA5 as an interupt pin I will try it.
    Otherwise I will have to shift my power fail detect to RA5 and have RA7 as an output. This will free up RA4.

  5. #5
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305


    Did you find this post helpful? Yes | No

    Default

    I finally got some time to play with this today. The timer is working. After playing with the code I managed to get the numbers to display all the time. The reason they were not is that the program did not display anything while waiting for the timer flag to be set.
    Two small problems I hope to fix shortly. One is that each time the display increments, it goes blank for maybe 100ms. I've tried several things to fix this but they only make the blank period longer or slightly shorter.
    The second thing to fix is the pulse count. I need to count 72000 pulses before incrementing the display. Problem is, timer0 is 8bit and limited to 256 pulses before rolling over. I need to have timer0 rolloever and each time the flag is set, but it into a register and increment it until there is the equivilant of 72000 pulses.
    If I preload the counter with 5, and have it rollover after 250 pulses, and do this 288 times, I get 72000 pulses which equals 1/10th of an hour. Would that work?

Similar Threads

  1. EEPROM Variables (EE_Vars.pbp)
    By Darrel Taylor in forum Code Examples
    Replies: 79
    Last Post: - 26th October 2012, 00:06
  2. 16F628A - Stops if release power switch.
    By dene12 in forum General
    Replies: 16
    Last Post: - 14th February 2009, 07:57
  3. Old and beyond help ?
    By DavidFMarks in forum mel PIC BASIC Pro
    Replies: 46
    Last Post: - 11th December 2008, 15:23
  4. Replies: 3
    Last Post: - 13th September 2008, 17:40
  5. Problem with saving to EEPROM...
    By Tear in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 1st July 2005, 00:10

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