Simple "backround" timer


Closed Thread
Results 1 to 5 of 5
  1. #1

    Default Simple "backround" timer

    Hello Everyone. A while back, I asked for an easy way to program a "backround" timer. Melanie told me my code was very close to it and she posted a link to her "Olympic Timer" program. I still can't get it to work for me. Here is a simple program. I know I could put a pause 1000 and count 60 pauses but the actual program is complicated and I need a way for it to jump to "oneminutecheck" once per minute automatically - even while it is doing something else. Thank you.

    'COMPILED FOR 16F73 USING 4MHZ CERAMIC RESONATOR

    ADCON1 = 7 ' SET INPUTS TO DIGITAL
    @ DEVICE WDT_ON, BOD_ON, PWRT_ON, PROTECT_ON
    TRISA = %00000011 'RA0 & RA1 ARE INPUTS ALL OTHER PORTS ARE OUTPUTS
    TRISB = %00000000
    TRISC = %00000000
    PORTB = %00000000 'ALL PORTB LOW
    PAUSE 100 'SETTLE DOWN AFTER POWERUP

    START:
    LOW PORTB.0
    IF PORTA.0 = 1 THEN START
    GOTO WORK 'RA0 WENT LOW

    WORK:
    HIGH PORTB.0
    IF PORTA.0 = 0 THEN WORK 'KEEP RB0 HIGH IF RA0 IS LOW
    GOTO START 'RA0 WENT HIGH

    ONEMINUTECHECK: 'COME HERE EVERY MINUTE TO CHECK RA1
    IF PORTA.1 = 0 THEN WORKMORE
    GOTO WORK

    WORKMORE:
    DO THIS & THAT
    GOTO WORK

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


    Did you find this post helpful? Yes | No

    Default

    I won't comment of fixing your problem cos im too dumb, but you might want to turn off your code protect while playing with the code, or you are going to go through several pics rather quickly.

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


    Did you find this post helpful? Yes | No

    Default

    but you might want to turn off your code protect while playing with the code,
    Even if you have enabled the CodeProtect... it just prevent to read it, you can Still program your PIC.

    peterdeco1, what else your PIC will do, here i don't see any part of your code that seems to do some delay job. Did you read TIMER0, TIMER1 section of your datasheet? what sbout the Elapsed timer of darrel?
    Steve

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

  4. #4


    Did you find this post helpful? Yes | No

    Default

    Thanks for the code protect advice but I'm using a flash PIC. I'm going to look for the Elapsed Timer now.

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


    Did you find this post helpful? Yes | No

    Default

    Here is some code I use in an 18F8720 using a 20Mhz Xtal, You can modify the technique to run on nearly any other part. It works best when you you need accuracy over a long period, but don't need to time over very small intervals.

    '---------------------------------------------------------------------
    T0CON =$87 ' Set up TMR0 prescaler with 256 divisor


    LOOP:

    IF INTCON.2 = 1 THEN
    INTCON.2 = 0
    TMR0H = $69
    TMR0L = $7A
    GOSUB CountTime
    EndIF

    'Your code goes here'

    GOTO LOOP

    CountTime:

    Seconds = Seconds + 2
    IF Seconds = 60 THEN
    Seconds = 0
    Minutes = Minutes + 1
    .
    .
    .
    RETURN




    '-------------------------------------------------------------------------

    Explanation of what is going on above:

    Hardware Timer0 is set up with a 256 (/8) divisor
    INTCON.2 gets set whenever the timer rolls over.
    When the timer rolls over, the timer gets loaded with a value
    that insures it will time out again in 2 seconds.

    Your program executes normally, but exactly every two seconds
    the CountTime sub is executed.

    The only caveat: Your program MUST complete at least one loop every
    2 seconds!

    I have used a similar technique with the 16F series, but since the timers are "shorter", sometimes the maximum timeout is only 65 ms. Still, the procedure can be very useful.

Similar Threads

  1. Elapsed Timer Demo
    By Darrel Taylor in forum Code Examples
    Replies: 111
    Last Post: - 29th October 2012, 17:39
  2. Simple Timer question
    By jimseng in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 24th September 2009, 23:12
  3. Timer interrupt frequency
    By Samoele in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 17th January 2009, 23:49
  4. a simple boxe timer (code included)
    By hozone in forum Code Examples
    Replies: 24
    Last Post: - 13th October 2008, 12:57
  5. timer interupt help 16f73
    By EDWARD in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 3rd July 2005, 08:41

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