PDA

View Full Version : Time taken to do mainloop



Gerald
- 29th March 2023, 07:47
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

Gerald
- 29th March 2023, 08:15
PS. How should one paste code into this forum?

HenrikOlsson
- 29th March 2023, 11:12
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:

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.

Ioannis
- 29th March 2023, 12:06
To add code in a frame use:

[ code ]

code....
[ /code ]

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

Ioannis

Gerald
- 29th March 2023, 14:16
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:

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: https://youtu.be/MC7C31mJ4ds
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.