View previous topic :: View next topic |
Author |
Message |
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
a simple task - but how can this be done with fewer clocks? |
Posted: Sat Feb 07, 2009 2:57 pm |
|
|
A relay controller maps 'ON' relays to a '1' in 16 bits of a word - for 16 relays - each relay draws a pretty exact 1 amp from the main 24V PSU. Different routines control different bits of controlvar.
To report the total current estimate - we need to find how many of the 16 bits are ON at any give time to get the current demand.
Question is - is the following the most efficient way to sum the bits in a 16 bit word ??
Code: |
unsigned int16 controlvar; //global
unsigned int8 total_bits=0; //
For (i=0;i<16;i++)
total_bits+=bit_test(controlvar,i);
|
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sat Feb 07, 2009 5:10 pm |
|
|
The original loop on a PIC18 takes 999 cycles.
Unrolling the loop is a common performance boost, for example: Code: | int16 controlvar; //global
int8 total_bits=0;
if (bit_test(controlvar,0)) total_bits++;
if (bit_test(controlvar,1)) total_bits++;
if (bit_test(controlvar,2)) total_bits++;
if (bit_test(controlvar,3)) total_bits++;
...
if (bit_test(controlvar,14)) total_bits++;
if (bit_test(controlvar,15)) total_bits++; | This reduces the loop to 32 cycles (independent of the value in controlvar). |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
trading code space for execution speed |
Posted: Sun Feb 08, 2009 10:40 pm |
|
|
most excellent reminder - i admit i forgot that i should look at trading a bit of code space for execution speed. THANK YOU so much. |
|
|
Ttelmah Guest
|
|
Posted: Mon Feb 09, 2009 4:34 am |
|
|
It'd be interesting to actually measure the code space as well.
I suspect it may not be much if any larger, and may actually be smaller!.....
The 'reason', is that the bit tests for fixed bit values, can all be translated into simple BTFS instructions, while the one using a variable, involves significant pre-calculation.
Remember also, that macros can be your friend here, so:
Code: |
#define t_bit(x) if (bit_test(controlvar,x)) total_bits++
t_bit(0);
t_bit(1);
t_bit(2);
....
|
Makes tidier looking code, and reduces the typing.
Best Wishes |
|
|
John P
Joined: 17 Sep 2003 Posts: 331
|
|
Posted: Mon Feb 09, 2009 10:13 am |
|
|
If you're really desperate for fast execution speed, use a lookup table. You'd have 256 bytes stored with every possible combination of bits representing the addresses, and the numbers would be the number of bits which were set in any particular byte. But I'm not sure that it actually saves time versus checking each bit in turn, and it uses a lot of ROM space. |
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
|
Posted: Mon Feb 09, 2009 10:18 am |
|
|
Considering that the bits you are representing the ON and OFF states of some mechanical relays, it seems pointless to be so concerned about the number of clocks it takes to add them up. Mechanical relays aren't normally changed that quickly. _________________ Robert Scott
Real-Time Specialties
Embedded Systems Consulting |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
about speed of 'relay change' |
Posted: Mon Feb 09, 2009 11:22 am |
|
|
while the time it takes for a relay or solenoid to close or open is in fact much longer than any of these count methods require - measured in tens or even hundreds of milliseconds -
the CURRENT draw requirements are much more sharply defined and happen in the time domain without much regard for how long it takes the mechanical result to occur.
the goal is track the total bits on - to determine NET ( average ) flow from multiple solenoid fluid valves - BUT the immediate total CURRENT demand is not so related to the actuator time- but IS tightly coupled to that pesky bit count I'm after . |
|
|
Ttelmah Guest
|
|
Posted: Mon Feb 09, 2009 1:36 pm |
|
|
The current involved will also change slowly.
Remember a relay coil is an inductor, so the current lags the actual switching change significantly.
Best Wishes |
|
|
|