![]() |
Forum Index : Microcontroller and PC projects : uM2(+) :micro-second timing
Author | Message | ||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10209 |
When tuning code it is useful to know how long things are taking and the Micromite is now so fast that the 1-millisecond resolution of the "timer" variable isn't always sufficient. The attached CFunctions allow you to zero the Micromites internal tick clock and then read it, automatically scaled by the CPU speed to give microsecond readings. The code works on both the Micromite and the Micromite+ Of course the command to set the clock and read it takes time so this needs to be measured and subtracted from the code being tested. I do this in the attached test code option explicit
option default none dim integer a dim float b ' zerotimer() a=gettimer() 'measure how long the clock takes to zero and read ' zerotimer() b=atan2(1.5.1.6) ? gettimer()-a," usec" 'print the time taken to execute the function ' end CSub zerotimer 'zero the tick clock 00000000 00001021 40824800 03E00008 00000000 End CSub ' CFunction gettimer 'get the number of microseconds since the tick clock was zeroed 00000000 40044800 3C029D00 8C420000 3C03001E 34638480 8C450000 00A3001B 006001F4 00001812 0083001B 006001F4 00001821 03E00008 00001012 End CFunction FUNCTION atan2(y as float,x as float) as float IF x > 0 THEN atan2 = ATN(y/x) ELSEIF y >= 0 AND x < 0 THEN atan2 = PI + ATN(y/x) ELSEIF y < 0 AND x < 0 THEN atan2 = ATN(y/x) - PI ELSEIF y > 0 AND x = 0 THEN atan2 = PI / 2 ELSEIF y < 0 AND x = 0 THEN atan2 = PI / -2 ENDIF IF atan2 < 0 THEN atan2 = atan2 + 2 * PI ENDIF END FUNCTION C-source void zerotimer(void){
unsigned int current_ticks = 0; asm volatile("mtc0 %0, $9": "+r"(current_ticks)); //set the current number of ticks to 0 } long long gettimer(void){ unsigned int current_ticks; asm volatile("mfc0 %0, $9" : "=r"(current_ticks));//get the time in ticks since zeroed unsigned int tick_rate; tick_rate = CurrentCpuSpeed /2000000; //get the number of clock ticks per usecond return current_ticks/tick_rate; } |
||||
drkl![]() Senior Member ![]() Joined: 18/10/2015 Location: HungaryPosts: 102 |
Dear Matherp, Here is the line from the program: b=atan2(1.5.1.6) What does it mean? 1.5.1.6 is a number? I don't understand. Can you explain for me? Thank you drkl |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10209 |
Should be 1.5,1.6 atan2 is just an example trig function (written by TassJim I think) that I was using to measure the execution duration. Not sure how the comma became a dot ![]() |
||||
Zonker![]() Guru ![]() Joined: 18/08/2012 Location: United StatesPosts: 767 |
Humm... Would there be a way to turn this very precise timing into a trigger timer..? Like for SCR or triac control... I would like to create a 3 phase controller to control 6 SCR's in such a way to allow full 4 quadrant selection, so the output would be programmable from negative, through 0 and swing into the positive side of voltages.... If you provide 3 zero cross triggers, the timer would then output the precise timing to 3 (or 6) output pins to activate the SCR's... With this setup, you could the create some very high power MM touch panel controlled units..! With some current feedback devices added, maybe a LEM sensor, you could control max power delivered to a load...Nice..! EDIT: I would pay money for this..! ![]() |
||||
robert.rozee Guru ![]() Joined: 31/12/2012 Location: New ZealandPosts: 2428 |
what you have described is called a "soft starter", basically the equivalent of 3 very large light dimmers used to ramp up (and down) the power applied to a 3-phase motor. i've spent quite a bit of time testing these for Aucom Electronics (a christchurch, nz based manufacturer of soft starters). one application is in water pumping systems, to prevent mechanical damage at startup. they are also far friendlier than VFDs in terms of electrical noise injected back into the grid (when up to speed the soft starter is bypassed). see: http://www.aucom.com/en/ cheers, rob :-) |
||||
drkl![]() Senior Member ![]() Joined: 18/10/2015 Location: HungaryPosts: 102 |
Hello, I try: zerotimer() a=gettimer() 'measure how long the clock takes to zero and read zerotimer() 'here is the measurable program part, now is nothing optime=gettimer()-a ' MUST SUBSTRACK SOME VALUE? ? optime," usec" 'print the time taken to execute the program-part end The result is: > RUN 9 usec > So, You should not use any correction? drkl |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10209 |
The program is doing exactly what is should. With no measurable part the output should be approximately zero. In fact the interpreter has to step over the comment which takes some time which you seem to be measuring at 9 usec. The idea of my example program was to show how to measure the time taken to do the "measurable" section by removing the time to set up and read the timer |
||||
drkl![]() Senior Member ![]() Joined: 18/10/2015 Location: HungaryPosts: 102 |
Perfect! drkl |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |