Time taken to do mainloop


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2023
    Location
    Cape Town, South Africa
    Posts
    26

    Default Time taken to do mainloop

    Okay, I have now got a functional program and don't need to do any further programming till I have tested on a real life scenario. But I am curious about how the internal clock of the PIC12F683 decides when to repeat the mainloop: (sub-routines omitted for clarity)

    #CONFIG
    __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF
    #ENDCONFIG


    clear
    DEFINE OSC 4


    TRISIO.4 = 0 ' GPIO.4 as output
    ANSEL = 0
    CMCON0 = 7
    OPTION_REG.7 = 0 ' Enable individual pullups
    WPU=%000101 ' Enable weak pullups on GPIO.0 & GPIO.2


    LEDIN VAR GPIO.3 ' Assign name "LEDIN" to GPOI 3 INPUT (yoctop green led)
    RECPB var GPIO.4 ' Assign name "RECPB" to GPIO 4 OUTPUT
    CYCLE var GPIO.0 ' Assign name "CYCLE" to GPIO 0 INPUT
    TRIG var GPIO.2 ' Assign name TRIG to GPIO 2 (from manual trigger) INPUT


    LC var word ' timing auto loop counter 0 - 60000
    k var word
    TT var word ' TRIG hold down time


    high RECPB
    low gpio.1


    Mainloop:


    pause 1 'run cycle at approx 1.73 ms intervals (I thought 1 = 1 second intervals?)

    if LEDIN = 0 then 'Invert green LED of yoctop
    low gpio.1 'to new red LED
    endif '
    if LEDIN = 1 and LC > 4500 then 'only show red LED in 57s silent phase
    high gpio.1 '
    endif '

    TT = 0 ' Reset trigger timer

    If CYCLE = 0 then ' START ONE MINUTE LOOP
    LC = LC+1
    endif
    if LC > 1 and LC < 500 then 'press RECPB to start snippet recording
    low RECPB
    endif
    if LC > 499 and LC < 1900 then '1900 counts give 3.0 sec recording
    high RECPB
    endif
    if LC > 1899 and LC < 2400 then 'press RECPB to end snippet recording
    low RECPB
    endif
    if LC > 2399 then 'release RECPB
    high RECPB
    endif
    if LC > 34650 then ' 34650 loop counts over 1 minute (this equates to 60 seconds)
    LC = 0
    endif

    if CYCLE = 1 and LC > 1 and LC <1950 then '
    low RECPB ' Switch off CYCLE while recording
    Pause 500 ' hold button down a little
    high RECPB
    LC = 0
    endif

    if TRIG = 0 and LC < 2400 then ' Press TRIG while already recording
    LC = 0
    gosub ManRecordExtend 'Extend an active recording
    endif

    if TRIG = 0 and LC > 2399 then 'Manual record in silent phase
    LC = 2401
    gosub ManRecordSilent
    endif

    GOTO Mainloop ' END ONE MINUTE LOOP

  2. #2
    Join Date
    Mar 2023
    Location
    Cape Town, South Africa
    Posts
    26


    Did you find this post helpful? Yes | No

    Default Re: Time taken to do mainloop

    PS. How should one paste code into this forum?

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Time taken to do mainloop

    The program runs as fast as it can and the execution time of each iteration thru the loop depends on the code that is executed at that iteration. In other words, which IF statements evaluates to true and which doesn't.

    For example:
    Code:
    Main:
      TOGGLE GPIO.0
      PAUSE 1  
    GOTO Main
    This will take the same amount of time each iteration and it's 1ms (which is the PAUSE statement) + however long the TOGGLE command takes (a couple of instruction cycles) + another two instruction cycles for the GOTO command.

    If the PIC runs at 4MHz each instruction cycle is 1us. Most ASM commands executes in one cycle, some (like jumps) takes two. Each PBP statements gets translated into several ASM instructions and there's no list of how many that is because it varies depending on various things.

    If you want to KNOW how fast your program runs the easiest way is to measure it. If you NEED it to run at specific pace you should resort to using a timer.

    /Henrik.

  4. #4
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,807


    1 out of 1 members found this post helpful. Did you find this post helpful? Yes | No

    Default Re: Time taken to do mainloop

    To add code in a frame use:

    [ code ]

    code....
    [ /code ]

    but without the space characters that I added for obvious reasons.

    Ioannis
    Last edited by Ioannis; - 29th March 2023 at 15:13.

  5. #5
    Join Date
    Mar 2023
    Location
    Cape Town, South Africa
    Posts
    26


    Did you find this post helpful? Yes | No

    Default Re: Time taken to do mainloop

    Quote Originally Posted by HenrikOlsson View Post
    The program runs as fast as it can and the execution time of each iteration thru the loop depends on the code that is executed at that iteration. In other words, which IF statements evaluates to true and which doesn't.

    For example:
    Code:
    Main:
      TOGGLE GPIO.0
      PAUSE 1  
    GOTO Main
    This will take the same amount of time each iteration and it's 1ms (which is the PAUSE statement) + however long the TOGGLE command takes (a couple of instruction cycles) + another two instruction cycles for the GOTO command.

    For those that sent private messages; I have a trigger button near my hand. A short press makes an immediate 3.5 second video and restarts the mainloop at the beginning of the 57 sec "mute" time. A long press records until the trigger is released. If mainloop is busy recording, the trigger extends that recording.

    If the PIC runs at 4MHz each instruction cycle is 1us. Most ASM commands executes in one cycle, some (like jumps) takes two. Each PBP statements gets translated into several ASM instructions and there's no list of how many that is because it varies depending on various things.

    If you want to KNOW how fast your program runs the easiest way is to measure it. If you NEED it to run at specific pace you should resort to using a timer.

    /Henrik.
    Thank you Henrik. I get a very repeatable 3 seconds of video recording every minute. I used a video of a "stopwatch" to calibrate the numbers:
    This does not need to be very precise. I just wanted to understand it better. When I get back from a long roadtrip in May, I will experiment further with the code, primarily to add watchdogs for things going wrong. Eg. No recording for 2 minutes, a recording longer than 20 seconds. The primary purpose of the system is to be able to use one SD card in the camera for 3 weeks and not fill it in a day or two.
    Last edited by Gerald; - 29th March 2023 at 14:22.

Similar Threads

  1. Outside the Mainloop
    By pescador in forum General
    Replies: 10
    Last Post: - 26th April 2016, 17:34
  2. PIC 18F1330 - first time, hard time - blink that LED
    By flotulopex in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 1st March 2015, 22:33
  3. How best to time a pin state for extended periods of time
    By Hylan in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 10th March 2011, 11:20
  4. DS1904 RTC - How to Convert Binary Time into Real Time/Date?
    By Ioannis in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 2nd December 2010, 10:45
  5. Linx RF -> HSERIN time delay / time critical app...
    By batee in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 15th October 2004, 15:04

Members who have read this thread : 1

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