The PIC® instructions are very deterministic in the time they take. There are exceptions, but in general a instruction takes 4 clocks (or 2 on some 24 bit chips) and if there is a change in the program counter it take s twice as long. Counting instructions in the LST file is one way to figure out the time code takes. Consider this example from the LST file:
The IF statement takes instruction times if a==b or 4 otherwise. On a PIC18 this is 16 clocks. So if the chip oscillator (fosc) is 40mhz. Then the instruction time is 4/40000000 or 100ns. This IF statement takes 300ns or 400ns to execute. The two assignments take 400ns so in total if a==b then it takes 700ns or 400ns otherwise.
IDE users can use the code profiling tool to find out how long functions take to execute or to time how long it takes to get from one point in code to another.
Use code like the following To do timing manually:
setup_timer_1(t1_internal|t1_div_by_4); // 1us tick
Unsigned 8 bit operations for math operations are quite fast and floating point is very slow. If possible consider fixed point instead of floating point.
For example, instead of "float cost_in_dollars;" do "long cost_in_cents;". You can also get the compiler to do the math for you by using a declaration like "long fixed(2) cost_in_dollars;"
Consider a lookup table for trig formulas instead of real time calculations (see EX_SINE.C for an example).
Note all times will vary depending on memory banks used and sometimes for multiply, divide and float operations the actual numbers will affect the time.
Like us on Facebook. Follow us on Twitter.
CCS is a leading worldwide supplier of embedded software development tools that enable companies to develop premium products based on Microchip PIC® MCU and dsPIC® DSC devices. Complete proven tool chains from CCS include a code optimizing C compiler, application specific hardware platforms and software development kits. CCS' products accelerate development of energy saving industrial automation, wireless and wired communication, automotive, medical device and consumer product applications. Established in 1992, CCS is a Microchip Premier 3rd Party Partner. For more information, please visit https://www.ccsinfo.com.
PIC® MCU, MPLAB® IDE, MPLAB® ICD2, MPLAB® ICD3 and dsPIC® are registered trademarks of Microchip Technology Inc. in the U.S. and other countries.