Measurement time of a peace of code
Hi Everyone;
How do you guys measyre the time that takes to execute a piece of code.
I have a piece of code, of a PI outine, and i want to measure how much time it takes to complete so a can achive a good sample rate, howaver i don't know how much time it takes to complete. Can somebody help here;
Here is the code
Code:
PI:
GetADC:
K var byte: K = 0
for K = 1 to 20
ADCIN 0,actual_speed
sample = actual_speed + sample
next K
actual_speed = sample/20
sample=0
calculate_error:
error = setPOINT - actual_speed
calculate_proportional:
P = Kp * error
calculate_integral:
error_sum = error_sum + error
sign = error_sum
GOSUB SetSign
IF ABS error_sum > (250*Ki) THEN error_sum = sign * (250*Ki)
I = ABS error_sum / Ki
I = sign * I
calculate_drive:
power = P + I
sign = power
GOSUB SetSign
IF sign = 1 THEN
IF power > 255 THEN power = 255
ELSE
power = 0
ENDIF
HPWM 1,power,30000
RETURN
SetSign:
IF sign.Bit15 = 0 THEN
sign = 1
ELSE
sign = -1
ENDIF
RETURN
Re: Measurement time of a peace of code
Hi,
I just use a timer.
Code:
Time VAR WORD
TMR1L = 0
TMR1H = 0
T1CON.0 = 1 'Start TMR1
'code here
'code here
T1CON.0 = 0 'Stop TMR1
Time.HighByte = TMR1H
Time.LowByte = TMR1L
HSEROUT ["Execution time in cycles: ", DEC Time,13]
Something like that though I'm sure I've missed a bracket somewhere.... :-)
/Henrik.
Re: Measurement time of a peace of code
MPLAB stopwatch also do the trick pretty well
If you have a Scope, set a pin high before the section of code you want measure, and clear it at thye end. not as accurate as Timer or StopWatch but easy easy easy :D
Re: Measurement time of a peace of code
Quote:
Something like that though I'm sure I've missed a bracket somewhere.... :-)
Ehehe, this time i don't think so!!! Can you tell me hoe do i do the math so i can have a value in seconds or miliseconds.
I'm using a 8Mhz osc, so with Fosc/a i get 2Mhz, so 0.5uS for cycle. I must multiply this value for the value that i've got in the time var?
Thank you Henrik
Quote:
MPLAB stopwatch also do the trick pretty well
If you have a Scope, set a pin high before the section of code you want measure, and clear it at thye end. not as accurate as Timer or StopWatch but easy easy easy :D
I'm using MCS, i'm not used to use MPLAB, i think that way with the scope its better. Thank you Steve
Re: Measurement time of a peace of code
Update;
I made this code
Code:
Main:
if Bmais = 0 then
pause 50
Time VAR WORD
TMR1L = 0
TMR1H = 0
T1CON.0 = 1 'Start TMR1
'Inserir rotina a medir
GOSUB PI
T1CON.0 = 0 'Stop TMR1
Time.HighByte = TMR1H
Time.LowByte = TMR1L
ENDIF
lcdout $FE, 1, " Medidor ciclos "
lcdout $FE,$C0,"N. Ciclos: ", #time
Pause 100
GOTO Main
When i press Bmais, i get always 7203 cycles, so multiply by 0.5uS, i get 0.003602 S, or 3.6mS. Is this calculations correct?
Re: Measurement time of a peace of code
maybe... replace the gosub by a pause 10, and see what happen ;)
Re: Measurement time of a peace of code
Quote:
Originally Posted by
mister_e
maybe... replace the gosub by a pause 10, and see what happen ;)
I get always 20009 cycles, so multiply by 0.5uS, i get 0.010005S, or 10mS. So i assume that is correct, and works great.
Thank you Henrik and Steve!
Re: Measurement time of a peace of code
Yes exactly, in this case it's just a matter of multiplying the value you get with 0.5 to get the answer in uS.
If you want to do that in PBP then perhaps:
Code:
' In us
LCDOUT $FE, 1, ["Timer ticks: ", DEC Time]
LCDOUT $FE, $C0, ["Execution time: ", DEC Time/2, "." DEC Time//2, "us",13]
I think that should do it.
Re: Measurement time of a peace of code
DT has a nice example here: http://www.picbasic.co.uk/forum/show...=1272#post1272
Something else to consider is where the code you're timing is located. See the 1st paragraph.
Re: Measurement time of a peace of code
I got it. Thank you all, once again!
1 Attachment(s)
Re: Measurement time of a peace of code
SteveB had a nice include file called codetimer.bas from here: http://www.picbasic.co.uk/forum/show...7197#post27197 for PIC18 devices.
Re: Measurement time of a peace of code
Quote:
Originally Posted by
scalerobotics
WoW, thast awesome, very good indeed!