View Single Post
  #6   Spotlight this post!  
Unread 22-01-2007, 12:40
Mike Bortfeldt Mike Bortfeldt is offline
Registered User
FRC #1126 (& 1511)
Team Role: Mentor
 
Join Date: Oct 2004
Rookie Year: 2004
Location: Rochester, NY
Posts: 119
Mike Bortfeldt has much to be proud ofMike Bortfeldt has much to be proud ofMike Bortfeldt has much to be proud ofMike Bortfeldt has much to be proud ofMike Bortfeldt has much to be proud ofMike Bortfeldt has much to be proud ofMike Bortfeldt has much to be proud ofMike Bortfeldt has much to be proud of
Re: CPU Load in FRC RC

Don,

As Alan mentioned, branching instructions by themselves only take two instruction clock cycles (an instruction clock is 4 clock cycles, so the 40mhz PIC processor in reality only has a 10mhz instruction clock. I will be strictly talking about instruction clock cycles in this note). It's the instructions that have to be done within the IF statement that take most of the time, not the actual branching itself. In general, addition & subtraction operations all occur in-line, that is, the actual assembly code is generated separately for each add/sub operation in the routine the operation is found and is quick. For more complex operations (multiplication, division, trig, all floating point operations), the operands are passed to a math subroutine (function) that will perform the actual operation and the result returned to the calling routine. The source code for the math routines can be found in the mcc18 directory if you selected the appropriate option during the original install of the software. Some of these routines actually have min/max/mean clock cycles to execute. Based on a couple of observations, these times do not include the time to copy your arguments to the math variables (minimum 2 clock cycles per byte), or the result back into another variable. It also doesn't include the necessary CALL/RETURN (4 clock cycles). Here are some samples - the math operation only, not the call/return or the passing of the arguments:

unsigned char * unsigned char = 6 clock cycles
signed short * signed short = 35 clock cycles
signed short / signed short = 85 clock cycles average (min 28, max 149)
signed long / signed short = 376 clock cycles average (min 84, max 421)
any floating point multiply/divide = 1835 clock cycles average
any floating point addition/subtraction = 80 clock cycles average

It should be noted that the floating point information came from the C18 compiler version 2.2 and may not be the same in version 2.4 (I believe they changed their floating point storage format between these two versions). Trig routines generally will have multiple floating point operations. I assume around 6 multiply/divide for lack of a better number (from a quick scan of the source – I may be way off base). Based on that assumption, a SINE call could consume upwards of 11,000 clock cycles (0.11% cpu). Doing one of these operations in the main loop of your code (approximately 38 times per second) would result in over 400,000 clock cycles per second (>4% cpu) - very costly.

I'm not sure if this answered your question or not, but hopefully it helped.

Mike