PDA

View Full Version : code needs much longer then expected



mischl
- 21st March 2006, 20:35
hi all

on a 12f675 with internal osc speed 4mhz i have the following main routine. this toggles two digital outputs asyncronus. i thinked (dangerous?) that one cycle of this while execution needs some us - each line1 us. but in fact it needs much longer, around 800 us... any ideas why and what can be done for speed up?

thanks a lot



while z < 60 ' 30 cycles : 60 toggles of x=out1
if x > 0 then ' as long as the half cycle is not at the end
x = x - 1 ' decrement
else ' when the half cycle is finish
x = an1 + base1 ' set the delay new with the old values
z = z + 1 ' inc loop counter
out1 = out1 ^ 1 ' toggle output
endif
if y > 0 then
y = y - 1
else
y = an2 + base2
out2 = out2 ^ 1
endif
wend

btaylor
- 22nd March 2006, 02:03
I am not surprised that each line averages 800 uSecs. You have some pretty complex logic in those statements.

Have a look at the machine code listing the basic source produces and you will see just how many instructions are involved.

Also, you will find big differences in execution time for BYTE and WORD variables.

For example
A var byte
w var word

if a<9 then dosomething
runs much faster than
if w<9 then dosomething

For any realtime work I define a port as a diagnostic pin and make sure it is an output then check the execution time with an oscilloscope.

Diag var portb.0 (for example)
Trisb = %11111110 (or whatever)

Then before each program block I want to test for execution time I put

Diag = 1
Code to be tested goes here
Diag = 0

This runs a little faster than
HIGH Diag
Code goes here
LOW Diag

The oscilloscope is invaluable if you have a time critical application.

HTH
Brian