Time delay by incrementing a variable using incoming pulse


Closed Thread
Results 1 to 9 of 9

Hybrid View

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

    Default Time delay by incrementing a variable using incoming pulse

    Im trying to capture the 60Hz mains signal count the pulses up to 600, then go and do the subroutine "addtenthhour", then update the display. Then return to counting the incoming pulses, hit 600 and do it all over again.
    However, it aint working.
    My input is RA5, I have a 0-4.5v square wave coming into the pin. Its robust and is clean on the scope.
    What the heck am I missing here??

    MainLoop:
    A0 = 0
    IF PORTA.5 = 1 THEN A0 = A0 + 1
    A1 = A0
    IF A1 = 600 THEN AddHourTenth
    Gosub DisplayRefresh
    Goto MainLoop


    AddHourTenth:
    HoursL = HoursL + 1
    IF HoursL = 0 THEN HoursH = HoursH + 1
    IF (HoursH = $F) and (HoursL = $4240) then ; Roll over at 1,000,000
    HoursH = 0 ; 99999.9 + .1
    HoursL = 0
    endif
    GOSUB WriteHours_EE
    A1 = 0
    Return

  2. #2
    Join Date
    Sep 2003
    Location
    Vermont
    Posts
    373


    Did you find this post helpful? Yes | No

    Default

    MainLoop:
    A0 = 0
    IF PORTA.5 = 1 THEN A0 = A0 + 1
    A1 = A0
    IF A1 = 600 THEN AddHourTenth
    Gosub DisplayRefresh
    Goto MainLoop

    It looks like every time you hit MainLoop, you reset A0 to zero. Subsequently, you put the A0 value into A1. This gets repeated every loop. The most A1 can increment to is 1. Also you have a goto statement to a sub loop, with a return at the end. Either have a gosub with a return or a goto with a follow up goto. Otherwise there will be stack problems.

    Ron

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


    Did you find this post helpful? Yes | No

    Default

    Ok, so if I make A0 = 0 before mainloop to initilize the count at zero. I change the line:
    IF A1 = 600 THEN AddHourTenth
    into
    IF A1 = 600 THEN Gosub AddHourTenth

    I end up with this...

    A0 = 0
    MainLoop:
    IF PORTA.5 = 1 THEN A0 = A0 + 1
    A1 = A0
    IF A1 > 600 THEN A1 = 0
    IF A1 = 600 THEN Gosub AddHourTenth 'adds increment of value to be displayed
    Gosub DisplayRefresh 'places new value on the 7 segment displays
    Goto MainLoop

    Does this take care of the issues?


    Or should I be doing it like this?

    IF A1 = 600 THEN
    Gosub AddHourTenth
    ELSE
    Goto Mainloop
    ENDIF

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


    Did you find this post helpful? Yes | No

    Default

    Curious: If you are trying to implement a "power on hours" counter, I can think of a better way - one that doesn't use so much processor time.

  5. #5
    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.

  6. #6
    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.

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